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.

feat(aio): persist dashboard timeframe (#81819)

willwearinge82aa680ff37
diff --git a/products/ai_observability/frontend/AIObservabilityScene.tsx b/products/ai_observability/frontend/AIObservabilityScene.tsx
index 487dea7c6531..cca83ab3b80f 100644
--- a/products/ai_observability/frontend/AIObservabilityScene.tsx
+++ b/products/ai_observability/frontend/AIObservabilityScene.tsx
@@ -78,7 +78,8 @@ const SENTIMENT_DATE_OPTIONS = dateMapping.filter(({ values }) =>
const Filters = ({ hidePropertyFilters = false }: { hidePropertyFilters?: boolean }): JSX.Element => {
const { dashboardDateFilter, dateFilter, shouldFilterTestAccounts, propertyFilters, activeTab } =
useValues(aiObservabilitySharedLogic)
- const { setDates, setShouldFilterTestAccounts, setPropertyFilters } = useActions(aiObservabilitySharedLogic)
+ const { setDashboardDates, setDates, setShouldFilterTestAccounts, setPropertyFilters } =
+ useActions(aiObservabilitySharedLogic)
const { generationsQuery } = useValues(aiObservabilityGenerationsLogic)
const { selectedDashboardId } = useValues(aiObservabilityDashboardLogic)
@@ -90,7 +91,7 @@ const Filters = ({ hidePropertyFilters = false }: { hidePropertyFilters?: boolea
<DateFilter
dateFrom={dateFrom}
dateTo={dateTo}
- onChange={setDates}
+ onChange={activeTab === 'dashboard' ? setDashboardDates : setDates}
dateOptions={activeTab === 'sentiment' ? SENTIMENT_DATE_OPTIONS : undefined}
showRollingRangePicker={activeTab !== 'sentiment'}
showCustomRangeOptions={activeTab !== 'sentiment'}
@@ -121,7 +122,7 @@ const Filters = ({ hidePropertyFilters = false }: { hidePropertyFilters?: boolea
}
function AIObservabilityDashboard(): JSX.Element {
- const { dashboardDateFilter, propertyFilters } = useValues(aiObservabilitySharedLogic)
+ const { dashboardExternalDateFilters, propertyFilters } = useValues(aiObservabilitySharedLogic)
const { selectedDashboardId, availableDashboardsLoading } = useValues(aiObservabilityDashboardLogic)
const dashboardLogicInstance = React.useMemo(
@@ -144,17 +145,16 @@ function AIObservabilityDashboard(): JSX.Element {
const nextExternalFilters = React.useMemo(
() => ({
- date_from: dashboardDateFilter.dateFrom,
- date_to: dashboardDateFilter.dateTo,
+ ...dashboardExternalDateFilters,
properties: propertyFilters.length > 0 ? propertyFilters : null,
}),
- [dashboardDateFilter.dateFrom, dashboardDateFilter.dateTo, propertyFilters]
+ [dashboardExternalDateFilters, propertyFilters]
)
const currentExternalFilters = React.useMemo(
() => ({
- date_from: externalFilters?.date_from ?? null,
- date_to: externalFilters?.date_to ?? null,
+ date_from: externalFilters?.date_from,
+ date_to: externalFilters?.date_to,
properties: externalFilters?.properties ?? null,
}),
[externalFilters?.date_from, externalFilters?.date_to, externalFilters?.properties]
diff --git a/products/ai_observability/frontend/aiObservabilityLogic.test.ts b/products/ai_observability/frontend/aiObservabilityLogic.test.ts
index 45e8d8f429a9..f798c256ab4f 100644
--- a/products/ai_observability/frontend/aiObservabilityLogic.test.ts
+++ b/products/ai_observability/frontend/aiObservabilityLogic.test.ts
@@ -187,6 +187,44 @@ describe('aiObservabilitySharedLogic', () => {
})
})
+ it.each(['-1h', 'all'])('keeps the explicit %s dashboard range through URL sync and refresh', (dateFrom) => {
+ logic.actions.setSavedDashboardDates('-30d', null)
+ router.actions.push(urls.aiObservabilityDashboard())
+
+ expectLogic(logic).toMatchValues({
+ dashboardDateFilter: { dateFrom: '-30d', dateTo: null },
+ dashboardDateOverride: false,
+ dashboardExternalDateFilters: { date_from: undefined, date_to: undefined },
+ })
+
+ logic.actions.setDashboardDates(dateFrom, null)
+
+ expect(router.values.searchParams).toMatchObject({ date_from: dateFrom })
+ expectLogic(logic).toMatchValues({
+ dashboardDateFilter: { dateFrom, dateTo: null },
+ dashboardDateOverride: true,
+ dashboardExternalDateFilters: { date_from: dateFrom, date_to: null },
+ })
+
+ logic.unmount()
+ logic = aiObservabilitySharedLogic({})
+ logic.mount()
+ logic.actions.setSavedDashboardDates('-30d', null)
+
+ expectLogic(logic).toMatchValues({
+ dashboardDateFilter: { dateFrom, dateTo: null },
+ dashboardDateOverride: true,
+ })
+
+ router.actions.push(urls.aiObservabilityDashboard())
+
+ expectLogic(logic).toMatchValues({
+ dashboardDateFilter: { dateFrom: '-30d', dateTo: null },
+ dashboardDateOverride: false,
+ dashboardExternalDateFilters: { date_from: undefined, date_to: undefined },
+ })
+ })
+
it('syncs the trace content search between the URL, state, and traces query', () => {
featureFlagLogic.mount()
featureFlagLogic.actions.setFeatureFlags([FEATURE_FLAGS.LLM_OBSERVABILITY_TRACE_SEARCH], {
@@ -473,9 +511,40 @@ describe('AI observability persisted preferences', () => {
})
afterEach(() => {
+ jest.restoreAllMocks()
window.localStorage.clear()
})
+ it('loads the saved dashboard date range from dashboard details', async () => {
+ const dashboardSummary = { id: 42, name: 'AI dashboard', description: '' }
+ jest.spyOn(api.dashboards, 'list').mockResolvedValue({
+ count: 1,
+ next: null,
+ previous: null,
+ results: [dashboardSummary],
+ } as unknown as Awaited<ReturnType<typeof api.dashboards.list>>)
+ const getDashboard = jest.spyOn(api.dashboards, 'get').mockResolvedValue({
+ ...dashboardSummary,
+ persisted_filters: { date_from: '-30d', date_to: null },
+ } as Awaited<ReturnType<typeof api.dashboards.get>>)
+
+ const sharedLogic = aiObservabilitySharedLogic()
+ const dashboardLogic = aiObservabilityDashboardLogic()
+ sharedLogic.mount()
+ dashboardLogic.mount()
+
+ await expectLogic(dashboardLogic, () => dashboardLogic.actions.loadLLMDashboards()).toFinishAllListeners()
+
+ expect(getDashboard).toHaveBeenCalledWith(42)
+ expect(dashboardLogic.values.availableDashboards).toEqual([
+ { ...dashboardSummary, dateFrom: '-30d', dateTo: null },
+ ])
+ expect(sharedLogic.values.dashboardDateFilter).toEqual({ dateFrom: '-30d', dateTo: null })
+
+ dashboardLogic.unmount()
+ sharedLogic.unmount()
+ })
+
it('persists generation column preferences across remount', () => {
const columns = ['uuid', 'timestamp']
const firstLogic = aiObservabilityGenerationsLogic()
@@ -508,7 +577,9 @@ describe('AI observability persisted preferences', () => {
it('persists selected dashboard across remount', () => {
const firstLogic = aiObservabilityDashboardLogic()
firstLogic.mount()
- firstLogic.actions.loadLLMDashboardsSuccess([{ id: 42, name: 'AI dashboard', description: '' }])
+ firstLogic.actions.loadLLMDashboardsSuccess([
+ { id: 42, name: 'AI dashboard', description: '', dateFrom: '-7d', dateTo: null },
+ ])
firstLogic.unmount()
const secondLogic = aiObservabilityDashboardLogic()
diff --git a/products/ai_observability/frontend/aiObservabilitySharedLogic.ts b/products/ai_observability/frontend/aiObservabilitySharedLogic.ts
index 7fdf4db5952e..1a02829170f0 100644
--- a/products/ai_observability/frontend/aiObservabilitySharedLogic.ts
+++ b/products/ai_observability/frontend/aiObservabilitySharedLogic.ts
@@ -150,6 +150,11 @@ export interface aiObservabilitySharedLogicValues {
dateFrom: string | null
dateTo: string | null
}
+ dashboardDateOverride: boolean
+ dashboardExternalDateFilters: {
+ date_from: string | null | undefined
+ date_to: string | null | undefined
+ }
dateFilter: {
dateFrom: string | null
dateTo: string | null
@@ -157,6 +162,10 @@ export interface aiObservabilitySharedLogicValues {
hasSentAiEvent: boolean | undefined
hasSentAiEventLoading: boolean
propertyFilters: AnyPropertyFilter[]
+ savedDashboardDateFilter: {
+ dateFrom: string | null
+ dateTo: string | null
+ }
searchQuery: string
shouldFilterSupportTraces: boolean
shouldFilterTestAccounts: boolean
@@ -171,6 +180,13 @@ export interface aiObservabilitySharedLogicActions {
status: ProductSetupStatus
} // productSetupStatusLogic
addProductIntent: (properties: ProductIntentProperties) => ProductIntentProperties // teamLogic
+ applyDashboardUrlDates: (
+ dateFrom: string | null,
+ dateTo: string | null
+ ) => {
+ dateFrom: string | null
+ dateTo: string | null
+ }
applyUrlState: (state: ApplyUrlStatePayload) => ApplyUrlStatePayload
loadAIEventDefinition: () => any
loadAIEventDefinitionFailure: (
@@ -187,6 +203,20 @@ export interface aiObservabilitySharedLogicActions {
hasSentAiEvent: boolean
payload?: any
}
+ restoreSavedDashboardDates: (
+ dateFrom: string | null,
+ dateTo: string | null
+ ) => {
+ dateFrom: string | null
+ dateTo: string | null
+ }
+ setDashboardDates: (
+ dateFrom: string | null,
+ dateTo: string | null
+ ) => {
+ dateFrom: string | null
+ dateTo: string | null
+ }
setDates: (
dateFrom: string | null,
dateTo: string | null
@@ -197,6 +227,13 @@ export interface aiObservabilitySharedLogicActions {
setPropertyFilters: (propertyFilters: AnyPropertyFilter[]) => {
propertyFilters: AnyPropertyFilter[]
}
+ setSavedDashboardDates: (
+ dateFrom: string | null,
+ dateTo: string | null
+ ) => {
+ dateFrom: string | null
+ dateTo: string | null
+ }
setSearchQuery: (searchQuery: string) => {
searchQuery: string
}
@@ -212,6 +249,16 @@ export interface aiObservabilitySharedLogicActions {
export interface aiObservabilitySharedLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
+ dashboardExternalDateFilters: (
+ dashboardDateFilter: {
+ dateFrom: string | null
+ dateTo: string | null
+ },
+ dashboardDateOverride: boolean
+ ) => {
+ date_from: string | null | undefined
+ date_to: string | null | undefined
+ }
activeTab: (sceneKey: string | null) => AIObservabilityTabId
}
}
@@ -257,6 +304,10 @@ export const aiObservabilitySharedLogic = kea<aiObservabilitySharedLogicType>([
actions({
setDates: (dateFrom: string | null, dateTo: string | null) => ({ dateFrom, dateTo }),
+ setDashboardDates: (dateFrom: string | null, dateTo: string | null) => ({ dateFrom, dateTo }),
+ applyDashboardUrlDates: (dateFrom: string | null, dateTo: string | null) => ({ dateFrom, dateTo }),
+ restoreSavedDashboardDates: (dateFrom: string | null, dateTo: string | null) => ({ dateFrom, dateTo }),
+ setSavedDashboardDates: (dateFrom: string | null, dateTo: string | null) => ({ dateFrom, dateTo }),
setShouldFilterTestAccounts: (shouldFilterTestAccounts: boolean) => ({ shouldFilterTestAccounts }),
setShouldFilterSupportTraces: (shouldFilterSupportTraces: boolean) => ({ shouldFilterSupportTraces }),
setPropertyFilters: (propertyFilters: AnyPropertyFilter[]) => ({ propertyFilters }),
@@ -286,8 +337,28 @@ export const aiObservabilitySharedLogic = kea<aiObservabilitySharedLogicType>([
},
{
setDates: (_, { dateFrom, dateTo }) => ({ dateFrom, dateTo }),
- applyUrlState: (state, { dateFrom, dateTo, datesChanged }) =>
- datesChanged ? { dateFrom, dateTo } : state,
+ setDashboardDates: (_, { dateFrom, dateTo }) => ({ dateFrom, dateTo }),
+ applyDashboardUrlDates: (_, { dateFrom, dateTo }) => ({ dateFrom, dateTo }),
+ restoreSavedDashboardDates: (_, { dateFrom, dateTo }) => ({ dateFrom, dateTo }),
+ },
+ ],
+
+ dashboardDateOverride: [
+ false,
+ {
+ setDashboardDates: () => true,
+ applyDashboardUrlDates: () => true,
+ restoreSavedDashboardDates: () => false,
+ },
+ ],
+
+ savedDashboardDateFilter: [
+ {
+ dateFrom: INITIAL_DASHBOARD_DATE_FROM,
+ dateTo: INITIAL_DATE_TO,
+ },
+ {
+ setSavedDashboardDates: (_, { dateFrom, dateTo }) => ({ dateFrom, dateTo }),
},
],
@@ -333,6 +404,11 @@ export const aiObservabilitySharedLogic = kea<aiObservabilitySharedLogicType>([
})),
listeners(({ actions, values, cache }) => ({
+ setSavedDashboardDates: ({ dateFrom, dateTo }) => {
+ if (!values.dashboardDateOverride) {
+ actions.restoreSavedDashboardDates(dateFrom, dateTo)
+ }
+ },
loadAIEventDefinitionSuccess: ({ hasSentAiEvent }) => {
// Feed the app-wide setup-status layer (drives the scene empty-state gate).
actions.setDetectedStatus(hasSentAiEvent ? 'has-data' : 'needs-setup')
@@ -359,6 +435,19 @@ export const aiObservabilitySharedLogic = kea<aiObservabilitySharedLogicType>([
})),
selectors({
+ dashboardExternalDateFilters: [
+ (s) => [s.dashboardDateFilter, s.dashboardDateOverride],
+ (
+ dashboardDateFilter: {
+ dateFrom: string | null
+ dateTo: string | null
+ },
+ dashboardDateOverride: boolean
+ ) => ({
+ date_from: dashboardDateOverride ? dashboardDateFilter.dateFrom : undefined,
+ date_to: dashboardDateOverride ? dashboardDateFilter.dateTo : undefined,
+ }),
+ ],
activeTab: [
(s) => [s.sceneKey],
(sceneKey: string | null): AIObservabilityTabId => {
@@ -476,9 +565,37 @@ export const aiObservabilitySharedLogic = kea<aiObservabilitySharedLogicType>([
applySearchParams(searchParams)
}
+ function applyDashboard(searchParams: Record<string, unknown>): void {
+ applySearchParams(searchParams)
+
+ const hasDateOverride =
+ typeof searchParams.date_from === 'string' || typeof searchParams.date_to === 'string'
+ if (hasDateOverride) {
+ const dateFrom =
+ typeof searchParams.date_from === 'string' ? searchParams.date_from : INITIAL_EVENTS_DATE_FROM
+ const dateTo = typeof searchParams.date_to === 'string' ? searchParams.date_to : INITIAL_DATE_TO
+ if (
+ !values.dashboardDateOverride ||
+ dateFrom !== values.dashboardDateFilter.dateFrom ||
+ dateTo !== values.dashboardDateFilter.dateTo
+ ) {
+ actions.applyDashboardUrlDates(dateFrom, dateTo)
+ }
+ } else if (
+ values.dashboardDateOverride ||
+ values.dashboardDateFilter.dateFrom !== values.savedDashboardDateFilter.dateFrom ||
+ values.dashboardDateFilter.dateTo !== values.savedDashboardDateFilter.dateTo
+ ) {
+ actions.restoreSavedDashboardDates(
+ values.savedDashboardDateFilter.dateFrom,
+ values.savedDashboardDateFilter.dateTo
+ )
+ }
+ }
+
return {
[urls.aiObservabilityDashboard()]: (_, searchParams) => {
- applySearchParams(searchParams)
+ applyDashboard(searchParams)
startDashboardTimer()
},
[urls.aiObservabilitySelfDriving()]: (_, searchParams) => applyNonDashboard(searchParams),
@@ -546,6 +663,14 @@ export const aiObservabilitySharedLogic = kea<aiObservabilitySharedLogicType>([
date_to: dateTo || undefined,
},
],
+ setDashboardDates: ({ dateFrom, dateTo }) => [
+ router.values.location.pathname,
+ {
+ ...sharedSearchParams(),
+ date_from: dateFrom ?? 'all',
+ date_to: dateTo || undefined,
+ },
+ ],
setShouldFilterTestAccounts: ({ shouldFilterTestAccounts }) => [
router.values.location.pathname,
{
diff --git a/products/ai_observability/frontend/tabs/aiObservabilityDashboardLogic.ts b/products/ai_observability/frontend/tabs/aiObservabilityDashboardLogic.ts
index 0b6c595b5c89..26afb20ff39e 100644
--- a/products/ai_observability/frontend/tabs/aiObservabilityDashboardLogic.ts
+++ b/products/ai_observability/frontend/tabs/aiObservabilityDashboardLogic.ts
@@ -58,6 +58,8 @@ export interface aiObservabilityDashboardLogicValues {
propertyFilters: AnyPropertyFilter[] // aiObservabilitySharedLogic
shouldFilterTestAccounts: boolean // aiObservabilitySharedLogic
availableDashboards: Array<{
+ dateFrom: string | null
+ dateTo: string | null
description: string
id: number
name: string
@@ -78,6 +80,13 @@ export interface aiObservabilityDashboardLogicValues {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface aiObservabilityDashboardLogicActions {
+ setSavedDashboardDates: (
+ dateFrom: string | null,
+ dateTo: string | null
+ ) => {
+ dateFrom: string | null
+ dateTo: string | null
+ } // aiObservabilitySharedLogic
loadLLMDashboards: () => {
value: true
}
@@ -90,6 +99,8 @@ export interface aiObservabilityDashboardLogicActions {
}
loadLLMDashboardsSuccess: (
availableDashboards: {
+ dateFrom: string | null
+ dateTo: string | null
description: string
id: number
name: string
@@ -99,6 +110,8 @@ export interface aiObservabilityDashboardLogicActions {
}
) => {
availableDashboards: {
+ dateFrom: string | null
+ dateTo: string | null
description: string
id: number
name: string
@@ -154,6 +167,7 @@ export const aiObservabilityDashboardLogic = kea<aiObservabilityDashboardLogicTy
props({} as AIObservabilityDashboardLogicProps),
connect(() => ({
values: [aiObservabilitySharedLogic, ['dashboardDateFilter', 'shouldFilterTestAccounts', 'propertyFilters']],
+ actions: [aiObservabilitySharedLogic, ['setSavedDashboardDates']],
})),
actions({
@@ -205,7 +219,13 @@ export const aiObservabilityDashboardLogic = kea<aiObservabilityDashboardLogicTy
loaders(() => ({
availableDashboards: [
- [] as Array<{ id: number; name: string; description: string }>,
+ [] as Array<{
+ id: number
+ name: string
+ description: string
+ dateFrom: string | null
+ dateTo: string | null
+ }>,
{
loadLLMDashboards: async () => {
const response = await api.dashboards.list({
@@ -214,11 +234,24 @@ export const aiObservabilityDashboardLogic = kea<aiObservabilityDashboardLogicTy
})
const dashboards = response.results || []
- return dashboards.map((d) => ({
- id: d.id,
- name: d.name,
- description: d.description || '',
- }))
+ return await Promise.all(
+ dashboards.map(async (dashboardSummary) => {
+ const dashboard = dashboardSummary.persisted_filters
+ ? dashboardSummary
+ : await api.dashboards.get(dashboardSummary.id)
+
+ return {
+ id: dashboard.id,
+ name: dashboard.name,
+ description: dashboard.description || '',
+ dateFrom:
+ dashboard.persisted_filters && 'date_from' in dashboard.persisted_filters
+ ? (dashboard.persisted_filters.date_from ?? null)
+ : '-7d',
+ dateTo: dashboard.persisted_filters?.date_to ?? null,
+ }
+ })
+ )
},
},
],
@@ -241,6 +274,10 @@ export const aiObservabilityDashboardLogic = kea<aiObservabilityDashboardLogicTy
console.error('Failed to create default AI observability dashboard:', error)
}
}
+ } else {
+ const selectedDashboard =
+ availableDashboards.find(({ id }) => id === values.selectedDashboardId) ?? availableDashboards[0]
+ actions.setSavedDashboardDates(selectedDashboard.dateFrom, selectedDashboard.dateTo)
}
},