mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Use a variable for the massive month date conversion.
Document the massive date function. Document the get_april_fools function.
This commit is contained in:
@@ -215,28 +215,28 @@ def get_notifications_for_job(service_id, job_id, filter_dict=None, page=1, page
|
|||||||
def get_notification_billable_unit_count_per_month(service_id, year):
|
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 database stores all timestamps as UTC without the timezone.
|
||||||
|
- First get the created_at as Europe/London or BST
|
||||||
|
- then extract the timezone portion of the datetime
|
||||||
|
- then add the timezone portion to the created_at datetime
|
||||||
|
- lastly truncate the datetime to month to group the sum of the billable_units
|
||||||
|
"""
|
||||||
|
month = func.date_trunc("month", (NotificationHistory.created_at +
|
||||||
|
extract("timezone",
|
||||||
|
func.timezone("Europe/London",
|
||||||
|
NotificationHistory.created_at)) * timedelta(seconds=1)))
|
||||||
notifications = db.session.query(
|
notifications = db.session.query(
|
||||||
func.date_trunc("month",
|
month,
|
||||||
(NotificationHistory.created_at +
|
|
||||||
extract("timezone", func.timezone("Europe/London",
|
|
||||||
NotificationHistory.created_at)) * timedelta(seconds=1))),
|
|
||||||
func.sum(NotificationHistory.billable_units)
|
func.sum(NotificationHistory.billable_units)
|
||||||
).filter(
|
).filter(
|
||||||
NotificationHistory.billable_units != 0,
|
NotificationHistory.billable_units != 0,
|
||||||
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(func.date_trunc("month",
|
).group_by(month
|
||||||
(NotificationHistory.created_at +
|
).order_by(month).all()
|
||||||
extract("timezone",
|
|
||||||
func.timezone("Europe/London",
|
|
||||||
NotificationHistory.created_at)) * timedelta(seconds=1)))
|
|
||||||
).order_by(func.date_trunc("month",
|
|
||||||
(NotificationHistory.created_at +
|
|
||||||
extract("timezone",
|
|
||||||
func.timezone("Europe/London",
|
|
||||||
NotificationHistory.created_at)) * timedelta(
|
|
||||||
seconds=1)))).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]
|
||||||
|
|
||||||
@@ -389,5 +389,11 @@ def get_financial_year(year):
|
|||||||
|
|
||||||
|
|
||||||
def get_april_fools(year):
|
def get_april_fools(year):
|
||||||
|
"""
|
||||||
|
This function converts the start of the financial year April 1, 00:00 as BST (British Standard Time) to UTC,
|
||||||
|
the tzinfo is lastly removed from the datetime becasue the database stores the timestamps without timezone.
|
||||||
|
:param year: the year to calculate the April 1, 00:00 BST for
|
||||||
|
:return: the datetime of April 1 for the given year, for example 2016 = 2016-03-31 23:00:00
|
||||||
|
"""
|
||||||
return pytz.timezone('Europe/London').localize(datetime(year, 4, 1, 0, 0, 0)).astimezone(pytz.UTC).replace(
|
return pytz.timezone('Europe/London').localize(datetime(year, 4, 1, 0, 0, 0)).astimezone(pytz.UTC).replace(
|
||||||
tzinfo=None)
|
tzinfo=None)
|
||||||
|
|||||||
@@ -1209,4 +1209,6 @@ def test_get_financial_year():
|
|||||||
|
|
||||||
|
|
||||||
def test_get_april_fools():
|
def test_get_april_fools():
|
||||||
assert str(get_april_fools(2016)) == '2016-03-31 23:00:00'
|
april_fools = get_april_fools(2016)
|
||||||
|
assert str(april_fools) == '2016-03-31 23:00:00'
|
||||||
|
assert april_fools.tzinfo is None
|
||||||
|
|||||||
Reference in New Issue
Block a user