Latest and greatest.

Signed-off-by: Cliff Hill <Clifford.hill@gsa.gov>
This commit is contained in:
Cliff Hill
2024-02-02 11:40:25 -05:00
parent 544bf35c06
commit 689ff10b71
6 changed files with 84 additions and 51 deletions

View File

@@ -146,7 +146,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(
stats_for_today = ( stats_for_today = (
db.session.query( db.session.query(
Notification.notification_type.cast(db.Text), Notification.notification_type,
Notification.status, Notification.status,
*([Notification.template_id] if by_template else []), *([Notification.template_id] if by_template else []),
func.count().label("count"), func.count().label("count"),
@@ -212,14 +212,14 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date):
if start_date <= datetime.utcnow().date() <= end_date: if start_date <= datetime.utcnow().date() <= end_date:
stats_for_today = ( stats_for_today = (
db.session.query( db.session.query(
Notification.notification_type.cast(db.Text).label("notification_type"), Notification.notification_type.label("notification_type"),
Notification.status, Notification.status,
Notification.key_type, Notification.key_type,
func.count().label("count"), func.count().label("count"),
) )
.filter(Notification.created_at >= today) .filter(Notification.created_at >= today)
.group_by( .group_by(
Notification.notification_type.cast(db.Text), Notification.notification_type,
Notification.status, Notification.status,
Notification.key_type, Notification.key_type,
) )
@@ -297,7 +297,7 @@ def fetch_stats_for_all_services_by_date_range(
today = get_midnight_in_utc(datetime.utcnow()) today = get_midnight_in_utc(datetime.utcnow())
subquery = ( subquery = (
db.session.query( db.session.query(
Notification.notification_type.cast(db.Text).label("notification_type"), Notification.notification_type.label("notification_type"),
Notification.status.label("status"), Notification.status.label("status"),
Notification.service_id.label("service_id"), Notification.service_id.label("service_id"),
func.count(Notification.id).label("count"), func.count(Notification.id).label("count"),
@@ -450,7 +450,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
def get_total_notifications_for_date_range(start_date, end_date): def get_total_notifications_for_date_range(start_date, end_date):
query = ( query = (
db.session.query( db.session.query(
FactNotificationStatus.local_date.cast(db.Text).label("local_date"), FactNotificationStatus.local_date.label("local_date"),
func.sum( func.sum(
case( case(
[ [

View File

@@ -1,5 +1,6 @@
from strenum import StrEnum # In 3.11 this is in the enum library. We will not need from strenum import StrEnum # In 3.11 this is in the enum library. We will not need
# this external library any more.
# this external library any more.
class TemplateType(StrEnum): class TemplateType(StrEnum):

View File

@@ -1973,10 +1973,7 @@ class Rate(db.Model):
notification_type = enum_column(NotificationType, index=True, nullable=False) notification_type = enum_column(NotificationType, index=True, nullable=False)
def __str__(self): def __str__(self):
the_string = "{}".format(self.rate) return f"{self.rate} {self.notification_type} {self.valid_from}"
the_string += " {}".format(self.notification_type)
the_string += " {}".format(self.valid_from)
return the_string
class InboundSms(db.Model): class InboundSms(db.Model):
@@ -2140,7 +2137,11 @@ class FactNotificationStatus(db.Model):
job_id = db.Column(UUID(as_uuid=True), primary_key=True, index=True, nullable=False) job_id = db.Column(UUID(as_uuid=True), primary_key=True, index=True, nullable=False)
notification_type = enum_column(NotificationType, primary_key=True, nullable=False) notification_type = enum_column(NotificationType, primary_key=True, nullable=False)
key_type = enum_column(KeyType, primary_key=True, nullable=False) key_type = enum_column(KeyType, primary_key=True, nullable=False)
notification_status = enum_column(NotificationStatus, primary_key=True, nullable=False) notification_status = enum_column(
NotificationStatus,
primary_key=True,
nullable=False,
)
notification_count = db.Column(db.Integer(), nullable=False) notification_count = db.Column(db.Integer(), nullable=False)
created_at = db.Column( created_at = db.Column(
db.DateTime, db.DateTime,

View File

@@ -578,7 +578,7 @@ def upgrade():
) )
op.alter_column( op.alter_column(
"ft_notification_status", "ft_notification_status",
"notification_type", "notification_status",
existing_type=sa.TEXT(), existing_type=sa.TEXT(),
type_=enum_type(NotificationStatus), type_=enum_type(NotificationStatus),
existing_nullable=False, existing_nullable=False,
@@ -613,7 +613,7 @@ def downgrade():
# Alter columns back # Alter columns back
op.alter_column( op.alter_column(
"ft_notification_status", "ft_notification_status",
"notification_type", "notification_status",
existing_type=enum_type(NotificationStatus), existing_type=enum_type(NotificationStatus),
type_=sa.TEXT(), type_=sa.TEXT(),
existing_nullable=False, existing_nullable=False,

View File

@@ -17,8 +17,8 @@ from app.dao.fact_notification_status_dao import (
get_total_notifications_for_date_range, get_total_notifications_for_date_range,
update_fact_notification_status, update_fact_notification_status,
) )
from app.enums import KeyType, NotificationStatus from app.enums import KeyType, NotificationStatus, NotificationType, TemplateType
from app.models import FactNotificationStatus, NotificationType, TemplateType from app.models import FactNotificationStatus
from tests.app.db import ( from tests.app.db import (
create_ft_notification_status, create_ft_notification_status,
create_job, create_job,
@@ -32,24 +32,31 @@ def test_fetch_notification_status_for_service_by_month(notify_db_session):
service_1 = create_service(service_name="service_1") service_1 = create_service(service_name="service_1")
service_2 = create_service(service_name="service_2") service_2 = create_service(service_name="service_2")
create_ft_notification_status(date(2018, 1, 1), "sms", service_1, count=4)
create_ft_notification_status(date(2018, 1, 2), "sms", service_1, count=10)
create_ft_notification_status( create_ft_notification_status(
date(2018, 1, 2), "sms", service_1, notification_status="created" date(2018, 1, 1), NotificationType.SMS, service_1, count=4
) )
create_ft_notification_status(date(2018, 1, 3), "email", service_1) create_ft_notification_status(
date(2018, 1, 2), NotificationType.SMS, service_1, count=10
)
create_ft_notification_status(
date(2018, 1, 2),
NotificationType.SMS,
service_1,
notification_status=NotificationStatus.CREATED,
)
create_ft_notification_status(date(2018, 1, 3), NotificationType.EMAIL, service_1)
create_ft_notification_status(date(2018, 2, 2), "sms", service_1) create_ft_notification_status(date(2018, 2, 2), NotificationType.SMS, service_1)
# not included - too early # not included - too early
create_ft_notification_status(date(2017, 12, 31), "sms", service_1) create_ft_notification_status(date(2017, 12, 31), NotificationType.SMS, service_1)
# not included - too late # not included - too late
create_ft_notification_status(date(2017, 3, 1), "sms", service_1) create_ft_notification_status(date(2017, 3, 1), NotificationType.SMS, service_1)
# not included - wrong service # not included - wrong service
create_ft_notification_status(date(2018, 1, 3), "sms", service_2) create_ft_notification_status(date(2018, 1, 3), NotificationType.SMS, service_2)
# not included - test keys # not included - test keys
create_ft_notification_status( create_ft_notification_status(
date(2018, 1, 3), "sms", service_1, key_type=KeyType.TEST date(2018, 1, 3), NotificationType.SMS, service_1, key_type=KeyType.TEST
) )
results = sorted( results = sorted(
@@ -62,23 +69,23 @@ def test_fetch_notification_status_for_service_by_month(notify_db_session):
assert len(results) == 4 assert len(results) == 4
assert results[0].month.date() == date(2018, 1, 1) assert results[0].month.date() == date(2018, 1, 1)
assert results[0].notification_type == "email" assert results[0].notification_type == NotificationType.EMAIL
assert results[0].notification_status == "delivered" assert results[0].notification_status == NotificationStatus.DELIVERED
assert results[0].count == 1 assert results[0].count == 1
assert results[1].month.date() == date(2018, 1, 1) assert results[1].month.date() == date(2018, 1, 1)
assert results[1].notification_type == "sms" assert results[1].notification_type == NotificationType.SMS
assert results[1].notification_status == "created" assert results[1].notification_status == NotificationStatus.CREATED
assert results[1].count == 1 assert results[1].count == 1
assert results[2].month.date() == date(2018, 1, 1) assert results[2].month.date() == date(2018, 1, 1)
assert results[2].notification_type == "sms" assert results[2].notification_type == NotificationType.SMS
assert results[2].notification_status == "delivered" assert results[2].notification_status == NotificationStatus.DELIVERED
assert results[2].count == 14 assert results[2].count == 14
assert results[3].month.date() == date(2018, 2, 1) assert results[3].month.date() == date(2018, 2, 1)
assert results[3].notification_type == "sms" assert results[3].notification_type == NotificationType.SMS
assert results[3].notification_status == "delivered" assert results[3].notification_status == NotificationStatus.DELIVERED
assert results[3].count == 1 assert results[3].count == 1
@@ -109,7 +116,7 @@ def test_fetch_notification_status_for_service_for_day(notify_db_session):
create_notification( create_notification(
service_1.templates[0], service_1.templates[0],
created_at=datetime(2018, 6, 1, 12, 0, 0), created_at=datetime(2018, 6, 1, 12, 0, 0),
status="delivered", status=NotificationStatus.DELIVERED,
) )
# test key # test key
@@ -138,13 +145,13 @@ def test_fetch_notification_status_for_service_for_day(notify_db_session):
assert len(results) == 2 assert len(results) == 2
assert results[0].month == datetime(2018, 6, 1, 0, 0) assert results[0].month == datetime(2018, 6, 1, 0, 0)
assert results[0].notification_type == "sms" assert results[0].notification_type == NotificationType.SMS
assert results[0].notification_status == "created" assert results[0].notification_status == NotificationStatus.CREATED
assert results[0].count == 3 assert results[0].count == 3
assert results[1].month == datetime(2018, 6, 1, 0, 0) assert results[1].month == datetime(2018, 6, 1, 0, 0)
assert results[1].notification_type == "sms" assert results[1].notification_type == NotificationType.SMS
assert results[1].notification_status == "delivered" assert results[1].notification_status == NotificationStatus.DELIVERED
assert results[1].count == 1 assert results[1].count == 1
@@ -159,20 +166,42 @@ def test_fetch_notification_status_for_service_for_today_and_7_previous_days(
service=service_1, template_type=TemplateType.EMAIL service=service_1, template_type=TemplateType.EMAIL
) )
create_ft_notification_status(date(2018, 10, 29), NotificationType.SMS, service_1, count=10,)
create_ft_notification_status(date(2018, 10, 25), NotificationType.SMS, service_1, count=8,)
create_ft_notification_status( create_ft_notification_status(
date(2018, 10, 29), NotificationType.SMS, service_1, notification_status=NotificationStatus.CREATED, date(2018, 10, 29),
NotificationType.SMS,
service_1,
count=10,
)
create_ft_notification_status(
date(2018, 10, 25),
NotificationType.SMS,
service_1,
count=8,
)
create_ft_notification_status(
date(2018, 10, 29),
NotificationType.SMS,
service_1,
notification_status=NotificationStatus.CREATED,
)
create_ft_notification_status(
date(2018, 10, 29),
NotificationType.EMAIL,
service_1,
count=3,
) )
create_ft_notification_status(date(2018, 10, 29), NotificationType.EMAIL, service_1, count=3,)
create_notification(sms_template, created_at=datetime(2018, 10, 31, 11, 0, 0)) create_notification(sms_template, created_at=datetime(2018, 10, 31, 11, 0, 0))
create_notification(sms_template_2, created_at=datetime(2018, 10, 31, 11, 0, 0)) create_notification(sms_template_2, created_at=datetime(2018, 10, 31, 11, 0, 0))
create_notification( create_notification(
sms_template, created_at=datetime(2018, 10, 31, 12, 0, 0), status=NotificationStatus.DELIVERED, sms_template,
created_at=datetime(2018, 10, 31, 12, 0, 0),
status=NotificationStatus.DELIVERED,
) )
create_notification( create_notification(
email_template, created_at=datetime(2018, 10, 31, 13, 0, 0), status=NotificationStatus.DELIVERED, email_template,
created_at=datetime(2018, 10, 31, 13, 0, 0),
status=NotificationStatus.DELIVERED,
) )
# too early, shouldn't be included # too early, shouldn't be included
@@ -299,16 +328,16 @@ def test_fetch_notification_status_totals_for_all_services(
assert len(results) == 3 assert len(results) == 3
assert results[0].notification_type == "email" assert results[0].notification_type == NotificationType.EMAIL
assert results[0].status == "delivered" assert results[0].status == NotificationStatus.DELIVERED
assert results[0].count == expected_email assert results[0].count == expected_email
assert results[1].notification_type == "sms" assert results[1].notification_type == NotificationType.SMS
assert results[1].status == "created" assert results[1].status == NotificationStatus.CREATED
assert results[1].count == expected_created_sms assert results[1].count == expected_created_sms
assert results[2].notification_type == "sms" assert results[2].notification_type == NotificationType.SMS
assert results[2].status == "delivered" assert results[2].status == NotificationStatus.DELIVERED
assert results[2].count == expected_sms assert results[2].count == expected_sms

View File

@@ -929,7 +929,9 @@ def set_up_usage_data(start_date):
# service with emails only: # service with emails only:
service_with_emails = create_service(service_name="b - emails") service_with_emails = create_service(service_name="b - emails")
email_template = create_template(service=service_with_emails, template_type=TemplateType.EMAIL) email_template = create_template(
service=service_with_emails, template_type=TemplateType.EMAIL
)
org_2 = create_organization( org_2 = create_organization(
name="Org for {}".format(service_with_emails.name), name="Org for {}".format(service_with_emails.name),
) )