Add in new endpoint with data for platform admin stats

Added in a new endpoint and DAO function to provide the data for the new
platform admin statistics page. The DAO method gets different data from
the Notifications / NotificationHistory table and also groups it differently.

The old endpoint has not been deleted yet to allow the numbers on the
old and new pages to be compared.
This commit is contained in:
Katie Smith
2018-06-20 14:04:38 +01:00
parent fa4ad93d40
commit 7e6076a41f
6 changed files with 226 additions and 8 deletions

View File

@@ -453,6 +453,34 @@ def fetch_aggregate_stats_by_date_range_for_all_services(start_date, end_date, i
return query.all()
@statsd(namespace='dao')
def fetch_new_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 start_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()
@transactional
@version_class(Service)
@version_class(ApiKey)

View File

@@ -44,6 +44,7 @@ from app.dao.services_dao import (
dao_suspend_service,
dao_update_service,
fetch_aggregate_stats_by_date_range_for_all_services,
fetch_new_aggregate_stats_by_date_range_for_all_services,
fetch_stats_by_date_range_for_all_services
)
from app.dao.service_whitelist_dao import (
@@ -131,6 +132,19 @@ def get_platform_stats():
return result
@service_blueprint.route('/platform-stats-new', methods=['GET'])
def get_new_platform_stats():
# 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_new_aggregate_stats_by_date_range_for_all_services(start_date=start_date, end_date=end_date)
stats = statistics.format_admin_stats(data)
return jsonify(stats)
@service_blueprint.route('', methods=['GET'])
def get_services():
only_active = request.args.get('only_active') == 'True'

View File

@@ -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'): {}