Merge pull request #829 from alphagov/feat-add-endpoint-for-service-monthly-usage-stats

Add endpoint for service monthly usage stats by template
This commit is contained in:
imdadahad
2017-03-09 16:12:22 +00:00
committed by GitHub
9 changed files with 270 additions and 46 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,39 @@ 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,
Template.template_type,
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)

View File

@@ -13,8 +13,8 @@ from app.dao.dao_utils import (
@transactional
@version_class(Template, TemplateHistory)
def dao_create_template(template, template_id=None):
template.id = template_id or uuid.uuid4() # must be set now so version history model can use same id
def dao_create_template(template):
template.id = uuid.uuid4() # must be set now so version history model can use same id
template.archived = False
db.session.add(template)