add day filter to template usage dao, and remove NotificationHistory

we should be very careful with when we get data from
NotificationHistory - this should probably only be from scheduled
tasks. `dao_get_template_usage` is only called from the template
statistics rest endpoint, so shouldn't ever hit template history.

also, moved tests out to new file to break up the 2k test file a bit
This commit is contained in:
Leo Hemsted
2018-04-11 18:17:29 +01:00
parent 901383777b
commit 67019fc5a1
4 changed files with 208 additions and 342 deletions

View File

@@ -46,40 +46,34 @@ from app.models import (
)
from app.dao.dao_utils import transactional
from app.utils import convert_utc_to_bst
from app.utils import convert_utc_to_bst, get_london_midnight_in_utc
@statsd(namespace="dao")
def dao_get_template_usage(service_id, limit_days=None):
def dao_get_template_usage(service_id, limit_days=None, day=None):
if bool(limit_days) == bool(day):
raise ValueError('Must filter on either limit_days or a specific day')
query_filter = []
table = NotificationHistory
if limit_days:
query_filter.append(Notification.created_at >= days_ago(limit_days))
else:
start = get_london_midnight_in_utc(day)
end = get_london_midnight_in_utc(day + timedelta(days=1))
query_filter.append(Notification.created_at >= start)
query_filter.append(Notification.created_at < end)
if limit_days is not None and limit_days <= 7:
table = Notification
# only limit days if it's not seven days, as 7 days == the whole of Notifications table.
if limit_days != 7:
query_filter.append(table.created_at >= days_ago(limit_days))
elif limit_days is not None:
# case where not under 7 days, so using NotificationsHistory so limit allowed
query_filter.append(table.created_at >= days_ago(limit_days))
query_filter.append(table.service_id == service_id)
query_filter.append(table.key_type != KEY_TYPE_TEST)
# only limit days if it's not seven days, as 7 days == the whole of Notifications table.
if limit_days is not None and limit_days != 7:
query_filter.append(table.created_at >= days_ago(limit_days))
query_filter.append(Notification.service_id == service_id)
query_filter.append(Notification.key_type != KEY_TYPE_TEST)
notifications_aggregate_query = db.session.query(
func.count().label('count'),
table.template_id
Notification.template_id
).filter(
*query_filter
).group_by(
table.template_id
Notification.template_id
).subquery()
query = db.session.query(

View File

@@ -35,7 +35,7 @@ def get_template_statistics_for_service_by_day(service_id):
message = {'limit_days': [error]}
raise InvalidRequest(message, status_code=400)
else:
limit_days = None
limit_days = 7
if limit_days == 7:
stats = get_template_statistics_for_7_days(limit_days, service_id)