Write some pseudo code for ft billing

This commit is contained in:
Rebecca Law
2018-04-09 11:38:00 +01:00
parent 8cd4e1be29
commit bd91aac763
2 changed files with 71 additions and 3 deletions

View File

@@ -1,7 +1,8 @@
from datetime import datetime, timedelta
from sqlalchemy import func
from app import db
from app.dao.date_util import get_month_start_and_end_date_in_utc
from app.dao.date_util import get_month_start_and_end_date_in_utc, get_financial_year
from app.models import FactBilling
from app.utils import convert_utc_to_bst
@@ -32,3 +33,52 @@ def fetch_annual_billing_by_month(service_id, billing_month, notification_type):
).all()
return monthly_data, start_date
def need_deltas(start_date, end_date, service_id, notification_type):
max_fact_billing_date = db.session.query(
func.max(FactBilling.bst_date)
).filter(
FactBilling.notification_type == notification_type,
FactBilling.service_id == service_id,
FactBilling.bst_date >= start_date,
FactBilling.bst_date <= end_date
).one()
print(max_fact_billing_date)
return max_fact_billing_date < end_date
def fetch_annual_billing_for_year(service_id, year):
year_start_date, year_end_date = get_financial_year(year)
utcnow = datetime.utcnow()
today = convert_utc_to_bst(utcnow)
last_2_days = utcnow - timedelta(days=2)
last_bst_date_for_ft = convert_utc_to_bst(last_2_days)
# if year end date is less than today, we are calculating for data in the past and have no need for deltas.
if year_end_date >= today:
todays_data = get_deltas(service_id, last_2_days, today)
year_end_date = last_bst_date_for_ft
yearly_data = db.session.query(
FactBilling.notifications_sent,
FactBilling.billable_units,
FactBilling.service_id,
FactBilling.notification_type,
FactBilling.rate,
FactBilling.rate_multiplier,
FactBilling.international
).filter(
FactBilling.service_id == service_id,
FactBilling.bst_date >= year_start_date,
FactBilling.bst_date <= last_bst_date_for_ft
).all()
# today_data + yearly_data and aggregate by month
def get_deltas(service_id, start_date_end_date):
# query ft_billing data using queries from create_nightly_billing
return []

View File

@@ -1,8 +1,11 @@
from datetime import datetime
from decimal import Decimal
from app.dao.fact_billing_dao import fetch_annual_billing_by_month
from tests.app.db import create_ft_billing, create_service, create_template
from freezegun import freeze_time
from app.dao.date_util import get_month_start_and_end_date_in_utc
from app.dao.fact_billing_dao import fetch_annual_billing_by_month, need_deltas
from tests.app.db import create_ft_billing, create_service, create_template, create_notification
def test_fetch_annual_billing_by_month(notify_db_session):
@@ -22,3 +25,18 @@ def test_fetch_annual_billing_by_month(notify_db_session):
assert results[0] == (31, Decimal('31'), service.id, 'email', Decimal('0'), Decimal('1'), False)
assert month == datetime(2018, 1, 1)
@freeze_time("2018-01-21 13:00:00")
def test_need_deltas(notify_db_session):
service = create_service()
template = create_template(service=service, template_type="email")
for i in range(1, 21):
record = create_ft_billing(bst_date='2018-01-{}'.format(i),
service=service,
template=template,
notification_type='email')
start_date, end_date = get_month_start_and_end_date_in_utc(datetime.utcnow())
result = need_deltas(start_date=start_date, end_date=end_date,
service_id=service.id, notification_type='email')
assert result