Merge pull request #2298 from alphagov/use-ft-notification-statu-for-monthly-template-usage

Use ft_notification_status for monthly template usage
This commit is contained in:
Rebecca Law
2019-01-14 15:43:41 +00:00
committed by GitHub
4 changed files with 223 additions and 25 deletions

View File

@@ -4,12 +4,15 @@ 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,
NOTIFICATION_CANCELLED
)
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 +294,86 @@ 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,
FactNotificationStatus.notification_status != NOTIFICATION_CANCELLED
).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'),
).order_by(
extract('year', FactNotificationStatus.bst_date),
extract('month', FactNotificationStatus.bst_date),
Template.name
)
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,
Notification.key_type != KEY_TYPE_TEST,
Notification.status != NOTIFICATION_CANCELLED
).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,
func.cast(all_stats_table.c.month, Integer).label('month'),
func.cast(all_stats_table.c.year, Integer).label('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()

View File

@@ -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 (
@@ -48,7 +49,6 @@ from app.dao.services_dao import (
dao_create_service,
dao_fetch_all_services,
dao_fetch_all_services_by_user,
dao_fetch_monthly_historical_usage_by_template_for_service,
dao_fetch_service_by_id,
dao_fetch_todays_stats_for_service,
dao_fetch_todays_stats_for_all_services,
@@ -579,11 +579,12 @@ 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
)
stats = list()
for i in data:
stats.append(