mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-18 05:28:49 -04:00
make perf platform client handle more stuff sensibly
specifically, all of the performance platform specific data layout now happens in performance_platform_client.py - stuff like setting the _timestamp, period etc, and the perf platform-specific nomenclature is all handled there.
This commit is contained in:
@@ -87,7 +87,7 @@ def delete_verify_codes():
|
||||
current_app.logger.info(
|
||||
"Delete job started {} finished {} deleted {} verify codes".format(start, datetime.utcnow(), deleted)
|
||||
)
|
||||
except SQLAlchemyError as e:
|
||||
except SQLAlchemyError:
|
||||
current_app.logger.exception("Failed to delete verify codes")
|
||||
raise
|
||||
|
||||
@@ -106,7 +106,7 @@ def delete_sms_notifications_older_than_seven_days():
|
||||
deleted
|
||||
)
|
||||
)
|
||||
except SQLAlchemyError as e:
|
||||
except SQLAlchemyError:
|
||||
current_app.logger.exception("Failed to delete sms notifications")
|
||||
raise
|
||||
|
||||
@@ -125,7 +125,7 @@ def delete_email_notifications_older_than_seven_days():
|
||||
deleted
|
||||
)
|
||||
)
|
||||
except SQLAlchemyError as e:
|
||||
except SQLAlchemyError:
|
||||
current_app.logger.exception("Failed to delete sms notifications")
|
||||
raise
|
||||
|
||||
@@ -144,7 +144,7 @@ def delete_letter_notifications_older_than_seven_days():
|
||||
deleted
|
||||
)
|
||||
)
|
||||
except SQLAlchemyError as e:
|
||||
except SQLAlchemyError:
|
||||
current_app.logger.exception("Failed to delete sms notifications")
|
||||
raise
|
||||
|
||||
@@ -158,7 +158,7 @@ def delete_invitations():
|
||||
current_app.logger.info(
|
||||
"Delete job started {} finished {} deleted {} invitations".format(start, datetime.utcnow(), deleted)
|
||||
)
|
||||
except SQLAlchemyError as e:
|
||||
except SQLAlchemyError:
|
||||
current_app.logger.exception("Failed to delete invitations")
|
||||
raise
|
||||
|
||||
@@ -189,15 +189,13 @@ def send_daily_performance_platform_stats():
|
||||
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
||||
start_date,
|
||||
'sms',
|
||||
sms_sent_count,
|
||||
'day'
|
||||
sms_sent_count
|
||||
)
|
||||
|
||||
total_sent_notifications.send_total_notifications_sent_for_day_stats(
|
||||
start_date,
|
||||
'email',
|
||||
email_sent_count,
|
||||
'day'
|
||||
email_sent_count
|
||||
)
|
||||
|
||||
|
||||
@@ -254,7 +252,7 @@ def delete_inbound_sms_older_than_seven_days():
|
||||
deleted
|
||||
)
|
||||
)
|
||||
except SQLAlchemyError as e:
|
||||
except SQLAlchemyError:
|
||||
current_app.logger.exception("Failed to delete inbound sms notifications")
|
||||
raise
|
||||
|
||||
@@ -289,7 +287,7 @@ def delete_dvla_response_files_older_than_seven_days():
|
||||
len(older_than_seven_days)
|
||||
)
|
||||
)
|
||||
except SQLAlchemyError as e:
|
||||
except SQLAlchemyError:
|
||||
current_app.logger.exception("Failed to delete dvla response files")
|
||||
raise
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ import json
|
||||
from flask import current_app
|
||||
import requests
|
||||
|
||||
from app.utils import convert_utc_to_bst
|
||||
|
||||
|
||||
class PerformancePlatformClient:
|
||||
|
||||
@@ -17,15 +19,15 @@ class PerformancePlatformClient:
|
||||
self.performance_platform_url = app.config.get('PERFORMANCE_PLATFORM_URL')
|
||||
self.performance_platform_endpoints = app.config.get('PERFORMANCE_PLATFORM_ENDPOINTS')
|
||||
|
||||
def send_stats_to_performance_platform(self, dataset, payload):
|
||||
def send_stats_to_performance_platform(self, payload):
|
||||
if self.active:
|
||||
bearer_token = self.performance_platform_endpoints[dataset]
|
||||
bearer_token = self.performance_platform_endpoints[payload['dataType']]
|
||||
headers = {
|
||||
'Content-Type': "application/json",
|
||||
'Authorization': 'Bearer {}'.format(bearer_token)
|
||||
}
|
||||
resp = requests.post(
|
||||
self.performance_platform_url + dataset,
|
||||
self.performance_platform_url + payload['dataType'],
|
||||
json=payload,
|
||||
headers=headers
|
||||
)
|
||||
@@ -44,13 +46,37 @@ class PerformancePlatformClient:
|
||||
resp.raise_for_status()
|
||||
|
||||
@staticmethod
|
||||
def add_id_to_payload(payload):
|
||||
def format_payload(*, dataset, date, group_name, group_value, count, period='day'):
|
||||
"""
|
||||
:param dataset - the name of the overall graph, as referred to in the endpoint.
|
||||
:param date - the date we're sending stats for
|
||||
:param group_name - the name of the individual groups of data, eg "channel" or "status"
|
||||
:param group_value - the value of the group, eg "sms" or "email" for group_name=channel
|
||||
:param count - the actual numeric value to send
|
||||
:param period - the period that this data covers - "day", "week", "month", "quarter".
|
||||
"""
|
||||
payload = {
|
||||
'_timestamp': convert_utc_to_bst(date).isoformat(),
|
||||
'service': 'govuk-notify',
|
||||
'dataType': dataset,
|
||||
'period': period,
|
||||
'count': count,
|
||||
group_name: group_value,
|
||||
}
|
||||
payload['_id'] = PerformancePlatformClient.generate_payload_id(payload, group_name)
|
||||
return payload
|
||||
|
||||
@staticmethod
|
||||
def generate_payload_id(payload, group_name):
|
||||
"""
|
||||
group_name is the name of the group - eg "channel" or "status"
|
||||
"""
|
||||
payload_string = '{}{}{}{}{}'.format(
|
||||
payload['_timestamp'],
|
||||
payload['service'],
|
||||
payload['channel'],
|
||||
payload[group_name],
|
||||
payload['dataType'],
|
||||
payload['period']
|
||||
)
|
||||
_id = base64.b64encode(payload_string.encode('utf-8'))
|
||||
payload.update({'_id': _id.decode('utf-8')})
|
||||
return _id.decode('utf-8')
|
||||
|
||||
@@ -4,27 +4,21 @@ 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,
|
||||
get_midnight_for_day_before
|
||||
)
|
||||
|
||||
|
||||
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(
|
||||
def send_total_notifications_sent_for_day_stats(date, notification_type, count):
|
||||
payload = performance_platform_client.format_payload(
|
||||
dataset='notifications',
|
||||
payload=payload
|
||||
date=date,
|
||||
group_name='channel',
|
||||
group_value=notification_type,
|
||||
count=count
|
||||
)
|
||||
|
||||
performance_platform_client.send_stats_to_performance_platform(payload)
|
||||
|
||||
|
||||
def get_total_sent_notifications_yesterday():
|
||||
today = datetime.utcnow()
|
||||
|
||||
Reference in New Issue
Block a user