2018-06-28 08:39:25 +01:00
|
|
|
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
|
2018-06-29 15:54:32 +01:00
|
|
|
from app.platform_stats.platform_stats_schema import platform_stats_request
|
2018-06-28 08:39:25 +01:00
|
|
|
from app.service.statistics import format_admin_stats
|
2018-06-29 15:54:32 +01:00
|
|
|
from app.schema_validation import validate
|
2018-06-28 08:39:25 +01:00
|
|
|
|
|
|
|
|
platform_stats_blueprint = Blueprint('platform_stats', __name__)
|
|
|
|
|
|
|
|
|
|
register_errors(platform_stats_blueprint)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@platform_stats_blueprint.route('')
|
2018-06-29 15:54:32 +01:00
|
|
|
def get_platform_stats():
|
|
|
|
|
if request.args:
|
|
|
|
|
validate(request.args, platform_stats_request)
|
|
|
|
|
|
2018-06-28 08:39:25 +01:00
|
|
|
# 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)
|