Files
notifications-api/app/dao/fact_notification_status_dao.py
T

653 lines
24 KiB
Python
Raw Normal View History

2024-08-28 15:05:42 -04:00
from datetime import timedelta
2024-10-15 12:25:10 -07:00
from sqlalchemy import Date, case, cast, delete, func, select, union_all
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.orm import aliased
2021-03-10 13:55:06 +00:00
from sqlalchemy.sql.expression import extract, literal
from sqlalchemy.types import DateTime, Integer, Text
from app import db
2021-04-14 07:11:01 +01:00
from app.dao.dao_utils import autocommit
2024-01-16 07:37:21 -05:00
from app.enums import KeyType, NotificationStatus, NotificationType
2019-01-14 15:28:26 +00:00
from app.models import (
2021-03-10 13:55:06 +00:00
FactNotificationStatus,
Notification,
NotificationAllTimeView,
Service,
Template,
TemplateFolder,
User,
template_folder_map,
2019-01-14 15:28:26 +00:00
)
from app.utils import (
get_midnight_in_utc,
2023-05-30 12:16:49 -07:00
get_month_from_utc_column,
2021-03-10 13:55:06 +00:00
midnight_n_days_ago,
2024-05-23 13:59:51 -07:00
utc_now,
)
@autocommit
def update_fact_notification_status(process_day, notification_type, service_id):
start_date = get_midnight_in_utc(process_day)
end_date = get_midnight_in_utc(process_day + timedelta(days=1))
2022-02-16 12:30:41 +00:00
# delete any existing rows in case some no longer exist e.g. if all messages are sent
2024-12-20 08:09:19 -08:00
stmt = delete(FactNotificationStatus).where(
2022-11-21 11:49:59 -05:00
FactNotificationStatus.local_date == process_day,
2022-02-16 12:30:41 +00:00
FactNotificationStatus.notification_type == notification_type,
FactNotificationStatus.service_id == service_id,
2024-10-15 12:25:10 -07:00
)
db.session.execute(stmt)
db.session.commit()
2022-02-16 12:30:41 +00:00
2023-08-29 14:54:30 -07:00
query = (
2024-10-15 12:25:10 -07:00
select(
2023-08-29 14:54:30 -07:00
literal(process_day).label("process_day"),
NotificationAllTimeView.template_id,
literal(service_id).label("service_id"),
func.coalesce(
NotificationAllTimeView.job_id, "00000000-0000-0000-0000-000000000000"
).label("job_id"),
literal(notification_type).label("notification_type"),
NotificationAllTimeView.key_type,
NotificationAllTimeView.status,
func.count().label("notification_count"),
)
2024-10-15 12:25:10 -07:00
.select_from(NotificationAllTimeView)
2024-12-20 08:09:19 -08:00
.where(
2023-08-29 14:54:30 -07:00
NotificationAllTimeView.created_at >= start_date,
NotificationAllTimeView.created_at < end_date,
NotificationAllTimeView.notification_type == notification_type,
NotificationAllTimeView.service_id == service_id,
2024-01-18 10:28:50 -05:00
NotificationAllTimeView.key_type.in_((KeyType.NORMAL, KeyType.TEAM)),
2023-08-29 14:54:30 -07:00
)
.group_by(
NotificationAllTimeView.template_id,
NotificationAllTimeView.template_id,
"job_id",
NotificationAllTimeView.key_type,
NotificationAllTimeView.status,
)
)
2022-02-16 12:30:41 +00:00
db.session.connection().execute(
insert(FactNotificationStatus.__table__).from_select(
[
2022-11-21 11:49:59 -05:00
FactNotificationStatus.local_date,
2022-02-16 12:30:41 +00:00
FactNotificationStatus.template_id,
FactNotificationStatus.service_id,
FactNotificationStatus.job_id,
FactNotificationStatus.notification_type,
FactNotificationStatus.key_type,
FactNotificationStatus.notification_status,
2023-08-29 14:54:30 -07:00
FactNotificationStatus.notification_count,
2022-02-16 12:30:41 +00:00
],
2023-08-29 14:54:30 -07:00
query,
2022-02-16 12:30:41 +00:00
)
)
def fetch_notification_status_for_service_by_month(start_date, end_date, service_id):
2024-10-15 12:25:10 -07:00
stmt = (
select(
func.date_trunc("month", NotificationAllTimeView.created_at).label("month"),
NotificationAllTimeView.notification_type,
NotificationAllTimeView.status.label("notification_status"),
func.count(NotificationAllTimeView.id).label("count"),
2023-08-29 14:54:30 -07:00
)
2024-10-15 12:25:10 -07:00
.select_from(NotificationAllTimeView)
2024-12-20 08:09:19 -08:00
.where(
NotificationAllTimeView.service_id == service_id,
NotificationAllTimeView.created_at >= start_date,
NotificationAllTimeView.created_at < end_date,
NotificationAllTimeView.key_type != KeyType.TEST,
2023-08-29 14:54:30 -07:00
)
.group_by(
func.date_trunc("month", NotificationAllTimeView.created_at).label("month"),
NotificationAllTimeView.notification_type,
NotificationAllTimeView.status,
2023-08-29 14:54:30 -07:00
)
)
2024-10-15 12:25:10 -07:00
return db.session.execute(stmt).all()
2023-06-01 07:07:10 -07:00
def fetch_notification_status_for_service_for_day(fetch_day, service_id):
2024-10-15 12:34:24 -07:00
stmt = (
select(
2023-08-29 14:54:30 -07:00
# return current month as a datetime so the data has the same shape as the ft_notification_status query
literal(fetch_day.replace(day=1), type_=DateTime).label("month"),
Notification.notification_type,
Notification.status.label("notification_status"),
func.count().label("count"),
)
2024-10-15 12:34:24 -07:00
.select_from(Notification)
2024-12-20 08:09:19 -08:00
.where(
2023-08-29 14:54:30 -07:00
Notification.created_at >= get_midnight_in_utc(fetch_day),
Notification.created_at
< get_midnight_in_utc(fetch_day + timedelta(days=1)),
Notification.service_id == service_id,
2024-01-18 10:28:50 -05:00
Notification.key_type != KeyType.TEST,
2023-08-29 14:54:30 -07:00
)
.group_by(Notification.notification_type, Notification.status)
)
2024-10-15 12:34:24 -07:00
return db.session.execute(stmt).all()
2023-08-29 14:54:30 -07:00
def fetch_notification_status_for_service_for_today_and_7_previous_days(
2024-08-28 15:05:42 -04:00
service_id: str, by_template: bool = False, limit_days: int = 7
) -> list[dict | None]:
start_date = midnight_n_days_ago(limit_days)
2024-08-28 15:05:42 -04:00
now = get_midnight_in_utc(utc_now())
# Query for the last 7 days
stats_for_7_days = select(
cast(FactNotificationStatus.notification_type, Text).label("notification_type"),
cast(FactNotificationStatus.notification_status, Text).label("status"),
2023-08-29 14:54:30 -07:00
*(
2024-08-21 11:09:50 -04:00
[
FactNotificationStatus.template_id.label("template_id"),
FactNotificationStatus.local_date.label("date_used"),
2024-08-28 15:05:42 -04:00
]
if by_template
else []
2023-08-29 14:54:30 -07:00
),
FactNotificationStatus.notification_count.label("count"),
).where(
FactNotificationStatus.service_id == service_id,
2022-11-21 11:49:59 -05:00
FactNotificationStatus.local_date >= start_date,
2024-01-18 10:28:50 -05:00
FactNotificationStatus.key_type != KeyType.TEST,
)
# Query for today's stats
2024-08-28 15:05:42 -04:00
stats_for_today = (
select(
cast(Notification.notification_type, Text),
cast(Notification.status, Text),
*(
[
Notification.template_id,
literal(now).label("date_used"),
]
if by_template
else []
),
func.count().label("count"),
)
.where(
Notification.created_at >= now,
Notification.service_id == service_id,
Notification.key_type != KeyType.TEST,
)
.group_by(
Notification.notification_type,
*([Notification.template_id] if by_template else []),
Notification.status,
)
2021-06-02 16:06:33 +01:00
)
# Combine the queries using union_all
all_stats_union = union_all(stats_for_7_days, stats_for_today).subquery()
2024-08-28 15:05:42 -04:00
all_stats_alias = aliased(all_stats_union, name="all_stats")
# Final query with optional template joins
2024-11-21 09:38:43 -08:00
stmt = select(
2023-08-29 14:54:30 -07:00
*(
[
2024-08-28 15:05:42 -04:00
TemplateFolder.name.label("folder"),
2023-08-29 14:54:30 -07:00
Template.name.label("template_name"),
False, # TODO: Handle `is_precompiled_letter`
2024-08-28 15:05:42 -04:00
template_folder_map.c.template_folder_id,
all_stats_alias.c.template_id,
User.name.label("created_by"),
2024-08-28 15:05:42 -04:00
Template.created_by_id,
func.max(all_stats_alias.c.date_used).label(
"last_used"
), # Get the most recent date
]
if by_template
else []
2023-08-29 14:54:30 -07:00
),
all_stats_alias.c.notification_type,
all_stats_alias.c.status,
cast(func.sum(all_stats_alias.c.count), Integer).label("count"),
)
if by_template:
2024-11-21 09:38:43 -08:00
stmt = (
stmt.join(Template, all_stats_alias.c.template_id == Template.id)
2024-08-28 15:05:42 -04:00
.join(User, Template.created_by_id == User.id)
.outerjoin(
template_folder_map, Template.id == template_folder_map.c.template_id
)
.outerjoin(
TemplateFolder,
TemplateFolder.id == template_folder_map.c.template_folder_id,
)
)
# Group by all necessary fields except date_used
2024-11-21 09:38:43 -08:00
stmt = stmt.group_by(
*(
[
2024-08-28 15:05:42 -04:00
TemplateFolder.name,
Template.name,
all_stats_alias.c.template_id,
User.name,
2024-08-28 15:05:42 -04:00
template_folder_map.c.template_folder_id,
Template.created_by_id,
]
if by_template
else []
),
all_stats_alias.c.notification_type,
all_stats_alias.c.status,
)
# Execute the query using Flask-SQLAlchemy's session
2024-11-21 09:38:43 -08:00
result = db.session.execute(stmt)
return result.mappings().all()
def fetch_notification_status_totals_for_all_services(start_date, end_date):
2023-08-29 14:54:30 -07:00
stats = (
2024-10-15 12:34:24 -07:00
select(
2024-02-06 16:01:08 -05:00
FactNotificationStatus.notification_type.cast(db.Text).label(
"notification_type"
),
FactNotificationStatus.notification_status.cast(db.Text).label("status"),
FactNotificationStatus.key_type.cast(db.Text).label("key_type"),
2023-08-29 14:54:30 -07:00
func.sum(FactNotificationStatus.notification_count).label("count"),
)
2024-10-15 12:34:24 -07:00
.select_from(FactNotificationStatus)
2024-12-20 08:09:19 -08:00
.where(
2023-08-29 14:54:30 -07:00
FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.local_date <= end_date,
)
.group_by(
FactNotificationStatus.notification_type,
FactNotificationStatus.notification_status,
FactNotificationStatus.key_type,
)
)
2024-05-23 13:59:51 -07:00
today = get_midnight_in_utc(utc_now())
if start_date <= utc_now().date() <= end_date:
2023-08-29 14:54:30 -07:00
stats_for_today = (
2024-10-15 12:34:24 -07:00
select(
Notification.notification_type.cast(db.Text).label("notification_type"),
Notification.status.cast(db.Text),
Notification.key_type.cast(db.Text),
2023-08-29 14:54:30 -07:00
func.count().label("count"),
)
2024-12-20 08:09:19 -08:00
.where(Notification.created_at >= today)
2023-08-29 14:54:30 -07:00
.group_by(
2024-02-02 11:40:25 -05:00
Notification.notification_type,
2023-08-29 14:54:30 -07:00
Notification.status,
Notification.key_type,
)
)
all_stats_table = stats.union_all(stats_for_today).subquery()
2023-08-29 14:54:30 -07:00
query = (
2024-10-15 12:34:24 -07:00
select(
2023-08-29 14:54:30 -07:00
all_stats_table.c.notification_type,
all_stats_table.c.status,
all_stats_table.c.key_type,
func.cast(func.sum(all_stats_table.c.count), Integer).label("count"),
)
.group_by(
all_stats_table.c.notification_type,
all_stats_table.c.status,
all_stats_table.c.key_type,
)
.order_by(all_stats_table.c.notification_type)
)
else:
2023-08-29 14:54:30 -07:00
query = stats.order_by(FactNotificationStatus.notification_type)
2024-10-15 12:34:24 -07:00
return db.session.execute(query).all()
def fetch_notification_statuses_for_job(job_id):
2024-10-15 12:52:44 -07:00
stmt = (
select(
2023-08-29 14:54:30 -07:00
FactNotificationStatus.notification_status.label("status"),
func.sum(FactNotificationStatus.notification_count).label("count"),
)
2024-10-15 12:52:44 -07:00
.select_from(FactNotificationStatus)
2024-12-20 08:09:19 -08:00
.where(
2023-08-29 14:54:30 -07:00
FactNotificationStatus.job_id == job_id,
)
.group_by(FactNotificationStatus.notification_status)
)
2024-10-15 12:52:44 -07:00
return db.session.execute(stmt).all()
2023-08-29 14:54:30 -07:00
def fetch_stats_for_all_services_by_date_range(
start_date, end_date, include_from_test_key=True
):
stats = (
2024-10-15 12:52:44 -07:00
select(
2023-08-29 14:54:30 -07:00
FactNotificationStatus.service_id.label("service_id"),
Service.name.label("name"),
Service.restricted.label("restricted"),
Service.active.label("active"),
Service.created_at.label("created_at"),
2024-02-06 16:01:08 -05:00
FactNotificationStatus.notification_type.cast(db.Text).label(
"notification_type"
),
FactNotificationStatus.notification_status.cast(db.Text).label("status"),
2023-08-29 14:54:30 -07:00
func.sum(FactNotificationStatus.notification_count).label("count"),
)
2024-10-15 12:52:44 -07:00
.select_from(FactNotificationStatus)
2024-12-20 08:09:19 -08:00
.where(
2023-08-29 14:54:30 -07:00
FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.local_date <= end_date,
FactNotificationStatus.service_id == Service.id,
)
.group_by(
FactNotificationStatus.service_id.label("service_id"),
Service.name,
Service.restricted,
Service.active,
Service.created_at,
FactNotificationStatus.notification_type,
FactNotificationStatus.notification_status,
)
.order_by(
FactNotificationStatus.service_id, FactNotificationStatus.notification_type
)
)
if not include_from_test_key:
2024-12-20 08:09:19 -08:00
stats = stats.where(FactNotificationStatus.key_type != KeyType.TEST)
2024-05-23 13:59:51 -07:00
if start_date <= utc_now().date() <= end_date:
today = get_midnight_in_utc(utc_now())
2024-11-21 09:38:43 -08:00
substmt = (
2024-10-15 12:52:44 -07:00
select(
2024-02-02 11:40:25 -05:00
Notification.notification_type.label("notification_type"),
2023-08-29 14:54:30 -07:00
Notification.status.label("status"),
Notification.service_id.label("service_id"),
func.count(Notification.id).label("count"),
)
2024-10-15 12:52:44 -07:00
.select_from(Notification)
2024-12-20 08:09:19 -08:00
.where(Notification.created_at >= today)
2023-08-29 14:54:30 -07:00
.group_by(
Notification.notification_type,
Notification.status,
Notification.service_id,
)
)
if not include_from_test_key:
2024-12-20 08:09:19 -08:00
substmt = substmt.where(Notification.key_type != KeyType.TEST)
2024-11-21 09:38:43 -08:00
substmt = substmt.subquery()
2024-10-15 12:52:44 -07:00
stats_for_today = select(
2023-08-29 14:54:30 -07:00
Service.id.label("service_id"),
Service.name.label("name"),
Service.restricted.label("restricted"),
Service.active.label("active"),
Service.created_at.label("created_at"),
2024-11-21 09:38:43 -08:00
substmt.c.notification_type.cast(db.Text).label("notification_type"),
substmt.c.status.cast(db.Text).label("status"),
substmt.c.count.label("count"),
).outerjoin(substmt, substmt.c.service_id == Service.id)
all_stats_table = stats.union_all(stats_for_today).subquery()
2023-08-29 14:54:30 -07:00
query = (
2024-10-15 12:52:44 -07:00
select(
2023-08-29 14:54:30 -07:00
all_stats_table.c.service_id,
all_stats_table.c.name,
all_stats_table.c.restricted,
all_stats_table.c.active,
all_stats_table.c.created_at,
all_stats_table.c.notification_type,
all_stats_table.c.status,
func.cast(func.sum(all_stats_table.c.count), Integer).label("count"),
)
.group_by(
all_stats_table.c.service_id,
all_stats_table.c.name,
all_stats_table.c.restricted,
all_stats_table.c.active,
all_stats_table.c.created_at,
all_stats_table.c.notification_type,
all_stats_table.c.status,
)
.order_by(
all_stats_table.c.name,
all_stats_table.c.notification_type,
all_stats_table.c.status,
)
)
else:
query = stats
2024-10-15 12:52:44 -07:00
return db.session.execute(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
2023-08-29 14:54:30 -07:00
stats = (
2024-10-15 13:13:42 -07:00
select(
2023-08-29 14:54:30 -07:00
FactNotificationStatus.template_id.label("template_id"),
Template.name.label("name"),
Template.template_type.label("template_type"),
extract("month", FactNotificationStatus.local_date).label("month"),
extract("year", FactNotificationStatus.local_date).label("year"),
func.sum(FactNotificationStatus.notification_count).label("count"),
)
.join(Template, FactNotificationStatus.template_id == Template.id)
2024-12-20 08:09:19 -08:00
.where(
2023-08-29 14:54:30 -07:00
FactNotificationStatus.service_id == service_id,
FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.local_date <= end_date,
2024-01-18 10:28:50 -05:00
FactNotificationStatus.key_type != KeyType.TEST,
FactNotificationStatus.notification_status != NotificationStatus.CANCELLED,
2023-08-29 14:54:30 -07:00
)
.group_by(
FactNotificationStatus.template_id,
Template.name,
Template.template_type,
extract("month", FactNotificationStatus.local_date).label("month"),
extract("year", FactNotificationStatus.local_date).label("year"),
)
.order_by(
extract("year", FactNotificationStatus.local_date),
extract("month", FactNotificationStatus.local_date),
Template.name,
)
)
2024-05-23 13:59:51 -07:00
if start_date <= utc_now() <= end_date:
today = get_midnight_in_utc(utc_now())
2023-05-30 12:16:49 -07:00
month = get_month_from_utc_column(Notification.created_at)
2023-08-29 14:54:30 -07:00
stats_for_today = (
2024-10-15 13:13:42 -07:00
select(
2023-08-29 14:54:30 -07:00
Notification.template_id.label("template_id"),
Template.name.label("name"),
Template.template_type.label("template_type"),
extract("month", month).label("month"),
extract("year", month).label("year"),
func.count().label("count"),
)
.join(
Template,
Notification.template_id == Template.id,
)
2024-12-20 08:09:19 -08:00
.where(
2023-08-29 14:54:30 -07:00
Notification.created_at >= today,
Notification.service_id == service_id,
2024-01-18 10:28:50 -05:00
Notification.key_type != KeyType.TEST,
Notification.status != NotificationStatus.CANCELLED,
2023-08-29 14:54:30 -07:00
)
.group_by(
Notification.template_id,
Template.hidden,
Template.name,
Template.template_type,
month,
)
)
all_stats_table = stats.union_all(stats_for_today).subquery()
2023-08-29 14:54:30 -07:00
query = (
2024-10-15 13:13:42 -07:00
select(
2023-08-29 14:54:30 -07:00
all_stats_table.c.template_id,
all_stats_table.c.name,
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.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
2024-10-15 13:13:42 -07:00
return db.session.execute(query).all()
def get_total_notifications_for_date_range(start_date, end_date):
2024-11-21 09:38:43 -08:00
stmt = (
2024-10-15 13:13:42 -07:00
select(
2024-02-02 11:40:25 -05:00
FactNotificationStatus.local_date.label("local_date"),
2023-08-29 14:54:30 -07:00
func.sum(
case(
2024-04-24 16:27:20 -04:00
(
FactNotificationStatus.notification_type
== NotificationType.EMAIL,
FactNotificationStatus.notification_count,
),
2023-08-29 14:54:30 -07:00
else_=0,
)
).label("emails"),
func.sum(
case(
2024-04-24 16:27:20 -04:00
(
FactNotificationStatus.notification_type
== NotificationType.SMS,
FactNotificationStatus.notification_count,
),
2023-08-29 14:54:30 -07:00
else_=0,
)
).label("sms"),
)
2024-12-20 08:09:19 -08:00
.where(
2024-01-18 10:28:50 -05:00
FactNotificationStatus.key_type != KeyType.TEST,
2023-08-29 14:54:30 -07:00
)
.group_by(FactNotificationStatus.local_date)
.order_by(FactNotificationStatus.local_date)
)
2021-03-10 11:12:29 +00:00
if start_date and end_date:
2024-12-20 08:09:19 -08:00
stmt = stmt.where(
2022-11-21 11:49:59 -05:00
FactNotificationStatus.local_date >= start_date,
2023-08-29 14:54:30 -07:00
FactNotificationStatus.local_date <= end_date,
2021-03-10 11:12:29 +00:00
)
2024-11-21 09:38:43 -08:00
return db.session.execute(stmt).all()
def fetch_monthly_notification_statuses_per_service(start_date, end_date):
2024-10-15 13:13:42 -07:00
stmt = (
select(
2023-08-29 14:54:30 -07:00
func.date_trunc("month", FactNotificationStatus.local_date)
.cast(Date)
.label("date_created"),
Service.id.label("service_id"),
Service.name.label("service_name"),
FactNotificationStatus.notification_type,
func.sum(
case(
2024-04-24 16:27:20 -04:00
(
FactNotificationStatus.notification_status.in_(
[NotificationStatus.SENDING, NotificationStatus.PENDING]
),
FactNotificationStatus.notification_count,
),
2023-08-29 14:54:30 -07:00
else_=0,
)
).label("count_sending"),
func.sum(
case(
2024-04-24 16:27:20 -04:00
(
FactNotificationStatus.notification_status
== NotificationStatus.DELIVERED,
FactNotificationStatus.notification_count,
),
2023-08-29 14:54:30 -07:00
else_=0,
)
).label("count_delivered"),
func.sum(
case(
2024-04-24 16:27:20 -04:00
(
FactNotificationStatus.notification_status.in_(
[
NotificationStatus.TECHNICAL_FAILURE,
NotificationStatus.FAILED,
]
),
FactNotificationStatus.notification_count,
),
2023-08-29 14:54:30 -07:00
else_=0,
)
).label("count_technical_failure"),
func.sum(
case(
2024-04-24 16:27:20 -04:00
(
FactNotificationStatus.notification_status
== NotificationStatus.TEMPORARY_FAILURE,
FactNotificationStatus.notification_count,
),
2023-08-29 14:54:30 -07:00
else_=0,
)
).label("count_temporary_failure"),
func.sum(
case(
2024-04-24 16:27:20 -04:00
(
FactNotificationStatus.notification_status
== NotificationStatus.PERMANENT_FAILURE,
FactNotificationStatus.notification_count,
),
2023-08-29 14:54:30 -07:00
else_=0,
)
).label("count_permanent_failure"),
func.sum(
case(
2024-04-24 16:27:20 -04:00
(
FactNotificationStatus.notification_status
== NotificationStatus.SENT,
FactNotificationStatus.notification_count,
),
2023-08-29 14:54:30 -07:00
else_=0,
)
).label("count_sent"),
)
.join(Service, FactNotificationStatus.service_id == Service.id)
2024-12-20 08:09:19 -08:00
.where(
FactNotificationStatus.notification_status != NotificationStatus.CREATED,
2023-08-29 14:54:30 -07:00
Service.active.is_(True),
2024-01-18 10:28:50 -05:00
FactNotificationStatus.key_type != KeyType.TEST,
2023-08-29 14:54:30 -07:00
Service.restricted.is_(False),
FactNotificationStatus.local_date >= start_date,
FactNotificationStatus.local_date <= end_date,
)
.group_by(
Service.id,
Service.name,
func.date_trunc("month", FactNotificationStatus.local_date).cast(Date),
FactNotificationStatus.notification_type,
)
.order_by(
func.date_trunc("month", FactNotificationStatus.local_date).cast(Date),
Service.id,
FactNotificationStatus.notification_type,
)
)
2024-10-15 13:13:42 -07:00
return db.session.execute(stmt).all()