Merge pull request #1390 from alphagov/rc-monthly-template-usage-endpoint

Added new endpoint to get the new template stats
This commit is contained in:
Richard Chapman
2017-11-15 16:20:04 +00:00
committed by GitHub
9 changed files with 602 additions and 39 deletions

View File

@@ -12,31 +12,32 @@ 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,
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
@@ -539,5 +540,63 @@ def dao_fetch_monthly_historical_stats_by_template():
month,
year
).order_by(
NotificationHistory.template_id
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 = list()
for result in results:
stat = type("", (), {})()
stat.template_id = result.template_id
stat.name = str(result.name)
stat.month = result.month
stat.year = result.year
stat.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,
Template.name,
extract('month', month).label('month'),
extract('year', year).label('year'),
func.count().label('count')
).join(
Template, Notification.template_id == Template.id
).filter(
Notification.created_at >= start_date
).group_by(
Notification.template_id,
Template.name,
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 = type("", (), {})()
new_stat.template_id = today_result.template_id
new_stat.name = today_result.name
new_stat.month = today_result.month
new_stat.year = today_result.year
new_stat.count = today_result.count
stats.append(new_stat)
return stats

View File

@@ -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,21 @@ 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, year):
return db.session.query(
StatsTemplateUsageByMonth.template_id,
Template.name,
StatsTemplateUsageByMonth.month,
StatsTemplateUsageByMonth.year,
StatsTemplateUsageByMonth.count
).join(
Template, StatsTemplateUsageByMonth.template_id == Template.id
).filter(
Template.service_id == service_id,
StatsTemplateUsageByMonth.year == year
).all()