Files
notifications-api/app/celery/reporting_tasks.py

40 lines
1.5 KiB
Python
Raw Normal View History

from datetime import datetime, timedelta, time
from flask import current_app
from notifications_utils.statsd_decorators import statsd
from app import notify_celery
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
@notify_celery.task(name="create-nightly-billing")
@statsd(namespace="tasks")
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)
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))
transit_data = fetch_billing_data(start_date=ds, end_date=de)
2018-03-27 10:37:56 +01:00
updated_records = 0
inserted_records = 0
2018-03-27 10:37:56 +01:00
for data in transit_data:
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))