Add statsd annotations for the fact table queries.

This commit is contained in:
Rebecca Law
2020-02-18 14:33:17 +00:00
parent ce253a990b
commit 291c6d6dc9
2 changed files with 26 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
from datetime import datetime, timedelta, time, date from datetime import datetime, timedelta, time, date
from flask import current_app from flask import current_app
from notifications_utils.statsd_decorators import statsd
from notifications_utils.timezones import convert_bst_to_utc, convert_utc_to_bst from notifications_utils.timezones import convert_bst_to_utc, convert_utc_to_bst
from sqlalchemy.dialects.postgresql import insert from sqlalchemy.dialects.postgresql import insert
from sqlalchemy import func, case, desc, Date, Integer, and_ from sqlalchemy import func, case, desc, Date, Integer, and_
@@ -29,6 +30,7 @@ from app.models import (
from app.utils import get_london_midnight_in_utc, get_notification_table_to_use from app.utils import get_london_midnight_in_utc, get_notification_table_to_use
@statsd(namespace="dao")
def fetch_sms_free_allowance_remainder(start_date): def fetch_sms_free_allowance_remainder(start_date):
# ASSUMPTION: AnnualBilling has been populated for year. # ASSUMPTION: AnnualBilling has been populated for year.
billing_year = get_financial_year_for_datetime(start_date) billing_year = get_financial_year_for_datetime(start_date)
@@ -59,6 +61,7 @@ def fetch_sms_free_allowance_remainder(start_date):
return query return query
@statsd(namespace="dao")
def fetch_sms_billing_for_all_services(start_date, end_date): def fetch_sms_billing_for_all_services(start_date, end_date):
# ASSUMPTION: AnnualBilling has been populated for year. # ASSUMPTION: AnnualBilling has been populated for year.
@@ -111,6 +114,7 @@ def fetch_sms_billing_for_all_services(start_date, end_date):
return query.all() return query.all()
@statsd(namespace="dao")
def fetch_letter_costs_for_all_services(start_date, end_date): def fetch_letter_costs_for_all_services(start_date, end_date):
query = db.session.query( query = db.session.query(
Organisation.name.label("organisation_name"), Organisation.name.label("organisation_name"),
@@ -142,6 +146,7 @@ def fetch_letter_costs_for_all_services(start_date, end_date):
return query.all() return query.all()
@statsd(namespace="dao")
def fetch_letter_line_items_for_all_services(start_date, end_date): def fetch_letter_line_items_for_all_services(start_date, end_date):
query = db.session.query( query = db.session.query(
Organisation.name.label("organisation_name"), Organisation.name.label("organisation_name"),
@@ -177,6 +182,7 @@ def fetch_letter_line_items_for_all_services(start_date, end_date):
return query.all() return query.all()
@statsd(namespace="dao")
def fetch_billing_totals_for_year(service_id, year): def fetch_billing_totals_for_year(service_id, year):
year_start_date, year_end_date = get_financial_year(year) year_start_date, year_end_date = get_financial_year(year)
""" """
@@ -225,6 +231,7 @@ def fetch_billing_totals_for_year(service_id, year):
return yearly_data return yearly_data
@statsd(namespace="dao")
def fetch_monthly_billing_for_year(service_id, year): def fetch_monthly_billing_for_year(service_id, year):
year_start_datetime, year_end_datetime = get_financial_year(year) year_start_datetime, year_end_datetime = get_financial_year(year)
@@ -287,6 +294,7 @@ def fetch_monthly_billing_for_year(service_id, year):
return yearly_data return yearly_data
@statsd(namespace="dao")
def delete_billing_data_for_service_for_day(process_day, service_id): def delete_billing_data_for_service_for_day(process_day, service_id):
""" """
Delete all ft_billing data for a given service on a given bst_date Delete all ft_billing data for a given service on a given bst_date
@@ -299,6 +307,7 @@ def delete_billing_data_for_service_for_day(process_day, service_id):
).delete() ).delete()
@statsd(namespace="dao")
def fetch_billing_data_for_day(process_day, service_id=None): def fetch_billing_data_for_day(process_day, service_id=None):
start_date = convert_bst_to_utc(datetime.combine(process_day, time.min)) start_date = convert_bst_to_utc(datetime.combine(process_day, time.min))
end_date = convert_bst_to_utc(datetime.combine(process_day + timedelta(days=1), time.min)) end_date = convert_bst_to_utc(datetime.combine(process_day + timedelta(days=1), time.min))
@@ -373,12 +382,14 @@ def _query_for_billing_data(table, notification_type, start_date, end_date, serv
return query.all() return query.all()
@statsd(namespace="dao")
def get_rates_for_billing(): def get_rates_for_billing():
non_letter_rates = Rate.query.order_by(desc(Rate.valid_from)).all() non_letter_rates = Rate.query.order_by(desc(Rate.valid_from)).all()
letter_rates = LetterRate.query.order_by(desc(LetterRate.start_date)).all() letter_rates = LetterRate.query.order_by(desc(LetterRate.start_date)).all()
return non_letter_rates, letter_rates return non_letter_rates, letter_rates
@statsd(namespace="dao")
def get_service_ids_that_need_billing_populated(start_date, end_date): def get_service_ids_that_need_billing_populated(start_date, end_date):
return db.session.query( return db.session.query(
NotificationHistory.service_id NotificationHistory.service_id
@@ -419,6 +430,7 @@ def get_rate(
return 0 return 0
@statsd(namespace="dao")
def update_fact_billing(data, process_day): def update_fact_billing(data, process_day):
non_letter_rates, letter_rates = get_rates_for_billing() non_letter_rates, letter_rates = get_rates_for_billing()
rate = get_rate(non_letter_rates, rate = get_rate(non_letter_rates,
@@ -462,6 +474,7 @@ def update_fact_billing(data, process_day):
db.session.commit() db.session.commit()
@statsd(namespace="dao")
def create_billing_record(data, rate, process_day): def create_billing_record(data, rate, process_day):
billing_record = FactBilling( billing_record = FactBilling(
bst_date=process_day, bst_date=process_day,

View File

@@ -1,6 +1,7 @@
from datetime import datetime, timedelta, time from datetime import datetime, timedelta, time
from flask import current_app from flask import current_app
from notifications_utils.statsd_decorators import statsd
from notifications_utils.timezones import convert_bst_to_utc from notifications_utils.timezones import convert_bst_to_utc
from sqlalchemy import case, func, Date from sqlalchemy import case, func, Date
from sqlalchemy.dialects.postgresql import insert from sqlalchemy.dialects.postgresql import insert
@@ -33,6 +34,7 @@ from app.utils import (
) )
@statsd(namespace="dao")
def fetch_notification_status_for_day(process_day, notification_type): def fetch_notification_status_for_day(process_day, notification_type):
start_date = convert_bst_to_utc(datetime.combine(process_day, time.min)) start_date = convert_bst_to_utc(datetime.combine(process_day, time.min))
end_date = convert_bst_to_utc(datetime.combine(process_day + timedelta(days=1), time.min)) end_date = convert_bst_to_utc(datetime.combine(process_day + timedelta(days=1), time.min))
@@ -58,6 +60,7 @@ def fetch_notification_status_for_day(process_day, notification_type):
return all_data_for_process_day return all_data_for_process_day
@statsd(namespace="dao")
def query_for_fact_status_data(table, start_date, end_date, notification_type, service_id): def query_for_fact_status_data(table, start_date, end_date, notification_type, service_id):
query = db.session.query( query = db.session.query(
table.template_id, table.template_id,
@@ -82,6 +85,7 @@ def query_for_fact_status_data(table, start_date, end_date, notification_type, s
return query.all() return query.all()
@statsd(namespace="dao")
@transactional @transactional
def update_fact_notification_status(data, process_day, notification_type): def update_fact_notification_status(data, process_day, notification_type):
table = FactNotificationStatus.__table__ table = FactNotificationStatus.__table__
@@ -104,6 +108,7 @@ def update_fact_notification_status(data, process_day, notification_type):
db.session.connection().execute(stmt) db.session.connection().execute(stmt)
@statsd(namespace="dao")
def fetch_notification_status_for_service_by_month(start_date, end_date, service_id): def fetch_notification_status_for_service_by_month(start_date, end_date, service_id):
return db.session.query( return db.session.query(
func.date_trunc('month', FactNotificationStatus.bst_date).label('month'), func.date_trunc('month', FactNotificationStatus.bst_date).label('month'),
@@ -122,6 +127,7 @@ def fetch_notification_status_for_service_by_month(start_date, end_date, service
).all() ).all()
@statsd(namespace="dao")
def fetch_notification_status_for_service_for_day(bst_day, service_id): def fetch_notification_status_for_service_for_day(bst_day, service_id):
return db.session.query( return db.session.query(
# return current month as a datetime so the data has the same shape as the ft_notification_status query # return current month as a datetime so the data has the same shape as the ft_notification_status query
@@ -140,6 +146,7 @@ def fetch_notification_status_for_service_for_day(bst_day, service_id):
).all() ).all()
@statsd(namespace="dao")
def fetch_notification_status_for_service_for_today_and_7_previous_days(service_id, by_template=False, limit_days=7): def fetch_notification_status_for_service_for_today_and_7_previous_days(service_id, by_template=False, limit_days=7):
start_date = midnight_n_days_ago(limit_days) start_date = midnight_n_days_ago(limit_days)
now = datetime.utcnow() now = datetime.utcnow()
@@ -192,6 +199,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(service_
).all() ).all()
@statsd(namespace="dao")
def fetch_notification_status_totals_for_all_services(start_date, end_date): def fetch_notification_status_totals_for_all_services(start_date, end_date):
stats = db.session.query( stats = db.session.query(
FactNotificationStatus.notification_type.label('notification_type'), FactNotificationStatus.notification_type.label('notification_type'),
@@ -240,6 +248,7 @@ def fetch_notification_status_totals_for_all_services(start_date, end_date):
return query.all() return query.all()
@statsd(namespace="dao")
def fetch_notification_statuses_for_job(job_id): def fetch_notification_statuses_for_job(job_id):
return db.session.query( return db.session.query(
FactNotificationStatus.notification_status.label('status'), FactNotificationStatus.notification_status.label('status'),
@@ -251,6 +260,7 @@ def fetch_notification_statuses_for_job(job_id):
).all() ).all()
@statsd(namespace="dao")
def fetch_stats_for_all_services_by_date_range(start_date, end_date, include_from_test_key=True): def fetch_stats_for_all_services_by_date_range(start_date, end_date, include_from_test_key=True):
stats = db.session.query( stats = db.session.query(
FactNotificationStatus.service_id.label('service_id'), FactNotificationStatus.service_id.label('service_id'),
@@ -345,6 +355,7 @@ def fetch_stats_for_all_services_by_date_range(start_date, end_date, include_fro
return query.all() return query.all()
@statsd(namespace="dao")
def fetch_monthly_template_usage_for_service(start_date, end_date, service_id): def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
# services_dao.replaces dao_fetch_monthly_historical_usage_by_template_for_service # services_dao.replaces dao_fetch_monthly_historical_usage_by_template_for_service
stats = db.session.query( stats = db.session.query(
@@ -429,6 +440,7 @@ def fetch_monthly_template_usage_for_service(start_date, end_date, service_id):
return query.all() return query.all()
@statsd(namespace="dao")
def get_total_sent_notifications_for_day_and_type(day, notification_type): def get_total_sent_notifications_for_day_and_type(day, notification_type):
result = db.session.query( result = db.session.query(
func.sum(FactNotificationStatus.notification_count).label('count') func.sum(FactNotificationStatus.notification_count).label('count')
@@ -441,6 +453,7 @@ def get_total_sent_notifications_for_day_and_type(day, notification_type):
return result or 0 return result or 0
@statsd(namespace="dao")
def fetch_monthly_notification_statuses_per_service(start_date, end_date): def fetch_monthly_notification_statuses_per_service(start_date, end_date):
return db.session.query( return db.session.query(
func.date_trunc('month', FactNotificationStatus.bst_date).cast(Date).label('date_created'), func.date_trunc('month', FactNotificationStatus.bst_date).cast(Date).label('date_created'),