ケーススタディ:通知システムの構築

Python で始める AWS Boto 入門

Maksim Pecherskiy

Data Engineer

最終成果物

最終成果物

最終成果物

Python で始める AWS Boto 入門

最終成果物

最終成果物

Python で始める AWS Boto 入門

最終成果物

最終成果物

Python で始める AWS Boto 入門

最終成果物

最終成果物

Python で始める AWS Boto 入門

通知システムの構築

トピックの設定

  • トピックを作成
  • 連絡先CSVをダウンロード
  • サービスごとにトピックを作成
  • 連絡先を各トピックに購読登録
Python で始める AWS Boto 入門

通知システムの構築

集計値の取得

  • 月次「Get It Done」レポートをダウンロード
  • Potholes の件数を取得
  • Illegal dumping の通知件数を取得

アラート送信

  • Potholes が100超ならアラート送信
  • Illegal dumping が30超ならアラート送信
Python で始める AWS Boto 入門

トピックの設定

SNSクライアントの初期化

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

トピックを作成しARNを保存

trash_arn = sns.create_topic(Name="trash_notifications")['TopicArn']
streets_arn = sns.create_topic(Name="streets_notifications")['TopicArn']
Python で始める AWS Boto 入門

トピックの設定

トピックの設定

Python で始める AWS Boto 入門

ユーザーをトピックに購読登録

contacts = pd.read_csv('http://gid-staging.contacts.csv')
Python で始める AWS Boto 入門

ユーザーをトピックに購読登録

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 入門

ユーザーをトピックに購読登録

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

全行に適用

contacts.apply(subscribe_user, axis=1)
Python で始める AWS Boto 入門

ユーザーをトピックに購読登録

ユーザーをトピックに購読登録

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 入門

集計値の取得

サービス名で直接参照できるようにindexを設定

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 入門

Ayo berlatih!

Python で始める AWS Boto 入門

Preparing Video For Download...