The previous commit was failing the test in the docker container.

My local version of postgres is 9.6 and docker is using 9.5 (so I am ahead).
However, we found a more simple solution that works for both versions.
This commit is contained in:
Rebecca Law
2017-01-10 11:21:33 +00:00
parent 34a9b8aba4
commit ffa54821d9

View File

@@ -216,17 +216,15 @@ def get_notification_billable_unit_count_per_month(service_id, year):
start, end = get_financial_year(year) start, end = get_financial_year(year)
""" """
The query needs to sum the billable_units per month, but this needs to be the month in BST (Britsh Standard Time). The query needs to sum the billable_units per month, but this needs to be the month in BST (British Standard Time).
The database stores all timestamps as UTC without the timezone. The database stores all timestamps as UTC without the timezone.
- First get the created_at as Europe/London or BST - First set the timezone on created_at to UTC
- then extract the timezone portion of the datetime - then convert the timezone to BST (or Europe/London)
- then add the timezone portion to the created_at datetime
- lastly truncate the datetime to month to group the sum of the billable_units - lastly truncate the datetime to month to group the sum of the billable_units
""" """
month = func.date_trunc("month", (NotificationHistory.created_at + month = func.date_trunc("month",
extract("timezone", func.timezone("Europe/London", func.timezone("UTC",
func.timezone("Europe/London", NotificationHistory.created_at)))
NotificationHistory.created_at)) * timedelta(seconds=1)))
notifications = db.session.query( notifications = db.session.query(
month, month,
func.sum(NotificationHistory.billable_units) func.sum(NotificationHistory.billable_units)
@@ -235,8 +233,11 @@ def get_notification_billable_unit_count_per_month(service_id, year):
NotificationHistory.service_id == service_id, NotificationHistory.service_id == service_id,
NotificationHistory.created_at >= start, NotificationHistory.created_at >= start,
NotificationHistory.created_at < end NotificationHistory.created_at < end
).group_by(month ).group_by(
).order_by(month).all() month
).order_by(
month
).all()
return [(datetime.strftime(x[0], "%B"), x[1]) for x in notifications] return [(datetime.strftime(x[0], "%B"), x[1]) for x in notifications]