: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(engineering-analytics): cap long windows from a shared link (#102168)
Julian Bez6203e9601955
diff --git a/products/engineering_analytics/frontend/scenes/engineeringAnalyticsFiltersLogic.test.ts b/products/engineering_analytics/frontend/scenes/engineeringAnalyticsFiltersLogic.test.tsnew file mode 100644index 000000000000..12ac65cdc90b--- /dev/null+++ b/products/engineering_analytics/frontend/scenes/engineeringAnalyticsFiltersLogic.test.ts@@ -0,0 +1,31 @@+import { MAX_WINDOW_DAYS, windowStartFromUrl } from './engineeringAnalyticsFiltersLogic'++describe('windowStartFromUrl', () => {+ beforeEach(() => {+ jest.useFakeTimers()+ jest.setSystemTime(new Date('2026-09-17T12:00:00Z'))+ })++ afterEach(() => {+ jest.useRealTimers()+ })++ test.each([+ ['a window inside the cap', '-90d', null, '-90d'],+ ['a relative window past the cap', '-2y', null, `-${MAX_WINDOW_DAYS}d`],+ ['a relative start with a relative end, as a relative start', '-2y', '-30d', '-395d'],+ ['a relative start with a fixed end, as a date', '-2y', '2026-06-30', '2025-06-30'],+ ['a window ending far in the future, as a date', '-2y', '2028-01-01', '2027-01-01'],+ ['an absolute window past the cap', '2024-01-01', '2026-06-30', '2025-06-30'],+ ['an absolute start without an end, as a date', '2024-01-01', null, '2025-09-17'],+ ['an absolute window inside the cap', '2026-01-01', '2026-06-30', '2026-01-01'],+ ['a window with an unsigned relative end', '-3y', '2y', '-3y'],+ ['a window with a plus relative start', '+3y', null, '+3y'],+ ['a window ending at a week boundary', '-2y', '-1wStart', '-2y'],+ ['a calendar date that does not exist', '2024-02-31', null, '2024-02-31'],+ ['an unparseable start', 'garbage', null, 'garbage'],+ ['a malformed date', '2026-01-01garbage', null, '2026-01-01garbage'],+ ])('keeps or caps %s', (_name, dateFrom, dateTo, expected) => {+ expect(windowStartFromUrl(dateFrom, dateTo)).toEqual(expected)+ })+})diff --git a/products/engineering_analytics/frontend/scenes/engineeringAnalyticsFiltersLogic.ts b/products/engineering_analytics/frontend/scenes/engineeringAnalyticsFiltersLogic.tsindex 5f787459bd74..4480491e9f0f 100644--- a/products/engineering_analytics/frontend/scenes/engineeringAnalyticsFiltersLogic.ts+++ b/products/engineering_analytics/frontend/scenes/engineeringAnalyticsFiltersLogic.ts@@ -1,6 +1,9 @@import { MakeLogicType, actions, kea, path, reducers, selectors } from 'kea'import { actionToUrl, router, urlToAction } from 'kea-router'+import { Dayjs, dayjs } from 'lib/dayjs'+import { dateStringToDayJs } from 'lib/utils/dateFilters'+import { EngineeringAnalyticsWorkflowHealthRunScope } from '../generated/api.schemas'// One window shared by every time-scoped CI-analytics surface, so a window picked on one page carries@@ -60,6 +63,49 @@ function runScopeFromUrl(runScope: string | undefined, legacyBranch: string | unreturn EngineeringAnalyticsWorkflowHealthRunScope.All}+// The backend rejects a window longer than 366 days. One day less absorbs the offset between the team's+// time zone and the UTC dates parsed here.+export const MAX_WINDOW_DAYS = 365++// A plain past offset such as -90d or -2y. The cap leaves every other relative form alone: the backend reads+// an unsigned or `+` offset as time ago where the browser reads time ahead, and it resolves a -1wStart style+// boundary in the team's time zone.+const PAST_OFFSET = /^-\d+[dwmqy]$/+const ISO_DATE = /^\d{4}-\d{2}-\d{2}/++// Null for a value the cap should not reason about. That includes a calendar date Dayjs rolls over, such as+// 2024-02-31, which the backend does not read as March 2.+function parseUrlDate(date: string): Dayjs | null {+ if (PAST_OFFSET.test(date)) {+ return dateStringToDayJs(date)+ }+ if (!ISO_DATE.test(date)) {+ return null+ }+ try {+ const parsed = dayjs.utc(date)+ return parsed.isValid() && parsed.format('YYYY-MM-DD') === date.slice(0, 10) ? parsed : null+ } catch {+ return null+ }+}++/** The start of a URL's window, moved up to MAX_WINDOW_DAYS before its end when the URL asks for more, so+ * a shared link with a long range still loads instead of failing every panel. A rolling window (a past offset+ * with no end or a past-offset end) stays rolling. Anything with a fixed date gets a fixed start: a relative+ * start against a fixed end would drift past the end over time. */+export function windowStartFromUrl(dateFrom: string, dateTo: string | null): string {+ const from = parseUrlDate(dateFrom)+ const to = dateTo ? parseUrlDate(dateTo) : dayjs.utc()+ if (!from || !to || to.diff(from, 'day') <= MAX_WINDOW_DAYS) {+ return dateFrom+ }+ const start = to.utc().subtract(MAX_WINDOW_DAYS, 'day').startOf('day')+ const rolling = PAST_OFFSET.test(dateFrom) && (!dateTo || PAST_OFFSET.test(dateTo))+ const daysAgo = dayjs.utc().startOf('day').diff(start, 'day')+ return rolling && daysAgo > 0 ? `-${daysAgo}d` : start.format('YYYY-MM-DD')+}+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface engineeringAnalyticsFiltersLogicValues {dateFrom: string | null@@ -160,8 +206,8 @@ export const engineeringAnalyticsFiltersLogic = kea<engineeringAnalyticsFiltersL// Hydrate from the URL, dispatching only on real change (also breaks the actionToUrl loop).// '*' is safe: the logic is only mounted while a CI-analytics scene connects it.'*': (_, searchParams) => {- const dateFrom = searchParams.date_from ?? SHARED_DEFAULT_DATE_FROMconst dateTo = searchParams.date_to ?? null+ const dateFrom = windowStartFromUrl(searchParams.date_from ?? SHARED_DEFAULT_DATE_FROM, dateTo)if (dateFrom !== values.dateFrom || dateTo !== values.dateTo) {actions.setDateRange(dateFrom, dateTo)}