Create a new query for template monthly stats.

This commit is contained in:
Rebecca Law
2019-01-10 16:24:51 +00:00
parent 1719f31909
commit 507138cc94
4 changed files with 123 additions and 23 deletions

View File

@@ -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()

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 (
@@ -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:

View File

@@ -11,7 +11,8 @@ from app.dao.fact_notification_status_dao import (
fetch_notification_status_for_service_for_today_and_7_previous_days,
fetch_notification_status_totals_for_all_services,
fetch_notification_statuses_for_job,
fetch_stats_for_all_services_by_date_range)
fetch_stats_for_all_services_by_date_range, fetch_monthly_template_usage_for_service
)
from app.models import FactNotificationStatus, KEY_TYPE_TEST, KEY_TYPE_TEAM, EMAIL_TYPE, SMS_TYPE, LETTER_TYPE
from freezegun import freeze_time
from tests.app.db import create_notification, create_service, create_template, create_ft_notification_status, create_job
@@ -338,3 +339,29 @@ def test_fetch_stats_for_all_services_by_date_range(notify_db_session):
assert not results[4].notification_type
assert not results[4].status
assert not results[4].count
def test_fetch_monthly_template_usage_for_service(sample_service):
template_one = create_template(service=sample_service, template_type='sms', template_name='one')
template_two = create_template(service=sample_service, template_type='email', template_name='one')
template_three = create_template(service=sample_service, template_type='letter', template_name='one')
create_ft_notification_status(bst_date=date(2018, 1, 1),
service=sample_service,
template=template_one,
count=2)
create_ft_notification_status(bst_date=date(2018, 2, 1),
service=sample_service,
template=template_two,
count=3)
create_ft_notification_status(bst_date=date(2018, 3, 1),
service=sample_service,
template=template_three,
count=5)
results = fetch_monthly_template_usage_for_service(
datetime(2017, 4, 1), datetime(2018, 3, 31), sample_service.id
)
print(results)
assert len(results) == 3

View File

@@ -28,13 +28,7 @@ def test_get_template_usage_by_month_returns_correct_data(
admin_request,
sample_template
):
create_notification(sample_template, created_at=datetime(2016, 4, 1), status='created')
create_notification(sample_template, created_at=datetime(2017, 4, 1), status='sending')
create_notification(sample_template, created_at=datetime(2017, 4, 1), status='permanent-failure')
create_notification(sample_template, created_at=datetime(2017, 4, 1), status='temporary-failure')
daily_stats_template_usage_by_month()
create_ft_notification_status(bst_date=date(2017, 4, 2), template=sample_template, count=3)
create_notification(sample_template, created_at=datetime.utcnow())
resp_json = admin_request.get(
@@ -85,14 +79,8 @@ def test_get_template_usage_by_month_returns_two_templates(admin_request, sample
template_name=PRECOMPILED_TEMPLATE_NAME,
hidden=True
)
create_notification(template_one, created_at=datetime(2017, 4, 1), status='created')
create_notification(sample_template, created_at=datetime(2017, 4, 1), status='sending')
create_notification(sample_template, created_at=datetime(2017, 4, 1), status='permanent-failure')
create_notification(sample_template, created_at=datetime(2017, 4, 1), status='temporary-failure')
daily_stats_template_usage_by_month()
create_ft_notification_status(bst_date=datetime(2017, 4, 1), template=template_one, count=1)
create_ft_notification_status(bst_date=datetime(2017, 4, 1), template=sample_template, count=3)
create_notification(sample_template, created_at=datetime.utcnow())
resp_json = admin_request.get(