mirror of
https://github.com/GSA/notifications-api.git
synced 2026-09-07 16:38:24 -04:00
Merge pull request #1386 from alphagov/rc-monthly-template-usage
Handling Null template_id for monthly stats
This commit is contained in:
@@ -414,9 +414,10 @@ def daily_stats_template_usage_by_month():
|
|||||||
results = dao_fetch_monthly_historical_stats_by_template()
|
results = dao_fetch_monthly_historical_stats_by_template()
|
||||||
|
|
||||||
for result in results:
|
for result in results:
|
||||||
insert_or_update_stats_for_template(
|
if result.template_id:
|
||||||
result.template_id,
|
insert_or_update_stats_for_template(
|
||||||
result.month,
|
result.template_id,
|
||||||
result.year,
|
result.month,
|
||||||
result.count
|
result.year,
|
||||||
)
|
result.count
|
||||||
|
)
|
||||||
|
|||||||
@@ -44,16 +44,20 @@ from app.dao.provider_details_dao import (
|
|||||||
get_current_provider
|
get_current_provider
|
||||||
)
|
)
|
||||||
from app.models import (
|
from app.models import (
|
||||||
Service, Template,
|
MonthlyBilling,
|
||||||
SMS_TYPE, LETTER_TYPE,
|
NotificationHistory,
|
||||||
|
Service,
|
||||||
|
StatsTemplateUsageByMonth,
|
||||||
|
Template,
|
||||||
JOB_STATUS_READY_TO_SEND,
|
JOB_STATUS_READY_TO_SEND,
|
||||||
JOB_STATUS_IN_PROGRESS,
|
JOB_STATUS_IN_PROGRESS,
|
||||||
JOB_STATUS_SENT_TO_DVLA,
|
JOB_STATUS_SENT_TO_DVLA,
|
||||||
NOTIFICATION_PENDING,
|
|
||||||
NOTIFICATION_CREATED,
|
|
||||||
KEY_TYPE_TEST,
|
KEY_TYPE_TEST,
|
||||||
MonthlyBilling,
|
LETTER_TYPE,
|
||||||
StatsTemplateUsageByMonth)
|
NOTIFICATION_CREATED,
|
||||||
|
NOTIFICATION_PENDING,
|
||||||
|
SMS_TYPE
|
||||||
|
)
|
||||||
from app.utils import get_london_midnight_in_utc
|
from app.utils import get_london_midnight_in_utc
|
||||||
from app.v2.errors import JobIncompleteError
|
from app.v2.errors import JobIncompleteError
|
||||||
from tests.app.db import create_notification, create_service, create_template, create_job, create_rate
|
from tests.app.db import create_notification, create_service, create_template, create_job, create_rate
|
||||||
@@ -943,3 +947,44 @@ def test_daily_stats_template_usage_by_month_multiple_runs(notify_db, notify_db_
|
|||||||
assert result[3].month == 11
|
assert result[3].month == 11
|
||||||
assert result[3].year == 2017
|
assert result[3].year == 2017
|
||||||
assert result[3].count == 1
|
assert result[3].count == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_dao_fetch_monthly_historical_stats_by_template_null_template_id_not_counted(notify_db, notify_db_session):
|
||||||
|
notification_history = functools.partial(
|
||||||
|
create_notification_history,
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
status='delivered'
|
||||||
|
)
|
||||||
|
|
||||||
|
template_one = create_sample_template(notify_db, notify_db_session, template_name='1')
|
||||||
|
history = notification_history(created_at=datetime(2017, 2, 1), sample_template=template_one)
|
||||||
|
|
||||||
|
NotificationHistory.query.filter(
|
||||||
|
NotificationHistory.id == history.id
|
||||||
|
).update(
|
||||||
|
{
|
||||||
|
'template_id': None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
daily_stats_template_usage_by_month()
|
||||||
|
|
||||||
|
result = db.session.query(
|
||||||
|
StatsTemplateUsageByMonth
|
||||||
|
).all()
|
||||||
|
|
||||||
|
assert len(result) == 0
|
||||||
|
|
||||||
|
notification_history(created_at=datetime(2017, 2, 1), sample_template=template_one)
|
||||||
|
|
||||||
|
daily_stats_template_usage_by_month()
|
||||||
|
|
||||||
|
result = db.session.query(
|
||||||
|
StatsTemplateUsageByMonth
|
||||||
|
).order_by(
|
||||||
|
StatsTemplateUsageByMonth.year,
|
||||||
|
StatsTemplateUsageByMonth.month
|
||||||
|
).all()
|
||||||
|
|
||||||
|
assert len(result) == 1
|
||||||
|
|||||||
Reference in New Issue
Block a user