fix message ratio endpoint

This commit is contained in:
Kenneth Kehl
2025-02-24 12:29:31 -08:00
parent cd8564204f
commit 3bd2e1d158
2 changed files with 48 additions and 13 deletions

View File

@@ -279,6 +279,44 @@ def dao_get_notification_count_for_service(*, service_id):
return db.session.execute(stmt).scalar()
def dao_get_notification_count_for_service_message_ratio(service_id, current_year):
start_date = datetime(current_year, 1, 1)
end_date = datetime(current_year + 1, 1, 1)
stmt1 = (
select(func.count())
.select_from(Notification)
.where(
Notification.service_id == service_id,
Notification.status
not in [
NotificationStatus.CANCELLED,
NotificationStatus.CREATED,
NotificationStatus.SENDING,
],
Notification.created_at >= start_date,
Notification.created_at < end_date,
)
)
stmt2 = (
select(func.count())
.select_from(NotificationHistory)
.where(
NotificationHistory.service_id == service_id,
NotificationHistory.status
not in [
NotificationStatus.CANCELLED,
NotificationStatus.CREATED,
NotificationStatus.SENDING,
],
NotificationHistory.created_at >= start_date,
NotificationHistory.created_at < end_date,
)
)
recent_count = db.session.execute(stmt1).scalar_one()
old_count = db.session.execute(stmt2).scalar_one()
return recent_count + old_count
def dao_get_failed_notification_count():
stmt = select(func.count(Notification.id)).where(
Notification.status == NotificationStatus.FAILED