Merge pull request #1399 from alphagov/rc-monthly-template-usage-endpoint

Added a template_type to the rest call and Aggregation Bug Fix
This commit is contained in:
Richard Chapman
2017-11-16 15:55:55 +00:00
committed by GitHub
7 changed files with 131 additions and 14 deletions

View File

@@ -243,7 +243,7 @@ class Config(object):
'options': {'queue': QueueNames.PERIODIC}
},
'daily-stats-template-usage-by-month': {
'task': 'daily-stats-template_usage_by_month',
'task': 'daily-stats-template-usage-by-month',
'schedule': crontab(hour=0, minute=50),
'options': {'queue': QueueNames.PERIODIC}
}

View File

@@ -554,6 +554,7 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
for result in results:
stat = type("", (), {})()
stat.template_id = result.template_id
stat.template_type = result.template_type
stat.name = str(result.name)
stat.month = result.month
stat.year = result.year
@@ -567,16 +568,18 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
today_results = db.session.query(
Notification.template_id,
Template.name,
Template.template_type,
extract('month', month).label('month'),
extract('year', year).label('year'),
func.count().label('count')
).join(
Template, Notification.template_id == Template.id
Template, Notification.template_id == Template.id,
).filter(
Notification.created_at >= start_date
).group_by(
Notification.template_id,
Template.name,
Template.template_type,
month,
year
).order_by(
@@ -586,13 +589,15 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
for today_result in today_results:
add_to_stats = True
for stat in stats:
if today_result.template_id == stat.template_id:
if today_result.template_id == stat.template_id and today_result.month == stat.month \
and today_result.year == stat.year:
stat.count = stat.count + today_result.count
add_to_stats = False
if add_to_stats:
new_stat = type("", (), {})()
new_stat.template_id = today_result.template_id
new_stat.template_type = today_result.template_type
new_stat.name = today_result.name
new_stat.month = today_result.month
new_stat.year = today_result.year

View File

@@ -34,6 +34,7 @@ def dao_get_template_usage_stats_by_service(service_id, year):
return db.session.query(
StatsTemplateUsageByMonth.template_id,
Template.name,
Template.template_type,
StatsTemplateUsageByMonth.month,
StatsTemplateUsageByMonth.year,
StatsTemplateUsageByMonth.count

View File

@@ -539,6 +539,7 @@ def get_monthly_template_usage(service_id):
{
'template_id': str(i.template_id),
'name': i.name,
'type': i.template_type,
'month': i.month,
'year': i.year,
'count': i.count