mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-31 15:15:38 -05:00
Report for total notifications sent per day for each channel.
Daily volumes report: total volumes across the platform aggregated by whole business day (bst_date) Volumes by service report: total volumes per service aggregated by the date range given. NB: start and end dates are inclusive
This commit is contained in:
@@ -2,7 +2,7 @@ from datetime import date, datetime, timedelta
|
||||
|
||||
from flask import current_app
|
||||
from notifications_utils.timezones import convert_utc_to_bst
|
||||
from sqlalchemy import Date, Integer, and_, desc, func
|
||||
from sqlalchemy import Date, Integer, Numeric, and_, desc, func
|
||||
from sqlalchemy.dialects.postgresql import insert
|
||||
from sqlalchemy.sql.expression import case, literal
|
||||
|
||||
@@ -716,3 +716,151 @@ def fetch_billing_details_for_all_services():
|
||||
).all()
|
||||
|
||||
return billing_details
|
||||
|
||||
|
||||
def fetch_daily_volumes_for_platform(start_date, end_date):
|
||||
# query to return the total notifications sent per day for each channel. NB start and end dates are inclusive
|
||||
|
||||
daily_volume_stats = db.session.query(
|
||||
FactBilling.bst_date,
|
||||
func.sum(case(
|
||||
[
|
||||
(FactBilling.notification_type == SMS_TYPE, FactBilling.notifications_sent)
|
||||
], else_=0
|
||||
)).label('sms_totals'),
|
||||
func.sum(case(
|
||||
[
|
||||
(FactBilling.notification_type == SMS_TYPE, FactBilling.billable_units)
|
||||
], else_=0
|
||||
)).label('sms_fragment_totals'),
|
||||
func.sum(case(
|
||||
[
|
||||
(FactBilling.notification_type == SMS_TYPE, FactBilling.billable_units * FactBilling.rate_multiplier)
|
||||
], else_=0
|
||||
)).label('sms_fragments_times_multiplier'),
|
||||
func.sum(case(
|
||||
[
|
||||
(FactBilling.notification_type == EMAIL_TYPE, FactBilling.notifications_sent)
|
||||
], else_=0
|
||||
)).label('email_totals'),
|
||||
func.sum(case(
|
||||
[
|
||||
(FactBilling.notification_type == LETTER_TYPE, FactBilling.notifications_sent)
|
||||
], else_=0
|
||||
)).label('letter_totals'),
|
||||
func.sum(case(
|
||||
[
|
||||
(FactBilling.notification_type == LETTER_TYPE, FactBilling.billable_units)
|
||||
], else_=0
|
||||
)).label('letter_sheet_totals')
|
||||
).filter(
|
||||
FactBilling.bst_date >= start_date,
|
||||
FactBilling.bst_date <= end_date
|
||||
).group_by(
|
||||
FactBilling.bst_date,
|
||||
FactBilling.notification_type
|
||||
).subquery()
|
||||
|
||||
aggregated_totals = db.session.query(
|
||||
daily_volume_stats.c.bst_date.cast(db.Text).label('bst_date'),
|
||||
func.sum(daily_volume_stats.c.sms_totals).cast(Integer).label('sms_totals'),
|
||||
func.sum(daily_volume_stats.c.sms_fragment_totals).cast(Integer).label('sms_fragment_totals'),
|
||||
func.sum(
|
||||
daily_volume_stats.c.sms_fragments_times_multiplier).cast(Integer).label('sms_chargeable_units'),
|
||||
func.sum(daily_volume_stats.c.email_totals).cast(Integer).label('email_totals'),
|
||||
func.sum(daily_volume_stats.c.letter_totals).cast(Integer).label('letter_totals'),
|
||||
func.sum(daily_volume_stats.c.letter_sheet_totals).cast(Integer).label('letter_sheet_totals')
|
||||
).group_by(
|
||||
daily_volume_stats.c.bst_date
|
||||
).order_by(
|
||||
daily_volume_stats.c.bst_date
|
||||
).all()
|
||||
|
||||
return aggregated_totals
|
||||
|
||||
|
||||
def fetch_volumes_by_service(start_date, end_date):
|
||||
# query to return the volume totals by service aggregated for the date range given
|
||||
# start and end dates are inclusive.
|
||||
year_end_date = int(end_date.strftime('%Y'))
|
||||
|
||||
volume_stats = db.session.query(
|
||||
FactBilling.bst_date,
|
||||
FactBilling.service_id,
|
||||
func.sum(case([
|
||||
(FactBilling.notification_type == SMS_TYPE, FactBilling.notifications_sent)
|
||||
], else_=0)).label('sms_totals'),
|
||||
func.sum(case([
|
||||
(FactBilling.notification_type == SMS_TYPE, FactBilling.billable_units * FactBilling.rate_multiplier)
|
||||
], else_=0)).label('sms_fragments_times_multiplier'),
|
||||
func.sum(case([
|
||||
(FactBilling.notification_type == EMAIL_TYPE, FactBilling.notifications_sent)
|
||||
], else_=0)).label('email_totals'),
|
||||
func.sum(case([
|
||||
(FactBilling.notification_type == LETTER_TYPE, FactBilling.notifications_sent)
|
||||
], else_=0)).label('letter_totals'),
|
||||
func.sum(case([
|
||||
(FactBilling.notification_type == LETTER_TYPE, FactBilling.notifications_sent * FactBilling.rate)
|
||||
], else_=0)).label("letter_cost"),
|
||||
func.sum(case(
|
||||
[
|
||||
(FactBilling.notification_type == LETTER_TYPE, FactBilling.billable_units)
|
||||
], else_=0
|
||||
)).label('letter_sheet_totals')
|
||||
).filter(
|
||||
FactBilling.bst_date >= start_date,
|
||||
FactBilling.bst_date <= end_date
|
||||
).group_by(
|
||||
FactBilling.bst_date,
|
||||
FactBilling.service_id,
|
||||
FactBilling.notification_type
|
||||
).subquery()
|
||||
|
||||
annual_billing = db.session.query(
|
||||
func.max(AnnualBilling.financial_year_start).label('financial_year_start'),
|
||||
AnnualBilling.service_id,
|
||||
AnnualBilling.free_sms_fragment_limit
|
||||
).filter(
|
||||
AnnualBilling.financial_year_start <= year_end_date
|
||||
).group_by(
|
||||
AnnualBilling.service_id,
|
||||
AnnualBilling.free_sms_fragment_limit
|
||||
).subquery()
|
||||
|
||||
results = db.session.query(
|
||||
Service.name.label("service_name"),
|
||||
Service.id.label("service_id"),
|
||||
Service.organisation_id.label("organisation_id"),
|
||||
Organisation.name.label("organisation_name"),
|
||||
annual_billing.c.free_sms_fragment_limit.cast(Integer).label("free_allowance"),
|
||||
func.coalesce(func.sum(volume_stats.c.sms_totals), 0).cast(Integer).label("sms_notifications"),
|
||||
func.coalesce(func.sum(volume_stats.c.sms_fragments_times_multiplier), 0
|
||||
).cast(Integer).label("sms_chargeable_units"),
|
||||
func.coalesce(func.sum(volume_stats.c.email_totals), 0).cast(Integer).label("email_totals"),
|
||||
func.coalesce(func.sum(volume_stats.c.letter_totals), 0).cast(Integer).label("letter_totals"),
|
||||
func.coalesce(func.sum(volume_stats.c.letter_cost), 0).cast(Numeric).label("letter_cost"),
|
||||
func.coalesce(func.sum(volume_stats.c.letter_sheet_totals), 0).cast(Integer).label("letter_sheet_totals")
|
||||
).select_from(
|
||||
Service
|
||||
).outerjoin(
|
||||
Organisation, Service.organisation_id == Organisation.id
|
||||
).join(
|
||||
annual_billing, Service.id == annual_billing.c.service_id
|
||||
).outerjoin( # include services without volume
|
||||
volume_stats, Service.id == volume_stats.c.service_id
|
||||
).filter(
|
||||
Service.restricted.is_(False),
|
||||
Service.count_as_live.is_(True),
|
||||
Service.active.is_(True)
|
||||
).group_by(
|
||||
Service.id,
|
||||
Service.name,
|
||||
Service.organisation_id,
|
||||
Organisation.name,
|
||||
annual_billing.c.free_sms_fragment_limit
|
||||
).order_by(
|
||||
Organisation.name,
|
||||
Service.name,
|
||||
).all()
|
||||
|
||||
return results
|
||||
|
||||
@@ -5,9 +5,11 @@ from flask import Blueprint, jsonify, request
|
||||
from app.dao.date_util import get_financial_year_for_datetime
|
||||
from app.dao.fact_billing_dao import (
|
||||
fetch_billing_details_for_all_services,
|
||||
fetch_daily_volumes_for_platform,
|
||||
fetch_letter_costs_and_totals_for_all_services,
|
||||
fetch_letter_line_items_for_all_services,
|
||||
fetch_sms_billing_for_all_services,
|
||||
fetch_volumes_by_service,
|
||||
)
|
||||
from app.dao.fact_notification_status_dao import (
|
||||
fetch_notification_status_totals_for_all_services,
|
||||
@@ -40,12 +42,17 @@ def get_platform_stats():
|
||||
return jsonify(stats)
|
||||
|
||||
|
||||
def validate_date_range_is_within_a_financial_year(start_date, end_date):
|
||||
def validate_date_format(date_to_validate):
|
||||
try:
|
||||
start_date = datetime.strptime(start_date, "%Y-%m-%d").date()
|
||||
end_date = datetime.strptime(end_date, "%Y-%m-%d").date()
|
||||
validated_date = datetime.strptime(date_to_validate, "%Y-%m-%d").date()
|
||||
except ValueError:
|
||||
raise InvalidRequest(message="Input must be a date in the format: YYYY-MM-DD", status_code=400)
|
||||
return validated_date
|
||||
|
||||
|
||||
def validate_date_range_is_within_a_financial_year(start_date, end_date):
|
||||
start_date = validate_date_format(start_date)
|
||||
end_date = validate_date_format(end_date)
|
||||
if end_date < start_date:
|
||||
raise InvalidRequest(message="Start date must be before end date", status_code=400)
|
||||
|
||||
@@ -133,6 +140,53 @@ def get_data_for_billing_report():
|
||||
return jsonify(result)
|
||||
|
||||
|
||||
@platform_stats_blueprint.route('daily-volumes-report')
|
||||
def daily_volumes_report():
|
||||
start_date = validate_date_format(request.args.get('start_date'))
|
||||
end_date = validate_date_format(request.args.get('end_date'))
|
||||
|
||||
daily_volumes = fetch_daily_volumes_for_platform(start_date, end_date)
|
||||
report = []
|
||||
|
||||
for row in daily_volumes:
|
||||
report.append({
|
||||
"day": row.bst_date,
|
||||
"sms_totals": row.sms_totals,
|
||||
"sms_fragment_totals": row.sms_fragment_totals,
|
||||
"sms_chargeable_units": row.sms_chargeable_units,
|
||||
"email_totals": row.email_totals,
|
||||
"letter_totals": row.letter_totals,
|
||||
"letter_sheet_totals": row.letter_sheet_totals
|
||||
})
|
||||
return jsonify(report)
|
||||
|
||||
|
||||
@platform_stats_blueprint.route('volumes-by-service')
|
||||
def volumes_by_service_report():
|
||||
start_date = validate_date_format(request.args.get('start_date'))
|
||||
end_date = validate_date_format(request.args.get('end_date'))
|
||||
|
||||
volumes_by_service = fetch_volumes_by_service(start_date, end_date)
|
||||
report = []
|
||||
|
||||
for row in volumes_by_service:
|
||||
report.append({
|
||||
"service_name": row.service_name,
|
||||
"service_id": str(row.service_id),
|
||||
"organisation_name": row.organisation_name if row.organisation_name else '',
|
||||
"organisation_id": str(row.organisation_id) if row.organisation_id else '',
|
||||
"free_allowance": row.free_allowance,
|
||||
"sms_notifications": row.sms_notifications,
|
||||
"sms_chargeable_units": row.sms_chargeable_units,
|
||||
"email_totals": row.email_totals,
|
||||
"letter_totals": row.letter_totals,
|
||||
"letter_sheet_totals": row.letter_sheet_totals,
|
||||
"letter_cost": float(row.letter_cost),
|
||||
})
|
||||
|
||||
return jsonify(report)
|
||||
|
||||
|
||||
def postage_description(postage):
|
||||
if postage in UK_POSTAGE_TYPES:
|
||||
return f'{postage} class'
|
||||
|
||||
Reference in New Issue
Block a user