Skip to content

Commit

Permalink
Fixed up loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite committed Oct 9, 2023
1 parent b190423 commit e1aa5ef
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 32 deletions.
4 changes: 2 additions & 2 deletions frontend/src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1221,8 +1221,8 @@ const api = {
},

recordings: {
async list(params: string): Promise<SessionRecordingsResponse> {
return await new ApiRequest().recordings().withQueryString(params).get()
async list(params: Record<string, any>): Promise<SessionRecordingsResponse> {
return await new ApiRequest().recordings().withQueryString(toParams(params)).get()
},
async getMatchingEvents(params: string): Promise<{ results: string[] }> {
return await new ApiRequest().recordingMatchingEvents().withQueryString(params).get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,9 @@ export function PlayerMetaLinks(): JSX.Element {

{logicProps.setPinned ? (
<LemonButton
onClick={() => {
logicProps.setPinned?.(!logicProps.pinned)
}}
onClick={() => logicProps.setPinned?.(!logicProps.pinned)}
size="small"
tooltip={logicProps.pinned ? 'Unpin from this list' : 'Pin to this list'}
icon={logicProps.pinned ? <IconPinFilled /> : <IconPinOutline />}
/>
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ describe('sessionRecordingPlayerLogic', () => {
silenceKeaLoadersErrors()
const listLogic = sessionRecordingsPlaylistLogic({ updateSearchParams: true })
listLogic.mount()
logic = sessionRecordingPlayerLogic({ sessionRecordingId: '3', playerKey: 'test' })
logic = sessionRecordingPlayerLogic({
sessionRecordingId: '3',
playerKey: 'test',
playlistLogic: listLogic,
})
logic.mount()
jest.spyOn(api, 'delete')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { actions, afterMount, connect, kea, key, listeners, path, props, propsChanged, reducers, selectors } from 'kea'
import api from 'lib/api'
import { objectClean, objectsEqual, toParams } from 'lib/utils'
import { objectClean, objectsEqual } from 'lib/utils'
import {
AnyPropertyFilter,
PropertyFilterType,
Expand Down Expand Up @@ -241,23 +241,20 @@ export const sessionRecordingsPlaylistLogic = kea<sessionRecordingsPlaylistLogic
} as SessionRecordingsResponse,
{
loadSessionRecordings: async ({ direction }, breakpoint) => {
const paramsDict = {
const params = {
...values.filters,
person_uuid: props.personUUID ?? '',
limit: RECORDINGS_LIMIT,
}

if (direction === 'older') {
paramsDict['date_to'] =
values.sessionRecordings[values.sessionRecordings.length - 1]?.start_time
params['date_to'] = values.sessionRecordings[values.sessionRecordings.length - 1]?.start_time
}

if (direction === 'newer') {
paramsDict['date_from'] = values.sessionRecordings[0]?.start_time
params['date_from'] = values.sessionRecordings[0]?.start_time
}

const params = toParams(paramsDict)

await breakpoint(100) // Debounce for lots of quick filter changes

const startTime = performance.now()
Expand Down Expand Up @@ -288,20 +285,15 @@ export const sessionRecordingsPlaylistLogic = kea<sessionRecordingsPlaylistLogic
// props.pinnedRecordings can be strings or objects.
// If objects we can simply use them, if strings we need to fetch them

let recordings = props.pinnedRecordings?.filter(
(x) => typeof x !== 'string'
) as SessionRecordingType[]
const recordingIds = props.pinnedRecordings?.filter((x) => typeof x === 'string') as string[]

if (recordingIds) {
// TODO: This is broken - we don't return only certain session_ids for some reason....
const fetchedRecordings = await api.recordings.list(
toParams({
filters: {
session_ids: recordingIds,
},
})
)
const pinnedRecordings = props.pinnedRecordings ?? []

let recordings = pinnedRecordings.filter((x) => typeof x !== 'string') as SessionRecordingType[]
const recordingIds = pinnedRecordings.filter((x) => typeof x === 'string') as string[]

if (recordingIds.length) {
const fetchedRecordings = await api.recordings.list({
session_ids: recordingIds,
})

recordings = [...recordings, ...fetchedRecordings.results]
}
Expand Down
17 changes: 14 additions & 3 deletions posthog/models/filters/mixins/session_recordings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,23 @@ def recording_duration_filter(self) -> Optional[Property]:

@cached_property
def session_ids(self) -> Optional[List[str]]:
# Can be ['a', 'b'] or "['a', 'b']" or "a,b"
session_ids_str = self._data.get(SESSION_RECORDINGS_FILTER_IDS, None)

if session_ids_str is None:
return None

recordings_ids = json.loads(session_ids_str)
if isinstance(recordings_ids, list) and all(isinstance(recording_id, str) for recording_id in recordings_ids):
if isinstance(session_ids_str, list):
recordings_ids = session_ids_str
elif isinstance(session_ids_str, str):
if session_ids_str.startswith("["):
recordings_ids = json.loads(session_ids_str)
else:
recordings_ids = session_ids_str.split(",")

if all(isinstance(recording_id, str) for recording_id in recordings_ids):
# Sort for stable queries
return sorted(recordings_ids)
return None

# If the property is at all present, we assume that the user wants to filter by it
return []
4 changes: 3 additions & 1 deletion posthog/session_recordings/session_recording_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class SessionRecordingViewSet(StructuredViewSetMixin, viewsets.GenericViewSet):
permission_classes = [IsAuthenticated, ProjectMembershipNecessaryPermissions, TeamMemberAccessPermission]
throttle_classes = [ClickHouseBurstRateThrottle, ClickHouseSustainedRateThrottle]
serializer_class = SessionRecordingSerializer
# We don't use this
queryset = SessionRecording.objects.none()

sharing_enabled_actions = ["retrieve", "snapshots", "snapshot_file"]

Expand Down Expand Up @@ -522,7 +524,7 @@ def list_recordings(filter: SessionRecordingsFilter, request: request.Request, c
recordings = recordings + list(persisted_recordings)

remaining_session_ids = list(set(all_session_ids) - {x.session_id for x in persisted_recordings})
filter = filter.shallow_clone({SESSION_RECORDINGS_FILTER_IDS: json.dumps(remaining_session_ids)})
filter = filter.shallow_clone({SESSION_RECORDINGS_FILTER_IDS: remaining_session_ids})

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
Expand Down
1 change: 0 additions & 1 deletion posthog/tasks/usage_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ def get_teams_with_survey_responses_count_in_period(
begin: datetime,
end: datetime,
) -> List[Tuple[int, int]]:

results = sync_execute(
"""
SELECT team_id, COUNT() as count
Expand Down

0 comments on commit e1aa5ef

Please sign in to comment.