mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-16 20:48:37 -04:00
refactor performance platform code
so that it doesn't appear generic when it's actually specific to sending the daily notification totals. To do this, split it out into a separate performance_platform directory, containing the business logic, and make the performance_platform_client incredibly thin - all it handles is adding ids to payloads, and sending stats. Also, some changes to the config (not all done yet) since there is one token per endpoint, not one for the whole platform as we'd previously coded
This commit is contained in:
@@ -8,6 +8,7 @@ from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from app.aws import s3
|
||||
from app import notify_celery
|
||||
from app.performance_platform import total_sent_notifications
|
||||
from app import performance_platform_client
|
||||
from app.dao.date_util import get_month_start_and_end_date_in_utc
|
||||
from app.dao.inbound_sms_dao import delete_inbound_sms_created_more_than_a_week_ago
|
||||
@@ -175,7 +176,7 @@ def timeout_notifications():
|
||||
@statsd(namespace="tasks")
|
||||
def send_daily_performance_platform_stats():
|
||||
if performance_platform_client.active:
|
||||
count_dict = performance_platform_client.get_total_sent_notifications_yesterday()
|
||||
count_dict = total_sent_notifications.get_total_sent_notifications_yesterday()
|
||||
email_sent_count = count_dict.get('email').get('count')
|
||||
sms_sent_count = count_dict.get('sms').get('count')
|
||||
start_date = count_dict.get('start_date')
|
||||
@@ -185,14 +186,14 @@ def send_daily_performance_platform_stats():
|
||||
.format(start_date, email_sent_count, sms_sent_count)
|
||||
)
|
||||
|
||||
performance_platform_client.send_performance_stats(
|
||||
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
||||
start_date,
|
||||
'sms',
|
||||
sms_sent_count,
|
||||
'day'
|
||||
)
|
||||
|
||||
performance_platform_client.send_performance_stats(
|
||||
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
||||
start_date,
|
||||
'email',
|
||||
email_sent_count,
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import base64
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
import requests
|
||||
from flask import current_app
|
||||
|
||||
from app.utils import get_midnight_for_day_before, get_london_midnight_in_utc, convert_utc_to_bst
|
||||
import requests
|
||||
|
||||
|
||||
class PerformancePlatformClient:
|
||||
@@ -14,72 +11,40 @@ class PerformancePlatformClient:
|
||||
def active(self):
|
||||
return self._active
|
||||
|
||||
@active.setter
|
||||
def active(self, value):
|
||||
self._active = value
|
||||
|
||||
def init_app(self, app):
|
||||
self._active = app.config.get('PERFORMANCE_PLATFORM_ENABLED')
|
||||
if self.active:
|
||||
self.bearer_token = app.config.get('PERFORMANCE_PLATFORM_TOKEN')
|
||||
self.performance_platform_url = app.config.get('PERFORMANCE_PLATFORM_URL')
|
||||
self.performance_platform_endpoints = app.config.get('PERFORMANCE_PLATFORM_ENDPOINTS')
|
||||
|
||||
def send_performance_stats(self, date, channel, count, period):
|
||||
def send_stats_to_performance_platform(self, dataset, payload):
|
||||
if self.active:
|
||||
payload = {
|
||||
'_timestamp': convert_utc_to_bst(date).isoformat(),
|
||||
'service': 'govuk-notify',
|
||||
'channel': channel,
|
||||
'count': count,
|
||||
'dataType': 'notifications',
|
||||
'period': period
|
||||
bearer_token = self.performance_platform_endpoints[dataset]
|
||||
headers = {
|
||||
'Content-Type': "application/json",
|
||||
'Authorization': 'Bearer {}'.format(bearer_token)
|
||||
}
|
||||
self._add_id_for_payload(payload)
|
||||
self._send_stats_to_performance_platform(payload)
|
||||
|
||||
def get_total_sent_notifications_yesterday(self):
|
||||
today = datetime.utcnow()
|
||||
start_date = get_midnight_for_day_before(today)
|
||||
end_date = get_london_midnight_in_utc(today)
|
||||
|
||||
from app.dao.notifications_dao import get_total_sent_notifications_in_date_range
|
||||
email_count = get_total_sent_notifications_in_date_range(start_date, end_date, 'email')
|
||||
sms_count = get_total_sent_notifications_in_date_range(start_date, end_date, 'sms')
|
||||
|
||||
return {
|
||||
"start_date": start_date,
|
||||
"email": {
|
||||
"count": email_count
|
||||
},
|
||||
"sms": {
|
||||
"count": sms_count
|
||||
}
|
||||
}
|
||||
|
||||
def _send_stats_to_performance_platform(self, payload):
|
||||
headers = {
|
||||
'Content-Type': "application/json",
|
||||
'Authorization': 'Bearer {}'.format(self.bearer_token)
|
||||
}
|
||||
resp = requests.post(
|
||||
self.performance_platform_url,
|
||||
json=payload,
|
||||
headers=headers
|
||||
)
|
||||
|
||||
if resp.status_code == 200:
|
||||
current_app.logger.info(
|
||||
"Updated performance platform successfully with payload {}".format(json.dumps(payload))
|
||||
)
|
||||
else:
|
||||
current_app.logger.error(
|
||||
"Performance platform update request failed for payload with response details: {} '{}'".format(
|
||||
json.dumps(payload),
|
||||
resp.status_code,
|
||||
resp.json())
|
||||
resp = requests.post(
|
||||
self.performance_platform_url + dataset,
|
||||
json=payload,
|
||||
headers=headers
|
||||
)
|
||||
|
||||
def _add_id_for_payload(self, payload):
|
||||
if resp.status_code == 200:
|
||||
current_app.logger.info(
|
||||
"Updated performance platform successfully with payload {}".format(json.dumps(payload))
|
||||
)
|
||||
else:
|
||||
current_app.logger.error(
|
||||
"Performance platform update request failed for payload with response details: {} '{}'".format(
|
||||
json.dumps(payload),
|
||||
resp.status_code
|
||||
)
|
||||
)
|
||||
resp.raise_for_status()
|
||||
|
||||
@staticmethod
|
||||
def add_id_to_payload(payload):
|
||||
payload_string = '{}{}{}{}{}'.format(
|
||||
payload['_timestamp'],
|
||||
payload['service'],
|
||||
|
||||
@@ -93,7 +93,7 @@ class Config(object):
|
||||
|
||||
# Performance platform
|
||||
PERFORMANCE_PLATFORM_ENABLED = False
|
||||
PERFORMANCE_PLATFORM_URL = 'https://www.performance.service.gov.uk/data/govuk-notify/notifications'
|
||||
PERFORMANCE_PLATFORM_URL = 'https://www.performance.service.gov.uk/data/govuk-notify/'
|
||||
PERFORMANCE_PLATFORM_TOKEN = os.getenv('PERFORMANCE_PLATFORM_TOKEN')
|
||||
|
||||
# Logging
|
||||
|
||||
0
app/performance_platform/__init__.py
Normal file
0
app/performance_platform/__init__.py
Normal file
44
app/performance_platform/total_sent_notifications.py
Normal file
44
app/performance_platform/total_sent_notifications.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from datetime import datetime
|
||||
|
||||
from app import performance_platform_client
|
||||
from app.dao.notifications_dao import get_total_sent_notifications_in_date_range
|
||||
from app.utils import (
|
||||
get_london_midnight_in_utc,
|
||||
get_midnight_for_day_before,
|
||||
convert_utc_to_bst,
|
||||
)
|
||||
|
||||
|
||||
def send_total_notifications_sent_for_day_stats(date, channel, count, period):
|
||||
payload = {
|
||||
'_timestamp': convert_utc_to_bst(date).isoformat(),
|
||||
'service': 'govuk-notify',
|
||||
'channel': channel,
|
||||
'count': count,
|
||||
'dataType': 'notifications',
|
||||
'period': period
|
||||
}
|
||||
performance_platform_client.add_id_to_payload(payload)
|
||||
|
||||
performance_platform_client.send_stats_to_performance_platform(
|
||||
dataset='notifications',
|
||||
payload=payload
|
||||
)
|
||||
|
||||
def get_total_sent_notifications_yesterday():
|
||||
today = datetime.utcnow()
|
||||
start_date = get_midnight_for_day_before(today)
|
||||
end_date = get_london_midnight_in_utc(today)
|
||||
|
||||
email_count = get_total_sent_notifications_in_date_range(start_date, end_date, 'email')
|
||||
sms_count = get_total_sent_notifications_in_date_range(start_date, end_date, 'sms')
|
||||
|
||||
return {
|
||||
"start_date": start_date,
|
||||
"email": {
|
||||
"count": email_count
|
||||
},
|
||||
"sms": {
|
||||
"count": sms_count
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user