: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(metrics): don't flash the empty state while a superseded query refetches (#91988)
Jimmy Zhu9157a6bf811a
diff --git a/products/metrics/frontend/components/MetricsSamplesPanel.tsx b/products/metrics/frontend/components/MetricsSamplesPanel.tsxindex 40c82b341af3..a60178099613 100644--- a/products/metrics/frontend/components/MetricsSamplesPanel.tsx+++ b/products/metrics/frontend/components/MetricsSamplesPanel.tsx@@ -119,12 +119,12 @@ function SamplesTab(): JSX.Element {function AggregatesTab(): JSX.Element {const { aggregateRows } = useValues(metricsSamplesLogic)- const { queryResultsLoading, hasMetricName } = useValues(metricsViewerLogic)+ const { queryLoading, hasMetricName } = useValues(metricsViewerLogic)return (<LemonTabledataSource={aggregateRows}- loading={queryResultsLoading}+ loading={queryLoading}size="small"rowKey={(row: MetricsAggregateRow) => row.name}emptyState={diff --git a/products/metrics/frontend/components/MetricsViewer.tsx b/products/metrics/frontend/components/MetricsViewer.tsxindex 75448cccdf8a..4987516911f3 100644--- a/products/metrics/frontend/components/MetricsViewer.tsx+++ b/products/metrics/frontend/components/MetricsViewer.tsx@@ -127,7 +127,7 @@ export const MetricsViewer = (): JSX.Element => {chartSeries,anomalyBadge,liveRefresh,- queryResultsLoading,+ queryLoading,queryError,savedInsightLoading,savedInsight,@@ -393,12 +393,12 @@ export const MetricsViewer = (): JSX.Element => {fallbackName={metricName}exemplars={chartMarkers}/>- ) : !queryResultsLoading ? (+ ) : !queryLoading ? (<div className="h-full flex items-center justify-center text-secondary text-sm">No data for this metric in the selected range.</div>) : null}- {queryResultsLoading && <SpinnerOverlay />}+ {queryLoading && <SpinnerOverlay />}</div></div>{hasMetricName && (diff --git a/products/metrics/frontend/components/metricsViewerLogic.test.ts b/products/metrics/frontend/components/metricsViewerLogic.test.tsindex cb1ab10786e0..70f6b8222118 100644--- a/products/metrics/frontend/components/metricsViewerLogic.test.ts+++ b/products/metrics/frontend/components/metricsViewerLogic.test.ts@@ -253,6 +253,24 @@ describe('metricsViewerLogic', () => {expect(logic.values.queryError).toBeNull()})+ // A superseded query's abort lands as a failure while the replacement query is still in+ // flight. kea-loaders' auto `queryResultsLoading` drops to false then, which flashed the+ // "No data" empty state between the spinner and the chart, so `queryLoading` must ride+ // out the abort, while still clearing on a real failure.+ it.each([+ [+ 'a superseded (aborted) query',+ NEW_QUERY_STARTED_ERROR_MESSAGE,+ new DOMException(NEW_QUERY_STARTED_ERROR_MESSAGE, 'AbortError'),+ true,+ ],+ ['a real query failure', 'Invalid regex pattern', new Error('Invalid regex pattern'), false],+ ])('queryLoading after %s', (_name, message, errorObject, expected) => {+ logic.actions.fetchQueryResults({})+ logic.actions.fetchQueryResultsFailure(message, errorObject)+ expect(logic.values.queryLoading).toBe(expected)+ })+// The filter bar's property filters must translate into the backend's Prometheus-style// matchers: operator mapping, multi-value alternation (with regex escaping), and skipping// chips that are still being edited. A bad mapping silently filters the chart wrong.diff --git a/products/metrics/frontend/components/metricsViewerLogic.tsx b/products/metrics/frontend/components/metricsViewerLogic.tsxindex 3a6fdbe30994..1e94233731dc 100644--- a/products/metrics/frontend/components/metricsViewerLogic.tsx+++ b/products/metrics/frontend/components/metricsViewerLogic.tsx@@ -208,6 +208,7 @@ export interface metricsViewerLogicValues {queryAbortController: AbortController | nullqueryError: string | nullqueryFilters: _MetricFilterApi[]+ queryLoading: booleanqueryResults: MetricsViewerSeries[]queryResultsLoading: booleansavedInsight: QueryBasedInsightModel | null@@ -504,6 +505,18 @@ export const metricsViewerLogic = kea<metricsViewerLogicType>([isUserInitiatedError(error) ? state : error || 'Something went wrong running this query.',},],+ // kea-loaders' auto `queryResultsLoading` drops to false when a superseded query's+ // abort lands as a failure, flashing the empty state while the replacement query is+ // still in flight. This flag only clears on success or a real failure, so the UI+ // must read it instead of `queryResultsLoading`.+ queryLoading: [+ false as boolean,+ {+ fetchQueryResults: () => true,+ fetchQueryResultsSuccess: () => false,+ fetchQueryResultsFailure: (state, { error }) => (isUserInitiatedError(error) ? state : false),+ },+ ],}),listeners(({ actions, values, cache }) => ({// Narrows the metric picker to the filtered services, so it offers only the