mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 18:31:13 -05:00
add dao function to get notification stats for a server for toady only
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import uuid
|
||||
from datetime import date
|
||||
|
||||
from sqlalchemy import asc, func
|
||||
from sqlalchemy.orm import joinedload
|
||||
@@ -136,6 +137,16 @@ def delete_service_and_all_associated_db_objects(service):
|
||||
|
||||
|
||||
def dao_fetch_stats_for_service(service_id):
|
||||
return _stats_for_service_query(service_id).all()
|
||||
|
||||
|
||||
def dao_fetch_todays_stats_for_service(service_id):
|
||||
return _stats_for_service_query(service_id).filter(
|
||||
func.date(Notification.created_at) == date.today()
|
||||
).all()
|
||||
|
||||
|
||||
def _stats_for_service_query(service_id):
|
||||
return db.session.query(
|
||||
Notification.notification_type,
|
||||
Notification.status,
|
||||
@@ -145,4 +156,4 @@ def dao_fetch_stats_for_service(service_id):
|
||||
).group_by(
|
||||
Notification.notification_type,
|
||||
Notification.status,
|
||||
).all()
|
||||
)
|
||||
|
||||
@@ -319,12 +319,14 @@ def sample_notification(notify_db,
|
||||
to_field=None,
|
||||
status='created',
|
||||
reference=None,
|
||||
created_at=datetime.utcnow(),
|
||||
created_at=None,
|
||||
content_char_count=160,
|
||||
create=True,
|
||||
personalisation=None,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL):
|
||||
if created_at is None:
|
||||
created_at = datetime.utcnow()
|
||||
if service is None:
|
||||
service = sample_service(notify_db, notify_db_session)
|
||||
if template is None:
|
||||
|
||||
@@ -3,6 +3,7 @@ import pytest
|
||||
|
||||
from sqlalchemy.orm.exc import FlushError, NoResultFound
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from freezegun import freeze_time
|
||||
|
||||
from app import db
|
||||
from app.dao.services_dao import (
|
||||
@@ -14,7 +15,8 @@ from app.dao.services_dao import (
|
||||
dao_fetch_all_services_by_user,
|
||||
dao_update_service,
|
||||
delete_service_and_all_associated_db_objects,
|
||||
dao_fetch_stats_for_service
|
||||
dao_fetch_stats_for_service,
|
||||
dao_fetch_todays_stats_for_service
|
||||
)
|
||||
from app.dao.users_dao import save_model_user
|
||||
from app.models import (
|
||||
@@ -420,3 +422,22 @@ def test_fetch_stats_counts_correctly(notify_db, notify_db_session, sample_templ
|
||||
assert stats[2].notification_type == 'sms'
|
||||
assert stats[2].status == 'created'
|
||||
assert stats[2].count == 1
|
||||
|
||||
|
||||
def test_fetch_stats_for_today_only_includes_today(notify_db, notify_db_session, sample_template):
|
||||
# two created email, one failed email, and one created sms
|
||||
with freeze_time('2001-01-01T23:59:00'):
|
||||
just_before_midnight_yesterday = create_notification(notify_db, None, to_field='1', status='delivered')
|
||||
|
||||
with freeze_time('2001-01-02T00:01:00'):
|
||||
just_after_midnight_today = create_notification(notify_db, None, to_field='2', status='failed')
|
||||
|
||||
with freeze_time('2001-01-02T12:00:00'):
|
||||
right_now = create_notification(notify_db, None, to_field='3', status='created')
|
||||
|
||||
stats = dao_fetch_todays_stats_for_service(sample_template.service.id)
|
||||
|
||||
stats = {row.status: row.count for row in stats}
|
||||
assert 'delivered' not in stats
|
||||
assert stats['failed'] == 1
|
||||
assert stats['created'] == 1
|
||||
|
||||
Reference in New Issue
Block a user