Nghiên cứu tình huống: Xây hệ thống thông báo

Giới thiệu về AWS Boto trong Python

Maksim Pecherskiy

Data Engineer

Sản phẩm cuối

Sản phẩm cuối

Sản phẩm cuối

Giới thiệu về AWS Boto trong Python

Sản phẩm cuối

Sản phẩm cuối

Giới thiệu về AWS Boto trong Python

Sản phẩm cuối

Sản phẩm cuối

Giới thiệu về AWS Boto trong Python

Sản phẩm cuối

Sản phẩm cuối

Giới thiệu về AWS Boto trong Python

Xây hệ thống thông báo

Thiết lập topic

  • Tạo topic
  • Tải tệp csv danh bạ
  • Tạo topic cho từng dịch vụ
  • Đăng ký danh bạ vào topic tương ứng
Giới thiệu về AWS Boto trong Python

Xây hệ thống thông báo

Lấy số liệu tổng hợp

  • Tải báo cáo monthly get it done
  • Lấy số lượng Potholes
  • Lấy số lượng thông báo Illegal dumping

Gửi cảnh báo

  • Nếu potholes vượt 100, gửi cảnh báo
  • Nếu illegal dumping vượt 30, gửi cảnh báo
Giới thiệu về AWS Boto trong Python

Thiết lập topic

Khởi tạo SNS client

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

Tạo topic và lưu ARN

trash_arn = sns.create_topic(Name="trash_notifications")['TopicArn']
streets_arn = sns.create_topic(Name="streets_notifications")['TopicArn']
Giới thiệu về AWS Boto trong Python

Thiết lập topic

Thiết lập topic

Giới thiệu về AWS Boto trong Python

Đăng ký người dùng vào topic

contacts = pd.read_csv('http://gid-staging.contacts.csv')
Giới thiệu về AWS Boto trong Python

Đăng ký người dùng vào topic

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
Giới thiệu về AWS Boto trong Python

Đăng ký người dùng vào topic

Tạo hàm 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'])

Áp dụng subscribe_user cho mọi dòng

contacts.apply(subscribe_user, axis=1)
Giới thiệu về AWS Boto trong Python

Đăng ký người dùng vào topic

Đăng ký người dùng vào topic

Giới thiệu về AWS Boto trong Python

Lấy số liệu tổng hợp

Nạp báo cáo tháng 1 vào DataFrame

df = pd.read_csv('http://gid-reports.2019/feb/final_report.csv')
Giới thiệu về AWS Boto trong Python

Lấy số liệu tổng hợp

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
Giới thiệu về AWS Boto trong Python

Lấy số liệu tổng hợp

Đặt index để truy cập số theo tên dịch vụ

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

Lấy số liệu tổng hợp

trash_violations_count = df.at['Illegal Dumping', 'count']
streets_violations_count = df.at['Pothole', 'count']
Giới thiệu về AWS Boto trong Python

Gửi cảnh báo

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")
Giới thiệu về AWS Boto trong Python

Gửi cảnh báo

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")
Giới thiệu về AWS Boto trong Python

Kết quả cuối

Kết quả cuối

Giới thiệu về AWS Boto trong Python

Ayo berlatih!

Giới thiệu về AWS Boto trong Python

Preparing Video For Download...