mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-19 05:58:53 -04:00
Merge branch 'master' into noti-stats-cleanup
This commit is contained in:
@@ -33,7 +33,6 @@ from app.dao.dao_utils import transactional
|
||||
from app.statsd_decorators import statsd
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_notification_statistics_for_service_and_day(service_id, day):
|
||||
# only used by stat-updating code in tasks.py
|
||||
return NotificationStatistics.query.filter_by(
|
||||
@@ -92,7 +91,6 @@ def create_notification_statistics_dict(service_id, day):
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_template_usage(service_id, limit_days=None):
|
||||
|
||||
table = NotificationHistory
|
||||
|
||||
if limit_days and limit_days <= 7: # can get this data from notifications table
|
||||
@@ -110,28 +108,18 @@ def dao_get_template_usage(service_id, limit_days=None):
|
||||
query_filter.append(table.created_at >= days_ago(limit_days))
|
||||
|
||||
return query.filter(*query_filter) \
|
||||
.join(Template)\
|
||||
.group_by(table.template_id, Template.name, Template.template_type)\
|
||||
.order_by(asc(Template.name))\
|
||||
.join(Template) \
|
||||
.group_by(table.template_id, Template.name, Template.template_type) \
|
||||
.order_by(asc(Template.name)) \
|
||||
.all()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_template_statistics_for_service(service_id, limit_days=None):
|
||||
query_filter = [TemplateStatistics.service_id == service_id]
|
||||
if limit_days is not None:
|
||||
query_filter.append(TemplateStatistics.day >= days_ago(limit_days))
|
||||
return TemplateStatistics.query.filter(*query_filter).order_by(
|
||||
desc(TemplateStatistics.updated_at)).all()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_template_statistics_for_template(template_id):
|
||||
return TemplateStatistics.query.filter(
|
||||
TemplateStatistics.template_id == template_id
|
||||
).order_by(
|
||||
desc(TemplateStatistics.updated_at)
|
||||
).all()
|
||||
def dao_get_last_template_usage(template_id):
|
||||
return NotificationHistory.query.filter(NotificationHistory.template_id == template_id)\
|
||||
.join(Template) \
|
||||
.order_by(desc(NotificationHistory.created_at)) \
|
||||
.first()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
|
||||
@@ -6,11 +6,9 @@ from flask import (
|
||||
|
||||
from app.dao.notifications_dao import (
|
||||
dao_get_template_usage,
|
||||
dao_get_template_statistics_for_service,
|
||||
dao_get_template_statistics_for_template
|
||||
)
|
||||
dao_get_last_template_usage)
|
||||
|
||||
from app.schemas import template_statistics_schema
|
||||
from app.schemas import notifications_filter_schema, NotificationWithTemplateSchema, notification_with_template_schema
|
||||
|
||||
template_statistics = Blueprint('template-statistics',
|
||||
__name__,
|
||||
@@ -47,6 +45,10 @@ def get_template_statistics_for_service_by_day(service_id):
|
||||
|
||||
@template_statistics.route('/<template_id>')
|
||||
def get_template_statistics_for_template_id(service_id, template_id):
|
||||
stats = dao_get_template_statistics_for_template(template_id)
|
||||
data = template_statistics_schema.dump(stats, many=True).data
|
||||
notification = dao_get_last_template_usage(template_id)
|
||||
if not notification:
|
||||
message = 'No template found for id {}'.format(template_id)
|
||||
errors = {'template_id': [message]}
|
||||
raise InvalidRequest(errors, status_code=404)
|
||||
data = notification_with_template_schema.dump(notification).data
|
||||
return jsonify(data=data)
|
||||
|
||||
Reference in New Issue
Block a user