Return financial year not calendar and ensure double precision values

are not returned from queries

- Updated stats_template_usage_by_month_dao.py to return the results for
financial year not calendar, as the report os for FY only and hence
only the FY data is required
- Updated services_dao.py to ensure double precision values are converted
to an int as the 'exact' function returns double precision from the
database query, as the admin code requires the value for month to be an
int
This commit is contained in:
Richard Chapman
2017-11-20 10:01:45 +00:00
parent b56825a680
commit 58b0658a13
4 changed files with 210 additions and 31 deletions

View File

@@ -550,7 +550,7 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
results = dao_get_template_usage_stats_by_service(service_id, year) results = dao_get_template_usage_stats_by_service(service_id, year)
stats = list() stats = []
for result in results: for result in results:
stat = type("", (), {})() stat = type("", (), {})()
stat.template_id = result.template_id stat.template_id = result.template_id
@@ -575,7 +575,7 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
).join( ).join(
Template, Notification.template_id == Template.id, Template, Notification.template_id == Template.id,
).filter( ).filter(
Notification.created_at >= start_date Notification.created_at >= start_date,
).group_by( ).group_by(
Notification.template_id, Notification.template_id,
Template.name, Template.name,
@@ -599,8 +599,8 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year)
new_stat.template_id = today_result.template_id new_stat.template_id = today_result.template_id
new_stat.template_type = today_result.template_type new_stat.template_type = today_result.template_type
new_stat.name = today_result.name new_stat.name = today_result.name
new_stat.month = today_result.month new_stat.month = int(today_result.month)
new_stat.year = today_result.year new_stat.year = int(today_result.year)
new_stat.count = today_result.count new_stat.count = today_result.count
stats.append(new_stat) stats.append(new_stat)

View File

@@ -1,3 +1,5 @@
from sqlalchemy import or_, and_
from app import db from app import db
from app.statsd_decorators import statsd from app.statsd_decorators import statsd
from app.dao.dao_utils import transactional from app.dao.dao_utils import transactional
@@ -41,6 +43,15 @@ def dao_get_template_usage_stats_by_service(service_id, year):
).join( ).join(
Template, StatsTemplateUsageByMonth.template_id == Template.id Template, StatsTemplateUsageByMonth.template_id == Template.id
).filter( ).filter(
Template.service_id == service_id, Template.service_id == service_id
StatsTemplateUsageByMonth.year == year ).filter(
or_(
and_(
StatsTemplateUsageByMonth.month.in_([4, 5, 6, 7, 8, 9, 10, 11, 12]),
StatsTemplateUsageByMonth.year == year
), and_(
StatsTemplateUsageByMonth.month.in_([1, 2, 3]),
StatsTemplateUsageByMonth.year == year + 1
)
)
).all() ).all()

View File

@@ -1380,3 +1380,151 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_combined_his
assert result[1].month == 11 assert result[1].month == 11
assert result[1].year == 2017 assert result[1].year == 2017
assert result[1].count == 1 assert result[1].count == 1
@freeze_time("2017-11-10 11:09:00.000000")
def test_dao_fetch_monthly_historical_usage_by_template_for_service_does_not_return_double_precision_values(
notify_db,
notify_db_session,
sample_service
):
template_one = create_sample_template(notify_db, notify_db_session, template_name='1')
n = create_notification(
notify_db,
notify_db_session,
service=sample_service,
template=template_one,
created_at=datetime.utcnow()
)
result = sorted(
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017),
key=lambda x: (x.month, x.year)
)
assert len(result) == 1
assert result[0].template_id == template_one.id
assert result[0].name == template_one.name
assert result[0].template_type == template_one.template_type
assert result[0].month == 11
assert len(str(result[0].month)) == 2
assert result[0].year == 2017
assert len(str(result[0].year)) == 4
assert result[0].count == 1
@freeze_time("2018-03-10 11:09:00.000000")
def test_dao_fetch_monthly_historical_usage_by_template_for_service_returns_financial_year(
notify_db,
notify_db_session,
sample_service
):
template_one = create_sample_template(notify_db, notify_db_session, template_name='1', template_type='email')
notification_history = functools.partial(
create_notification_history,
notify_db,
notify_db_session,
status='delivered',
sample_template=template_one
)
date = datetime.now()
day = date.day
year = date.year
notification_history(created_at=datetime(year - 1, 1, day))
notification_history(created_at=datetime(year - 1, 3, day))
notification_history(created_at=datetime(year - 1, 4, day))
notification_history(created_at=datetime(year - 1, 5, day))
notification_history(created_at=datetime(year, 1, day))
notification_history(created_at=datetime(year, 2, day))
daily_stats_template_usage_by_month()
n = create_notification(
notify_db,
notify_db_session,
service=sample_service,
template=template_one,
created_at=datetime.utcnow()
)
result = sorted(
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017),
key=lambda x: (x.year, x.month)
)
assert len(result) == 5
assert result[0].month == 4
assert result[0].year == 2017
assert result[1].month == 5
assert result[1].year == 2017
assert result[2].month == 1
assert result[2].year == 2018
assert result[3].month == 2
assert result[3].year == 2018
assert result[4].month == 3
assert result[4].year == 2018
@freeze_time("2018-03-10 11:09:00.000000")
def test_dao_fetch_monthly_historical_usage_by_template_for_service_only_returns_for_service(
notify_db,
notify_db_session,
sample_service
):
template_one = create_sample_template(notify_db, notify_db_session, template_name='1', template_type='email')
notification_history = functools.partial(
create_notification_history,
notify_db,
notify_db_session,
status='delivered',
sample_template=template_one
)
date = datetime.now()
day = date.day
year = date.year
notification_history(created_at=datetime(year, 1, day))
notification_history(created_at=datetime(year, 2, day))
service_two = create_service(service_name='other_service')
daily_stats_template_usage_by_month()
n = create_notification(
notify_db,
notify_db_session,
service=sample_service,
template=template_one,
created_at=datetime.utcnow()
)
create_notification(
notify_db,
notify_db_session,
service=service_two,
template=template_one,
created_at=datetime.utcnow()
)
result = sorted(
dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2017),
key=lambda x: (x.year, x.month)
)
assert len(result) == 3
result = sorted(
dao_fetch_monthly_historical_usage_by_template_for_service(service_two.id, 2017),
key=lambda x: (x.year, x.month)
)
assert len(result) == 1

View File

@@ -51,55 +51,75 @@ def test_dao_get_template_usage_stats_by_service(sample_service):
email_template = create_template(service=sample_service, template_type="email") email_template = create_template(service=sample_service, template_type="email")
stats1 = StatsTemplateUsageByMonth( new_service = create_service(service_name="service_one")
template_new_service = create_template(service=new_service)
db.session.add(StatsTemplateUsageByMonth(
template_id=email_template.id, template_id=email_template.id,
month=1, month=4,
year=2017, year=2017,
count=10 count=10
) ))
stats2 = StatsTemplateUsageByMonth( db.session.add(StatsTemplateUsageByMonth(
template_id=email_template.id, template_id=template_new_service.id,
month=2, month=4,
year=2017, year=2017,
count=10 count=10
) ))
db.session.add(stats1)
db.session.add(stats2)
result = dao_get_template_usage_stats_by_service(sample_service.id, 2017) result = dao_get_template_usage_stats_by_service(sample_service.id, 2017)
assert len(result) == 2 assert len(result) == 1
def test_dao_get_template_usage_stats_by_service_specific_year(sample_service): def test_dao_get_template_usage_stats_by_service_specific_year(sample_service):
email_template = create_template(service=sample_service, template_type="email") email_template = create_template(service=sample_service, template_type="email")
stats1 = StatsTemplateUsageByMonth( db.session.add(StatsTemplateUsageByMonth(
template_id=email_template.id, template_id=email_template.id,
month=1, month=3,
year=2016,
count=10
)
stats2 = StatsTemplateUsageByMonth(
template_id=email_template.id,
month=2,
year=2017, year=2017,
count=10 count=10
) ))
db.session.add(stats1) db.session.add(StatsTemplateUsageByMonth(
db.session.add(stats2) template_id=email_template.id,
month=4,
year=2017,
count=10
))
db.session.add(StatsTemplateUsageByMonth(
template_id=email_template.id,
month=3,
year=2018,
count=10
))
db.session.add(StatsTemplateUsageByMonth(
template_id=email_template.id,
month=4,
year=2018,
count=10
))
result = dao_get_template_usage_stats_by_service(sample_service.id, 2017) result = dao_get_template_usage_stats_by_service(sample_service.id, 2017)
assert len(result) == 1 assert len(result) == 2
assert result[0].template_id == email_template.id assert result[0].template_id == email_template.id
assert result[0].name == email_template.name assert result[0].name == email_template.name
assert result[0].template_type == email_template.template_type assert result[0].template_type == email_template.template_type
assert result[0].month == 2 assert result[0].month == 4
assert result[0].year == 2017 assert result[0].year == 2017
assert result[0].count == 10 assert result[0].count == 10
assert result[1].template_id == email_template.id
assert result[1].name == email_template.name
assert result[1].template_type == email_template.template_type
assert result[1].month == 3
assert result[1].year == 2018
assert result[1].count == 10