Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(recordings): Optimize recordings list query #14458

Merged
merged 40 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
695b787
derivative class
EDsCODE Feb 28, 2023
e2add4e
derivative class
EDsCODE Feb 28, 2023
c7eff77
tests
EDsCODE Feb 28, 2023
9253921
Update query snapshots
github-actions[bot] Feb 28, 2023
bce115e
add flag
EDsCODE Feb 28, 2023
5ce263f
typing
EDsCODE Feb 28, 2023
68b86fc
Update query snapshots
github-actions[bot] Feb 28, 2023
eed5f89
Update query snapshots
github-actions[bot] Feb 28, 2023
f5edc76
Update query snapshots
github-actions[bot] Mar 1, 2023
d68ac85
Update query snapshots
github-actions[bot] Mar 1, 2023
034221a
use person on events with new query
EDsCODE Mar 1, 2023
f25e745
Update query snapshots
github-actions[bot] Mar 1, 2023
e9c559a
Update query snapshots
github-actions[bot] Mar 1, 2023
fc831d4
Update query snapshots
github-actions[bot] Mar 1, 2023
10efc7d
Update query snapshots
github-actions[bot] Mar 1, 2023
51893d6
Update query snapshots
github-actions[bot] Mar 1, 2023
a0c966e
Update query snapshots
github-actions[bot] Mar 1, 2023
5b1435d
Update query snapshots
github-actions[bot] Mar 2, 2023
36c0e01
Update query snapshots
github-actions[bot] Mar 2, 2023
da42388
Update query snapshots
github-actions[bot] Mar 2, 2023
24135c2
Update query snapshots
github-actions[bot] Mar 2, 2023
4bd7535
snapshots
EDsCODE Mar 2, 2023
a22cfb1
Update query snapshots
github-actions[bot] Mar 2, 2023
b72a7f1
Update query snapshots
github-actions[bot] Mar 2, 2023
37238f8
merge master
EDsCODE Mar 2, 2023
e679735
Update query snapshots
github-actions[bot] Mar 2, 2023
6f8f06e
merge master
EDsCODE Mar 13, 2023
e9efea6
merge master
EDsCODE Mar 13, 2023
9562ebf
merge master
EDsCODE Mar 13, 2023
16d4cf5
Merge branch 'master' into optimize-recordings-list-query
EDsCODE Mar 14, 2023
fd7a7fd
remove unnecessary joins with poe is active
EDsCODE Mar 14, 2023
6bd28d9
try turning off the test unless poe
EDsCODE Mar 14, 2023
c9d18a2
revert poe changes
EDsCODE Mar 14, 2023
9d746ea
merge master
EDsCODE Mar 21, 2023
3e5dbb4
don't use optimized recording list on hogql queries for now
EDsCODE Mar 21, 2023
695f328
Moved flag to frontend
benjackwhite Mar 22, 2023
4467504
Update query snapshots
github-actions[bot] Mar 22, 2023
5234c22
Update UI snapshots for `chromium` (1)
github-actions[bot] Mar 22, 2023
c52d00b
Update UI snapshots for `webkit` (2)
github-actions[bot] Mar 22, 2023
1d6e97f
Update UI snapshots for `webkit` (2)
github-actions[bot] Mar 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions frontend/src/lib/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export const FEATURE_FLAGS = {
FF_CODE_EXAMPLE: 'ff-code-example', // owner: @liyiy
FEEDBACK_SCENE: 'feedback-scene', // owner: @lharries
DATABASE: 'database', // owner @mariusandra
RECORDINGS_LIST_V2: 'recordings-list-v2-enabled', // owner: #team-session-recordings
}

/** Which self-hosted plan's features are available with Cloud's "Standard" plan (aka card attached). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { actionToUrl, router, urlToAction } from 'kea-router'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import equal from 'fast-deep-equal'
import { loaders } from 'kea-loaders'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { FEATURE_FLAGS } from 'lib/constants'

export type PersonUUID = string
interface Params {
Expand Down Expand Up @@ -122,6 +124,7 @@ export const sessionRecordingsListLogic = kea<sessionRecordingsListLogicType>([
key(generateSessionRecordingListLogicKey),
connect({
actions: [eventUsageLogic, ['reportRecordingsListFetched', 'reportRecordingsListFilterAdded']],
values: [featureFlagLogic, ['featureFlags']],
}),
actions({
setFilters: (filters: Partial<RecordingFilters>) => ({ filters }),
Expand All @@ -146,6 +149,7 @@ export const sessionRecordingsListLogic = kea<sessionRecordingsListLogicType>([
...values.filters,
person_uuid: props.personUUID ?? '',
limit: RECORDINGS_LIMIT,
version: values.featureFlags[FEATURE_FLAGS.RECORDINGS_LIST_V2] ? '2' : '1',
}

const params = toParams(paramsDict)
Expand Down
22 changes: 16 additions & 6 deletions posthog/api/session_recording.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import Any, List, cast
from typing import Any, List, Type, cast

import structlog
from dateutil import parser
Expand All @@ -22,7 +22,7 @@
from posthog.models.session_recording_event import SessionRecordingViewed
from posthog.models.team.team import Team
from posthog.permissions import ProjectMembershipNecessaryPermissions, TeamMemberAccessPermission
from posthog.queries.session_recordings.session_recording_list import SessionRecordingList
from posthog.queries.session_recordings.session_recording_list import SessionRecordingList, SessionRecordingListV2
from posthog.queries.session_recordings.session_recording_properties import SessionRecordingProperties
from posthog.rate_limit import ClickHouseBurstRateThrottle, ClickHouseSustainedRateThrottle
from posthog.utils import format_query_params_absolute_url
Expand Down Expand Up @@ -92,7 +92,9 @@ class SessionRecordingViewSet(StructuredViewSetMixin, viewsets.ViewSet):

def list(self, request: request.Request, *args: Any, **kwargs: Any) -> Response:
filter = SessionRecordingsFilter(request=request)
return Response(list_recordings(filter, request, self.team))
use_v2_list = request.GET.get("version") == "2"

return Response(list_recordings(filter, request, self.team, v2=use_v2_list))

# Returns meta data about the recording
def retrieve(self, request: request.Request, *args: Any, **kwargs: Any) -> Response:
Expand Down Expand Up @@ -206,7 +208,7 @@ def properties(self, request: request.Request, **kwargs):
return Response({"results": session_recording_serializer.data})


def list_recordings(filter: SessionRecordingsFilter, request: request.Request, team: Team) -> dict:
def list_recordings(filter: SessionRecordingsFilter, request: request.Request, team: Team, v2=False) -> dict:
"""
As we can store recordings in S3 or in Clickhouse we need to do a few things here

Expand All @@ -220,6 +222,7 @@ def list_recordings(filter: SessionRecordingsFilter, request: request.Request, t
all_session_ids = filter.session_ids
recordings: List[SessionRecording] = []
more_recordings_available = False
can_use_v2 = v2 and not any(entity.has_hogql_property for entity in filter.entities)

if all_session_ids:
# If we specify the session ids (like from pinned recordings) we can optimise by only going to Postgres
Expand All @@ -238,7 +241,14 @@ def list_recordings(filter: SessionRecordingsFilter, request: request.Request, t

if (all_session_ids and filter.session_ids) or not all_session_ids:
# Only go to clickhouse if we still have remaining specified IDs or we are not specifying IDs
(ch_session_recordings, more_recordings_available) = SessionRecordingList(filter=filter, team=team).run()

# TODO: once person on events is deployed, we can remove the check for hogql properties https://github.com/PostHog/posthog/pull/14458#discussion_r1135780372
session_recording_list_instance: Type[SessionRecordingList] = (
SessionRecordingListV2 if can_use_v2 else SessionRecordingList
)
(ch_session_recordings, more_recordings_available) = session_recording_list_instance(
filter=filter, team=team
).run()
recordings_from_clickhouse = SessionRecording.get_or_build_from_clickhouse(team, ch_session_recordings)
recordings = recordings + recordings_from_clickhouse

Expand Down Expand Up @@ -275,4 +285,4 @@ def list_recordings(filter: SessionRecordingsFilter, request: request.Request, t
session_recording_serializer = SessionRecordingSerializer(recordings, many=True)
results = session_recording_serializer.data

return {"results": results, "has_next": more_recordings_available}
return {"results": results, "has_next": more_recordings_available, "version": 2 if can_use_v2 else 1}
Loading