Updates after review

- Modified the services_dao to return an int instead of a datetime to
make usage easier and removed the BST function on year as it is not
relevant for year
- Improved tests do there is less logic by ordering the result so there
is less reliance on the template id
- Renamed variable in stats_template_usage_by_month_dao.py to make it
consistent with the method
This commit is contained in:
Richard Chapman
2017-11-09 14:13:42 +00:00
parent 59df6bdbb6
commit b78d989d4e
14 changed files with 76 additions and 148 deletions

View File

@@ -410,13 +410,13 @@ def check_job_status():
@notify_celery.task(name='daily-stats-template_usage_by_month')
@statsd(namespace="tasks")
def daily_stats_template_usage_my_month():
def daily_stats_template_usage_by_month():
results = dao_fetch_monthly_historical_stats_by_template()
for result in results:
insert_or_update_stats_for_template(
result.template_id,
result.month.month,
result.year.year,
result.month,
result.year,
result.count
)

View File

@@ -244,7 +244,7 @@ class Config(object):
},
'daily-stats-template_usage_by_month': {
'task': 'daily-stats-template_usage_by_month',
'schedule': crontab(hour=00, minute=50),
'schedule': crontab(hour=0, minute=50),
'options': {'queue': QueueNames.PERIODIC}
}
}

View File

@@ -40,7 +40,7 @@ from app.models import (
)
from app.service.statistics import format_monthly_template_notification_stats
from app.statsd_decorators import statsd
from app.utils import get_london_month_from_utc_column, get_london_midnight_in_utc, get_london_year_from_utc_column
from app.utils import get_london_month_from_utc_column, get_london_midnight_in_utc
from app.dao.annual_billing_dao import dao_insert_annual_billing
DEFAULT_SERVICE_PERMISSIONS = [
@@ -525,13 +525,13 @@ def dao_fetch_active_users_for_service(service_id):
@statsd(namespace="dao")
def dao_fetch_monthly_historical_stats_by_template():
month = get_london_month_from_utc_column(NotificationHistory.created_at)
year = get_london_year_from_utc_column(NotificationHistory.created_at)
year = func.date_trunc("year", NotificationHistory.created_at)
end_date = datetime.combine(date.today(), time.min)
return db.session.query(
NotificationHistory.template_id,
month.label('month'),
year.label('year'),
extract('month', month).label('month'),
extract('year', year).label('year'),
func.count().label('count')
).filter(
NotificationHistory.created_at < end_date

View File

@@ -15,10 +15,10 @@ def insert_or_update_stats_for_template(template_id, month, year, count):
}
)
if result == 0:
new_sms_sender = StatsTemplateUsageByMonth(
monthly_stats = StatsTemplateUsageByMonth(
template_id=template_id,
month=month,
year=year,
count=count
)
db.session.add(new_sms_sender)
db.session.add(monthly_stats)

View File

@@ -71,23 +71,7 @@ def get_london_month_from_utc_column(column):
"""
return func.date_trunc(
"month",
func.timezone("Europe/London", func.timezone("UTC", column))
)
def get_london_year_from_utc_column(column):
"""
Where queries need to count notifications by month it needs to be
the month in BST (British Summer Time).
The database stores all timestamps as UTC without the timezone.
- First set the timezone on created_at to UTC
- then convert the timezone to BST (or Europe/London)
- lastly truncate the datetime to month with which we can group
queries
"""
return func.date_trunc(
"month",
func.timezone("Europe/London", func.timezone("UTC", column))
func.timezone("Europe/London", column)
)