Skip to content

: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.

fix(flags): compare the flag calls retention notice in the team timezone (#124556)

Phil Haack8a0797797600
diff --git a/frontend/src/queries/nodes/InsightViz/FlagCallsRetentionNotice.stories.tsx b/frontend/src/queries/nodes/InsightViz/FlagCallsRetentionNotice.stories.tsx
index 424c6dacb2a9..28a0056a6399 100644
--- a/frontend/src/queries/nodes/InsightViz/FlagCallsRetentionNotice.stories.tsx
+++ b/frontend/src/queries/nodes/InsightViz/FlagCallsRetentionNotice.stories.tsx
@@ -13,4 +13,4 @@ export default meta
type Story = StoryObj<typeof FlagCallsRetentionNotice>
-export const Default: Story = {}
+export const Default: Story = { args: { retentionStartDate: '2026-07-09' } }
diff --git a/frontend/src/queries/nodes/InsightViz/FlagCallsRetentionNotice.tsx b/frontend/src/queries/nodes/InsightViz/FlagCallsRetentionNotice.tsx
index 761ece994031..ee7c375f2e0b 100644
--- a/frontend/src/queries/nodes/InsightViz/FlagCallsRetentionNotice.tsx
+++ b/frontend/src/queries/nodes/InsightViz/FlagCallsRetentionNotice.tsx
@@ -1,15 +1,13 @@
import { LemonBanner } from '@posthog/lemon-ui'
-import {
- FLAG_EVALUATIONS_RETENTION_DAYS,
- flagEvaluationsRetentionStart,
-} from 'scenes/feature-flags/flagEvaluationsTable'
+import { dayjs } from 'lib/dayjs'
+import { FLAG_EVALUATIONS_RETENTION_DAYS } from 'scenes/feature-flags/flagEvaluationsTable'
-export function FlagCallsRetentionNotice(): JSX.Element {
+export function FlagCallsRetentionNotice({ retentionStartDate }: { retentionStartDate: string }): JSX.Element {
return (
<LemonBanner type="info" className="m-2">
This insight includes feature flag calls from the last {FLAG_EVALUATIONS_RETENTION_DAYS} days only. Periods
- before {flagEvaluationsRetentionStart().format('MMMM D, YYYY')} show no calls.
+ before <span translate="no">{dayjs.utc(retentionStartDate).format('MMMM D, YYYY')}</span> show no calls.
</LemonBanner>
)
}
diff --git a/frontend/src/queries/nodes/InsightViz/InsightVizDisplay.tsx b/frontend/src/queries/nodes/InsightViz/InsightVizDisplay.tsx
index cd3da4d2f730..465ba2938361 100644
--- a/frontend/src/queries/nodes/InsightViz/InsightVizDisplay.tsx
+++ b/frontend/src/queries/nodes/InsightViz/InsightVizDisplay.tsx
@@ -218,6 +218,7 @@ export function InsightVizDisplay({
validationErrorCode,
theme,
showsFlagCallsRetentionNotice,
+ flagCallsRetentionStartDate,
} = useValues(insightVizDataLogic(insightProps))
const { loadData, updateQuerySource } = useActions(insightVizDataLogic(insightProps))
const { exportContext, queryId, insightDataError } = useValues(insightDataLogic(insightProps))
@@ -603,7 +604,9 @@ export function InsightVizDisplay({
</div>
)}
- {!embedded && showsFlagCallsRetentionNotice && <FlagCallsRetentionNotice />}
+ {!embedded && showsFlagCallsRetentionNotice && (
+ <FlagCallsRetentionNotice retentionStartDate={flagCallsRetentionStartDate} />
+ )}
<div
className={clsx(
'InsightVizDisplay__content',
diff --git a/frontend/src/scenes/feature-flags/flagEvaluationsTable.test.ts b/frontend/src/scenes/feature-flags/flagEvaluationsTable.test.ts
index 5b9a10809227..8d769361cf69 100644
--- a/frontend/src/scenes/feature-flags/flagEvaluationsTable.test.ts
+++ b/frontend/src/scenes/feature-flags/flagEvaluationsTable.test.ts
@@ -1,56 +1,127 @@
import { dayjs } from 'lib/dayjs'
-import { insightReachesPastFlagEvaluationsRetention } from './flagEvaluationsTable'
+import { FlagCallsInsightResponse, insightReachesPastFlagEvaluationsRetention } from './flagEvaluationsTable'
-const daysAgo = (days: number): string => dayjs.utc().subtract(days, 'day').toISOString()
-const now = (): string => dayjs.utc().toISOString()
+// The UTC date is a day ahead of the local date at UTC-12, and equal to it at UTC+14. Ninety days back is
+// November 2, 2025, when Havana repeats midnight.
+const NOW = dayjs.utc('2026-01-31T08:00:00Z')
+const daysAgo = (days: number): string => NOW.subtract(days, 'day').toISOString()
+const now = (): string => NOW.toISOString()
+const resolved = (dateFrom: string, response: FlagCallsInsightResponse = {}): FlagCallsInsightResponse => ({
+ resolved_date_range: { date_from: dateFrom, date_to: now() },
+ ...response,
+})
describe('flagEvaluationsTable', () => {
+ beforeEach(() => {
+ jest.useFakeTimers()
+ jest.setSystemTime(NOW.toDate())
+ })
+
+ afterEach(() => {
+ jest.useRealTimers()
+ })
+
it.each([
- ['a range inside the window', '-30d', { date_from: daysAgo(30), date_to: now() }, undefined, false],
- ['a range past the window', '-180d', { date_from: daysAgo(180), date_to: now() }, undefined, true],
+ ['a range inside the window', '-30d', resolved(daysAgo(30)), undefined, false],
+ ['a range past the window', '-180d', resolved(daysAgo(180)), undefined, true],
+ ['all time, which resolves inside the window', 'all', resolved(daysAgo(10)), undefined, true],
+ ['a query that has not run yet', '-180d', undefined, undefined, false],
+ [
+ 'the 90-day preset two hours ahead of UTC',
+ '-90d',
+ resolved('2025-11-02T00:00:00+02:00', { timezone: 'Africa/Johannesburg' }),
+ undefined,
+ false,
+ ],
+ [
+ 'the 90-day preset at UTC+14',
+ '-90d',
+ resolved('2025-11-02T00:00:00+14:00', { timezone: 'Pacific/Kiritimati' }),
+ undefined,
+ false,
+ ],
+ [
+ 'the 90-day preset at UTC-12',
+ '-90d',
+ resolved('2025-11-01T00:00:00-12:00', { timezone: 'Etc/GMT+12' }),
+ undefined,
+ false,
+ ],
+ [
+ 'the 90-day preset across a daylight saving change that repeats midnight',
+ '-90d',
+ resolved('2025-11-02T00:00:00-04:00', { timezone: 'America/Havana' }),
+ undefined,
+ false,
+ ],
[
- 'all time, which resolves inside the window',
- 'all',
- { date_from: daysAgo(10), date_to: now() },
+ 'the 90-day preset in a result cached before team-local midnight',
+ '-90d',
+ resolved('2025-11-01T00:00:00+02:00', {
+ timezone: 'Africa/Johannesburg',
+ last_refresh: '2026-01-30T21:00:00Z',
+ }),
+ undefined,
+ false,
+ ],
+ [
+ 'a range that starts an hour before the team-local window',
+ '2025-11-01T23:00:00',
+ resolved('2025-11-01T23:00:00+14:00', { timezone: 'Pacific/Kiritimati' }),
undefined,
true,
],
- ['a query that has not run yet', '-180d', undefined, undefined, false],
[
- 'a previous period inside the window',
+ 'a timezone the browser does not know',
'-30d',
- { date_from: daysAgo(30), date_to: now() },
- { compare: true },
+ resolved(daysAgo(30), { timezone: 'Mars/Olympus_Mons' }),
+ undefined,
false,
],
+ ['a previous period inside the window', '-30d', resolved(daysAgo(30)), { compare: true }, false],
[
'a previous period that doubles the range back past the window',
'-60d',
- { date_from: daysAgo(60), date_to: now() },
+ resolved(daysAgo(60)),
{ compare: true },
true,
],
[
'a comparison shifted inside the window',
'-30d',
- { date_from: daysAgo(30), date_to: now() },
+ resolved(daysAgo(30)),
{ compare: true, compare_to: '-30d' },
false,
],
[
'a comparison shifted past the window',
'-30d',
- { date_from: daysAgo(30), date_to: now() },
+ resolved(daysAgo(30)),
{ compare: true, compare_to: '-1y' },
true,
],
- ])(
- 'insightReachesPastFlagEvaluationsRetention: %s',
- (_name, dateFrom, resolvedDateRange, compareFilter, expected) => {
- expect(insightReachesPastFlagEvaluationsRetention({ dateFrom, resolvedDateRange, compareFilter })).toBe(
- expected
- )
- }
- )
+ [
+ 'a Metric whose change pill compares against the period before the 90-day preset',
+ '-90d',
+ resolved('2025-11-02T00:00:00+02:00', {
+ timezone: 'Africa/Johannesburg',
+ resolved_compare_date_range: {
+ date_from: '2025-08-03T00:00:00+02:00',
+ date_to: '2025-11-02T00:00:00+02:00',
+ },
+ }),
+ undefined,
+ true,
+ ],
+ [
+ 'a comparison whose start the backend resolved inside the window',
+ '-30d',
+ resolved(daysAgo(30), { resolved_compare_date_range: { date_from: daysAgo(60), date_to: daysAgo(30) } }),
+ { compare: true, compare_to: '-1y' },
+ false,
+ ],
+ ])('insightReachesPastFlagEvaluationsRetention: %s', (_name, dateFrom, response, compareFilter, expected) => {
+ expect(insightReachesPastFlagEvaluationsRetention({ dateFrom, response, compareFilter })).toBe(expected)
+ })
})
diff --git a/frontend/src/scenes/feature-flags/flagEvaluationsTable.ts b/frontend/src/scenes/feature-flags/flagEvaluationsTable.ts
index 968b2339e080..548c4f2cfeb5 100644
--- a/frontend/src/scenes/feature-flags/flagEvaluationsTable.ts
+++ b/frontend/src/scenes/feature-flags/flagEvaluationsTable.ts
@@ -2,7 +2,7 @@ import { dayjs } from 'lib/dayjs'
import { componentsToDayJs, dateStringToComponents, dateStringToDayJs } from 'lib/utils/dateFilters'
import { FlagEvaluationsModeEnumApi } from '~/generated/core/api.schemas'
-import { CompareFilter } from '~/queries/schema/schema-general'
+import { CompareFilter, ResolvedDateRangeResponse } from '~/queries/schema/schema-general'
import type { TeamPublicType, TeamType } from '~/types'
// Not a root table, so the `posthog.` prefix is part of the name. A team on the Events mode has no
@@ -30,11 +30,55 @@ export function storesFlagCallsOutsideEvents(team: TeamPublicType | TeamType | n
*/
export const FLAG_EVALUATIONS_RETENTION_DAYS = 90
+function dateFormatIn(timezone: string): Intl.DateTimeFormat {
+ try {
+ return new Intl.DateTimeFormat('en-US', {
+ timeZone: timezone,
+ year: 'numeric',
+ month: '2-digit',
+ day: '2-digit',
+ })
+ } catch {
+ // A browser with older timezone data throws a RangeError for a newer zone name, such as Europe/Kyiv.
+ return dateFormatIn('UTC')
+ }
+}
+
+// Intl reads the date in `timezone` without the browser's timezone. dayjs's tz() parses the wall time in the
+// browser's timezone. Near a DST change in that timezone, the result can move by an hour or to another day.
+function dateInTimezone(instant: dayjs.Dayjs, timezone: string): string {
+ const parts = dateFormatIn(timezone).formatToParts(instant.valueOf())
+ const part = (type: Intl.DateTimeFormatPartTypes): string => parts.find((p) => p.type === type)?.value ?? ''
+ return `${part('year')}-${part('month')}-${part('day')}`
+}
+
+export interface FlagCallsInsightResponse {
+ timezone?: string | null
+ last_refresh?: string | null
+ resolved_date_range?: ResolvedDateRangeResponse | null
+ resolved_compare_date_range?: ResolvedDateRangeResponse | null
+}
+
+function insightTimezone(response: FlagCallsInsightResponse | null | undefined): string {
+ return response?.timezone || 'UTC'
+}
+
+/** The oldest day the window covers, as a `YYYY-MM-DD` date. Without a response, it is the UTC date as of now. */
+export function flagEvaluationsRetentionStartDate(response?: FlagCallsInsightResponse | null): string {
+ // The backend resolves the 90-day preset from when the query ran, so a cached result counts back from then.
+ const asOf = response?.last_refresh ? dayjs(response.last_refresh) : dayjs()
+ // The subtraction runs on a UTC date so that no DST change can shift it.
+ return dayjs
+ .utc(dateInTimezone(asOf, insightTimezone(response)))
+ .subtract(FLAG_EVALUATIONS_RETENTION_DAYS, 'day')
+ .format('YYYY-MM-DD')
+}
+
// Start of the oldest day that FLAG_EVALUATIONS_RETENTION_DAYS covers. dateStringToDayJs resolves the ranges this is
// compared to against UTC, so the boundary is UTC too: a browser-local midnight sits hours off it,
// which drops the 90-day preset in a timezone ahead of UTC.
export function flagEvaluationsRetentionStart(): dayjs.Dayjs {
- return dayjs.utc().startOf('day').subtract(FLAG_EVALUATIONS_RETENTION_DAYS, 'day')
+ return dayjs.utc(flagEvaluationsRetentionStartDate())
}
export function reachesPastFlagEvaluationsRetention(dateFrom: string | null): boolean {
@@ -43,30 +87,49 @@ export function reachesPastFlagEvaluationsRetention(dateFrom: string | null): bo
return !parsed || parsed.isBefore(flagEvaluationsRetentionStart())
}
+// Trends sends the compared range it resolved in the team timezone. The Metric display's change pill gets one
+// even though its query has no compareFilter. Stickiness and funnels send none, so this estimates the range in the
+// browser's timezone. Across a DST change, the estimate can move the start to the previous day.
+function comparedPeriodStart(
+ start: dayjs.Dayjs,
+ response: FlagCallsInsightResponse | null | undefined,
+ compareFilter: CompareFilter | null | undefined
+): dayjs.Dayjs {
+ const resolvedFrom = response?.resolved_compare_date_range?.date_from
+ if (resolvedFrom) {
+ return dayjs(resolvedFrom)
+ }
+ if (!compareFilter?.compare) {
+ return start
+ }
+ const compareToComponents = dateStringToComponents(compareFilter.compare_to ?? null)
+ return compareToComponents
+ ? componentsToDayJs(compareToComponents, start)
+ : start.subtract(dayjs(response?.resolved_date_range?.date_to ?? undefined).diff(start), 'millisecond')
+}
+
/** Whether the range an insight ran over, or the period it compares against, starts before the table's oldest day. */
export function insightReachesPastFlagEvaluationsRetention({
dateFrom,
- resolvedDateRange,
+ response,
compareFilter,
}: {
/** The live editor range, which wins over the saved query's. */
dateFrom: string | null | undefined
- resolvedDateRange: { date_from?: string | null; date_to?: string | null } | null | undefined
+ response: FlagCallsInsightResponse | null | undefined
compareFilter: CompareFilter | null | undefined
}): boolean {
// All time resolves to the oldest row the table still holds, so the resolved range never shows the cutoff.
if (dateFrom === 'all') {
return true
}
+ const resolvedDateRange = response?.resolved_date_range
if (!resolvedDateRange?.date_from) {
return false
}
- const start = dayjs(resolvedDateRange.date_from)
- const compareToComponents = compareFilter?.compare ? dateStringToComponents(compareFilter.compare_to ?? null) : null
- const comparedStart = !compareFilter?.compare
- ? start
- : compareToComponents
- ? componentsToDayJs(compareToComponents, start)
- : start.subtract(dayjs(resolvedDateRange.date_to ?? undefined).diff(start), 'millisecond')
- return comparedStart.isBefore(flagEvaluationsRetentionStart())
+ const comparedStart = comparedPeriodStart(dayjs(resolvedDateRange.date_from), response, compareFilter)
+ // The window covers whole days in the team timezone. This compares dates, not instants. A DST change can
+ // repeat midnight. The backend starts the 90-day preset at the first midnight. An instant comparison against
+ // the second midnight would show the notice for the preset.
+ return dateInTimezone(comparedStart, insightTimezone(response)) < flagEvaluationsRetentionStartDate(response)
}
diff --git a/frontend/src/scenes/insights/insightVizDataLogic.ts b/frontend/src/scenes/insights/insightVizDataLogic.ts
index 65af5ad5c41a..cab0261d7cad 100644
--- a/frontend/src/scenes/insights/insightVizDataLogic.ts
+++ b/frontend/src/scenes/insights/insightVizDataLogic.ts
@@ -24,6 +24,7 @@ import { databaseTableListLogic } from 'scenes/data-management/database/database
import { dataThemeLogic } from 'scenes/dataThemeLogic'
import {
FLAG_EVALUATIONS_TABLE,
+ flagEvaluationsRetentionStartDate,
insightReachesPastFlagEvaluationsRetention,
} from 'scenes/feature-flags/flagEvaluationsTable'
import { MathAvailability } from 'scenes/insights/filters/ActionFilter/ActionFilterRow/types'
@@ -237,6 +238,7 @@ export interface insightVizDataLogicValues {
display: ChartDisplayType | null | undefined
enabledIntervals: Intervals
erroredQueryId: any
+ flagCallsRetentionStartDate: string
formula: string | null | undefined
formulaNodes: TrendsFormulaNode[]
formulas: string[] | null | undefined
@@ -1216,6 +1218,7 @@ export interface insightVizDataLogicMeta {
compareFilter: CompareFilter | null | undefined,
insightData: Record<string, any>
) => boolean
+ flagCallsRetentionStartDate: (insightData: Record<string, any>) => string
isNonTimeSeriesDisplay: (display: ChartDisplayType | null | undefined) => boolean
isSingleSeriesOutput: (
isTrends: boolean,
@@ -2325,10 +2328,14 @@ export const insightVizDataLogic = kea<insightVizDataLogicType>([
readsFlagCalls(querySource) &&
insightReachesPastFlagEvaluationsRetention({
dateFrom: dateRange?.date_from,
- resolvedDateRange: insightData?.resolved_date_range,
+ response: insightData,
compareFilter,
}),
],
+ flagCallsRetentionStartDate: [
+ (s) => [s.insightData],
+ (insightData: Record<string, any>): string => flagEvaluationsRetentionStartDate(insightData),
+ ],
isNonTimeSeriesDisplay: [
(s) => [s.display],
(display: ChartDisplayType | null | undefined) =>