mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
New db query that returns data about live services
This data includes service and org name, consent to research, contact details and both intended and factual notifications volumes by notification type. This query was created to get data for a csv report for our platform admins.
This commit is contained in:
@@ -2,7 +2,7 @@ import uuid
|
||||
from datetime import date, datetime, timedelta
|
||||
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from sqlalchemy import asc, func
|
||||
from sqlalchemy import asc, func, case
|
||||
from sqlalchemy.orm import joinedload
|
||||
from flask import current_app
|
||||
|
||||
@@ -21,11 +21,13 @@ from app.dao.template_folder_dao import dao_get_valid_template_folders_by_id
|
||||
from app.models import (
|
||||
AnnualBilling,
|
||||
ApiKey,
|
||||
FactBilling,
|
||||
InboundNumber,
|
||||
InvitedUser,
|
||||
Job,
|
||||
Notification,
|
||||
NotificationHistory,
|
||||
Organisation,
|
||||
Permission,
|
||||
Service,
|
||||
ServicePermission,
|
||||
@@ -72,6 +74,84 @@ def dao_count_live_services():
|
||||
).count()
|
||||
|
||||
|
||||
def dao_fetch_live_services_data():
|
||||
data = db.session.query(
|
||||
Service.id,
|
||||
Organisation.name.label("organisation_name"),
|
||||
Service.name.label("service_name"),
|
||||
Service.consent_to_research,
|
||||
Service.go_live_user_id,
|
||||
User.name.label('user_name'),
|
||||
User.email_address,
|
||||
User.mobile_number,
|
||||
Service.go_live_at.label("live_date"),
|
||||
Service.volume_sms,
|
||||
Service.volume_email,
|
||||
Service.volume_letter,
|
||||
case([
|
||||
(FactBilling.notification_type == 'email', func.sum(FactBilling.notifications_sent))
|
||||
], else_=0).label("email_totals"),
|
||||
case([
|
||||
(FactBilling.notification_type == 'sms', func.sum(FactBilling.notifications_sent))
|
||||
], else_=0).label("sms_totals"),
|
||||
case([
|
||||
(FactBilling.notification_type == 'letter', func.sum(FactBilling.notifications_sent))
|
||||
], else_=0).label("letter_totals"),
|
||||
).outerjoin(
|
||||
Service.organisation
|
||||
).outerjoin(
|
||||
FactBilling, Service.id == FactBilling.service_id
|
||||
).outerjoin(
|
||||
User, Service.go_live_user_id == User.id
|
||||
).group_by(
|
||||
Service.id,
|
||||
Organisation.name,
|
||||
Service.name,
|
||||
Service.consent_to_research,
|
||||
Service.go_live_user_id,
|
||||
User.name,
|
||||
User.email_address,
|
||||
User.mobile_number,
|
||||
Service.go_live_at,
|
||||
Service.volume_sms,
|
||||
Service.volume_email,
|
||||
Service.volume_letter,
|
||||
FactBilling.notification_type
|
||||
).all()
|
||||
results = []
|
||||
for row in data:
|
||||
is_service_in_list = None
|
||||
i = 0
|
||||
while i < len(results):
|
||||
if results[i]["service_id"] == row.id:
|
||||
is_service_in_list = i
|
||||
break
|
||||
else:
|
||||
i += 1
|
||||
if is_service_in_list is not None:
|
||||
results[is_service_in_list]["email_totals"] += row.email_totals
|
||||
results[is_service_in_list]["sms_totals"] += row.sms_totals
|
||||
results[is_service_in_list]["letter_totals"] += row.letter_totals
|
||||
else:
|
||||
results.append({
|
||||
"service_id": row.id,
|
||||
"service_name": row.service_name,
|
||||
"organisation_name": row.organisation_name,
|
||||
"consent_to_research": row.consent_to_research,
|
||||
"contact_name": row.user_name,
|
||||
"contact_email": row.email_address,
|
||||
"contact_mobile": row.mobile_number,
|
||||
"live_date": row.live_date,
|
||||
"sms_volume_intent": row.volume_sms,
|
||||
"email_volume_intent": row.volume_email,
|
||||
"letter_volume_intent": row.volume_letter,
|
||||
"sms_totals": row.sms_totals,
|
||||
"email_totals": row.email_totals,
|
||||
"letter_totals": row.letter_totals,
|
||||
})
|
||||
return results
|
||||
|
||||
|
||||
def dao_fetch_service_by_id(service_id, only_active=False):
|
||||
query = Service.query.filter_by(
|
||||
id=service_id
|
||||
|
||||
Reference in New Issue
Block a user