: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
perf(signals): stop double counting a loaded inbox section on refresh (#127471)
Dylan Martin720a67a389bc
diff --git a/products/signals/frontend/inbox/components/tabs/ReportsTab.test.tsx b/products/signals/frontend/inbox/components/tabs/ReportsTab.test.tsxindex f3cfa26a4d8f..bf3e246d87da 100644--- a/products/signals/frontend/inbox/components/tabs/ReportsTab.test.tsx+++ b/products/signals/frontend/inbox/components/tabs/ReportsTab.test.tsx@@ -308,6 +308,43 @@ describe('ReportsTab', () => {})})+ // The automatic For you to Entire project switch refreshes the already-loaded section through+ // its first page alone. Until that page lands the section still holds the For-you count, so the+ // event has to wait for it instead of reporting the project as empty.+ it('waits for a refreshed page before firing Inbox viewed', async () => {+ let releaseProjectPage: () => void = () => {}+ const heldProjectPage = new Promise<void>((resolve) => {+ releaseProjectPage = resolve+ })+ mockReportCounts([])+ useMocks({+ get: {+ '/api/projects/:team_id/signals/reports/': async ({ request }) => {+ const params = new URL(request.url).searchParams+ if (params.has('suggested_reviewers')) {+ return [200, { count: 0, next: null, previous: null, results: [] }]+ }+ const needsDecision = params.get('view') === 'needs_decision'+ if (needsDecision && params.get('count_only') !== 'true') {+ await heldProjectPage+ }+ return [200, { count: needsDecision ? 7 : 0, next: null, previous: null, results: [] }]+ },+ },+ })+ userLogic.actions.loadUserSuccess(MOCK_DEFAULT_USER)++ render(<ReportsTab />)+ await waitFor(() => expect(inboxFiltersLogic.values.scope).toBe(INBOX_SCOPE_ENTIRE_PROJECT))+ releaseProjectPage()++ await waitFor(() => {+ const viewed = (posthog.capture as jest.Mock).mock.calls.filter(([event]) => event === INBOX_EVENTS.VIEWED)+ expect(viewed).toHaveLength(1)+ expect(viewed[0][1]).toMatchObject({ total_count: 7, is_empty: false })+ })+ })+// The empty verdict must cover every state the current user can see, not only the counted// ones: a staff user on a project whose reports are all judged not actionable still has work// to review, so the waiting state would be wrong for them and right for everyone else.diff --git a/products/signals/frontend/inbox/components/tabs/ReportsTab.tsx b/products/signals/frontend/inbox/components/tabs/ReportsTab.tsxindex 034fbb55df5a..87b38202c828 100644--- a/products/signals/frontend/inbox/components/tabs/ReportsTab.tsx+++ b/products/signals/frontend/inbox/components/tabs/ReportsTab.tsx@@ -50,6 +50,7 @@ type CountedSectionKey = (typeof COUNTED_SECTION_KEYS)[number]interface SectionListState {count: number | nullcountLoading: boolean+ countSettled: booleanreports: SignalReport[]isLoaded: booleanreportsLoadFailed: boolean@@ -68,6 +69,7 @@ function useSectionState(sectionKey: InboxReportSectionKey): SectionListState {const {count,countLoading,+ countSettled,reports,isLoaded,reportsLoadFailed,@@ -79,6 +81,7 @@ function useSectionState(sectionKey: InboxReportSectionKey): SectionListState {return {count,countLoading,+ countSettled,reports,isLoaded,reportsLoadFailed,@@ -127,10 +130,9 @@ function useInboxViewedEvent(sections: Record<CountedSectionKey, SectionListStat!isRunsOpen &&!isTriageOpen- // A count is settled once its request is no longer in flight: loaded, refreshed, or failed- // (count stays null). Waiting on the loading flags rather than non-null values means a scope or- // filter refresh in progress doesn't fire the event with the previous query's counts.- const settled = COUNTED_SECTION_KEYS.every((key) => !sections[key].countLoading)+ // Waiting on `countSettled` rather than the loading flags means a scope or filter change+ // doesn't fire the event with the previous query's counts.+ const settled = COUNTED_SECTION_KEYS.every((key) => sections[key].countSettled)const firedRef = useRef(false)useEffect(() => {diff --git a/products/signals/frontend/inbox/logics/reportListLogic.test.ts b/products/signals/frontend/inbox/logics/reportListLogic.test.tsindex 0c01c7faeb95..2bc1b2245bd8 100644--- a/products/signals/frontend/inbox/logics/reportListLogic.test.ts+++ b/products/signals/frontend/inbox/logics/reportListLogic.test.ts@@ -21,7 +21,12 @@ import {} from '../types'import { inboxBulkActionsLogic } from './inboxBulkActionsLogic'import { inboxFiltersLogic } from './inboxFiltersLogic'-import { INBOX_REPORT_SECTION_LIST_PARAMS, reportListLogic, shouldDefaultToEntireProject } from './reportListLogic'+import {+ INBOX_REPORT_SECTION_LIST_PARAMS,+ reportListLogic,+ sectionListLogicProps,+ shouldDefaultToEntireProject,+} from './reportListLogic'const REPORTS_URL = '/api/projects/:team_id/signals/reports/'const REFRESH_METRICS_URL = '/api/projects/:team_id/signals/reports/refresh_metrics/'@@ -75,12 +80,12 @@ describe('reportListLogic', () => {[FEATURE_FLAGS.SIGNALS_REPORT_MONITORING]: true,})).toFinishAllListeners()- expect(requests).toHaveLength(2)+ expect(requests).toHaveLength(1)expect(requests.every((url) => new URL(url).searchParams.get('status') === 'monitoring')).toBe(true)expect(logic.values).toMatchObject({ count: 1, reports: [makeReport('monitored')] })await expectLogic(logic, () => featureFlagLogic.actions.setFeatureFlags([], {})).toFinishAllListeners()- expect(requests).toHaveLength(2)+ expect(requests).toHaveLength(1)expect(logic.values).toMatchObject({ count: 0, reports: [], hasMore: false })} finally {logic.unmount()@@ -283,6 +288,70 @@ describe('reportListLogic', () => {expect(logic.values.reports).toHaveLength(FIRST_PAGE.length + SECOND_PAGE.length)})+ it('takes the count of a loaded section from its reloaded page, not from a late count of the old query', async () => {+ logic.actions.setScope(INBOX_SCOPE_ENTIRE_PROJECT)+ await expectLogic(logic).toFinishAllListeners()+ const MINE = FIRST_PAGE.slice(0, 3)+ const requests: string[] = []+ let releaseOldCount: () => void = () => {}+ const heldOldCount = new Promise<void>((resolve) => {+ releaseOldCount = resolve+ })+ useMocks({+ get: {+ [REPORTS_URL]: async ({ request }) => {+ const params = new URL(request.url).searchParams+ if (params.get('count_only') === 'true') {+ requests.push(`count:${params.get('assignee') ?? 'all'}`)+ await heldOldCount+ return [200, { count: 99, next: null, previous: null, results: [] }]+ }+ requests.push(`page:${params.get('assignee') ?? 'all'}`)+ return [200, { count: MINE.length, next: null, previous: null, results: MINE }]+ },+ },+ })++ logic.actions.loadCount()+ logic.actions.setScope(INBOX_SCOPE_CLAIMED_BY_YOU)+ await expectLogic(logic).toDispatchActions(['loadReportsSuccess'])+ releaseOldCount()+ await expectLogic(logic).toFinishAllListeners()++ expect(requests).toEqual(['count:all', 'page:mine'])+ expect(logic.values.count).toBe(MINE.length)+ })++ it('takes the count of a loaded section from its reloaded page, not from a count the bulk action outran', async () => {+ const REMAINING = FIRST_PAGE.slice(0, 4)+ let releaseOldCount: () => void = () => {}+ const heldOldCount = new Promise<void>((resolve) => {+ releaseOldCount = resolve+ })+ useMocks({+ get: {+ [REPORTS_URL]: async ({ request }) => {+ if (new URL(request.url).searchParams.get('count_only') === 'true') {+ await heldOldCount+ return [200, { count: FIRST_PAGE.length, next: null, previous: null, results: [] }]+ }+ return [200, { count: REMAINING.length, next: null, previous: null, results: REMAINING }]+ },+ },+ })+ const bulkLogic = inboxBulkActionsLogic()+ bulkLogic.mount()++ logic.actions.loadCount()+ bulkLogic.actions.bulkDismissSuccess()+ await expectLogic(logic).toDispatchActions(['loadReportsSuccess'])+ releaseOldCount()+ await expectLogic(logic).toFinishAllListeners()++ expect(logic.values.count).toBe(REMAINING.length)+ bulkLogic.unmount()+ })+it('keeps a claim made while the first page reloads, which the reload read before the claim saved', async () => {let releasePage: () => void = () => {}const heldPage = new Promise<void>((resolve) => {@@ -419,6 +488,46 @@ describe('reportListLogic', () => {)})+ // `InboxStateFilter` hides the Closed state until the project holds a closed report, so this+ // project-wide check has to keep up with reports that close while the inbox is open.+ describe('closed state availability', () => {+ let projectClosedReports: number+ let logic: ReturnType<typeof reportListLogic.build>++ beforeEach(async () => {+ projectClosedReports = 0+ useMocks({+ get: {+ '/api/projects/:team_id/signals/reports/available_reviewers': {},+ [REPORTS_URL]: ({ request }) => {+ const params = new URL(request.url).searchParams+ if (params.get('count_only') === 'true') {+ return [200, { count: projectClosedReports, next: null, previous: null, results: [] }]+ }+ return [200, { count: 0, next: null, previous: null, results: [] }]+ },+ },+ })+ initKeaTests()+ logic = reportListLogic(sectionListLogicProps('closed'))+ logic.mount()+ logic.actions.ensureLoaded()+ await expectLogic(logic).toFinishAllListeners()+ })++ afterEach(() => logic.unmount())++ it('rechecks the project for closed reports when the loaded section refreshes', async () => {+ expect(logic.values.hasProjectClosedReports).toBe(false)++ projectClosedReports = 1+ logic.actions.refresh()+ await expectLogic(logic).toFinishAllListeners()++ expect(logic.values.hasProjectClosedReports).toBe(true)+ })+ })+// A scope change while a request for the old query is in flight. The old response lands last,// and the list must still show only the new query's rows.describe('query change while a request is in flight', () => {diff --git a/products/signals/frontend/inbox/logics/reportListLogic.ts b/products/signals/frontend/inbox/logics/reportListLogic.tsindex ca527a21ac32..294f2a932b0e 100644--- a/products/signals/frontend/inbox/logics/reportListLogic.ts+++ b/products/signals/frontend/inbox/logics/reportListLogic.ts@@ -169,6 +169,29 @@ export function shouldDefaultToEntireProject(input: {)}+// First For-you count for the primary section: if the user has no reports suggested to+// them, default to Entire project so they don't land on an empty inbox. Only when they haven't+// picked a scope themselves, and only once the user's uuid has resolved (so the count is+// genuinely theirs, not an unfiltered project-wide count). A loaded section takes its count from+// its first page, so this runs on both counts.+function applyDefaultScopeIfEmpty(+ props: reportListLogicType['props'],+ values: reportListLogicType['values'],+ actions: reportListLogicType['actions']+): void {+ if (+ shouldDefaultToEntireProject({+ sectionKey: props.sectionKey,+ scope: values.scope,+ hasUserChosenScope: values.hasUserChosenScope,+ hasResolvedUser: !!values.user?.uuid,+ count: values.count,+ })+ ) {+ actions.applyDefaultScope(INBOX_SCOPE_ENTIRE_PROJECT)+ }+}+/*** A write that timed out or lost its response can still have saved, so every surface that shows the report reloads* it. This list can be mounted without the inbox scene, for example in a row menu, and then only reloads itself.@@ -201,6 +224,7 @@ export interface reportListLogicValues {claimSavingReportIds: string[]count: number | nullcountLoading: boolean+ countSettled: booleanhasMore: booleanhasProjectClosedReports: boolean | nullhasProjectClosedReportsLoading: boolean@@ -307,10 +331,10 @@ export interface reportListLogicActions {errorObject?: any}loadCountSuccess: (- count: number,+ count: number | null,payload?: void) => {- count: number+ count: number | nullpayload?: void}loadHasProjectClosedReports: (_: void) => void@@ -465,6 +489,14 @@ export interface reportListLogicMeta {livePrReportIds: (reports: SignalReport[]) => string[]totalCount: (reportsResponse: ReportListResponse | null) => number | nullloadedQueryKey: (reportsResponse: ReportListResponse | null) => string | null+ countSettled: (+ countLoading: boolean,+ reportsResponseLoading: boolean,+ reportsResponse: ReportListResponse | null,+ reportsLoadFailed: boolean,+ loadedQueryKey: string | null,+ listApiParams: any+ ) => booleanloadedContext: (reportsResponse: ReportListResponse | null) => ReportListRequestContext | nullloadedRanker: (reportsResponse: ReportListResponse | null) => string | nullloadedRankingPill: (reportsResponse: ReportListResponse | null) => boolean@@ -598,14 +630,25 @@ export const reportListLogic = kea<reportListLogicType>([if (props.sectionKey === 'outcome-monitoring' && !values.monitoringAvailable) {return 0}+ const params = values.listApiParams+ const pageAtRequest = values.reportsResponse// nosemgrep: prefer-codegen-api-namespaced-signals -- the Inbox renders the handwritten SignalReport. The generated report type is wider (string status and priority, read-only arrays), so the list calls move to generated types together with the Inbox.const response = await api.signalReports.list({- ...listRequestParams(values.listApiParams),+ ...listRequestParams(params),limit: 1,count_only: 'true',})// A newer count request for a changed query supersedes this one.breakpoint()+ // A loaded section sends no new count request, so nothing else retires this+ // one. Drop it when the query moved on, and when a page landed while it was in+ // flight, because that page carries the total for the rows now on screen.+ if (+ JSON.stringify(values.listApiParams) !== JSON.stringify(params) ||+ values.reportsResponse !== pageAtRequest+ ) {+ return values.count+ }return response.count},},@@ -892,6 +935,30 @@ export const reportListLogic = kea<reportListLogicType>([(reportsResponse: ReportListResponse | null): string | null =>reportsResponse?.requestParams ? JSON.stringify(reportsResponse.requestParams) : null,],+ // Nothing that can still change the count is in flight, and the count on screen belongs to+ // the current query. A failed page settles the section on what it had, rather than holding+ // a consumer forever.+ countSettled: [+ (s) => [+ s.countLoading,+ s.reportsResponseLoading,+ s.reportsResponse,+ s.reportsLoadFailed,+ s.loadedQueryKey,+ s.listApiParams,+ ],+ (+ countLoading: boolean,+ reportsResponseLoading: boolean,+ reportsResponse: ReportListResponse | null,+ reportsLoadFailed: boolean,+ loadedQueryKey: string | null,+ listApiParams: any+ ): boolean =>+ !countLoading &&+ !reportsResponseLoading &&+ (reportsResponse === null || reportsLoadFailed || loadedQueryKey === JSON.stringify(listApiParams)),+ ],// Display context (scope, active-filters flag) as it was when the loaded response was// requested — for telemetry, so an in-flight response can't be labeled with context the// user switched to after the request went out.@@ -922,6 +989,7 @@ export const reportListLogic = kea<reportListLogicType>([// loaders report: the first page and each appended page bring rows that need painting. An// empty announcement matters too, because it retires the rows a narrowed filter dropped.loadReportsSuccess: () => {+ applyDefaultScopeIfEmpty(props, values, actions)setInboxServedRanker(values.loadedRanker, values.loadedRankingPill)actions.trackReports(props.sectionKey, values.livePrReportIds)actions.refreshReportMetrics(values.staleMetricReportIds)@@ -986,23 +1054,7 @@ export const reportListLogic = kea<reportListLogicType>([actions.applyReportMetricSnapshots([...response.reports])}},- // First For-you count for the primary section: if the user has no reports suggested to- // them, default to Entire project so they don't land on an empty inbox. Only when they haven't- // picked a scope themselves, and only once the user's uuid has resolved (so the count is- // genuinely theirs, not an unfiltered project-wide count).- loadCountSuccess: () => {- if (- shouldDefaultToEntireProject({- sectionKey: props.sectionKey,- scope: values.scope,- hasUserChosenScope: values.hasUserChosenScope,- hasResolvedUser: !!values.user?.uuid,- count: values.count,- })- ) {- actions.applyDefaultScope(INBOX_SCOPE_ENTIRE_PROJECT)- }- },+ loadCountSuccess: () => applyDefaultScopeIfEmpty(props, values, actions),ensureLoaded: () => {if (values.reportsResponse === null && !values.reportsResponseLoading) {actions.loadReports()@@ -1020,13 +1072,21 @@ export const reportListLogic = kea<reportListLogicType>([}},refresh: () => {- actions.loadCount()- // An in-flight first page was requested with the old filters, so it reloads too.+ // An in-flight first page was requested with the old filters, so it reloads too. The page+ // response carries the total, so a separate count would run the same COUNT(*) twice.if (values.isLoaded || values.reportsResponseLoading) {actions.loadReports()+ // Whether the project holds any closed report is a project-wide question that this+ // section's page does not answer, and the `loadCount` listener that asks it does not+ // run on this path. The Closed tab stays hidden until this check runs again.+ if (props.sectionKey === 'closed') {+ actions.loadHasProjectClosedReports()+ }+ } else {+ actions.loadCount()}},- // User-driven filter/scope changes re-fetch the count always, and the rows if this section is loaded.+ // User-driven filter/scope changes re-fetch the rows if this section is loaded, and only the count if not.setSearchQuery: async (_, breakpoint) => {await breakpoint(300)actions.refresh()