mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Create a new query for template monthly stats.
This commit is contained in:
@@ -4,12 +4,12 @@ from flask import current_app
|
||||
from notifications_utils.timezones import convert_bst_to_utc
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
from sqlalchemy.sql.expression import literal
|
||||
from sqlalchemy.sql.expression import literal, extract
|
||||
from sqlalchemy.types import DateTime, Integer
|
||||
|
||||
from app import db
|
||||
from app.models import Notification, NotificationHistory, FactNotificationStatus, KEY_TYPE_TEST, Service
|
||||
from app.utils import get_london_midnight_in_utc, midnight_n_days_ago
|
||||
from app.models import Notification, NotificationHistory, FactNotificationStatus, KEY_TYPE_TEST, Service, Template
|
||||
from app.utils import get_london_midnight_in_utc, midnight_n_days_ago, get_london_month_from_utc_column
|
||||
|
||||
|
||||
def fetch_notification_status_for_day(process_day, service_id=None):
|
||||
@@ -291,3 +291,81 @@ def fetch_stats_for_all_services_by_date_range(start_date, end_date, include_fro
|
||||
else:
|
||||
query = stats
|
||||
return query.all()
|
||||
|
||||
|
||||
def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
|
||||
# services_dao.replaces dao_fetch_monthly_historical_usage_by_template_for_service
|
||||
stats = db.session.query(
|
||||
FactNotificationStatus.template_id.label('template_id'),
|
||||
Template.name.label('name'),
|
||||
Template.template_type.label('template_type'),
|
||||
Template.is_precompiled_letter.label('is_precompiled_letter'),
|
||||
extract('month', FactNotificationStatus.bst_date).label('month'),
|
||||
extract('year', FactNotificationStatus.bst_date).label('year'),
|
||||
func.sum(FactNotificationStatus.notification_count).label('count')
|
||||
).join(
|
||||
Template, FactNotificationStatus.template_id == Template.id
|
||||
).filter(
|
||||
FactNotificationStatus.service_id == service_id,
|
||||
FactNotificationStatus.bst_date >= start_date,
|
||||
FactNotificationStatus.bst_date <= end_date,
|
||||
).group_by(
|
||||
FactNotificationStatus.template_id,
|
||||
Template.name,
|
||||
Template.template_type,
|
||||
Template.is_precompiled_letter,
|
||||
extract('month', FactNotificationStatus.bst_date).label('month'),
|
||||
extract('year', FactNotificationStatus.bst_date).label('year'),
|
||||
)
|
||||
|
||||
if start_date <= datetime.utcnow() <= end_date:
|
||||
today = get_london_midnight_in_utc(datetime.utcnow())
|
||||
month = get_london_month_from_utc_column(Notification.created_at)
|
||||
|
||||
stats_for_today = db.session.query(
|
||||
Notification.template_id.label('template_id'),
|
||||
Template.name.label('name'),
|
||||
Template.template_type.label('template_type'),
|
||||
Template.is_precompiled_letter.label('is_precompiled_letter'),
|
||||
extract('month', month).label('month'),
|
||||
extract('year', month).label('year'),
|
||||
func.count().label('count')
|
||||
).join(
|
||||
Template, Notification.template_id == Template.id,
|
||||
).filter(
|
||||
Notification.created_at >= today,
|
||||
Notification.service_id == service_id,
|
||||
# we don't want to include test keys
|
||||
Notification.key_type != KEY_TYPE_TEST
|
||||
).group_by(
|
||||
Notification.template_id,
|
||||
Template.hidden,
|
||||
Template.name,
|
||||
Template.template_type,
|
||||
month
|
||||
)
|
||||
|
||||
all_stats_table = stats.union_all(stats_for_today).subquery()
|
||||
query = db.session.query(
|
||||
all_stats_table.c.template_id,
|
||||
all_stats_table.c.name,
|
||||
all_stats_table.c.is_precompiled_letter,
|
||||
all_stats_table.c.template_type,
|
||||
all_stats_table.c.month,
|
||||
all_stats_table.c.year,
|
||||
func.cast(func.sum(all_stats_table.c.count), Integer).label('count'),
|
||||
).group_by(
|
||||
all_stats_table.c.template_id,
|
||||
all_stats_table.c.name,
|
||||
all_stats_table.c.is_precompiled_letter,
|
||||
all_stats_table.c.template_type,
|
||||
all_stats_table.c.month,
|
||||
all_stats_table.c.year,
|
||||
).order_by(
|
||||
all_stats_table.c.year,
|
||||
all_stats_table.c.month,
|
||||
all_stats_table.c.name
|
||||
)
|
||||
else:
|
||||
query = stats
|
||||
return query.all()
|
||||
|
||||
@@ -24,7 +24,8 @@ from app.dao.fact_notification_status_dao import (
|
||||
fetch_notification_status_for_service_by_month,
|
||||
fetch_notification_status_for_service_for_day,
|
||||
fetch_notification_status_for_service_for_today_and_7_previous_days,
|
||||
fetch_stats_for_all_services_by_date_range)
|
||||
fetch_stats_for_all_services_by_date_range, fetch_monthly_template_usage_for_service
|
||||
)
|
||||
from app.dao.inbound_numbers_dao import dao_allocate_number_for_service
|
||||
from app.dao.organisation_dao import dao_get_organisation_by_service_id
|
||||
from app.dao.service_data_retention_dao import (
|
||||
@@ -579,10 +580,16 @@ def resume_service(service_id):
|
||||
@service_blueprint.route('/<uuid:service_id>/notifications/templates_usage/monthly', methods=['GET'])
|
||||
def get_monthly_template_usage(service_id):
|
||||
try:
|
||||
data = dao_fetch_monthly_historical_usage_by_template_for_service(
|
||||
service_id,
|
||||
int(request.args.get('year', 'NaN'))
|
||||
start_date, end_date = get_financial_year(int(request.args.get('year', 'NaN')))
|
||||
data = fetch_monthly_template_usage_for_service(
|
||||
start_date=start_date,
|
||||
end_date=end_date,
|
||||
service_id=service_id
|
||||
)
|
||||
# data = dao_fetch_monthly_historical_usage_by_template_for_service(
|
||||
# service_id,
|
||||
# int(request.args.get('year', 'NaN'))
|
||||
# )
|
||||
|
||||
stats = list()
|
||||
for i in data:
|
||||
|
||||
Reference in New Issue
Block a user