mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
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:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ from app.dao.services_dao import (
|
||||
dao_suspend_service,
|
||||
dao_resume_service,
|
||||
dao_fetch_monthly_historical_stats_for_service,
|
||||
dao_fetch_monthly_historical_stats_by_template_for_service
|
||||
)
|
||||
from app.dao.service_whitelist_dao import (
|
||||
dao_fetch_service_whitelist,
|
||||
@@ -399,3 +400,15 @@ def get_billable_unit_count(service_id):
|
||||
))
|
||||
except TypeError:
|
||||
return jsonify(result='error', message='No valid year provided'), 400
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/notifications/templates/monthly', methods=['GET'])
|
||||
def get_monthly_template_stats(service_id):
|
||||
service = dao_fetch_service_by_id(service_id)
|
||||
try:
|
||||
return jsonify(data=dao_fetch_monthly_historical_stats_by_template_for_service(
|
||||
service.id,
|
||||
int(request.args.get('year', 'NaN'))
|
||||
))
|
||||
except ValueError:
|
||||
raise InvalidRequest('Year must be a number', status_code=400)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import itertools
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from app.models import TEMPLATE_TYPES
|
||||
from app.models import NOTIFICATION_STATUS_TYPES, TEMPLATE_TYPES
|
||||
|
||||
|
||||
def format_statistics(statistics):
|
||||
@@ -15,6 +15,29 @@ def format_statistics(statistics):
|
||||
return counts
|
||||
|
||||
|
||||
def format_monthly_template_notification_stats(year, rows):
|
||||
stats = {
|
||||
datetime.strftime(date, '%Y-%m'): {}
|
||||
for date in [
|
||||
datetime(year, month, 1) for month in range(4, 13)
|
||||
] + [
|
||||
datetime(year + 1, month, 1) for month in range(1, 4)
|
||||
]
|
||||
}
|
||||
|
||||
for row in rows:
|
||||
formatted_month = row.month.strftime('%Y-%m')
|
||||
if str(row.template_id) not in stats[formatted_month]:
|
||||
stats[formatted_month][str(row.template_id)] = {
|
||||
"name": row.name,
|
||||
"type": row.template_type,
|
||||
"counts": dict.fromkeys(NOTIFICATION_STATUS_TYPES, 0)
|
||||
}
|
||||
stats[formatted_month][str(row.template_id)]["counts"][row.status] += row.count
|
||||
|
||||
return stats
|
||||
|
||||
|
||||
def create_zeroed_stats_dicts():
|
||||
return {
|
||||
template_type: {
|
||||
|
||||
Reference in New Issue
Block a user