mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-19 14:09:20 -04:00
Derive markup dashboard from rendered partials
This commit extends the `ajax_block` component to take a `dict` of partials, from which it can select the partial matching its `key` argument and print its HTML to the page. This means that the same markup is only rendered in one place, rather than in two (individually in the JSON endpoint and as `include`s in the parent template).
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from datetime import datetime, date, timedelta
|
||||
from collections import namedtuple
|
||||
from itertools import groupby
|
||||
import json
|
||||
|
||||
from flask import (
|
||||
render_template,
|
||||
@@ -48,9 +49,8 @@ def service_dashboard(service_id):
|
||||
|
||||
return render_template(
|
||||
'views/dashboard/dashboard.html',
|
||||
templates=service_api_client.get_service_templates(service_id)['data'],
|
||||
updates_url=url_for(".service_dashboard_updates", service_id=service_id),
|
||||
**get_dashboard_statistics_for_service(service_id)
|
||||
partials=get_dashboard_partials(service_id)
|
||||
)
|
||||
|
||||
|
||||
@@ -58,25 +58,7 @@ def service_dashboard(service_id):
|
||||
@login_required
|
||||
@user_has_permissions('view_activity', admin_override=True)
|
||||
def service_dashboard_updates(service_id):
|
||||
dashboard_statistics = get_dashboard_statistics_for_service(service_id)
|
||||
return jsonify(**{
|
||||
'totals': render_template(
|
||||
'views/dashboard/_totals.html',
|
||||
**dashboard_statistics
|
||||
),
|
||||
'template-statistics': render_template(
|
||||
'views/dashboard/template-statistics.html',
|
||||
**dashboard_statistics
|
||||
),
|
||||
'jobs': render_template(
|
||||
'views/dashboard/_jobs.html',
|
||||
**dashboard_statistics
|
||||
),
|
||||
'usage': render_template(
|
||||
'views/dashboard/_usage.html',
|
||||
**dashboard_statistics
|
||||
),
|
||||
})
|
||||
return jsonify(**get_dashboard_partials(service_id))
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/template-activity")
|
||||
@@ -101,7 +83,7 @@ def template_history(service_id):
|
||||
def usage(service_id):
|
||||
return render_template(
|
||||
'views/usage.html',
|
||||
**get_dashboard_statistics_for_service(service_id)
|
||||
**calculate_usage(service_api_client.get_service_usage(service_id)['data'])
|
||||
)
|
||||
|
||||
|
||||
@@ -157,35 +139,54 @@ def aggregate_usage(template_statistics):
|
||||
)
|
||||
|
||||
|
||||
def get_dashboard_statistics_for_service(service_id):
|
||||
|
||||
usage = service_api_client.get_service_usage(service_id)
|
||||
sms_free_allowance = 250000
|
||||
sms_rate = 0.018
|
||||
|
||||
sms_sent = usage['data'].get('sms_count', 0)
|
||||
emails_sent = usage['data'].get('email_count', 0)
|
||||
def get_dashboard_partials(service_id):
|
||||
|
||||
template_statistics = aggregate_usage(
|
||||
template_statistics_client.get_template_statistics_for_service(service_id, limit_days=7)
|
||||
)
|
||||
|
||||
jobs = add_rate_to_jobs(filter(
|
||||
lambda job: job['original_file_name'] != current_app.config['TEST_MESSAGE_FILENAME'],
|
||||
job_api_client.get_job(service_id, limit_days=7)['data']
|
||||
))
|
||||
|
||||
return {
|
||||
'statistics': add_rates_to(sum_of_statistics(
|
||||
statistics_api_client.get_statistics_for_service(service_id, limit_days=7)['data']
|
||||
)),
|
||||
'template_statistics': template_statistics,
|
||||
'most_used_template_count': max(
|
||||
[row['usage_count'] for row in template_statistics] or [0]
|
||||
'totals': render_template(
|
||||
'views/dashboard/_totals.html',
|
||||
statistics=add_rates_to(sum_of_statistics(
|
||||
statistics_api_client.get_statistics_for_service(service_id, limit_days=7)['data']
|
||||
))
|
||||
),
|
||||
'template-statistics': render_template(
|
||||
'views/dashboard/template-statistics.html',
|
||||
template_statistics=template_statistics,
|
||||
most_used_template_count=max(
|
||||
[row['usage_count'] for row in template_statistics] or [0]
|
||||
),
|
||||
),
|
||||
'has_template_statistics': bool(template_statistics),
|
||||
'jobs': render_template(
|
||||
'views/dashboard/_jobs.html',
|
||||
jobs=jobs
|
||||
),
|
||||
'has_jobs': bool(jobs),
|
||||
'usage': render_template(
|
||||
'views/dashboard/_usage.html',
|
||||
**calculate_usage(service_api_client.get_service_usage(service_id)['data'])
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
def calculate_usage(usage, sms_free_allowance=250000, sms_rate=0.018):
|
||||
|
||||
sms_sent = usage.get('sms_count', 0)
|
||||
emails_sent = usage.get('email_count', 0)
|
||||
|
||||
return {
|
||||
'emails_sent': emails_sent,
|
||||
'sms_free_allowance': sms_free_allowance,
|
||||
'sms_sent': sms_sent,
|
||||
'sms_allowance_remaining': max(0, (sms_free_allowance - sms_sent)),
|
||||
'sms_chargeable': max(0, sms_sent - sms_free_allowance),
|
||||
'sms_rate': sms_rate,
|
||||
'jobs': add_rate_to_jobs(filter(
|
||||
lambda job: job['original_file_name'] != current_app.config['TEST_MESSAGE_FILENAME'],
|
||||
job_api_client.get_job(service_id, limit_days=7)['data']
|
||||
))
|
||||
'sms_rate': sms_rate
|
||||
}
|
||||
|
||||
@@ -70,22 +70,13 @@ def view_jobs(service_id):
|
||||
@login_required
|
||||
@user_has_permissions('view_activity', admin_override=True)
|
||||
def view_job(service_id, job_id):
|
||||
job = job_api_client.get_job(service_id, job_id)['data']
|
||||
template = service_api_client.get_service_template(service_id=service_id,
|
||||
template_id=job['template'],
|
||||
version=job['template_version'])['data']
|
||||
|
||||
job = job_api_client.get_job(service_id, job_id)['data']
|
||||
filter_args = _parse_filter_args(request.args)
|
||||
_set_status_filters(filter_args)
|
||||
notifications = notification_api_client.get_notifications_for_service(
|
||||
service_id, job_id, status=filter_args.get('status'),
|
||||
)
|
||||
finished = job['status'] == 'finished'
|
||||
|
||||
return render_template(
|
||||
'views/jobs/job.html',
|
||||
notifications=notifications['notifications'],
|
||||
job=job,
|
||||
uploaded_at=job['created_at'],
|
||||
finished=job.get('notifications_sent', 0) and ((
|
||||
job.get('notifications_sent', 0) -
|
||||
job.get('notifications_delivered', 0) -
|
||||
@@ -93,17 +84,21 @@ def view_job(service_id, job_id):
|
||||
) == 0),
|
||||
uploaded_file_name=job['original_file_name'],
|
||||
template=Template(
|
||||
template,
|
||||
service_api_client.get_service_template(
|
||||
service_id=service_id,
|
||||
template_id=job['template'],
|
||||
version=job['template_version']
|
||||
)['data'],
|
||||
prefix=current_service['name']
|
||||
),
|
||||
counts=_get_job_counts(job, request.args.get('help', 0)),
|
||||
status=request.args.get('status', ''),
|
||||
updates_url=url_for(
|
||||
".view_job_updates",
|
||||
service_id=service_id,
|
||||
job_id=job['id'],
|
||||
status=request.args.get('status', '')
|
||||
)
|
||||
),
|
||||
partials=get_job_partials(job)
|
||||
)
|
||||
|
||||
|
||||
@@ -144,38 +139,9 @@ def view_job_csv(service_id, job_id):
|
||||
@login_required
|
||||
@user_has_permissions('view_activity', admin_override=True)
|
||||
def view_job_updates(service_id, job_id):
|
||||
job = job_api_client.get_job(service_id, job_id)['data']
|
||||
filter_args = _parse_filter_args(request.args)
|
||||
_set_status_filters(filter_args)
|
||||
notifications = notification_api_client.get_notifications_for_service(
|
||||
service_id, job_id, status=filter_args.get('status')
|
||||
)
|
||||
finished = (
|
||||
job.get('notifications_sent', 0) -
|
||||
job.get('notifications_delivered', 0) -
|
||||
job.get('notifications_failed', 0)
|
||||
) == 0
|
||||
return jsonify(**{
|
||||
'counts': render_template(
|
||||
'partials/jobs/count.html',
|
||||
job=job,
|
||||
finished=finished,
|
||||
counts=_get_job_counts(job, request.args.get('help', 0)),
|
||||
status=request.args.get('status', '')
|
||||
),
|
||||
'notifications': render_template(
|
||||
'partials/jobs/notifications.html',
|
||||
job=job,
|
||||
notifications=notifications['notifications'],
|
||||
finished=finished,
|
||||
status=request.args.get('status', '')
|
||||
),
|
||||
'status': render_template(
|
||||
'partials/jobs/status.html',
|
||||
job=job,
|
||||
finished=finished
|
||||
),
|
||||
})
|
||||
return jsonify(**get_job_partials(
|
||||
job_api_client.get_job(service_id, job_id)['data']
|
||||
))
|
||||
|
||||
|
||||
@main.route('/services/<service_id>/notifications/<message_type>')
|
||||
@@ -325,3 +291,30 @@ def _get_job_counts(job, help_argument):
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
|
||||
def get_job_partials(job):
|
||||
|
||||
filter_args = _parse_filter_args(request.args)
|
||||
_set_status_filters(filter_args)
|
||||
|
||||
return {
|
||||
'counts': render_template(
|
||||
'partials/jobs/count.html',
|
||||
job=job,
|
||||
counts=_get_job_counts(job, request.args.get('help', 0)),
|
||||
status=request.args.get('status', '')
|
||||
),
|
||||
'notifications': render_template(
|
||||
'partials/jobs/notifications.html',
|
||||
job=job,
|
||||
notifications=notification_api_client.get_notifications_for_service(
|
||||
job['service'], job['id'], status=filter_args.get('status')
|
||||
)['notifications'],
|
||||
status=request.args.get('status', '')
|
||||
),
|
||||
'status': render_template(
|
||||
'partials/jobs/status.html',
|
||||
job=job
|
||||
),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user