Merge pull request #1205 from alphagov/perf-stats

Perf stats refactor
This commit is contained in:
Leo Hemsted
2017-08-29 10:44:09 +01:00
committed by GitHub
12 changed files with 261 additions and 200 deletions

View File

@@ -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
@@ -86,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
@@ -105,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
@@ -124,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
@@ -143,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
@@ -157,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
@@ -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,18 +186,16 @@ 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'
sms_sent_count
)
performance_platform_client.send_performance_stats(
total_sent_notifications.send_total_notifications_sent_for_day_stats(
start_date,
'email',
email_sent_count,
'day'
email_sent_count
)
@@ -253,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
@@ -288,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

View File

@@ -1,11 +1,10 @@
import base64
import json
from datetime import datetime
import requests
from flask import current_app
import requests
from app.utils import get_midnight_for_day_before, get_london_midnight_in_utc, convert_utc_to_bst
from app.utils import convert_utc_to_bst
class PerformancePlatformClient:
@@ -14,78 +13,70 @@ 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, 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[payload['dataType']]
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 + payload['dataType'],
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 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')

View File

@@ -34,6 +34,8 @@ def set_config_env_vars(vcap_services):
extract_firetext_config(s)
elif s['name'] == 'redis':
extract_redis_config(s)
elif s['name'] == 'performance-platform':
extract_performance_platform_config(s)
def extract_notify_config(notify_config):
@@ -42,10 +44,13 @@ def extract_notify_config(notify_config):
os.environ['ADMIN_CLIENT_SECRET'] = notify_config['credentials']['admin_client_secret']
os.environ['SECRET_KEY'] = notify_config['credentials']['secret_key']
os.environ['DANGEROUS_SALT'] = notify_config['credentials']['dangerous_salt']
os.environ['PERFORMANCE_PLATFORM_TOKEN'] = notify_config['credentials'].get('performance_platform_token', '')
os.environ['SMS_INBOUND_WHITELIST'] = json.dumps(notify_config['credentials']['allow_ip_inbound_sms'])
def extract_performance_platform_config(performance_platform_config):
os.environ['PERFORMANCE_PLATFORM_ENDPOINTS'] = json.dumps(performance_platform_config['credentials'])
def extract_notify_aws_config(aws_config):
os.environ['NOTIFICATION_QUEUE_PREFIX'] = aws_config['credentials']['sqs_queue_prefix']
os.environ['AWS_ACCESS_KEY_ID'] = aws_config['credentials']['aws_access_key_id']

View File

@@ -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
@@ -273,6 +273,10 @@ class Config(object):
SMS_INBOUND_WHITELIST = json.loads(os.environ.get('SMS_INBOUND_WHITELIST', '[]'))
# Format is as follows:
# {"dataset_1": "token_1", ...}
PERFORMANCE_PLATFORM_ENDPOINTS = json.loads(os.environ.get('PERFORMANCE_PLATFORM_ENDPOINTS', '{}'))
######################
# Config overrides ###

View File

View File

@@ -0,0 +1,39 @@
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
)
def send_total_notifications_sent_for_day_stats(date, notification_type, count):
payload = performance_platform_client.format_payload(
dataset='notifications',
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()
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
}
}