: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
fix(taxonomic-filter): stop property filters evicting event recents (#95172)
Paul D'Ambra0b235cdb07f4
diff --git a/frontend/src/lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic.test.ts b/frontend/src/lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic.test.tsindex fa3541a411a7..ee77e6cb3e50 100644--- a/frontend/src/lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic.test.ts+++ b/frontend/src/lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic.test.ts@@ -586,4 +586,63 @@ describe('recentTaxonomicFiltersLogic', () => {expect(logic.values.recentFilters.map((f) => f.propertyFilter?.value)).toEqual(['baz', 'foobar'])})})++ describe('one shared list, many groups', () => {+ const personFilterFor = (key: string, value: string): PersonPropertyFilter => ({+ type: PropertyFilterType.Person,+ key,+ operator: PropertyOperator.Exact,+ value,+ })++ it('keeps event recents when property filters flood the list', () => {+ for (let i = 0; i < 5; i++) {+ logic.actions.recordRecentFilter({+ groupType: TaxonomicFilterGroupType.Events,+ groupName: 'Events',+ value: `event-${i}`,+ item: { name: `event-${i}` },+ })+ }+ // A morning of person filtering: more writes on its own than the whole cap holds.+ for (let i = 0; i < MAX_RECENT_FILTERS * 2; i++) {+ logic.actions.recordRecentFilter({+ groupType: TaxonomicFilterGroupType.PersonProperties,+ groupName: 'Person properties',+ value: `person-key-${i}`,+ item: { name: `person-key-${i}` },+ propertyFilter: personFilterFor(`person-key-${i}`, 'x'),+ })+ }++ const events = logic.values.recentFilters.filter((f) => f.groupType === TaxonomicFilterGroupType.Events)+ expect(events.map((f) => f.value)).toEqual(['event-4', 'event-3', 'event-2', 'event-1', 'event-0'])+ expect(logic.values.recentFilters).toHaveLength(MAX_RECENT_FILTERS)+ })++ it('drops the oldest entry of the busiest group, not the oldest overall', () => {+ logic.actions.recordRecentFilter({+ groupType: TaxonomicFilterGroupType.Events,+ groupName: 'Events',+ value: '$pageview',+ item: { name: '$pageview' },+ })+ for (let i = 0; i < MAX_RECENT_FILTERS; i++) {+ logic.actions.recordRecentFilter({+ groupType: TaxonomicFilterGroupType.PersonProperties,+ groupName: 'Person properties',+ value: `person-key-${i}`,+ item: { name: `person-key-${i}` },+ propertyFilter: personFilterFor(`person-key-${i}`, 'x'),+ })+ }++ const filters = logic.values.recentFilters+ expect(filters).toHaveLength(MAX_RECENT_FILTERS)+ expect(filters.map((f) => f.value)).toContain('$pageview')+ expect(filters.find((f) => f.value === 'person-key-0')).toBeUndefined()+ // Newest-first order survives the trim.+ expect(filters[0].value).toBe(`person-key-${MAX_RECENT_FILTERS - 1}`)+ })+ })})diff --git a/frontend/src/lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic.ts b/frontend/src/lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic.tsindex f12a924d20d5..d5c3780c5f70 100644--- a/frontend/src/lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic.ts+++ b/frontend/src/lib/components/TaxonomicFilter/recentTaxonomicFiltersLogic.ts@@ -15,7 +15,7 @@ import {TaxonomicFilterValue,} from './types'-export const MAX_RECENT_FILTERS = 20+export const MAX_RECENT_FILTERS = 60export const RECENT_FILTER_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000const EXCLUDED_RECENT_FILTER_GROUP_TYPES = new Set<TaxonomicFilterGroupType>([...META_GROUP_TYPES,@@ -150,6 +150,29 @@ function isDuplicateRecentFilter(return true}+/**+ * Every picker on the page shares this one list, and its groups write at wildly different+ * rates: a property filter records a recent for each completed value, an event records one+ * when it is picked. Trimming the globally oldest entries would let a burst from one group+ * evict every entry of another, so trim round-robin: keep each group's most recent entry,+ * then each group's second, and so on, and every group present holds a share of the cap.+ * Input and output are both ordered newest-first.+ */+export function capRecentFilters(filters: RecentTaxonomicFilter[]): RecentTaxonomicFilter[] {+ if (filters.length <= MAX_RECENT_FILTERS) {+ return filters+ }+ const rankInGroup = new Map<TaxonomicFilterGroupType, number>()+ const ranked = filters.map((filter, index) => {+ const rank = rankInGroup.get(filter.groupType) ?? 0+ rankInGroup.set(filter.groupType, rank + 1)+ return { index, rank }+ })+ ranked.sort((a, b) => a.rank - b.rank || a.index - b.index)+ const kept = new Set(ranked.slice(0, MAX_RECENT_FILTERS).map((r) => r.index))+ return filters.filter((_, index) => kept.has(index))+}+const teamId = window.POSTHOG_APP_CONTEXT?.current_team?.id// Generated by kea-typegen. Update if you're an agent, ignore if you're human.@@ -266,7 +289,7 @@ export const recentTaxonomicFiltersLogic = kea<recentTaxonomicFiltersLogicType>(const withoutExpired = withoutDuplicate.filter((f) => f.timestamp > cutoff)- return [entry, ...withoutExpired].slice(0, MAX_RECENT_FILTERS)+ return capRecentFilters([entry, ...withoutExpired])},},],