mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-24 01:11:38 -05:00
Add query to get template usage from the Notifications History table
- groups by template Id and Day. Returns count per day, template name, template id, template type, and day. Ordered by day (desc) and template name (acc)
This commit is contained in:
@@ -9,7 +9,6 @@ from flask import current_app
|
||||
from werkzeug.datastructures import MultiDict
|
||||
from sqlalchemy import (desc, func, Integer, or_, and_, asc)
|
||||
from sqlalchemy.sql.expression import cast
|
||||
from notifications_utils.template import get_sms_fragment_count
|
||||
|
||||
from app import db
|
||||
from app.dao import days_ago
|
||||
@@ -135,6 +134,28 @@ def dao_get_7_day_agg_notification_statistics_for_service(service_id,
|
||||
)
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_template_usage(service_id, limit_days=None):
|
||||
|
||||
query = db.session.query(
|
||||
func.count(NotificationHistory.template_id).label('count'),
|
||||
NotificationHistory.template_id,
|
||||
func.DATE(NotificationHistory.created_at).label('day'),
|
||||
Template.name,
|
||||
Template.template_type
|
||||
)
|
||||
|
||||
query_filter = [NotificationHistory.service_id == service_id]
|
||||
if limit_days is not None:
|
||||
query_filter.append(NotificationHistory.created_at >= days_ago(limit_days))
|
||||
|
||||
return query.filter(*query_filter) \
|
||||
.join(Template)\
|
||||
.group_by(NotificationHistory.template_id, func.DATE(NotificationHistory.created_at), Template.name, Template.template_type)\
|
||||
.order_by(desc(func.DATE(NotificationHistory.created_at)), asc(Template.name))\
|
||||
.all() # noqa
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_template_statistics_for_service(service_id, limit_days=None):
|
||||
query_filter = [TemplateStatistics.service_id == service_id]
|
||||
|
||||
Reference in New Issue
Block a user