Étude de cas : créer un système de notifications

Introduction à AWS Boto en Python

Maksim Pecherskiy

Data Engineer

Produit final

Produit final

Produit final

Introduction à AWS Boto en Python

Produit final

Produit final

Introduction à AWS Boto en Python

Produit final

Produit final

Introduction à AWS Boto en Python

Produit final

Produit final

Introduction à AWS Boto en Python

Créer le système de notifications

Configuration des sujets

  • Créer le sujet
  • Télécharger le CSV de contacts
  • Créer des sujets pour chaque service
  • Abonner les contacts à leurs sujets respectifs
Introduction à AWS Boto en Python

Créer le système de notifications

Obtenir les totaux agrégés

  • Télécharger le rapport mensuel « get it done »
  • Récupérer le nombre de Potholes
  • Récupérer le nombre de notifications d'Illegal dumping

Envoyer des alertes

  • Si Potholes dépasse 100, envoyer une alerte
  • Si Illegal dumping dépasse 30, envoyer une alerte
Introduction à AWS Boto en Python

Configuration des sujets

Initialiser le client SNS

sns = boto3.client('sns', 
                   region_name='us-east-1',
                   aws_access_key_id=AWS_KEY_ID, 
                   aws_secret_access_key=AWS_SECRET)

Créer des sujets et stocker leurs ARN

trash_arn = sns.create_topic(Name="trash_notifications")["TopicArn"]
streets_arn = sns.create_topic(Name="streets_notifications")["TopicArn"]
Introduction à AWS Boto en Python

Configuration des sujets

Configuration des sujets

Introduction à AWS Boto en Python

Abonner des utilisateurs aux sujets

contacts = pd.read_csv('http://gid-staging.contacts.csv')
Introduction à AWS Boto en Python

Abonner des utilisateurs aux sujets

contacts.csv

Name Email Phone Department
John Smith [email protected] +11224567890 trash
Fanny Mae [email protected] +11234597890 trash
Janessa Goldsmith [email protected] +11534567890 streets
Evelyn Monroe [email protected] +11234067890 streets
Max Pe [email protected] +11234517890 streets
Introduction à AWS Boto en Python

Abonner des utilisateurs aux sujets

Créer la fonction subscribe_user

def subscribe_user(user_row):

if user_row['Department'] == 'trash': sns.subscribe(TopicArn = trash_arn, Protocol='sms', Endpoint=str(user_row['Phone'])) sns.subscribe(TopicArn = trash_arn, Protocol='email', Endpoint=user_row['Email'])
else: sns.subscribe(TopicArn = streets_arn, Protocol='sms', Endpoint=str(user_row['Phone'])) sns.subscribe(TopicArn = streets_arn, Protocol='email', Endpoint=user_row['Email'])

Appliquer subscribe_user à chaque ligne

contacts.apply(subscribe_user, axis=1)
Introduction à AWS Boto en Python

Abonner des utilisateurs aux sujets

Abonner des utilisateurs à un sujet

Introduction à AWS Boto en Python

Obtenir les totaux agrégés

Charger le rapport de janvier dans un DataFrame

df = pd.read_csv('http://gid-reports.2019/feb/final_report.csv')
Introduction à AWS Boto en Python

Obtenir les totaux agrégés

service_name count
Illegal Dumping 2580
Potential Missed Collection 150
Pothole 1170
Traffic Sign - Maintain 210
Traffic Signal Head Turned 60
Traffic Signal Light Out 120
Introduction à AWS Boto en Python

Obtenir les totaux agrégés

Définir l'index pour accéder directement aux comptes par service

df.set_index('service_name', inplace=True)

Obtenir les totaux agrégés

trash_violations_count = df.at['Illegal Dumping', 'count']
streets_violations_count = df.at['Pothole', 'count']
Introduction à AWS Boto en Python

Envoyer des alertes

if trash_violations_count > 100:

# Construct the message to send message = "Trash violations count is now {}".format(trash_violations_count)
# Send message sns.publish(TopicArn = trash_arn, Message = message, Subject = "Trash Alert")
Introduction à AWS Boto en Python

Envoyer des alertes

if streets_violations_count > 30:

    # Construct the message to send
    message = "Streets violations count is now {}".format(streets_violations_count)

# Send message sns.publish(TopicArn = streets_arn, Message = message, Subject = "Streets Alert")
Introduction à AWS Boto en Python

Résultat final

Résultat final

Introduction à AWS Boto en Python

Passons à la pratique !

Introduction à AWS Boto en Python

Preparing Video For Download...