From 53b9ca5b71a03068760a3b042ed02c1eece014a1 Mon Sep 17 00:00:00 2001 From: Manzi Fabrice Date: Tue, 29 Oct 2019 10:35:39 +0200 Subject: [PATCH] CV3-55-story(notifications): make notifications calls run on celery. - hange these receive calls to be asynchronous and handled by celery [Delivers CV3-55] --- .gitignore | 3 ++ app.py | 7 ++- service/external_calls_notification.py | 67 ++++++++++++++++++++++++++ service/push_notification.py | 42 +++++----------- templates/log.html | 32 ++++++------ 5 files changed, 104 insertions(+), 47 deletions(-) create mode 100644 service/external_calls_notification.py diff --git a/.gitignore b/.gitignore index 1b1b64b..e330c9f 100644 --- a/.gitignore +++ b/.gitignore @@ -120,6 +120,9 @@ html_coverage_report/ # credentials credentials.json +# binary file where snapshots saved +dump.rdb + #logs service.err.log service.out.log diff --git a/app.py b/app.py index ff56d12..edadd43 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,4 @@ -from flask import Flask, render_template, request, Response +from flask import Flask, render_template, request, Response, jsonify from flask_json import FlaskJSON from flask_cors import CORS from config import config @@ -6,6 +6,7 @@ import os import json + vapid_public_key = os.getenv("VAPID_PUBLIC_KEY") @@ -23,7 +24,9 @@ def index(): @app.route("/notifications", methods=['POST', 'GET']) def calendar_notifications(): PushNotification().send_notifications_to_subscribers() - return PushNotification.send_notifications(PushNotification) + result = PushNotification.send_notifications(PushNotification) + return result + @app.route("/channels", methods=['POST', 'GET']) def create_channels(): diff --git a/service/external_calls_notification.py b/service/external_calls_notification.py new file mode 100644 index 0000000..0468410 --- /dev/null +++ b/service/external_calls_notification.py @@ -0,0 +1,67 @@ +import requests +import os +import json + +from flask import Flask +from pyfcm import FCMNotification + +from config import config +from utilities.utility import save_to_db +import celery + +config_name = os.getenv('APP_SETTINGS') +push_service = FCMNotification(api_key=os.getenv('FCM_API_KEY')) +api_token = os.getenv('USER_TOKEN') + +notification_url = config.get(config_name).NOTIFICATION_URL +url = config.get(config_name).CONVERGE_MRM_URL +app = Flask(__name__) + + +class ExternalCallsNotifications(): + #notify a single device and save notifications + @celery.task(name='push_notification-single-device') + def notify_device(firebase_token, calendar_id): + try: + results = push_service.notify_single_device( + registration_id = firebase_token, + message_body="success") + result = {} + result['results'] = results['results'] + result['subscriber_info'] = firebase_token + result['platform'] = 'android' + result['calendar_id'] = calendar_id + save_to_db(result) + return results + except Exception as error: + results = error + result = {} + result['results'] = results + result['subscriber_info'] = firebase_token + result['platform'] = 'android' + result['calendar_id'] = calendar_id + save_to_db(result) + return error + + + #update the backend API method + @celery.task(name='push_notification-update-backend') + def update_backend(calendar_id): + try: + notify_api_mutation = ( + { + 'query': + """ + mutation { + mrmNotification ( calendarId: \"%s\" ) { + message + } + } + """ % calendar_id + } + ) + headers = {'Authorization': 'Bearer %s' % api_token} + requests.post(url=url, json=notify_api_mutation, headers=headers) + except Exception as error: + return error + diff --git a/service/push_notification.py b/service/push_notification.py index b5ec2c3..bc94cae 100644 --- a/service/push_notification.py +++ b/service/push_notification.py @@ -5,10 +5,11 @@ import ast from helpers.database import db -from flask import jsonify, request, render_template, Flask +from flask import jsonify, request, render_template, Flask, Response from pyfcm import FCMNotification from pywebpush import webpush, WebPushException +from .external_calls_notification import ExternalCallsNotifications from config import config from helpers.credentials import Credentials from utilities.utility import stop_channel, save_to_db @@ -16,7 +17,6 @@ from helpers.calendar import update_calendar import celery - config_name = os.getenv('APP_SETTINGS') push_service = FCMNotification(api_key=os.getenv('FCM_API_KEY')) api_token = os.getenv('USER_TOKEN') @@ -132,9 +132,10 @@ def manual_create_channels(self): service = Credentials.set_api_credentials(self) calendar = {} calendars = [] - channels = [] + channels = [1] for key in db.keys('*Calendar*'): calendar = db.hgetall(key) + print(key) calendar['key'] = key calendars.append(calendar) @@ -171,39 +172,22 @@ def send_notifications(self): selected_calendar = calendar break if 'firebase_token' in selected_calendar.keys(): - results = push_service.notify_single_device( - registration_id=selected_calendar['firebase_token'], - message_body="success") - result = {} - result['results'] = results['results'] - result['subscriber_info'] = selected_calendar['firebase_token'] - result['platform'] = 'android' - result['calendar_id'] = selected_calendar['calendar_id'] - save_to_db(result) - + firebase_token = selected_calendar['firebase_token'] + calendar_id = selected_calendar['calendar_id'] + ExternalCallsNotifications.notify_device.delay(firebase_token, calendar_id) + # Send an update to the backend API if 'calendar_id' in selected_calendar.keys(): - notify_api_mutation = ( - { - 'query': - """ - mutation { - mrmNotification ( calendarId: \"%s\" ) { - message - } - } - """ % selected_calendar['calendar_id'] - } - ) - headers = {'Authorization': 'Bearer %s' % api_token} - requests.post(url=url, json=notify_api_mutation, headers=headers) + calendar_id = selected_calendar['calendar_id'] + ExternalCallsNotifications.update_backend.delay(calendar_id) + results = { + "message": "Notification has been sent" + } return jsonify(results) - data = { "message": "Notification received but no registered device" } response = jsonify(data) - return response def send_web_notification(self, subscriber, calendar_id): diff --git a/templates/log.html b/templates/log.html index a1ea521..5c7d366 100644 --- a/templates/log.html +++ b/templates/log.html @@ -29,23 +29,23 @@

Push Notification Logs

- - - - - - - - - - - {% for item in result%} - {% for value in item.values() %} - - {% endfor %} - - {% endfor %} + + + + + + + + + + {% for item in result%} + + {% for value in item.values() %} + + {% endfor %} + + {% endfor %}
TimeResultsSubscriber InfoPlatformCalendar_id
{{value}}
TimeResultsSubscriber InfoPlatformCalendar_id
{{value}}