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:
Leo Hemsted
2017-08-24 17:08:39 +01:00
parent 412c87cfc8
commit e85b621cbc
8 changed files with 64 additions and 47 deletions

View File

@@ -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')