:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(logs): skip fetching facet values while a facet is collapsed (#81906)
Frank Hamand6dee78ff03ea
diff --git a/products/logs/frontend/components/LogsViewer/FacetRail/RailFacet.tsx b/products/logs/frontend/components/LogsViewer/FacetRail/RailFacet.tsxindex 88ec5e48fcce..cd4d3fb126d9 100644--- a/products/logs/frontend/components/LogsViewer/FacetRail/RailFacet.tsx+++ b/products/logs/frontend/components/LogsViewer/FacetRail/RailFacet.tsx@@ -28,10 +28,9 @@ export interface RailFacetProps {export function RailFacet({ id, facet, hidden }: RailFacetProps): JSX.Element | null {// `facet` comes from the memoized visibleFacets selector, so this identity is stable.const logicProps = useMemo(() => ({ id, facet }), [id, facet])- const { facetValues, facetValuesLoading, facetSearch } = useValues(facetValuesLogic(logicProps))+ const { facetValues, facetValuesLoading, facetSearch, collapsed } = useValues(facetValuesLogic(logicProps))const { setFacetSearch } = useActions(facetValuesLogic(logicProps))const { severityLevels, serviceNames, filterGroup } = useValues(logsViewerFiltersLogic({ id }))- const { collapsedFacets } = useValues(facetRailLogic({ id }))const { toggleFacetValue, toggleFacetCollapsed } = useActions(facetRailLogic({ id }))if (hidden) {@@ -57,7 +56,6 @@ export function RailFacet({ id, facet, hidden }: RailFacetProps): JSX.Element |const fetched: FacetOption[] = facetValues.map((r) => ({ value: r.value, label: r.value, count: r.count }))const onToggle = (value: string): void => toggleFacetValue(source, value)const onToggleCollapsed = (): void => toggleFacetCollapsed(facet.key)- const collapsed = collapsedFacets.includes(facet.key)if (facet.kind === 'fixed') {// Fixed value set from config, counts overlaid. Missing values render as a dimmed 0.diff --git a/products/logs/frontend/components/LogsViewer/FacetRail/facetValuesLogic.test.ts b/products/logs/frontend/components/LogsViewer/FacetRail/facetValuesLogic.test.tsindex e5d07d299ccd..e269a525fc0a 100644--- a/products/logs/frontend/components/LogsViewer/FacetRail/facetValuesLogic.test.ts+++ b/products/logs/frontend/components/LogsViewer/FacetRail/facetValuesLogic.test.ts@@ -5,6 +5,7 @@ import { initKeaTests } from '~/test/init'import { logsAttributesRetrieve, logsFacetValuesCreate } from 'products/logs/frontend/generated/api'import { logsViewerFiltersLogic } from '../Filters/logsViewerFiltersLogic'+import { facetRailLogic } from './facetRailLogic'import { FACETS, FacetConfig } from './facets'import { facetValuesLogic } from './facetValuesLogic'@@ -25,6 +26,7 @@ const SERVICE = facetConfig('service')describe('facetValuesLogic', () => {let filtersLogic: ReturnType<typeof logsViewerFiltersLogic.build>+ let railLogic: ReturnType<typeof facetRailLogic.build>const mounted: ReturnType<typeof facetValuesLogic.build>[] = []const mountFacet = (facet: FacetConfig): ReturnType<typeof facetValuesLogic.build> => {@@ -41,10 +43,13 @@ describe('facetValuesLogic', () => {mockFacetValues.mockResolvedValue({ results: [{ value: 'api', count: 10 }] })filtersLogic = logsViewerFiltersLogic({ id: ID })filtersLogic.mount()+ railLogic = facetRailLogic({ id: ID })+ railLogic.mount()})afterEach(() => {mounted.splice(0).forEach((logic) => logic.unmount())+ railLogic.unmount()filtersLogic.unmount()})@@ -83,4 +88,47 @@ describe('facetValuesLogic', () => {}))})++ it('a collapsed facet defers its fetch until it is expanded, then only if the scope moved', async () => {+ railLogic.actions.toggleFacetCollapsed(SERVICE.key)+ const logic = mountFacet(SERVICE)+ const other = mountFacet(LEVEL)+ await expectLogic(other).toDispatchActions(['loadFacetValuesSuccess'])+ expect(mockFacetValues).toHaveBeenCalledTimes(1)++ filtersLogic.actions.setSearchTerm('timeout')+ await expectLogic(other).toDispatchActions(['loadFacetValuesSuccess'])+ expect(logic.values.facetValues).toEqual([])++ mockFacetValues.mockClear()+ railLogic.actions.toggleFacetCollapsed(SERVICE.key)+ await expectLogic(logic).toDispatchActions(['loadFacetValuesSuccess'])+ expect(mockFacetValues).toHaveBeenCalledTimes(1)++ // Collapsing and expanding again with nothing else changed has nothing to fetch.+ mockFacetValues.mockClear()+ railLogic.actions.toggleFacetCollapsed(SERVICE.key)+ railLogic.actions.toggleFacetCollapsed(SERVICE.key)+ await expectLogic(logic).toNotHaveDispatchedActions(['loadFacetValues'])+ expect(mockFacetValues).not.toHaveBeenCalled()+ })++ it('collapsing while a fetch is still debouncing drops it, and expanding fetches again', async () => {+ // The request is debounced, so a facet collapsed within that window would otherwise still+ // hit the endpoint — and, having recorded the signature, look fresh enough to skip the+ // refetch on expand, leaving the stale list it was showing before.+ const logic = mountFacet(SERVICE)+ await expectLogic(logic).toDispatchActions(['loadFacetValuesSuccess'])+ mockFacetValues.mockClear()++ filtersLogic.actions.setSearchTerm('timeout')+ await expectLogic(logic).toDispatchActions(['loadFacetValues'])+ railLogic.actions.toggleFacetCollapsed(SERVICE.key)+ await expectLogic(logic).toDispatchActions(['loadFacetValuesSuccess'])+ expect(mockFacetValues).not.toHaveBeenCalled()++ railLogic.actions.toggleFacetCollapsed(SERVICE.key)+ await expectLogic(logic).toDispatchActions(['loadFacetValuesSuccess'])+ expect(mockFacetValues).toHaveBeenCalledTimes(1)+ })})diff --git a/products/logs/frontend/components/LogsViewer/FacetRail/facetValuesLogic.ts b/products/logs/frontend/components/LogsViewer/FacetRail/facetValuesLogic.tsindex b3c73e471f3b..eaadedef6fc7 100644--- a/products/logs/frontend/components/LogsViewer/FacetRail/facetValuesLogic.ts+++ b/products/logs/frontend/components/LogsViewer/FacetRail/facetValuesLogic.ts@@ -11,6 +11,7 @@ import { logsViewerFiltersLogic } from 'products/logs/frontend/components/LogsViimport { logsFacetValuesCreate } from '../../../generated/api'import { _LogFacetValueApi, _LogPropertyFilterApi, _LogsFacetValuesBodyApi } from '../../../generated/api.schemas'import type { LogsViewerFilters } from '../config/types'+import { facetRailLogic } from './facetRailLogic'import { FacetConfig, facetScopeSignature, facetSourceKey } from './facets'export interface FacetValuesLogicProps {@@ -21,6 +22,7 @@ export interface FacetValuesLogicProps {// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface facetValuesLogicValues {+ collapsedFacets: string[] // facetRailLogicfilters: LogsViewerFilters // logsViewerFiltersLogicpersonId: string | undefined // logsViewerFiltersLogicqueryFilterGroup: UniversalFiltersGroup // logsViewerFiltersLogic@@ -30,18 +32,23 @@ export interface facetValuesLogicValues {explicitDate: boolean | null | undefined} // logsViewerFiltersLogiccurrentTeamId: number | null // teamLogic+ collapsed: booleanfacetSearch: stringfacetValues: _LogFacetValueApi[]facetValuesLoading: booleanfetchSignature: string+ fetchedSignature: string | nullscopeSignature: string}// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface facetValuesLogicActions {- loadFacetValues: () => {+ clearFetchedSignature: () => {value: true}+ loadFacetValues: (signature: string) => {+ signature: string+ }loadFacetValuesFailure: (error: string,errorObject?: any@@ -52,12 +59,12 @@ export interface facetValuesLogicActions {loadFacetValuesSuccess: (facetValues: _LogFacetValueApi[],payload?: {- value: true+ signature: string}) => {facetValues: _LogFacetValueApi[]payload?: {- value: true+ signature: string}}setFacetSearch: (search: string) => {@@ -69,6 +76,7 @@ export interface facetValuesLogicActions {export interface facetValuesLogicMeta {key: string__keaTypeGenInternalSelectorTypes: {+ collapsed: (collapsedFacets: string[], arg: any) => booleanscopeSignature: (filters: LogsViewerFilters,queryFilterGroup: UniversalFiltersGroup,@@ -98,7 +106,8 @@ export type facetValuesLogicType = MakeLogicType<** One instance per facet, so each fetches on its own schedule and holds its own loading state. What* it fetches on is `fetchSignature` — the scope with this facet's own filters removed, plus its- * type-ahead — so clicking a value here re-scopes the *other* facets and leaves this list alone.+ * type-ahead — so clicking a value here re-scopes the *other* facets and leaves this list alone,+ * and a facet whose section is collapsed doesn't fetch at all until it's expanded.*/export const facetValuesLogic = kea<facetValuesLogicType>([props({} as FacetValuesLogicProps),@@ -114,14 +123,17 @@ export const facetValuesLogic = kea<facetValuesLogicType>([['filters', 'utcDateRange', 'queryFilterGroup', 'personId'],teamLogic,['currentTeamId'],+ facetRailLogic({ id: props.id }),+ ['collapsedFacets'],],})),actions({setFacetSearch: (search: string) => ({ search }),- // Declared here rather than left to the loader so it carries no payload — the request is- // built from current values, and typegen would otherwise infer one from the loader's params.- loadFacetValues: true,+ // Declared here rather than left to the loader: the request is built from current values,+ // and the signature payload is only there for the reducer below to record.+ loadFacetValues: (signature: string) => ({ signature }),+ clearFetchedSignature: true,}),reducers({@@ -131,6 +143,18 @@ export const facetValuesLogic = kea<facetValuesLogicType>([setFacetSearch: (_, { search }) => search,},],+ // The signature the values in the store were fetched for. Set when the request goes out, not+ // when it lands, so an overlapping trigger (e.g. expanding the facet mid-flight) doesn't+ // restart the same request. Cleared on failure, and when collapsing abandons an in-flight+ // load, so the next trigger retries rather than trusting values that never arrived.+ fetchedSignature: [+ null as string | null,+ {+ loadFacetValues: (_, { signature }) => signature,+ loadFacetValuesFailure: () => null,+ clearFetchedSignature: () => null,+ },+ ],}),loaders(({ props, values }) => ({@@ -139,7 +163,9 @@ export const facetValuesLogic = kea<facetValuesLogicType>([{loadFacetValues: async (_, breakpoint) => {await breakpoint(300)- if (!values.currentTeamId) {+ // Collapsed while this was still debouncing: nobody can see the list, so drop the+ // request. clearFetchedSignature has already run, so expanding refetches.+ if (values.collapsed || !values.currentTeamId) {return values.facetValues}const { source } = props.facet@@ -173,6 +199,10 @@ export const facetValuesLogic = kea<facetValuesLogicType>([})),selectors({+ collapsed: [+ (s) => [s.collapsedFacets, (_, p) => p.facet],+ (collapsedFacets: string[], facet: FacetConfig): boolean => collapsedFacets.includes(facet.key),+ ],// Everything that can change this facet's values, minus its own selection.scopeSignature: [(s) => [@@ -213,8 +243,30 @@ export const facetValuesLogic = kea<facetValuesLogicType>([],}),- subscriptions(({ actions }) => ({- // Fires with its initial value on mount, so this is also the first fetch.- fetchSignature: () => actions.loadFacetValues(),- })),+ subscriptions(({ actions, values }) => {+ // Subscriptions fire with their initial value, so this is also the mount fetch.+ const loadIfStale = (): void => {+ if (values.collapsed || values.fetchSignature === values.fetchedSignature) {+ return+ }+ actions.loadFacetValues(values.fetchSignature)+ }+ return {+ fetchSignature: loadIfStale,+ collapsed: (collapsed: boolean) => {+ if (collapsed) {+ // The loader drops a still-debouncing load on collapse, so forget the signature it+ // recorded — otherwise expanding would trust values that never arrived. Only when+ // one is in flight: settled values stay valid for their signature.+ if (values.facetValuesLoading) {+ actions.clearFetchedSignature()+ }+ return+ }+ // Expanding fetches what it missed while collapsed — and nothing if the scope hasn't+ // moved since.+ loadIfStale()+ },+ }+ }),])