mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 08:25:15 -05:00
New query for finding if provider is slow
The delivery for provider is slow if more than threshold (currently we pass in threshold 10%) either took x (for now 4) minutes to deliver, or are still sending after that time. We look at all notifications for current provider which are delivered or sending, and are not under test key, for the last 10 minutes. We are using created_at to establish if notifications are from last 10 minutes because we have an index on it, so the query is faster. Also write tests for new is_delivery_slow_for_provider query
This commit is contained in:
@@ -437,22 +437,35 @@ def get_total_sent_notifications_in_date_range(start_date, end_date, notificatio
|
||||
|
||||
|
||||
def is_delivery_slow_for_provider(
|
||||
sent_at,
|
||||
created_at,
|
||||
provider,
|
||||
threshold,
|
||||
delivery_time,
|
||||
service_id,
|
||||
template_id
|
||||
):
|
||||
count = db.session.query(Notification).filter(
|
||||
Notification.service_id == service_id,
|
||||
Notification.template_id == template_id,
|
||||
Notification.sent_at >= sent_at,
|
||||
Notification.status == NOTIFICATION_DELIVERED,
|
||||
count = db.session.query(
|
||||
case(
|
||||
[(
|
||||
Notification.status == NOTIFICATION_DELIVERED,
|
||||
(Notification.updated_at - Notification.sent_at) >= delivery_time
|
||||
)],
|
||||
else_=(datetime.utcnow() - Notification.sent_at) >= delivery_time
|
||||
).label("slow"), func.count()
|
||||
|
||||
).filter(
|
||||
Notification.created_at >= created_at,
|
||||
Notification.sent_at.isnot(None),
|
||||
Notification.status.in_([NOTIFICATION_DELIVERED, NOTIFICATION_SENDING]),
|
||||
Notification.sent_by == provider,
|
||||
(Notification.updated_at - Notification.sent_at) >= delivery_time,
|
||||
).count()
|
||||
return count >= threshold
|
||||
Notification.key_type != KEY_TYPE_TEST
|
||||
).group_by("slow").all()
|
||||
|
||||
print(count)
|
||||
counts = {c[0]: c[1] for c in count}
|
||||
total_notifications = sum(counts.values())
|
||||
if total_notifications:
|
||||
return counts.get(True, 0) / total_notifications >= threshold
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
|
||||
Reference in New Issue
Block a user