mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 10:21:14 -05:00
Merge branch 'master' into noti-stats-cleanup
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import itertools
|
||||
from functools import wraps
|
||||
from app.history_meta import versioned_objects, create_history
|
||||
from functools import wraps, partial
|
||||
|
||||
from app.history_meta import create_history
|
||||
|
||||
|
||||
def transactional(func):
|
||||
@@ -19,14 +20,16 @@ def transactional(func):
|
||||
return commit_or_rollback
|
||||
|
||||
|
||||
def version_class(model_class):
|
||||
def version_class(model_class, history_cls=None):
|
||||
create_hist = partial(create_history, history_cls=history_cls)
|
||||
|
||||
def versioned(func):
|
||||
@wraps(func)
|
||||
def record_version(*args, **kwargs):
|
||||
from app import db
|
||||
func(*args, **kwargs)
|
||||
history_objects = [create_history(obj) for obj in
|
||||
versioned_objects(itertools.chain(db.session.new, db.session.dirty))
|
||||
history_objects = [create_hist(obj) for obj in
|
||||
itertools.chain(db.session.new, db.session.dirty)
|
||||
if isinstance(obj, model_class)]
|
||||
for h_obj in history_objects:
|
||||
db.session.add(h_obj)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -16,6 +16,7 @@ from app.models import (
|
||||
VerifyCode,
|
||||
ApiKey,
|
||||
Template,
|
||||
TemplateHistory,
|
||||
Job,
|
||||
NotificationHistory,
|
||||
Notification,
|
||||
@@ -122,7 +123,7 @@ def delete_service_and_all_associated_db_objects(service):
|
||||
_delete_commit(Notification.query.filter_by(service=service))
|
||||
_delete_commit(Job.query.filter_by(service=service))
|
||||
_delete_commit(Template.query.filter_by(service=service))
|
||||
_delete_commit(Template.get_history_model().query.filter_by(service_id=service.id))
|
||||
_delete_commit(TemplateHistory.query.filter_by(service_id=service.id))
|
||||
|
||||
verify_codes = VerifyCode.query.join(User).filter(User.id.in_([x.id for x in service.users]))
|
||||
list(map(db.session.delete, verify_codes))
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import uuid
|
||||
from app import db
|
||||
from app.models import (Template, Service)
|
||||
|
||||
from sqlalchemy import (asc, desc)
|
||||
|
||||
from app import db
|
||||
from app.models import (Template, TemplateHistory)
|
||||
from app.dao.dao_utils import (
|
||||
transactional,
|
||||
version_class
|
||||
@@ -10,7 +11,7 @@ from app.dao.dao_utils import (
|
||||
|
||||
|
||||
@transactional
|
||||
@version_class(Template)
|
||||
@version_class(Template, TemplateHistory)
|
||||
def dao_create_template(template):
|
||||
template.id = uuid.uuid4() # must be set now so version history model can use same id
|
||||
template.archived = False
|
||||
@@ -18,14 +19,14 @@ def dao_create_template(template):
|
||||
|
||||
|
||||
@transactional
|
||||
@version_class(Template)
|
||||
@version_class(Template, TemplateHistory)
|
||||
def dao_update_template(template):
|
||||
db.session.add(template)
|
||||
|
||||
|
||||
def dao_get_template_by_id_and_service_id(template_id, service_id, version=None):
|
||||
if version is not None:
|
||||
return Template.get_history_model().query.filter_by(
|
||||
return TemplateHistory.query.filter_by(
|
||||
id=template_id,
|
||||
service_id=service_id,
|
||||
version=version).one()
|
||||
@@ -34,7 +35,7 @@ def dao_get_template_by_id_and_service_id(template_id, service_id, version=None)
|
||||
|
||||
def dao_get_template_by_id(template_id, version=None):
|
||||
if version is not None:
|
||||
return Template.get_history_model().query.filter_by(
|
||||
return TemplateHistory.query.filter_by(
|
||||
id=template_id,
|
||||
version=version).one()
|
||||
return Template.query.filter_by(id=template_id).one()
|
||||
@@ -50,6 +51,8 @@ def dao_get_all_templates_for_service(service_id):
|
||||
|
||||
|
||||
def dao_get_template_versions(service_id, template_id):
|
||||
history_model = Template.get_history_model()
|
||||
return history_model.query.filter_by(service_id=service_id, id=template_id).order_by(
|
||||
desc(history_model.version)).all()
|
||||
return TemplateHistory.query.filter_by(
|
||||
service_id=service_id, id=template_id
|
||||
).order_by(
|
||||
desc(TemplateHistory.version)
|
||||
).all()
|
||||
|
||||
Reference in New Issue
Block a user