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:
Pea Tyczynska
2018-12-04 17:39:43 +00:00
parent 7ec3dbd667
commit 39ca5b9525
2 changed files with 93 additions and 121 deletions

View File

@@ -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")