사례 연구: 알림 시스템 구축

Python으로 시작하는 AWS Boto

Maksim Pecherskiy

Data Engineer

최종 결과물

최종 결과물

최종 결과물

Python으로 시작하는 AWS Boto

최종 결과물

최종 결과물

Python으로 시작하는 AWS Boto

최종 결과물

최종 결과물

Python으로 시작하는 AWS Boto

최종 결과물

최종 결과물

Python으로 시작하는 AWS Boto

알림 시스템 구축

토픽 설정

  • 토픽 생성
  • 연락처 CSV 다운로드
  • 서비스별 토픽 생성
  • 각 토픽에 연락처 구독 설정
Python으로 시작하는 AWS Boto

알림 시스템 구축

집계 수치 가져오기

  • 월간 보고서 다운로드
  • Pothole 건수 확인
  • Illegal dumping 알림 건수 확인

알림 전송

  • Pothole이 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

집계 수치 가져오기

서비스명을 인덱스로 설정하여 바로 조회

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:

# 보낼 메시지 구성 message = "Trash violations count is now {}".format(trash_violations_count)
# 메시지 전송 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...