Estudio de caso: Generar un repositorio de informes

Introducción a AWS Boto en Python

Maksim Pecherskiy

Data Engineer

Producto final

Producto final

Introducción a AWS Boto en Python

Pasos

Preparar los datos
  • Descarga los archivos del mes desde el bucket de datos en bruto
  • Concáténalos en un único CSV
  • Crea un DataFrame agregado
Introducción a AWS Boto en Python

Pasos

Crear el informe
  • Escribe el DataFrame a CSV y HTML
  • Genera un gráfico con Bokeh y guárdalo como HTML
Introducción a AWS Boto en Python

Pasos

Subir el informe a un sitio web compartible
  • Crea el bucket gid-reports
  • Sube los tres archivos del mes a S3
  • Genera un archivo index.html que liste todos los archivos
  • ¡Consigue la URL del sitio web!
Introducción a AWS Boto en Python

Bucket de datos en bruto

Bucket de datos en bruto

  • Archivos privados
  • CSVs diarios de solicitudes desde la app
  • Datos en bruto

Bucket de solicitudes Gid

Introducción a AWS Boto en Python

Leer archivos de datos en bruto

# 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']
Introducción a AWS Boto en Python

Leer archivos de datos en bruto

# 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)
Introducción a AWS Boto en Python

Leer archivos de datos en bruto

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

# Preview the DataFrame df.head()

Vista previa del DataFrame

Introducción a AWS Boto en Python

Crear informes agregados

  • Realiza alguna agregación
  • df.to_csv('jan_final_report.csv')
  • df.to_html('jan_final_report.html')
  • jan_final_chart.html

Informes agregados

Introducción a AWS Boto en Python

Bucket de informes

Bucket de informes

  • Sitio web del bucket
  • Accesible públicamente
  • Datos agregados e informes HTML

Bucket de informes

Introducción a AWS Boto en Python

Subir el CSV agregado

# 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'})
Introducción a AWS Boto en Python

Subir la tabla 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'})
Introducción a AWS Boto en Python

Subir el gráfico 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'})
Introducción a AWS Boto en Python

Informes subidos

Informes subidos

Introducción a AWS Boto en Python

Crear 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']
Introducción a AWS Boto en Python

Crear index.html

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

Escribir DF a html

Introducción a AWS Boto en Python

Subir 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'
  })
Introducción a AWS Boto en Python

¡Obtén la URL del índice!

URL del sitio web del bucket *


"http://gid-reports.index.html"
Introducción a AWS Boto en Python

¡Vamos a ajustar!

Introducción a AWS Boto en Python

Preparing Video For Download...