Кейс: створення репозиторію звітів

Вступ до AWS Boto в Python

Maksim Pecherskiy

Data Engineer

Фінальний результат

Фінальний результат

Вступ до AWS Boto в Python

Кроки

Підготуйте дані
  • Завантажте файли за місяць із бакета сирих даних
  • Об'єднайте їх в один csv
  • Створіть агрегований DataFrame
Вступ до AWS Boto в Python

Кроки

Створіть звіт
  • Запишіть DataFrame у CSV та HTML
  • Згенеруйте графік Bokeh, збережіть як HTML
Вступ до AWS Boto в Python

Кроки

Завантажте звіт на публічний сайт
  • Створіть бакет gid-reports
  • Завантажте всі три файли за місяць до S3
  • Згенеруйте файл index.html зі списком усіх файлів
  • Отримайте URL сайту!
Вступ до AWS Boto в Python

Бакет сирих даних

Бакет сирих даних

  • Приватні файли
  • Щоденні CSV із запитами з застосунку
  • Сирі дані

Бакет gid requests

Вступ до AWS Boto в Python

Зчитування сирих файлів даних

# Create list to hold our DataFrames
df_list = []

# Request the list of csv's from S3 with prefix; Get contents response = s3.list_objects( Bucket='gid-requests', Prefix='2019_jan')
# Get response contents request_files = response['Contents']
Вступ до AWS Boto в Python

Зчитування сирих файлів даних

# Iterate over each object
for file in request_files:
    obj = s3.get_object(Bucket='gid-requests', Key=file['Key'])

# Read it as DataFrame obj_df = pd.read_csv(obj['Body'])
# Append DataFrame to list df_list.append(obj_df)
Вступ до AWS Boto в Python

Зчитування сирих файлів даних

# Concatenate all the DataFrames in the list
df = pd.concat(df_list)

# Preview the DataFrame df.head()

Перегляд DataFrame

Вступ до AWS Boto в Python

Створіть агреговані звіти

  • Виконайте агрегування
  • df.to_csv('jan_final_report.csv')
  • df.to_html('jan_final_report.html')
  • jan_final_chart.html

Агреговані звіти

Вступ до AWS Boto в Python

Бакет зі звітами

Бакет зі звітами

  • Вебсайт на базі бакета
  • Публічний доступ
  • Агреговані дані та HTML-звіти

Бакет зі звітами

Вступ до AWS Boto в Python

Завантаження агрегованого CSV

# Upload Aggregated CSV to S3
s3.upload_file(Filename='./jan_final_report.csv', 
                      Key='2019/jan/final_report.csv', 
                      Bucket='gid-reports',
                      ExtraArgs = {'ACL': 'public-read'})
Вступ до AWS Boto в Python

Завантаження HTML-таблиці

# Upload HTML table to S3
s3.upload_file(Filename='./jan_final_report.html', 
                      Key='2019/jan/final_report.html', 
                      Bucket='gid-reports',
                      ExtraArgs = {
                        'ContentType': 'text/html',
                        'ACL': 'public-read'})
Вступ до AWS Boto в Python

Завантаження HTML-графіка

# Upload Aggregated Chart to S3
s3.upload_file(Filename='./jan_final_chart.html', 
                      Key='2019/jan/final_chart.html', 
                      Bucket='gid-reports',
                      ExtraArgs = {
                        'ContentType': 'text/html',
                        'ACL': 'public-read'})
Вступ до AWS Boto в Python

Звіти завантажено

Звіти завантажено

Вступ до AWS Boto в Python

Створення index.html

# List the gid-reports bucket objects starting with 2019/
r = s3.list_objects(Bucket='gid-reports', Prefix='2019/')

# Convert the response contents to DataFrame objects_df = pd.DataFrame(r['Contents'])
# Create a column "Link" that contains website url + key base_url = "https://gid-reports." objects_df['Link'] = base_url + objects_df['Key']
Вступ до AWS Boto в Python

Створення index.html

# Write DataFrame to html
objects_df.to_html('report_listing.html', 
                   columns=['Link', 'LastModified', 'Size'],
                   render_links=True)

Запис DF у html

Вступ до AWS Boto в Python

Завантаження index.html

# Upload the file to gid-reports bucket root.
s3.upload_file(
  Filename='./report_listing.html', 
  Key='index.html', 
  Bucket='gid-reports',
  ExtraArgs = {
    'ContentType': 'text/html', 
    'ACL': 'public-read'
  })
Вступ до AWS Boto в Python

Отримайте URL індексу!

URL сайту бакета *


"http://gid-reports.index.html"
Вступ до AWS Boto в Python

Зробімо невеликі зміни!

Вступ до AWS Boto в Python

Preparing Video For Download...