The /platform-admin takes a long time, probably because the marshmallow schema used joins to the service table to return all the service data and is inefficient.

The query itself has not been improved much at all but by not using a marshmallow schema I hope to get the performance gain I am looking for.
This commit is contained in:
Rebecca Law
2017-10-23 10:58:06 +01:00
parent f6cdfdb640
commit bfb8528ea9
3 changed files with 75 additions and 2 deletions

View File

@@ -412,6 +412,35 @@ def fetch_stats_by_date_range_for_all_services(start_date, end_date, include_fro
return query.all()
@statsd(namespace='dao')
def fetch_aggregate_stats_by_date_range_for_all_services(start_date, end_date, include_from_test_key=True):
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,
func.count(table.id).label('count')
).filter(
table.created_at >= start_date,
table.created_at < end_date
).group_by(
table.notification_type,
table.status
).order_by(
table.notification_type
)
if not include_from_test_key:
query = query.filter(table.key_type != KEY_TYPE_TEST)
return query.all()
@transactional
@version_class(Service)
@version_class(ApiKey)

View File

@@ -46,8 +46,8 @@ from app.dao.services_dao import (
dao_suspend_service,
dao_resume_service,
dao_fetch_monthly_historical_stats_for_service,
dao_fetch_monthly_historical_stats_by_template_for_service
)
dao_fetch_monthly_historical_stats_by_template_for_service,
fetch_aggregate_stats_by_date_range_for_all_services)
from app.dao.service_whitelist_dao import (
dao_fetch_service_whitelist,
dao_add_and_commit_whitelisted_contacts,
@@ -103,6 +103,23 @@ service_blueprint = Blueprint('service', __name__)
register_errors(service_blueprint)
@service_blueprint.route('/platform-stats', methods=['GET'])
def get_platform_stats():
include_from_test_key = request.args.get('include_from_test_key', 'True') != 'False'
# 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,
include_from_test_key=include_from_test_key
)
result = jsonify(data)
return result
@service_blueprint.route('', methods=['GET'])
def get_services():
only_active = request.args.get('only_active') == 'True'