Add dao method to get template stats per month in financial year for service

This commit is contained in:
Imdad Ahad
2017-02-14 17:59:18 +00:00
parent b0d5def289
commit 8272a4388d
5 changed files with 162 additions and 9 deletions

View File

@@ -29,6 +29,7 @@ from app.models import (
NOTIFICATION_STATUS_TYPES,
TEMPLATE_TYPES,
)
from app.service.statistics import format_monthly_template_notification_stats
from app.statsd_decorators import statsd
from app.utils import get_london_month_from_utc_column
@@ -224,6 +225,38 @@ def _stats_for_service_query(service_id):
)
@statsd(namespace="dao")
def dao_fetch_monthly_historical_stats_by_template_for_service(service_id, year):
month = get_london_month_from_utc_column(NotificationHistory.created_at)
sq = db.session.query(
NotificationHistory.template_id,
NotificationHistory.status,
month.label('month'),
func.count().label('count')
).filter(
NotificationHistory.service_id == service_id,
NotificationHistory.created_at.between(*get_financial_year(year))
).group_by(
month,
NotificationHistory.template_id,
NotificationHistory.status
).subquery()
rows = db.session.query(
Template.id.label('template_id'),
Template.name,
sq.c.status,
sq.c.count.label('count'),
sq.c.month
).join(
sq,
sq.c.template_id == Template.id
).all()
return format_monthly_template_notification_stats(year, rows)
@statsd(namespace="dao")
def dao_fetch_monthly_historical_stats_for_service(service_id, year):
month = get_london_month_from_utc_column(NotificationHistory.created_at)