From f8f741c4a2dd3b178a545221725ff3888ab2a3ed Mon Sep 17 00:00:00 2001 From: Alya Abbott Date: Thu, 26 Oct 2023 11:27:51 -0700 Subject: [PATCH] analytics: Replace get_realm_day_counts with a faster query. Use analytics_realmcount to improve query runtime. There are two known difference with previous query: - Messages that are deleted after hourly stats are aggregated are not decremented in new query. - Messages sent since the hourly aggregation last ran are not counted. --- analytics/views/installation_activity.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/analytics/views/installation_activity.py b/analytics/views/installation_activity.py index 289d559db6..a6245f37ff 100644 --- a/analytics/views/installation_activity.py +++ b/analytics/views/installation_activity.py @@ -37,29 +37,26 @@ if settings.BILLING_ENABLED: def get_realm_day_counts() -> Dict[str, Dict[str, Markup]]: - # Uses index: zerver_message_date_sent_3b5b05d8 + # To align with UTC days, we subtract an hour from end_time to + # get the start_time, since the hour that starts at midnight was + # on the previous day. query = SQL( """ select r.string_id, - (now()::date - date_sent::date) age, - count(*) cnt - from zerver_message m - join zerver_userprofile up on up.id = m.sender_id - join zerver_realm r on r.id = up.realm_id - join zerver_client c on c.id = m.sending_client_id + (now()::date - (end_time - interval '1 hour')::date) age, + coalesce(sum(value), 0) cnt + from zerver_realm r + join analytics_realmcount rc on r.id = rc.realm_id where - (not up.is_bot) + property = 'messages_sent:is_bot:hour' and - date_sent > now()::date - interval '8 day' + subgroup = 'false' and - c.name not in ('zephyr_mirror', 'ZulipMonitoring') + end_time > now()::date - interval '8 day' - interval '1 hour' group by r.string_id, age - order by - r.string_id, - age """ ) cursor = connection.cursor()