Skip to content

Commit

Permalink
fix: incorrect date range selection
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Dec 4, 2024
1 parent f2a09b9 commit f6cdb1c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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()

Check failure on line 802 in posthog/session_recordings/queries/test/test_session_recording_list_from_filters.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Missing positional argument "recordings_filter" in call to "_filter_recordings_by" of "TestSessionRecordingsListFromFilters"

# 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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f6cdb1c

Please sign in to comment.