Change get_financial_year to return ending date as 1 microsecond earlier.

That way we can write the queries as between start and end dates, making it easier to read.
This makes more sense.
This commit is contained in:
Rebecca Law
2017-05-02 10:00:47 +01:00
parent 88d92d6070
commit 3e0221adec
5 changed files with 13 additions and 17 deletions

View File

@@ -1,10 +1,10 @@
from datetime import datetime from datetime import datetime, timedelta
import pytz import pytz
def get_financial_year(year): def get_financial_year(year):
return get_april_fools(year), get_april_fools(year + 1) return get_april_fools(year), get_april_fools(year + 1) - timedelta(microseconds=1)
def get_april_fools(year): def get_april_fools(year):

View File

@@ -1,7 +1,7 @@
from datetime import datetime from datetime import datetime
from sqlalchemy import Float, Integer from sqlalchemy import Float, Integer
from sqlalchemy import func, case, cast, desc, between from sqlalchemy import func, case, cast
from sqlalchemy import literal_column from sqlalchemy import literal_column
from app import db from app import db
@@ -49,8 +49,7 @@ def get_monthly_billing_data(service_id, year):
def billing_data_filter(notification_type, start_date, end_date, service_id): def billing_data_filter(notification_type, start_date, end_date, service_id):
return [ return [
NotificationHistory.notification_type == notification_type, NotificationHistory.notification_type == notification_type,
NotificationHistory.created_at >= start_date, NotificationHistory.created_at.between(start_date, end_date),
NotificationHistory.created_at < end_date,
NotificationHistory.service_id == service_id, NotificationHistory.service_id == service_id,
NotificationHistory.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE), NotificationHistory.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE),
NotificationHistory.key_type != KEY_TYPE_TEST NotificationHistory.key_type != KEY_TYPE_TEST
@@ -106,11 +105,11 @@ def get_rates_for_year(start_date, end_date, notification_type):
rates = Rate.query.filter(Rate.notification_type == notification_type).order_by(Rate.valid_from).all() rates = Rate.query.filter(Rate.notification_type == notification_type).order_by(Rate.valid_from).all()
results = [] results = []
for current_rate, current_rate_expiry_date in zip(rates, rates[1:]): for current_rate, current_rate_expiry_date in zip(rates, rates[1:]):
if is_between_end_date_exclusive(current_rate.valid_from, start_date, end_date) or \ if is_between(current_rate.valid_from, start_date, end_date) or \
is_between_end_date_exclusive(current_rate_expiry_date.valid_from, start_date, end_date): is_between(current_rate_expiry_date.valid_from, start_date, end_date):
results.append(current_rate) results.append(current_rate)
if is_between_end_date_exclusive(rates[-1].valid_from, start_date, end_date): if is_between(rates[-1].valid_from, start_date, end_date):
results.append(rates[-1]) results.append(rates[-1])
if not results: if not results:
@@ -120,8 +119,8 @@ def get_rates_for_year(start_date, end_date, notification_type):
return results return results
def is_between_end_date_exclusive(date, start_date, end_date): def is_between(date, start_date, end_date):
return start_date <= date < end_date return start_date <= date <= end_date
def sms_billing_data_per_month_query(rate, service_id, start_date, end_date): def sms_billing_data_per_month_query(rate, service_id, start_date, end_date):

View File

@@ -250,8 +250,7 @@ def get_notification_billable_unit_count_per_month(service_id, year):
).filter( ).filter(
NotificationHistory.billable_units != 0, NotificationHistory.billable_units != 0,
NotificationHistory.service_id == service_id, NotificationHistory.service_id == service_id,
NotificationHistory.created_at >= start_date, NotificationHistory.created_at.between(start_date, end_date)
NotificationHistory.created_at < end_date
).group_by( ).group_by(
month month
).order_by( ).order_by(

View File

@@ -237,8 +237,7 @@ def dao_fetch_monthly_historical_stats_by_template_for_service(service_id, year)
func.count().label('count') func.count().label('count')
).filter( ).filter(
NotificationHistory.service_id == service_id, NotificationHistory.service_id == service_id,
NotificationHistory.created_at >= start_date, NotificationHistory.created_at.between(start_date, end_date)
NotificationHistory.created_at < end_date
).group_by( ).group_by(
month, month,
@@ -273,8 +272,7 @@ def dao_fetch_monthly_historical_stats_for_service(service_id, year):
func.count(NotificationHistory.id).label('count') func.count(NotificationHistory.id).label('count')
).filter( ).filter(
NotificationHistory.service_id == service_id, NotificationHistory.service_id == service_id,
NotificationHistory.created_at >= start_date, NotificationHistory.created_at.between(start_date, end_date)
NotificationHistory.created_at < end_date
).group_by( ).group_by(
NotificationHistory.notification_type, NotificationHistory.notification_type,
NotificationHistory.status, NotificationHistory.status,

View File

@@ -4,7 +4,7 @@ from app.dao.date_util import get_financial_year, get_april_fools
def test_get_financial_year(): def test_get_financial_year():
start, end = get_financial_year(2000) start, end = get_financial_year(2000)
assert str(start) == '2000-03-31 23:00:00' assert str(start) == '2000-03-31 23:00:00'
assert str(end) == '2001-03-31 23:00:00' assert str(end) == '2001-03-31 22:59:59.999999'
def test_get_april_fools(): def test_get_april_fools():