From f6cdb1cabbd5b2541d3536aa288c1a2fcadcf115 Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Wed, 4 Dec 2024 15:37:12 +0000 Subject: [PATCH] fix: incorrect date range selection --- .../session_recording_list_from_query.py | 10 ++-- ...est_session_recording_list_from_query.ambr | 35 ++++++++++++++ ...est_session_recording_list_from_filters.py | 24 +++++++++- .../test_session_recording_list_from_query.py | 20 ++++++++ .../test_session_recordings.ambr | 48 +++++++++---------- 5 files changed, 107 insertions(+), 30 deletions(-) diff --git a/posthog/session_recordings/queries/session_recording_list_from_query.py b/posthog/session_recordings/queries/session_recording_list_from_query.py index ae047e0191e0b..fe08fb85590f2 100644 --- a/posthog/session_recordings/queries/session_recording_list_from_query.py +++ b/posthog/session_recordings/queries/session_recording_list_from_query.py @@ -276,21 +276,23 @@ def _where_predicates(self) -> Union[ast.And, ast.Or]: ) ) - if self._query.date_from: + query_date_from = self.query_date_range.date_from() + if query_date_from: exprs.append( ast.CompareOperation( op=ast.CompareOperationOp.GtEq, left=ast.Field(chain=["s", "min_first_timestamp"]), - right=ast.Constant(value=self.query_date_range.date_from()), + right=ast.Constant(value=query_date_from), ) ) - if self._query.date_to: + query_date_to = self.query_date_range.date_to() + if query_date_to: exprs.append( ast.CompareOperation( op=ast.CompareOperationOp.LtEq, left=ast.Field(chain=["s", "min_first_timestamp"]), - right=ast.Constant(value=self.query_date_range.date_to()), + right=ast.Constant(value=query_date_to), ) ) diff --git a/posthog/session_recordings/queries/test/__snapshots__/test_session_recording_list_from_query.ambr b/posthog/session_recordings/queries/test/__snapshots__/test_session_recording_list_from_query.ambr index c74794ef87e62..c6cd370a4921f 100644 --- a/posthog/session_recordings/queries/test/__snapshots__/test_session_recording_list_from_query.ambr +++ b/posthog/session_recordings/queries/test/__snapshots__/test_session_recording_list_from_query.ambr @@ -3832,6 +3832,41 @@ allow_experimental_analyzer=0 ''' # --- +# name: TestSessionRecordingsListFromQuery.test_listing_ignores_future_replays + ''' + SELECT s.session_id AS session_id, + any(s.team_id), + any(s.distinct_id), + min(toTimeZone(s.min_first_timestamp, 'UTC')) AS start_time, + max(toTimeZone(s.max_last_timestamp, 'UTC')) AS end_time, + dateDiff('SECOND', start_time, end_time) AS duration, + argMinMerge(s.first_url) AS first_url, + sum(s.click_count) AS click_count, + sum(s.keypress_count) AS keypress_count, + sum(s.mouse_activity_count) AS mouse_activity_count, + divide(sum(s.active_milliseconds), 1000) AS active_seconds, + minus(duration, active_seconds) AS inactive_seconds, + sum(s.console_log_count) AS console_log_count, + sum(s.console_warn_count) AS console_warn_count, + sum(s.console_error_count) AS console_error_count, + ifNull(greaterOrEquals(max(toTimeZone(s._timestamp, 'UTC')), toDateTime64('2023-08-30 11:55:01.000000', 6, 'UTC')), 0) AS ongoing, + round(multiply(divide(plus(plus(plus(divide(sum(s.active_milliseconds), 1000), sum(s.click_count)), sum(s.keypress_count)), sum(s.console_error_count)), plus(plus(plus(plus(sum(s.mouse_activity_count), dateDiff('SECOND', start_time, end_time)), sum(s.console_error_count)), sum(s.console_log_count)), sum(s.console_warn_count))), 100), 2) AS activity_score + FROM session_replay_events AS s + WHERE and(equals(s.team_id, 99999), ifNull(greaterOrEquals(toTimeZone(s.min_first_timestamp, 'UTC'), toDateTime64('2023-08-09 12:00:01.000000', 6, 'UTC')), 0), ifNull(greaterOrEquals(toTimeZone(s.min_first_timestamp, 'UTC'), toDateTime64('2023-08-27 00:00:00.000000', 6, 'UTC')), 0), ifNull(lessOrEquals(toTimeZone(s.min_first_timestamp, 'UTC'), toDateTime64('2023-08-30 23:59:59.999999', 6, 'UTC')), 0)) + GROUP BY s.session_id + HAVING 1 + ORDER BY start_time DESC + LIMIT 51 + OFFSET 0 SETTINGS readonly=2, + max_execution_time=60, + allow_experimental_object_type=1, + format_csv_allow_double_quotes=0, + max_ast_elements=4000000, + max_expanded_ast_elements=4000000, + max_bytes_before_external_group_by=0, + allow_experimental_analyzer=0 + ''' +# --- # name: TestSessionRecordingsListFromQuery.test_multiple_event_filters ''' SELECT s.session_id AS session_id, diff --git a/posthog/session_recordings/queries/test/test_session_recording_list_from_filters.py b/posthog/session_recordings/queries/test/test_session_recording_list_from_filters.py index a1b730f149da2..ad6616d82bba7 100644 --- a/posthog/session_recordings/queries/test/test_session_recording_list_from_filters.py +++ b/posthog/session_recordings/queries/test/test_session_recording_list_from_filters.py @@ -81,8 +81,8 @@ def create_event( properties=properties, ) - def _filter_recordings_by(self, recordings_filter: dict) -> SessionRecordingQueryResult: - the_filter = SessionRecordingsFilter(team=self.team, data=recordings_filter) + def _filter_recordings_by(self, recordings_filter: dict | None) -> SessionRecordingQueryResult: + the_filter = SessionRecordingsFilter(team=self.team, data=recordings_filter or {}) session_recording_list_instance = SessionRecordingListFromFilters( filter=the_filter, team=self.team, hogql_query_modifiers=None ) @@ -784,6 +784,26 @@ def test_ttl_days(self): with freeze_time("2023-09-05T12:00:01Z"): assert ttl_days(self.team) == 35 + @snapshot_clickhouse_queries + def test_listing_ignores_future_replays(self): + with freeze_time("2023-08-29T12:00:01Z"): + produce_replay_summary(team_id=self.team.id, session_id="29th Aug") + + with freeze_time("2023-09-01T12:00:01Z"): + produce_replay_summary(team_id=self.team.id, session_id="1st-sep") + + with freeze_time("2023-09-02T12:00:01Z"): + produce_replay_summary(team_id=self.team.id, session_id="2nd-sep") + + with freeze_time("2023-09-03T12:00:01Z"): + produce_replay_summary(team_id=self.team.id, session_id="3rd-sep") + + with freeze_time("2023-08-30T12:00:01Z"): + recordings = self._filter_recordings_by() + + # recordings in the future don't show + assert [s["session_id"] for s in recordings.results] == ["29th Aug"] + @snapshot_clickhouse_queries def test_filter_on_session_ids(self): user = "test_session_ids-user" diff --git a/posthog/session_recordings/queries/test/test_session_recording_list_from_query.py b/posthog/session_recordings/queries/test/test_session_recording_list_from_query.py index 99ef6404f1ad6..abfd8ab98b956 100644 --- a/posthog/session_recordings/queries/test/test_session_recording_list_from_query.py +++ b/posthog/session_recordings/queries/test/test_session_recording_list_from_query.py @@ -773,6 +773,26 @@ def test_ttl_days(self): with freeze_time("2023-09-05T12:00:01Z"): assert ttl_days(self.team) == 35 + @snapshot_clickhouse_queries + def test_listing_ignores_future_replays(self): + with freeze_time("2023-08-29T12:00:01Z"): + produce_replay_summary(team_id=self.team.id, session_id="29th Aug") + + with freeze_time("2023-09-01T12:00:01Z"): + produce_replay_summary(team_id=self.team.id, session_id="1st-sep") + + with freeze_time("2023-09-02T12:00:01Z"): + produce_replay_summary(team_id=self.team.id, session_id="2nd-sep") + + with freeze_time("2023-09-03T12:00:01Z"): + produce_replay_summary(team_id=self.team.id, session_id="3rd-sep") + + with freeze_time("2023-08-30T12:00:01Z"): + recordings = self._filter_recordings_by() + + # recordings in the future don't show + assert [s["session_id"] for s in recordings.results] == ["29th Aug"] + @snapshot_clickhouse_queries def test_filter_on_session_ids(self): user = "test_session_ids-user" diff --git a/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr b/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr index c893026d31baf..270298f17b72d 100644 --- a/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr +++ b/posthog/session_recordings/test/__snapshots__/test_session_recordings.ambr @@ -640,12 +640,12 @@ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id") WHERE (("ee_accesscontrol"."organization_member_id" IS NULL AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '444' + AND "ee_accesscontrol"."resource_id" = '193' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("posthog_organizationmembership"."user_id" = 99999 AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '444' + AND "ee_accesscontrol"."resource_id" = '193' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("ee_accesscontrol"."organization_member_id" IS NULL @@ -1690,12 +1690,12 @@ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id") WHERE (("ee_accesscontrol"."organization_member_id" IS NULL AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("posthog_organizationmembership"."user_id" = 99999 AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("ee_accesscontrol"."organization_member_id" IS NULL @@ -2445,12 +2445,12 @@ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id") WHERE (("ee_accesscontrol"."organization_member_id" IS NULL AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("posthog_organizationmembership"."user_id" = 99999 AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("ee_accesscontrol"."organization_member_id" IS NULL @@ -3136,12 +3136,12 @@ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id") WHERE (("ee_accesscontrol"."organization_member_id" IS NULL AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("posthog_organizationmembership"."user_id" = 99999 AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("ee_accesscontrol"."organization_member_id" IS NULL @@ -3890,12 +3890,12 @@ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id") WHERE (("ee_accesscontrol"."organization_member_id" IS NULL AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("posthog_organizationmembership"."user_id" = 99999 AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("ee_accesscontrol"."organization_member_id" IS NULL @@ -4608,12 +4608,12 @@ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id") WHERE (("ee_accesscontrol"."organization_member_id" IS NULL AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("posthog_organizationmembership"."user_id" = 99999 AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("ee_accesscontrol"."organization_member_id" IS NULL @@ -5408,12 +5408,12 @@ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id") WHERE (("ee_accesscontrol"."organization_member_id" IS NULL AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("posthog_organizationmembership"."user_id" = 99999 AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("ee_accesscontrol"."organization_member_id" IS NULL @@ -5673,12 +5673,12 @@ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id") WHERE (("ee_accesscontrol"."organization_member_id" IS NULL AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("posthog_organizationmembership"."user_id" = 99999 AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("ee_accesscontrol"."organization_member_id" IS NULL @@ -6107,12 +6107,12 @@ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id") WHERE (("ee_accesscontrol"."organization_member_id" IS NULL AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("posthog_organizationmembership"."user_id" = 99999 AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("ee_accesscontrol"."organization_member_id" IS NULL @@ -6573,12 +6573,12 @@ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id") WHERE (("ee_accesscontrol"."organization_member_id" IS NULL AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("posthog_organizationmembership"."user_id" = 99999 AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("ee_accesscontrol"."organization_member_id" IS NULL @@ -7267,12 +7267,12 @@ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id") WHERE (("ee_accesscontrol"."organization_member_id" IS NULL AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("posthog_organizationmembership"."user_id" = 99999 AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("ee_accesscontrol"."organization_member_id" IS NULL @@ -8018,12 +8018,12 @@ LEFT OUTER JOIN "posthog_organizationmembership" ON ("ee_accesscontrol"."organization_member_id" = "posthog_organizationmembership"."id") WHERE (("ee_accesscontrol"."organization_member_id" IS NULL AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("posthog_organizationmembership"."user_id" = 99999 AND "ee_accesscontrol"."resource" = 'project' - AND "ee_accesscontrol"."resource_id" = '451' + AND "ee_accesscontrol"."resource_id" = '200' AND "ee_accesscontrol"."role_id" IS NULL AND "ee_accesscontrol"."team_id" = 99999) OR ("ee_accesscontrol"."organization_member_id" IS NULL