Případová studie: Vytvoření systému notifikací

Introduction to AWS Boto in Python

Maksim Pecherskiy

Data Engineer

Výsledný produkt

Výsledný produkt

Výsledný produkt

Introduction to AWS Boto in Python

Výsledný produkt

Výsledný produkt

Introduction to AWS Boto in Python

Výsledný produkt

Výsledný produkt

Introduction to AWS Boto in Python

Výsledný produkt

Výsledný produkt

Introduction to AWS Boto in Python

Vytvoření systému notifikací

Nastavení tématu

  • Vytvoření tématu
  • Stažení CSV se seznamem kontaktů
  • Vytvoření témat pro každou službu
  • Přihlášení kontaktů k příslušným tématům
Introduction to AWS Boto in Python

Vytvoření systému notifikací

Získání agregovaných čísel

  • Stažení měsíčního reportu
  • Získání počtu výmolů
  • Získání počtu hlášení o nelegálním skládkování

Odeslání upozornění

  • Pokud výmoly překročí 100, odeslat upozornění
  • Pokud nelegální skládkování překročí 30, odeslat upozornění
Introduction to AWS Boto in Python

Nastavení tématu

Inicializace klienta SNS

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

Vytvoření témat a uložení jejich ARN

trash_arn = sns.create_topic(Name="trash_notifications")['TopicArn']
streets_arn = sns.create_topic(Name="streets_notifications")['TopicArn']
Introduction to AWS Boto in Python

Nastavení tématu

Nastavení tématu

Introduction to AWS Boto in Python

Přihlašování uživatelů k tématům

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

Přihlašování uživatelů k tématům

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 to AWS Boto in Python

Přihlašování uživatelů k tématům

Vytvoření metody 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'])

Použití metody subscribe_user na každý řádek

contacts.apply(subscribe_user, axis=1)
Introduction to AWS Boto in Python

Přihlašování uživatelů k tématům

Přihlašování uživatelů k tématu

Introduction to AWS Boto in Python

Získání agregovaných čísel

Načtení lednového reportu do DataFrame

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

Získání agregovaných čísel

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 to AWS Boto in Python

Získání agregovaných čísel

Nastavení indexu pro přímý přístup podle názvu služby

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

Získání agregovaných čísel

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

Odeslání upozornění

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 to AWS Boto in Python

Odeslání upozornění

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 to AWS Boto in Python

Výsledek

Výsledek

Introduction to AWS Boto in Python

Pojďme si procvičit!

Introduction to AWS Boto in Python

Preparing Video For Download...