mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
Merge branch 'master' into noti-stats-cleanup
This commit is contained in:
@@ -7,9 +7,8 @@ from datetime import (
|
||||
|
||||
from flask import current_app
|
||||
from werkzeug.datastructures import MultiDict
|
||||
from sqlalchemy import (desc, func, Integer, or_, and_, asc)
|
||||
from sqlalchemy.sql.expression import cast
|
||||
from notifications_utils.template import get_sms_fragment_count
|
||||
from sqlalchemy import (desc, func, or_, and_, asc)
|
||||
from sqlalchemy.orm import joinedload
|
||||
|
||||
from app import db
|
||||
from app.dao import days_ago
|
||||
@@ -91,6 +90,32 @@ 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
|
||||
table = Notification
|
||||
|
||||
query = db.session.query(
|
||||
func.count(table.template_id).label('count'),
|
||||
table.template_id,
|
||||
Template.name,
|
||||
Template.template_type
|
||||
)
|
||||
|
||||
query_filter = [table.service_id == service_id]
|
||||
if limit_days is not 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))\
|
||||
.all()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_template_statistics_for_service(service_id, limit_days=None):
|
||||
query_filter = [TemplateStatistics.service_id == service_id]
|
||||
@@ -307,12 +332,12 @@ def get_notifications_for_job(service_id, job_id, filter_dict=None, page=1, page
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def get_notification(service_id, notification_id, key_type=None):
|
||||
def get_notification_with_personalisation(service_id, notification_id, key_type):
|
||||
filter_dict = {'service_id': service_id, 'id': notification_id}
|
||||
if key_type:
|
||||
filter_dict['key_type'] = key_type
|
||||
|
||||
return Notification.query.filter_by(**filter_dict).one()
|
||||
return Notification.query.filter_by(**filter_dict).options(joinedload('template_history')).one()
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
@@ -330,7 +355,8 @@ def get_notifications_for_service(service_id,
|
||||
page=1,
|
||||
page_size=None,
|
||||
limit_days=None,
|
||||
key_type=None):
|
||||
key_type=None,
|
||||
personalisation=False):
|
||||
if page_size is None:
|
||||
page_size = current_app.config['PAGE_SIZE']
|
||||
filters = [Notification.service_id == service_id]
|
||||
@@ -344,6 +370,10 @@ def get_notifications_for_service(service_id,
|
||||
|
||||
query = Notification.query.filter(*filters)
|
||||
query = _filter_query(query, filter_dict)
|
||||
if personalisation:
|
||||
query = query.options(
|
||||
joinedload('template_history')
|
||||
)
|
||||
return query.order_by(desc(Notification.created_at)).paginate(
|
||||
page=page,
|
||||
per_page=page_size
|
||||
|
||||
Reference in New Issue
Block a user