mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Add query to get processing time stats for performance platform
We are only interested in API notifications, not including test messages. Letters are not included.
This commit is contained in:
@@ -16,6 +16,8 @@ from notifications_utils.recipients import (
|
|||||||
from werkzeug.datastructures import MultiDict
|
from werkzeug.datastructures import MultiDict
|
||||||
from sqlalchemy import (desc, func, or_, and_, asc)
|
from sqlalchemy import (desc, func, or_, and_, asc)
|
||||||
from sqlalchemy.orm import joinedload
|
from sqlalchemy.orm import joinedload
|
||||||
|
from sqlalchemy.sql.expression import case
|
||||||
|
from sqlalchemy.sql import functions
|
||||||
from notifications_utils.international_billing_rates import INTERNATIONAL_BILLING_RATES
|
from notifications_utils.international_billing_rates import INTERNATIONAL_BILLING_RATES
|
||||||
|
|
||||||
from app import db, create_uuid
|
from app import db, create_uuid
|
||||||
@@ -519,3 +521,37 @@ def set_scheduled_notification_to_processed(notification_id):
|
|||||||
{'pending': False}
|
{'pending': False}
|
||||||
)
|
)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def dao_get_total_notifications_sent_per_day_for_perfomance_platform(start_date, end_date):
|
||||||
|
"""
|
||||||
|
SELECT
|
||||||
|
count(notifications),
|
||||||
|
sum(CASE WHEN sent_at - created_at <= interval '10 seconds' THEN 1 ELSE 0 END)
|
||||||
|
FROM notifications
|
||||||
|
WHERE
|
||||||
|
created_at > 'START DATE' AND
|
||||||
|
created_at < 'END DATE' AND
|
||||||
|
api_key_id IS NOT NULL AND
|
||||||
|
key_type != 'test' AND
|
||||||
|
notification_type != 'letter';
|
||||||
|
"""
|
||||||
|
under_10_secs = Notification.sent_at - Notification.created_at <= timedelta(seconds=10)
|
||||||
|
sum_column = functions.sum(
|
||||||
|
case(
|
||||||
|
[
|
||||||
|
(under_10_secs, 1)
|
||||||
|
],
|
||||||
|
else_=0
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return db.session.query(
|
||||||
|
func.count(Notification.id).label('messages_total'),
|
||||||
|
sum_column.label('messages_within_10_secs')
|
||||||
|
).filter(
|
||||||
|
Notification.created_at >= start_date,
|
||||||
|
Notification.created_at < end_date,
|
||||||
|
Notification.api_key_id.isnot(None),
|
||||||
|
Notification.key_type != KEY_TYPE_TEST,
|
||||||
|
Notification.notification_type != LETTER_TYPE
|
||||||
|
).one()
|
||||||
|
|||||||
0
tests/app/dao/notification_dao/__init__.py
Normal file
0
tests/app/dao/notification_dao/__init__.py
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
from datetime import date, datetime, timedelta
|
||||||
|
|
||||||
|
from freezegun import freeze_time
|
||||||
|
|
||||||
|
from app.dao.notifications_dao import dao_get_total_notifications_sent_per_day_for_perfomance_platform
|
||||||
|
from app.models import KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST
|
||||||
|
|
||||||
|
from tests.app.db import create_notification
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_total_notifications_filters_on_date(sample_template):
|
||||||
|
create_notification(sample_template, created_at=datetime(2016, 10, 17, 10, 0))
|
||||||
|
create_notification(sample_template, created_at=datetime(2016, 10, 18, 10, 0))
|
||||||
|
create_notification(sample_template, created_at=datetime(2016, 10, 19, 10, 0))
|
||||||
|
result = dao_get_total_notifications_sent_per_day_for_perfomance_platform(date(2016, 10, 18), date(2016, 10, 19))
|
||||||
|
assert result.messages_total == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_total_notifications_filters_on_date_at_midnight(sample_template):
|
||||||
|
create_notification(sample_template, created_at=datetime(2016, 10, 17, 23, 59, 59))
|
||||||
|
create_notification(sample_template, created_at=datetime(2016, 10, 18, 0, 0))
|
||||||
|
create_notification(sample_template, created_at=datetime(2016, 10, 18, 23, 59, 59))
|
||||||
|
create_notification(sample_template, created_at=datetime(2016, 10, 19, 0, 0))
|
||||||
|
result = dao_get_total_notifications_sent_per_day_for_perfomance_platform(date(2016, 10, 18), date(2016, 10, 19))
|
||||||
|
assert result.messages_total == 2
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time('2016-10-18T10:00')
|
||||||
|
def test_get_total_notifications_only_counts_api_notifications(sample_template, sample_job, sample_api_key):
|
||||||
|
create_notification(sample_template, one_off=True)
|
||||||
|
create_notification(sample_template, one_off=True)
|
||||||
|
create_notification(sample_template, job=sample_job)
|
||||||
|
create_notification(sample_template, job=sample_job)
|
||||||
|
create_notification(sample_template, api_key=sample_api_key)
|
||||||
|
result = dao_get_total_notifications_sent_per_day_for_perfomance_platform(date(2016, 10, 18), date(2016, 10, 19))
|
||||||
|
assert result.messages_total == 1
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time('2016-10-18T10:00')
|
||||||
|
def test_get_total_notifications_ignores_test_keys(sample_template):
|
||||||
|
# Creating multiple templates with normal and team keys but only 1 template
|
||||||
|
# with a test key to test that the count ignores letters
|
||||||
|
create_notification(sample_template, key_type=KEY_TYPE_NORMAL)
|
||||||
|
create_notification(sample_template, key_type=KEY_TYPE_NORMAL)
|
||||||
|
create_notification(sample_template, key_type=KEY_TYPE_TEAM)
|
||||||
|
create_notification(sample_template, key_type=KEY_TYPE_TEAM)
|
||||||
|
create_notification(sample_template, key_type=KEY_TYPE_TEST)
|
||||||
|
result = dao_get_total_notifications_sent_per_day_for_perfomance_platform(date(2016, 10, 18), date(2016, 10, 19))
|
||||||
|
assert result.messages_total == 4
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time('2016-10-18T10:00')
|
||||||
|
def test_get_total_notifications_ignores_letters(
|
||||||
|
sample_template,
|
||||||
|
sample_email_template,
|
||||||
|
sample_letter_template
|
||||||
|
):
|
||||||
|
# Creating multiple sms and email templates but only 1 letter template to
|
||||||
|
# test that the count ignores letters
|
||||||
|
create_notification(sample_template)
|
||||||
|
create_notification(sample_template)
|
||||||
|
create_notification(sample_email_template)
|
||||||
|
create_notification(sample_email_template)
|
||||||
|
create_notification(sample_letter_template)
|
||||||
|
result = dao_get_total_notifications_sent_per_day_for_perfomance_platform(date(2016, 10, 18), date(2016, 10, 19))
|
||||||
|
assert result.messages_total == 4
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time('2016-10-18T10:00')
|
||||||
|
def test_get_total_notifications_counts_messages_within_10_seconds(sample_template):
|
||||||
|
created_at = datetime.utcnow()
|
||||||
|
|
||||||
|
create_notification(sample_template, sent_at=created_at + timedelta(seconds=5))
|
||||||
|
create_notification(sample_template, sent_at=created_at + timedelta(seconds=10))
|
||||||
|
create_notification(sample_template, sent_at=created_at + timedelta(seconds=15))
|
||||||
|
|
||||||
|
result = dao_get_total_notifications_sent_per_day_for_perfomance_platform(date(2016, 10, 18), date(2016, 10, 19))
|
||||||
|
assert result.messages_total == 3
|
||||||
|
assert result.messages_within_10_secs == 2
|
||||||
|
|
||||||
|
|
||||||
|
@freeze_time('2016-10-18T10:00')
|
||||||
|
def test_get_total_notifications_counts_messages_that_have_not_sent(sample_template):
|
||||||
|
create_notification(sample_template, status='created', sent_at=None)
|
||||||
|
result = dao_get_total_notifications_sent_per_day_for_perfomance_platform(date(2016, 10, 18), date(2016, 10, 19))
|
||||||
|
assert result.messages_total == 1
|
||||||
|
assert result.messages_within_10_secs == 0
|
||||||
Reference in New Issue
Block a user