2019-01-15 12:15:20 +00:00
|
|
|
from flask import Blueprint, jsonify, request
|
2020-02-05 13:03:54 +00:00
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.dao.fact_notification_status_dao import (
|
|
|
|
|
fetch_notification_status_for_service_for_today_and_7_previous_days,
|
|
|
|
|
)
|
2020-02-07 15:50:54 +00:00
|
|
|
from app.dao.notifications_dao import dao_get_last_date_template_was_used
|
2019-01-15 12:15:20 +00:00
|
|
|
from app.dao.templates_dao import dao_get_template_by_id_and_service_id
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.errors import InvalidRequest, register_errors
|
2020-12-18 17:39:35 +00:00
|
|
|
from app.utils import DATETIME_FORMAT
|
2016-04-04 12:21:38 +01:00
|
|
|
|
2018-04-12 10:46:22 +01:00
|
|
|
template_statistics = Blueprint('template_statistics',
|
2016-04-04 12:21:38 +01:00
|
|
|
__name__,
|
|
|
|
|
url_prefix='/service/<service_id>/template-statistics')
|
|
|
|
|
|
|
|
|
|
register_errors(template_statistics)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@template_statistics.route('')
|
2016-08-18 14:01:31 +01:00
|
|
|
def get_template_statistics_for_service_by_day(service_id):
|
2018-07-10 14:39:51 +01:00
|
|
|
whole_days = request.args.get('whole_days', request.args.get('limit_days', ''))
|
2018-04-12 10:46:22 +01:00
|
|
|
try:
|
2018-07-10 14:39:51 +01:00
|
|
|
whole_days = int(whole_days)
|
2018-04-12 10:46:22 +01:00
|
|
|
except ValueError:
|
2018-07-10 14:39:51 +01:00
|
|
|
error = '{} is not an integer'.format(whole_days)
|
|
|
|
|
message = {'whole_days': [error]}
|
2018-04-12 10:46:22 +01:00
|
|
|
raise InvalidRequest(message, status_code=400)
|
2016-08-18 14:01:31 +01:00
|
|
|
|
2018-07-10 14:39:51 +01:00
|
|
|
if whole_days < 0 or whole_days > 7:
|
|
|
|
|
raise InvalidRequest({'whole_days': ['whole_days must be between 0 and 7']}, status_code=400)
|
2019-01-15 11:55:45 +00:00
|
|
|
data = fetch_notification_status_for_service_for_today_and_7_previous_days(
|
|
|
|
|
service_id, by_template=True, limit_days=whole_days
|
|
|
|
|
)
|
2018-04-12 10:46:22 +01:00
|
|
|
|
2019-01-15 11:55:45 +00:00
|
|
|
return jsonify(data=[
|
|
|
|
|
{
|
|
|
|
|
'count': row.count,
|
|
|
|
|
'template_id': str(row.template_id),
|
|
|
|
|
'template_name': row.template_name,
|
|
|
|
|
'template_type': row.notification_type,
|
|
|
|
|
'is_precompiled_letter': row.is_precompiled_letter,
|
|
|
|
|
'status': row.status
|
|
|
|
|
}
|
|
|
|
|
for row in data
|
|
|
|
|
])
|
2016-08-18 14:01:31 +01:00
|
|
|
|
|
|
|
|
|
2020-02-05 13:03:54 +00:00
|
|
|
@template_statistics.route('/last-used/<uuid:template_id>')
|
|
|
|
|
def get_last_used_datetime_for_template(service_id, template_id):
|
2020-02-05 16:43:17 +00:00
|
|
|
# Check the template and service exist
|
|
|
|
|
dao_get_template_by_id_and_service_id(template_id, service_id)
|
2020-02-05 13:03:54 +00:00
|
|
|
|
|
|
|
|
last_date_used = dao_get_last_date_template_was_used(template_id=template_id,
|
|
|
|
|
service_id=service_id)
|
|
|
|
|
|
2020-02-06 11:23:50 +00:00
|
|
|
return jsonify(last_date_used=last_date_used.strftime(DATETIME_FORMAT) if last_date_used else last_date_used)
|