analytics: Specify exact end_time in realm summary query.

Fetchings rows with end_time within the last 25 hours would result
in the realmcount queries returning two rows for each realm
if the analytics page was opened within an hour since the
count stats were updated.
This commit is contained in:
Vishnu KS 2020-12-22 23:10:30 +05:30 committed by Tim Abbott
parent 9d5a1271d4
commit 9fe39646fa
2 changed files with 12 additions and 7 deletions

View File

@ -559,7 +559,7 @@ def realm_summary_table(realm_minutes: Dict[str, float]) -> str:
analytics_realmcount
WHERE
property = 'realm_active_humans::day'
AND end_time > now() - interval '25 hours'
AND end_time = %(realm_active_humans_end_time)s
) as _14day_active_humans_table ON realm.id = _14day_active_humans_table.realm_id
LEFT OUTER JOIN (
SELECT
@ -569,7 +569,7 @@ def realm_summary_table(realm_minutes: Dict[str, float]) -> str:
analytics_realmcount
WHERE
property = '7day_actives::day'
AND end_time > now() - interval '25 hours'
AND end_time = %(seven_day_actives_end_time)s
) as wau_table ON realm.id = wau_table.realm_id
LEFT OUTER JOIN (
SELECT
@ -579,7 +579,7 @@ def realm_summary_table(realm_minutes: Dict[str, float]) -> str:
analytics_realmcount
WHERE
property = '1day_actives::day'
AND end_time > now() - interval '25 hours'
AND end_time = %(one_day_actives_end_time)s
) as dau_table ON realm.id = dau_table.realm_id
LEFT OUTER JOIN (
SELECT
@ -590,7 +590,7 @@ def realm_summary_table(realm_minutes: Dict[str, float]) -> str:
WHERE
property = 'active_users_audit:is_bot:day'
AND subgroup = 'false'
AND end_time > now() - interval '25 hours'
AND end_time = %(active_users_audit_end_time)s
) as user_count_table ON realm.id = user_count_table.realm_id
LEFT OUTER JOIN (
SELECT
@ -601,7 +601,7 @@ def realm_summary_table(realm_minutes: Dict[str, float]) -> str:
WHERE
property = 'active_users_audit:is_bot:day'
AND subgroup = 'true'
AND end_time > now() - interval '25 hours'
AND end_time = %(active_users_audit_end_time)s
) as bot_count_table ON realm.id = bot_count_table.realm_id
WHERE
_14day_active_humans IS NOT NULL
@ -612,7 +612,12 @@ def realm_summary_table(realm_minutes: Dict[str, float]) -> str:
''')
cursor = connection.cursor()
cursor.execute(query)
cursor.execute(query, {
'realm_active_humans_end_time': COUNT_STATS['realm_active_humans::day'].last_successful_fill(),
'seven_day_actives_end_time': COUNT_STATS['7day_actives::day'].last_successful_fill(),
'one_day_actives_end_time': COUNT_STATS['1day_actives::day'].last_successful_fill(),
'active_users_audit_end_time': COUNT_STATS['active_users_audit:is_bot:day'].last_successful_fill(),
})
rows = dictfetchall(cursor)
cursor.close()

View File

@ -52,7 +52,7 @@ class ActivityTest(ZulipTestCase):
result = self.client_get('/activity')
self.assertEqual(result.status_code, 200)
self.assert_length(queries, 14)
self.assert_length(queries, 18)
flush_per_request_caches()
with queries_captured() as queries: