clean up usage of dates/datetimes in performance platform tasks

* call variables unambiguous things like `start_time` or `bst_date` to
  reduce risk of passing in the wrong thing
* simplify the count_dict object - remove nested dict and start_date
  fields as superfluous
* use static datetime objects in tests rather than calculating them
  each time
This commit is contained in:
Leo Hemsted
2019-04-02 11:49:20 +01:00
parent 4abbb7137a
commit 3739d9055d
7 changed files with 64 additions and 79 deletions

View File

@@ -37,6 +37,7 @@ from app.models import (
)
from app.performance_platform import total_sent_notifications, processing_time
from app.cronitor import cronitor
from app.utils import get_london_midnight_in_utc
@notify_celery.task(name="remove_sms_email_jobs")
@@ -152,36 +153,37 @@ def timeout_notifications():
def send_daily_performance_platform_stats():
if performance_platform_client.active:
yesterday = datetime.utcnow() - timedelta(days=1)
send_total_sent_notifications_to_performance_platform(yesterday)
send_total_sent_notifications_to_performance_platform(bst_date=yesterday.date())
processing_time.send_processing_time_to_performance_platform()
def send_total_sent_notifications_to_performance_platform(day):
count_dict = total_sent_notifications.get_total_sent_notifications_for_day(day.date())
email_sent_count = count_dict.get('email').get('count')
sms_sent_count = count_dict.get('sms').get('count')
letter_sent_count = count_dict.get('letter').get('count')
start_date = count_dict.get('start_date')
def send_total_sent_notifications_to_performance_platform(bst_date):
count_dict = total_sent_notifications.get_total_sent_notifications_for_day(bst_date)
start_time = get_london_midnight_in_utc(bst_date)
email_sent_count = count_dict['email']
sms_sent_count = count_dict['sms']
letter_sent_count = count_dict['letter']
current_app.logger.info(
"Attempting to update Performance Platform for {} with {} emails, {} text messages and {} letters"
.format(start_date, email_sent_count, sms_sent_count, letter_sent_count)
.format(bst_date, email_sent_count, sms_sent_count, letter_sent_count)
)
total_sent_notifications.send_total_notifications_sent_for_day_stats(
start_date,
start_time,
'sms',
sms_sent_count
)
total_sent_notifications.send_total_notifications_sent_for_day_stats(
start_date,
start_time,
'email',
email_sent_count
)
total_sent_notifications.send_total_notifications_sent_for_day_stats(
start_date,
start_time,
'letter',
letter_sent_count
)

View File

@@ -46,17 +46,17 @@ class PerformancePlatformClient:
resp.raise_for_status()
@staticmethod
def format_payload(*, dataset, date, group_name, group_value, count, period='day'):
def format_payload(*, dataset, start_time, 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 start_time - UTC midnight of the day 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(),
'_timestamp': convert_utc_to_bst(start_time).isoformat(),
'service': 'govuk-notify',
'dataType': dataset,
'period': period,

View File

@@ -9,29 +9,29 @@ from app import performance_platform_client
def send_processing_time_to_performance_platform():
today = datetime.utcnow()
start_date = get_midnight_for_day_before(today)
end_date = get_london_midnight_in_utc(today)
start_time = get_midnight_for_day_before(today)
end_time = get_london_midnight_in_utc(today)
send_processing_time_for_start_and_end(start_date, end_date)
send_processing_time_for_start_and_end(start_time, end_time)
def send_processing_time_for_start_and_end(start_date, end_date):
result = dao_get_total_notifications_sent_per_day_for_performance_platform(start_date, end_date)
def send_processing_time_for_start_and_end(start_time, end_time):
result = dao_get_total_notifications_sent_per_day_for_performance_platform(start_time, end_time)
current_app.logger.info(
'Sending processing-time to performance platform for date {}. Total: {}, under 10 secs {}'.format(
start_date, result.messages_total, result.messages_within_10_secs
start_time, result.messages_total, result.messages_within_10_secs
)
)
send_processing_time_data(start_date, 'messages-total', result.messages_total)
send_processing_time_data(start_date, 'messages-within-10-secs', result.messages_within_10_secs)
send_processing_time_data(start_time, 'messages-total', result.messages_total)
send_processing_time_data(start_time, 'messages-within-10-secs', result.messages_within_10_secs)
def send_processing_time_data(date, status, count):
def send_processing_time_data(start_time, status, count):
payload = performance_platform_client.format_payload(
dataset='processing-time',
date=date,
start_time=start_time,
group_name='status',
group_value=status,
count=count

View File

@@ -1,12 +1,11 @@
from app import performance_platform_client
from app.dao.fact_notification_status_dao import get_total_sent_notifications_for_day_and_type
from app.utils import get_london_midnight_in_utc
def send_total_notifications_sent_for_day_stats(date, notification_type, count):
def send_total_notifications_sent_for_day_stats(start_time, notification_type, count):
payload = performance_platform_client.format_payload(
dataset='notifications',
date=date,
start_time=start_time,
group_name='channel',
group_value=notification_type,
count=count
@@ -20,17 +19,8 @@ def get_total_sent_notifications_for_day(day):
sms_count = get_total_sent_notifications_for_day_and_type(day, 'sms')
letter_count = get_total_sent_notifications_for_day_and_type(day, 'letter')
start_date = get_london_midnight_in_utc(day)
return {
"start_date": start_date,
"email": {
"count": email_count
},
"sms": {
"count": sms_count
},
"letter": {
"count": letter_count
},
"email": email_count,
"sms": sms_count,
"letter": letter_count,
}