mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 02:11:11 -05:00
Added new endpoint to get the new template stats
Added a new endpoint which combines the usage of the stats table and the data from the notifications tables, instead of using all the data from the notification_history table. This should speed up the query times and improve the page performance. - Updated to make the stats create and update function transactional as it actually wasn't committing the data to the table - Added the get from the stats table - Add a a method to combine the two results - Added the endpoint
This commit is contained in:
@@ -12,31 +12,33 @@ from app.dao.dao_utils import (
|
||||
)
|
||||
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 (
|
||||
ProviderStatistics,
|
||||
VerifyCode,
|
||||
AnnualBilling,
|
||||
ApiKey,
|
||||
InboundNumber,
|
||||
InvitedUser,
|
||||
Job,
|
||||
JobStatistics,
|
||||
Notification,
|
||||
NotificationHistory,
|
||||
Permission,
|
||||
ProviderStatistics,
|
||||
Service,
|
||||
ServicePermission,
|
||||
ServiceSmsSender,
|
||||
StatsTemplateUsageByMonth,
|
||||
Template,
|
||||
TemplateHistory,
|
||||
TemplateRedacted,
|
||||
InboundNumber,
|
||||
Job,
|
||||
NotificationHistory,
|
||||
Notification,
|
||||
Permission,
|
||||
User,
|
||||
InvitedUser,
|
||||
Service,
|
||||
ServicePermission,
|
||||
KEY_TYPE_TEST,
|
||||
NOTIFICATION_STATUS_TYPES,
|
||||
TEMPLATE_TYPES,
|
||||
JobStatistics,
|
||||
SMS_TYPE,
|
||||
VerifyCode,
|
||||
EMAIL_TYPE,
|
||||
INTERNATIONAL_SMS_TYPE,
|
||||
ServiceSmsSender,
|
||||
AnnualBilling
|
||||
KEY_TYPE_TEST,
|
||||
NOTIFICATION_STATUS_TYPES,
|
||||
SMS_TYPE,
|
||||
TEMPLATE_TYPES
|
||||
)
|
||||
from app.service.statistics import format_monthly_template_notification_stats
|
||||
from app.statsd_decorators import statsd
|
||||
@@ -540,5 +542,61 @@ def dao_fetch_monthly_historical_stats_by_template():
|
||||
month,
|
||||
year
|
||||
).order_by(
|
||||
NotificationHistory.template_id
|
||||
year,
|
||||
month
|
||||
).all()
|
||||
|
||||
|
||||
@transactional
|
||||
@statsd(namespace="dao")
|
||||
def dao_fetch_monthly_historical_usage_by_template_for_service(service_id):
|
||||
|
||||
results = dao_get_template_usage_stats_by_service(service_id)
|
||||
|
||||
stats = list()
|
||||
for result in results:
|
||||
stat = StatsTemplateUsageByMonth(
|
||||
template_id=result.template_id,
|
||||
month=result.month,
|
||||
year=result.year,
|
||||
count=result.count
|
||||
)
|
||||
stats.append(stat)
|
||||
|
||||
month = get_london_month_from_utc_column(Notification.created_at)
|
||||
year = func.date_trunc("year", Notification.created_at)
|
||||
start_date = datetime.combine(date.today(), time.min)
|
||||
|
||||
today_results = db.session.query(
|
||||
Notification.template_id,
|
||||
extract('month', month).label('month'),
|
||||
extract('year', year).label('year'),
|
||||
func.count().label('count')
|
||||
).filter(
|
||||
Notification.created_at >= start_date
|
||||
).group_by(
|
||||
Notification.template_id,
|
||||
month,
|
||||
year
|
||||
).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:
|
||||
stat.count = stat.count + today_result.count
|
||||
add_to_stats = False
|
||||
|
||||
if add_to_stats:
|
||||
new_stat = StatsTemplateUsageByMonth(
|
||||
template_id=today_result.template_id,
|
||||
month=today_result.month,
|
||||
year=today_result.year,
|
||||
count=today_result.count
|
||||
)
|
||||
|
||||
stats.append(new_stat)
|
||||
|
||||
return stats
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
from app import db
|
||||
from app.models import StatsTemplateUsageByMonth
|
||||
from app.statsd_decorators import statsd
|
||||
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
|
||||
@@ -21,4 +25,16 @@ def insert_or_update_stats_for_template(template_id, month, year, count):
|
||||
year=year,
|
||||
count=count
|
||||
)
|
||||
|
||||
db.session.add(monthly_stats)
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_template_usage_stats_by_service(service_id):
|
||||
return db.session.query(
|
||||
StatsTemplateUsageByMonth
|
||||
).join(
|
||||
Template, StatsTemplateUsageByMonth.template_id == Template.id
|
||||
).filter(
|
||||
Template.service_id == service_id
|
||||
).all()
|
||||
|
||||
Reference in New Issue
Block a user