Optimise the query for getting the platform statistics for all services. The page should render for all time after this change.

This is one step closer to eliminating the need to read from NotificationHistory.
This commit is contained in:
Rebecca Law
2019-01-04 16:45:39 +00:00
parent 39963d9784
commit bd9a6352fd
4 changed files with 63 additions and 31 deletions

View File

@@ -199,7 +199,7 @@ def fetch_notification_statuses_for_job(job_id):
).all()
def fetch_stats_for_all_services_by_date_range(start_date, end_date,include_from_test_key=True):
def fetch_stats_for_all_services_by_date_range(start_date, end_date, include_from_test_key=True):
stats = db.session.query(
FactNotificationStatus.service_id.label('service_id'),
Service.name.label('name'),
@@ -230,8 +230,8 @@ def fetch_stats_for_all_services_by_date_range(start_date, end_date,include_from
if not include_from_test_key:
stats = stats.filter(FactNotificationStatus.key_type != KEY_TYPE_TEST)
today = get_london_midnight_in_utc(datetime.utcnow())
if start_date <= today.date() <= end_date:
if start_date <= datetime.utcnow().date() <= end_date:
today = get_london_midnight_in_utc(datetime.utcnow())
subquery = db.session.query(
Notification.notification_type.cast(db.Text).label('notification_type'),
Notification.status.label('status'),
@@ -284,7 +284,9 @@ def fetch_stats_for_all_services_by_date_range(start_date, end_date,include_from
all_stats_table.c.notification_type,
all_stats_table.c.status,
).order_by(
all_stats_table.c.service_id
all_stats_table.c.name,
all_stats_table.c.notification_type,
all_stats_table.c.status
)
else:
query = stats

View File

@@ -23,8 +23,8 @@ from app.dao.api_key_dao import (
from app.dao.fact_notification_status_dao import (
fetch_notification_status_for_service_by_month,
fetch_notification_status_for_service_for_day,
fetch_notification_status_for_service_for_today_and_7_previous_days
)
fetch_notification_status_for_service_for_today_and_7_previous_days,
fetch_stats_for_all_services_by_date_range)
from app.dao.inbound_numbers_dao import dao_allocate_number_for_service
from app.dao.organisation_dao import dao_get_organisation_by_service_id
from app.dao.service_data_retention_dao import (
@@ -56,7 +56,6 @@ from app.dao.services_dao import (
dao_remove_user_from_service,
dao_suspend_service,
dao_update_service,
fetch_stats_by_date_range_for_all_services
)
from app.dao.service_whitelist_dao import (
dao_fetch_service_whitelist,
@@ -472,10 +471,10 @@ def get_detailed_services(start_date, end_date, only_active=False, include_from_
only_active=only_active)
else:
stats = fetch_stats_by_date_range_for_all_services(start_date=start_date,
stats = fetch_stats_for_all_services_by_date_range(start_date=start_date,
end_date=end_date,
include_from_test_key=include_from_test_key,
only_active=only_active)
)
results = []
for service_id, rows in itertools.groupby(stats, lambda x: x.service_id):
rows = list(rows)