Étude de cas : générer un dépôt de rapports

Introduction à AWS Boto en Python

Maksim Pecherskiy

Data Engineer

Produit final

Produit final

Introduction à AWS Boto en Python

Les étapes

Préparer les données
  • Télécharger les fichiers du mois depuis le bucket de données brutes
  • Les concaténer en un seul CSV
  • Créer un DataFrame agrégé
Introduction à AWS Boto en Python

Les étapes

Créer le rapport
  • Écrire le DataFrame en CSV et HTML
  • Générer un graphique Bokeh, l'enregistrer en HTML
Introduction à AWS Boto en Python

Les étapes

Mettre en ligne le rapport sur un site partageable
  • Créer le bucket gid-reports
  • Charger les trois fichiers du mois sur S3
  • Générer un fichier index.html listant tous les fichiers
  • Récupérer l'URL du site !
Introduction à AWS Boto en Python

Bucket de données brutes

Bucket de données brutes

  • Fichiers privés
  • CSV quotidiens des requêtes de l'app
  • Données brutes

Bucket des requêtes Gid

Introduction à AWS Boto en Python

Lire les fichiers bruts

# 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']
Introduction à AWS Boto en Python

Lire les fichiers bruts

# 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)
Introduction à AWS Boto en Python

Lire les fichiers bruts

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

# Preview the DataFrame df.head()

Aperçu du DataFrame

Introduction à AWS Boto en Python

Créer des rapports agrégés

  • Effectuer une agrégation
  • df.to_csv('jan_final_report.csv')
  • df.to_html('jan_final_report.html')
  • jan_final_chart.html

Rapports agrégés

Introduction à AWS Boto en Python

Bucket de rapports

Bucket de rapports

  • Site web du bucket
  • Accessible publiquement
  • Données agrégées et rapports HTML

Bucket de rapports

Introduction à AWS Boto en Python

Charger le CSV agrégé

# 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'})
Introduction à AWS Boto en Python

Charger le tableau 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'})
Introduction à AWS Boto en Python

Charger le graphique 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'})
Introduction à AWS Boto en Python

Rapports chargés

Rapports chargés

Introduction à AWS Boto en Python

Créer 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']
Introduction à AWS Boto en Python

Créer index.html

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

Écrire le DF en HTML

Introduction à AWS Boto en Python

Charger 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'
  })
Introduction à AWS Boto en Python

Obtenir l'URL de l'index !

URL du site du bucket *


"http://gid-reports.index.html"
Introduction à AWS Boto en Python

Ajustons un peu !

Introduction à AWS Boto en Python

Preparing Video For Download...