Group new template-statistics response by template

New API template-statistics response returns notification counts for
each template and status combination. This can be used for both
service statistics counts and template statistics by grouping the
counts either by status or by template.
This commit is contained in:
Alexey Bezhan
2019-01-15 15:55:04 +00:00
parent 7c92847b85
commit 46bfb541c8

View File

@@ -1,6 +1,7 @@
import calendar
from datetime import datetime
from functools import partial
from itertools import groupby
from flask import (
Response,
@@ -265,16 +266,24 @@ def get_inbox_partials(service_id):
)}
def aggregate_usage(template_statistics, sort_key='count'):
return sorted(
template_statistics,
key=lambda template_statistic: template_statistic[sort_key],
reverse=True
)
def aggregate_template_usage(template_statistics, sort_key='count'):
templates = []
for k, v in groupby(sorted(template_statistics, key=lambda x: x['template_id']), key=lambda x: x['template_id']):
template_stats = [s for s in v if s.get('status') != 'cancelled']
templates.append({
"template_id": k,
"template_name": template_stats[0]['template_name'],
"template_type": template_stats[0]['template_type'],
"is_precompiled_letter": template_stats[0]['is_precompiled_letter'],
"count": sum(s['count'] for s in template_stats)
})
return sorted(templates, key=lambda x: x[sort_key], reverse=True)
def get_dashboard_partials(service_id):
template_statistics = aggregate_usage(
template_statistics = aggregate_template_usage(
template_statistics_client.get_template_statistics_for_service(service_id, limit_days=7)
)