mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-03 13:18:32 -04:00
refactor template stats endpoint to read from new redis keys
New redis keys are partitioned per service per day. New process is as follows: * require a count of days to filter by. Currently admin always gives 7. * for each day, check and see if there's anything in redis. There won't be if either a) redis is/was down or b) the service didn't send any notifications that day - if there isn't, go to the database and get a count out. * combine all these stats together * get the names/template types etc out of the DB at the end.
This commit is contained in:
20
app/utils.py
20
app/utils.py
@@ -80,6 +80,9 @@ def cache_key_for_service_template_counter(service_id, limit_days=7):
|
||||
|
||||
|
||||
def cache_key_for_service_template_usage_per_day(service_id, datetime):
|
||||
"""
|
||||
You should probably pass a BST datetime into this function
|
||||
"""
|
||||
return "service-{}-template-usage-{}".format(service_id, datetime.date().isoformat())
|
||||
|
||||
|
||||
@@ -96,4 +99,19 @@ def days_ago(number_of_days):
|
||||
"""
|
||||
Returns midnight a number of days ago. Takes care of daylight savings etc.
|
||||
"""
|
||||
return get_london_midnight_in_utc(datetime.utcnow() - timedelta(number_of_days))
|
||||
return get_london_midnight_in_utc(datetime.utcnow() - timedelta(days=number_of_days))
|
||||
|
||||
|
||||
def last_n_days(limit_days):
|
||||
"""
|
||||
Returns the last n dates, oldest first. Takes care of daylight savings (but returns a date, be careful how you manipulate it later! Don't
|
||||
directly use the date for comparing to UTC datetimes!). Includes today.
|
||||
"""
|
||||
return [
|
||||
datetime.combine(
|
||||
(convert_utc_to_bst(datetime.utcnow()) - timedelta(days=x)),
|
||||
datetime.min.time()
|
||||
)
|
||||
# reverse the countdown, -1 from first two args to ensure it stays 0-indexed
|
||||
for x in range(limit_days - 1, -1, -1)
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user