mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 06:21:50 -05:00
There is no need to have a separate table to store template monthly statistics. It's easy enough to aggregate the stats from ft_notification_status.
This removes the nightly task, and all the dao methods. The next PR will remove the table.
This commit is contained in:
@@ -40,10 +40,6 @@ from app.dao.provider_details_dao import (
|
||||
dao_toggle_sms_provider
|
||||
)
|
||||
from app.dao.service_callback_api_dao import get_service_delivery_status_callback_api_for_service
|
||||
from app.dao.services_dao import (
|
||||
dao_fetch_monthly_historical_stats_by_template
|
||||
)
|
||||
from app.dao.stats_template_usage_by_month_dao import insert_or_update_stats_for_template
|
||||
from app.dao.users_dao import delete_codes_older_created_more_than_a_day_ago
|
||||
from app.exceptions import NotificationTechnicalFailureException
|
||||
from app.models import (
|
||||
@@ -405,21 +401,6 @@ def check_job_status():
|
||||
raise JobIncompleteError("Job(s) {} have not completed.".format(job_ids))
|
||||
|
||||
|
||||
@notify_celery.task(name='daily-stats-template-usage-by-month')
|
||||
@statsd(namespace="tasks")
|
||||
def daily_stats_template_usage_by_month():
|
||||
results = dao_fetch_monthly_historical_stats_by_template()
|
||||
|
||||
for result in results:
|
||||
if result.template_id:
|
||||
insert_or_update_stats_for_template(
|
||||
result.template_id,
|
||||
result.month,
|
||||
result.year,
|
||||
result.count
|
||||
)
|
||||
|
||||
|
||||
@notify_celery.task(name='raise-alert-if-no-letter-ack-file')
|
||||
@statsd(namespace="tasks")
|
||||
def letter_raise_alert_if_no_ack_file_for_zip():
|
||||
|
||||
@@ -195,11 +195,6 @@ class Config(object):
|
||||
'schedule': crontab(hour=0, minute=5),
|
||||
'options': {'queue': QueueNames.PERIODIC}
|
||||
},
|
||||
'daily-stats-template-usage-by-month': {
|
||||
'task': 'daily-stats-template-usage-by-month',
|
||||
'schedule': crontab(hour=0, minute=10),
|
||||
'options': {'queue': QueueNames.PERIODIC}
|
||||
},
|
||||
'create-nightly-billing': {
|
||||
'task': 'create-nightly-billing',
|
||||
'schedule': crontab(hour=0, minute=15),
|
||||
|
||||
@@ -11,9 +11,7 @@ from app.dao.dao_utils import (
|
||||
transactional,
|
||||
version_class
|
||||
)
|
||||
from app.dao.date_util import get_financial_year
|
||||
from app.dao.service_sms_sender_dao import insert_service_sms_sender
|
||||
from app.dao.stats_template_usage_by_month_dao import dao_get_template_usage_stats_by_service
|
||||
from app.models import (
|
||||
AnnualBilling,
|
||||
ApiKey,
|
||||
@@ -389,75 +387,3 @@ def dao_fetch_monthly_historical_stats_by_template():
|
||||
year,
|
||||
month
|
||||
).all()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year):
|
||||
|
||||
results = dao_get_template_usage_stats_by_service(service_id, year)
|
||||
|
||||
stats = []
|
||||
for result in results:
|
||||
stat = type("", (), {})()
|
||||
stat.template_id = result.template_id
|
||||
stat.template_type = result.template_type
|
||||
stat.name = str(result.name)
|
||||
stat.month = result.month
|
||||
stat.year = result.year
|
||||
stat.count = result.count
|
||||
stat.is_precompiled_letter = result.is_precompiled_letter
|
||||
stats.append(stat)
|
||||
|
||||
month = get_london_month_from_utc_column(Notification.created_at)
|
||||
year_func = func.date_trunc("year", Notification.created_at)
|
||||
start_date = datetime.combine(date.today(), time.min)
|
||||
|
||||
fy_start, fy_end = get_financial_year(year)
|
||||
|
||||
if fy_start < datetime.now() < fy_end:
|
||||
today_results = db.session.query(
|
||||
Notification.template_id,
|
||||
Template.is_precompiled_letter,
|
||||
Template.name,
|
||||
Template.template_type,
|
||||
extract('month', month).label('month'),
|
||||
extract('year', year_func).label('year'),
|
||||
func.count().label('count')
|
||||
).join(
|
||||
Template, Notification.template_id == Template.id,
|
||||
).filter(
|
||||
Notification.created_at >= start_date,
|
||||
Notification.service_id == service_id,
|
||||
# we don't want to include test keys
|
||||
Notification.key_type != KEY_TYPE_TEST
|
||||
).group_by(
|
||||
Notification.template_id,
|
||||
Template.hidden,
|
||||
Template.name,
|
||||
Template.template_type,
|
||||
month,
|
||||
year_func
|
||||
).order_by(
|
||||
Notification.template_id
|
||||
).all()
|
||||
|
||||
for today_result in today_results:
|
||||
add_to_stats = True
|
||||
for stat in stats:
|
||||
if today_result.template_id == stat.template_id and today_result.month == stat.month \
|
||||
and today_result.year == stat.year:
|
||||
stat.count = stat.count + today_result.count
|
||||
add_to_stats = False
|
||||
|
||||
if add_to_stats:
|
||||
new_stat = type("StatsTemplateUsageByMonth", (), {})()
|
||||
new_stat.template_id = today_result.template_id
|
||||
new_stat.template_type = today_result.template_type
|
||||
new_stat.name = today_result.name
|
||||
new_stat.month = int(today_result.month)
|
||||
new_stat.year = int(today_result.year)
|
||||
new_stat.count = today_result.count
|
||||
new_stat.is_precompiled_letter = today_result.is_precompiled_letter
|
||||
stats.append(new_stat)
|
||||
|
||||
return stats
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
from notifications_utils.statsd_decorators import statsd
|
||||
from sqlalchemy import or_, and_, desc
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import transactional
|
||||
from app.models import StatsTemplateUsageByMonth, Template
|
||||
|
||||
|
||||
@transactional
|
||||
@statsd(namespace="dao")
|
||||
def insert_or_update_stats_for_template(template_id, month, year, count):
|
||||
result = db.session.query(
|
||||
StatsTemplateUsageByMonth
|
||||
).filter(
|
||||
StatsTemplateUsageByMonth.template_id == template_id,
|
||||
StatsTemplateUsageByMonth.month == month,
|
||||
StatsTemplateUsageByMonth.year == year
|
||||
).update(
|
||||
{
|
||||
'count': count
|
||||
}
|
||||
)
|
||||
if result == 0:
|
||||
monthly_stats = StatsTemplateUsageByMonth(
|
||||
template_id=template_id,
|
||||
month=month,
|
||||
year=year,
|
||||
count=count
|
||||
)
|
||||
|
||||
db.session.add(monthly_stats)
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_template_usage_stats_by_service(service_id, year):
|
||||
return db.session.query(
|
||||
StatsTemplateUsageByMonth.template_id,
|
||||
Template.name,
|
||||
Template.template_type,
|
||||
Template.is_precompiled_letter,
|
||||
StatsTemplateUsageByMonth.month,
|
||||
StatsTemplateUsageByMonth.year,
|
||||
StatsTemplateUsageByMonth.count
|
||||
).join(
|
||||
Template, StatsTemplateUsageByMonth.template_id == Template.id
|
||||
).filter(
|
||||
Template.service_id == service_id
|
||||
).filter(
|
||||
or_(
|
||||
and_(
|
||||
StatsTemplateUsageByMonth.month.in_([4, 5, 6, 7, 8, 9, 10, 11, 12]),
|
||||
StatsTemplateUsageByMonth.year == year
|
||||
), and_(
|
||||
StatsTemplateUsageByMonth.month.in_([1, 2, 3]),
|
||||
StatsTemplateUsageByMonth.year == year + 1
|
||||
)
|
||||
)
|
||||
).order_by(
|
||||
desc(StatsTemplateUsageByMonth.month)
|
||||
).all()
|
||||
Reference in New Issue
Block a user