2018-03-28 10:39:25 +01:00
|
|
|
from datetime import datetime, timedelta, time
|
2018-04-24 17:37:04 +01:00
|
|
|
|
|
|
|
|
from flask import current_app
|
2018-03-20 13:53:31 +00:00
|
|
|
from notifications_utils.statsd_decorators import statsd
|
2018-04-24 17:37:04 +01:00
|
|
|
|
2018-03-20 13:53:31 +00:00
|
|
|
from app import notify_celery
|
2018-04-24 17:37:04 +01:00
|
|
|
from app.dao.fact_billing_dao import (
|
|
|
|
|
fetch_billing_data,
|
|
|
|
|
update_fact_billing
|
|
|
|
|
)
|
2018-03-27 10:37:56 +01:00
|
|
|
from app.utils import convert_bst_to_utc
|
2018-03-14 14:47:30 +00:00
|
|
|
|
|
|
|
|
|
2018-03-20 13:53:31 +00:00
|
|
|
@notify_celery.task(name="create-nightly-billing")
|
2018-03-14 14:47:30 +00:00
|
|
|
@statsd(namespace="tasks")
|
2018-03-20 13:53:31 +00:00
|
|
|
def create_nightly_billing(day_start=None):
|
2018-03-27 10:37:56 +01:00
|
|
|
# day_start is a datetime.date() object. e.g.
|
|
|
|
|
# 3 days of data counting back from day_start is consolidated
|
2018-03-16 09:22:34 +00:00
|
|
|
if day_start is None:
|
2018-03-27 10:37:56 +01:00
|
|
|
day_start = datetime.today() - timedelta(days=1)
|
2018-03-16 09:22:34 +00:00
|
|
|
|
2018-03-27 10:37:56 +01:00
|
|
|
for i in range(0, 3):
|
|
|
|
|
process_day = day_start - timedelta(days=i)
|
2018-03-28 10:39:25 +01:00
|
|
|
ds = convert_bst_to_utc(datetime.combine(process_day, time.min))
|
|
|
|
|
de = convert_bst_to_utc(datetime.combine(process_day + timedelta(days=1), time.min))
|
2018-03-14 14:47:30 +00:00
|
|
|
|
2018-04-24 17:37:04 +01:00
|
|
|
transit_data = fetch_billing_data(start_date=ds, end_date=de)
|
2018-03-21 14:14:16 +00:00
|
|
|
|
2018-03-27 10:37:56 +01:00
|
|
|
updated_records = 0
|
|
|
|
|
inserted_records = 0
|
2018-03-21 14:14:16 +00:00
|
|
|
|
2018-03-27 10:37:56 +01:00
|
|
|
for data in transit_data:
|
2018-04-24 17:37:04 +01:00
|
|
|
inserted_records, updated_records = update_fact_billing(data,
|
|
|
|
|
inserted_records,
|
|
|
|
|
process_day,
|
|
|
|
|
updated_records)
|
2018-03-27 10:37:56 +01:00
|
|
|
|
|
|
|
|
current_app.logger.info('ft_billing {} to {}: {} rows updated, {} rows inserted'
|
|
|
|
|
.format(ds, de, updated_records, inserted_records))
|