mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-31 06:52:06 -05:00
Merge pull request #1919 from alphagov/new-platform-stats-endpoint
New endpoint for the updated platform admin stats page
This commit is contained in:
@@ -113,6 +113,7 @@ def register_blueprint(application):
|
||||
from app.organisation.rest import organisation_blueprint
|
||||
from app.organisation.invite_rest import organisation_invite_blueprint
|
||||
from app.complaint.complaint_rest import complaint_blueprint
|
||||
from app.platform_stats.rest import platform_stats_blueprint
|
||||
|
||||
service_blueprint.before_request(requires_admin_auth)
|
||||
application.register_blueprint(service_blueprint, url_prefix='/service')
|
||||
@@ -192,6 +193,9 @@ def register_blueprint(application):
|
||||
complaint_blueprint.before_request(requires_admin_auth)
|
||||
application.register_blueprint(complaint_blueprint)
|
||||
|
||||
platform_stats_blueprint.before_request(requires_admin_auth)
|
||||
application.register_blueprint(platform_stats_blueprint, url_prefix='/platform-stats')
|
||||
|
||||
|
||||
def register_v2_blueprints(application):
|
||||
from app.v2.inbound_sms.get_inbound_sms import v2_inbound_sms_blueprint as get_inbound_sms
|
||||
|
||||
@@ -609,3 +609,31 @@ def guess_notification_type(search_term):
|
||||
return EMAIL_TYPE
|
||||
else:
|
||||
return SMS_TYPE
|
||||
|
||||
|
||||
@statsd(namespace='dao')
|
||||
def fetch_aggregate_stats_by_date_range_for_all_services(start_date, end_date):
|
||||
start_date = get_london_midnight_in_utc(start_date)
|
||||
end_date = get_london_midnight_in_utc(end_date + timedelta(days=1))
|
||||
table = NotificationHistory
|
||||
|
||||
if end_date >= datetime.utcnow() - timedelta(days=7):
|
||||
table = Notification
|
||||
|
||||
query = db.session.query(
|
||||
table.notification_type,
|
||||
table.status,
|
||||
table.key_type,
|
||||
func.count(table.id).label('count')
|
||||
).filter(
|
||||
table.created_at >= start_date,
|
||||
table.created_at < end_date
|
||||
).group_by(
|
||||
table.notification_type,
|
||||
table.key_type,
|
||||
table.status
|
||||
).order_by(
|
||||
table.notification_type,
|
||||
)
|
||||
|
||||
return query.all()
|
||||
|
||||
0
app/platform_stats/__init__.py
Normal file
0
app/platform_stats/__init__.py
Normal file
10
app/platform_stats/platform_stats_schema.py
Normal file
10
app/platform_stats/platform_stats_schema.py
Normal file
@@ -0,0 +1,10 @@
|
||||
platform_stats_request = {
|
||||
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||
"description": "platform stats request schema",
|
||||
"type": "object",
|
||||
"title": "Platform stats request",
|
||||
"properties": {
|
||||
"start_date": {"type": ["string", "null"], "format": "date"},
|
||||
"end_date": {"type": ["string", "null"], "format": "date"},
|
||||
}
|
||||
}
|
||||
29
app/platform_stats/rest.py
Normal file
29
app/platform_stats/rest.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from datetime import datetime
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
from app.dao.notifications_dao import fetch_aggregate_stats_by_date_range_for_all_services
|
||||
from app.errors import register_errors
|
||||
from app.platform_stats.platform_stats_schema import platform_stats_request
|
||||
from app.service.statistics import format_admin_stats
|
||||
from app.schema_validation import validate
|
||||
|
||||
platform_stats_blueprint = Blueprint('platform_stats', __name__)
|
||||
|
||||
register_errors(platform_stats_blueprint)
|
||||
|
||||
|
||||
@platform_stats_blueprint.route('')
|
||||
def get_platform_stats():
|
||||
if request.args:
|
||||
validate(request.args, platform_stats_request)
|
||||
|
||||
# If start and end date are not set, we are expecting today's stats.
|
||||
today = str(datetime.utcnow().date())
|
||||
|
||||
start_date = datetime.strptime(request.args.get('start_date', today), '%Y-%m-%d').date()
|
||||
end_date = datetime.strptime(request.args.get('end_date', today), '%Y-%m-%d').date()
|
||||
data = fetch_aggregate_stats_by_date_range_for_all_services(start_date=start_date, end_date=end_date)
|
||||
stats = format_admin_stats(data)
|
||||
|
||||
return jsonify(stats)
|
||||
@@ -14,6 +14,37 @@ def format_statistics(statistics):
|
||||
return counts
|
||||
|
||||
|
||||
def format_admin_stats(statistics):
|
||||
counts = create_stats_dict()
|
||||
|
||||
for row in statistics:
|
||||
if row.key_type == 'test':
|
||||
counts[row.notification_type]['test-key'] += row.count
|
||||
else:
|
||||
counts[row.notification_type]['total'] += row.count
|
||||
if row.status in ('technical-failure', 'permanent-failure', 'temporary-failure', 'virus-scan-failed'):
|
||||
counts[row.notification_type]['failures'][row.status] += row.count
|
||||
|
||||
return counts
|
||||
|
||||
|
||||
def create_stats_dict():
|
||||
stats_dict = {}
|
||||
for template in TEMPLATE_TYPES:
|
||||
stats_dict[template] = {}
|
||||
|
||||
for status in ('total', 'test-key'):
|
||||
stats_dict[template][status] = 0
|
||||
|
||||
stats_dict[template]['failures'] = {
|
||||
'technical-failure': 0,
|
||||
'permanent-failure': 0,
|
||||
'temporary-failure': 0,
|
||||
'virus-scan-failed': 0,
|
||||
}
|
||||
return stats_dict
|
||||
|
||||
|
||||
def format_monthly_template_notification_stats(year, rows):
|
||||
stats = {
|
||||
datetime.strftime(date, '%Y-%m'): {}
|
||||
|
||||
Reference in New Issue
Block a user