change querie to stmt

This commit is contained in:
Kenneth Kehl
2024-11-21 09:38:43 -08:00
parent 4a03f5b58b
commit 1a1de39949
7 changed files with 91 additions and 93 deletions
+14 -14
View File
@@ -514,7 +514,7 @@ def dao_fetch_todays_stats_for_all_services(
start_date = get_midnight_in_utc(today)
end_date = get_midnight_in_utc(today + timedelta(days=1))
subquerie = (
substmt = (
select(
Notification.notification_type,
Notification.status,
@@ -530,9 +530,9 @@ def dao_fetch_todays_stats_for_all_services(
)
if not include_from_test_key:
subquerie = subquerie.filter(Notification.key_type != KeyType.TEST)
substmt = substmt.filter(Notification.key_type != KeyType.TEST)
subquerie = subquerie.subquery()
substmt = substmt.subquery()
stmt = (
select(
@@ -541,11 +541,11 @@ def dao_fetch_todays_stats_for_all_services(
Service.restricted,
Service.active,
Service.created_at,
subquerie.c.notification_type,
subquerie.c.status,
subquerie.c.count,
substmt.c.notification_type,
substmt.c.status,
substmt.c.count,
)
.outerjoin(subquerie, subquerie.c.service_id == Service.id)
.outerjoin(substmt, substmt.c.service_id == Service.id)
.order_by(Service.id)
)
@@ -617,7 +617,7 @@ def dao_find_services_sending_to_tv_numbers(start_date, end_date, threshold=500)
def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10000):
subquerie = (
substmt = (
select(
func.count(Notification.id).label("total_count"),
Notification.service_id.label("service_id"),
@@ -637,19 +637,19 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
.having(func.count(Notification.id) >= threshold)
)
subquerie = subquerie.subquery()
substmt = substmt.subquery()
stmt = (
select(
Notification.service_id.label("service_id"),
func.count(Notification.id).label("permanent_failure_count"),
subquerie.c.total_count.label("total_count"),
substmt.c.total_count.label("total_count"),
(
cast(func.count(Notification.id), Float)
/ cast(subquerie.c.total_count, Float)
/ cast(substmt.c.total_count, Float)
).label("permanent_failure_rate"),
)
.join(subquerie, subquerie.c.service_id == Notification.service_id)
.join(substmt, substmt.c.service_id == Notification.service_id)
.filter(
Notification.service_id == Service.id,
Notification.created_at >= start_date,
@@ -660,10 +660,10 @@ def dao_find_services_with_high_failure_rates(start_date, end_date, threshold=10
Service.restricted == False, # noqa
Service.active == True, # noqa
)
.group_by(Notification.service_id, subquerie.c.total_count)
.group_by(Notification.service_id, substmt.c.total_count)
.having(
cast(func.count(Notification.id), Float)
/ cast(subquerie.c.total_count, Float)
/ cast(substmt.c.total_count, Float)
>= 0.25
)
)