Skip to content

Conversation

timgl
Copy link
Collaborator

@timgl timgl commented Oct 9, 2020

Changes

Please describe.
If this affects the front-end, include screenshots.

Checklist

  • All querysets/queries filter by Team (if this PR affects any querysets/queries)
  • Backend tests (if this PR affects the backend)
  • Cypress E2E tests (if this PR affects the front and/or backend)

@timgl timgl temporarily deployed to posthog-add-property-fi-9wc4fh October 9, 2020 22:35 Inactive
if (
!values.selectedDate ||
values.selectedDate.format('YYYY-MM-DD') !== newDate.format('YYYY-MM-DD') ||
JSON.stringify(properties || {}) !== JSON.stringify(values.filters)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There already exists an objectsEqual method in frontend/src/lib/utils.js, perhaps we should use that?

if (
!values.selectedDate ||
values.selectedDate.format('YYYY-MM-DD') !== newDate.format('YYYY-MM-DD') ||
JSON.stringify(properties || {}) !== JSON.stringify(values.filters)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: JSON.stringfy(x) is dependent on key order, this means:

JSON.stringify({ a: 1, b: 2 }) !== JSON.stringify({ b: 2, a: 1 })

Is that relevant in this case?

previousDay: true,
nextDay: true,
setFilters: (filters: Record<string, any>, selectedDate: Moment | null) => ({ filters, selectedDate }),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage of any is generally discouraged within ts, but I guess to type this properly we'd need to port PropertyFilters.js and some other code as well as well?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would definitely be cool to have a completely typed filter! Indeed lots of "legacy" code to upgrade to make this work well. Until then any is probably fine here. There's also unknown, but that's a small difference.

actions.setNextOffset(null)
actions.loadSessions(true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the action order change significant here?

@mariusandra
Copy link
Collaborator

@timgl we worked together on this at the offsite to try to avoid the situation where the request in loadSessions is called twice or even three times due to various things updating the URL, state, etc.

I now came upon an extremely simple solution that would solve all problems without any of the hacks we implemented:

export const sessionsTableLogic = kea<sessionsTableLogicType<Moment, SessionType>>({
    loaders: ({ actions, values, props }) => ({
        sessions: {
            __default: [] as SessionType[],
            loadSessions: async (_: any, breakpoint) => {
                const { selectedDateURLparam } = values
                const params = toParams({
                    date_from: selectedDateURLparam,
                    date_to: selectedDateURLparam,
                    offset: 0,
                    distinct_id: props.personIds ? props.personIds[0] : '',
                    properties: values.filters,
                })
                // add this!
                await breakpoint(10)

                const response = await api.get(`api/insight/session/?${params}`)
                breakpoint()
                if (response.offset) {
                    actions.setNextOffset(response.offset)
                }
                return response.result
            },
        },
    }) 
)}

Adding a simple breakpoint before the api.get debounces the query and if another loadSesssions is called within 10ms since the first one, no call is made. This is a simple and cool debouncing pattern that's super easy to implement with kea loaders. The number could even be smaller, since all the actions fire right after each other.

Not saying it's needed to redo this PR, but something to definitely keep in mind for the future :).

@timgl
Copy link
Collaborator Author

timgl commented Oct 15, 2020

@macobo @mariusandra Thanks both. Marius, I've implemented your suggestion which made things a lot easier. I'm now also using PropertyFilter's built in urlToAction which means back and forward history navigation works too!

@mariusandra
Copy link
Collaborator

Great! One comment (nit, feel free to ignore) coming back to what Karl said. It might be a good idea to replace all those filter types Record<string, any>s with some PropertyFilterType from src/types, even if for now it's just:

export type PropertyFilterType = Record<string, any>

It can be converted into a real type some time later. This will however already help future refactors.

@timgl
Copy link
Collaborator Author

timgl commented Oct 15, 2020

Fixed! Also cleaned up filters/properties confusion

Copy link
Collaborator

@mariusandra mariusandra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@timgl timgl merged commit 8d84eda into master Oct 15, 2020
@timgl timgl deleted the add-property-filters-to-sessions branch October 15, 2020 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants