案例研究:打造通知系統

Python 中的 AWS Boto 入門

Maksim Pecherskiy

Data Engineer

最終成果

最終成果

最終成果

Python 中的 AWS Boto 入門

最終成果

最終成果

Python 中的 AWS Boto 入門

最終成果

最終成果

Python 中的 AWS Boto 入門

最終成果

最終成果

Python 中的 AWS Boto 入門

建立通知系統

Topic Set Up

  • 建立 topic
  • 下載聯絡人清單 csv
  • 為各服務建立 topics
  • 將聯絡人訂閱到各自的 topic
Python 中的 AWS Boto 入門

建立通知系統

取得彙總數量

  • 下載每月「Get It Done」報表
  • 取得 Potholes 的計數
  • 取得 Illegal dumping 通知的計數

發送警示

  • 若 potholes 超過 100,發送警示
  • 若 illegal dumping 超過 30,發送警示
Python 中的 AWS Boto 入門

Topic 設定

初始化 SNS 用戶端

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

建立 topics 並儲存其 ARN

trash_arn = sns.create_topic(Name="trash_notifications")['TopicArn']
streets_arn = sns.create_topic(Name="streets_notifications")['TopicArn']
Python 中的 AWS Boto 入門

Topic 設定

Topic 設定

Python 中的 AWS Boto 入門

將使用者訂閱到 topics

contacts = pd.read_csv('http://gid-staging.contacts.csv')
Python 中的 AWS Boto 入門

將使用者訂閱到 topics

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
Python 中的 AWS Boto 入門

將使用者訂閱到 topics

建立 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'])

將 subscribe_user 套用到每一列

contacts.apply(subscribe_user, axis=1)
Python 中的 AWS Boto 入門

將使用者訂閱到 topics

將使用者訂閱到 topic

Python 中的 AWS Boto 入門

取得彙總數量

將 1 月報表載入 DataFrame

df = pd.read_csv('http://gid-reports.2019/feb/final_report.csv')
Python 中的 AWS Boto 入門

取得彙總數量

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
Python 中的 AWS Boto 入門

取得彙總數量

設定索引,便於用服務名稱直接取得計數

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

取得彙總數量

trash_violations_count = df.at['Illegal Dumping', 'count']
streets_violations_count = df.at['Pothole', 'count']
Python 中的 AWS Boto 入門

發送警示

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")
Python 中的 AWS Boto 入門

發送警示

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")
Python 中的 AWS Boto 入門

最終結果

最終結果

Python 中的 AWS Boto 入門

一起來練習吧!

Python 中的 AWS Boto 入門

Preparing Video For Download...