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(data-warehouse): guard invalid dates in materialization run history UI (#76960)

Thiago Salvatore7102131e3f5c
diff --git a/frontend/src/scenes/data-warehouse/editor/output-pane-tabs/QueryInfo.tsx b/frontend/src/scenes/data-warehouse/editor/output-pane-tabs/QueryInfo.tsx
index 2d7827e0c7f9..0d1caf7a8e72 100644
--- a/frontend/src/scenes/data-warehouse/editor/output-pane-tabs/QueryInfo.tsx
+++ b/frontend/src/scenes/data-warehouse/editor/output-pane-tabs/QueryInfo.tsx
@@ -128,7 +128,7 @@ export function QueryInfo({ tabId, view }: QueryInfoProps): JSX.Element {
</Tooltip>
)
}
- if (last_run_at === 'never' && !status) {
+ if (!last_run_at && !status) {
return (
<Tooltip title="This is a view, so it's always available with the latest data">
<span className="text-secondary">Available</span>
@@ -149,8 +149,12 @@ export function QueryInfo({ tabId, view }: QueryInfoProps): JSX.Element {
</Tooltip>
)
}
- if (last_run_at === 'never' && !status) {
- return (
+ if (!last_run_at) {
+ return status ? (
+ <Tooltip title="This materialized view hasn't completed a run yet">
+ <span className="text-secondary">Not yet run</span>
+ </Tooltip>
+ ) : (
<Tooltip title="This is a view, so it is never run">
<span className="text-secondary">N/A</span>
</Tooltip>
diff --git a/frontend/src/scenes/data-warehouse/editor/output-pane-tabs/infoTabLogic.ts b/frontend/src/scenes/data-warehouse/editor/output-pane-tabs/infoTabLogic.ts
index 20889f86ee30..34424f7f1e6c 100644
--- a/frontend/src/scenes/data-warehouse/editor/output-pane-tabs/infoTabLogic.ts
+++ b/frontend/src/scenes/data-warehouse/editor/output-pane-tabs/infoTabLogic.ts
@@ -18,7 +18,7 @@ export interface InfoTableRow {
type: 'source' | 'table'
view_id?: string
status?: string
- last_run_at?: string
+ last_run_at?: string | null
}
export interface InfoTabLogicProps {
@@ -41,7 +41,7 @@ export interface infoTabLogicValues {
view_id?: undefined
}
| {
- last_run_at: string
+ last_run_at: string | null
name: string
status: string | undefined
type: string
@@ -73,7 +73,7 @@ export interface infoTabLogicMeta {
view_id?: undefined
}
| {
- last_run_at: string
+ last_run_at: string | null
name: string
status: string | undefined
type: string
@@ -125,7 +125,7 @@ export const infoTabLogic = kea<infoTabLogicType>([
type: 'table',
view_id: view.id,
status: view.status,
- last_run_at: view.last_run_at || 'never',
+ last_run_at: view.last_run_at ?? null,
}
}
diff --git a/frontend/src/scenes/data-warehouse/saved_queries/MaterializationStatusPanel.tsx b/frontend/src/scenes/data-warehouse/saved_queries/MaterializationStatusPanel.tsx
index b02ead28e5fd..9667951e10e4 100644
--- a/frontend/src/scenes/data-warehouse/saved_queries/MaterializationStatusPanel.tsx
+++ b/frontend/src/scenes/data-warehouse/saved_queries/MaterializationStatusPanel.tsx
@@ -4,7 +4,6 @@ import { IconRefresh, IconRevert, IconX } from '@posthog/icons'
import { LemonBanner, LemonDialog, LemonTable, Link, Spinner } from '@posthog/lemon-ui'
import { FEATURE_FLAGS } from 'lib/constants'
-import { dayjsUtcToTimezone } from 'lib/dayjs'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonProgress } from 'lib/lemon-ui/LemonProgress'
import { LemonSelect } from 'lib/lemon-ui/LemonSelect'
@@ -13,7 +12,6 @@ import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { getAccessControlDisabledReason } from 'lib/utils/accessControlUtils'
import { humanFriendlyDetailedTime } from 'lib/utils/datetime'
-import { humanFriendlyDuration } from 'lib/utils/durations'
import { humanFriendlyNumber } from 'lib/utils/numbers'
import { LogsViewer } from 'scenes/hog-functions/logs/LogsViewer'
import { teamLogic } from 'scenes/teamLogic'
@@ -30,6 +28,7 @@ import {
import { dataWarehouseViewsLogic } from './dataWarehouseViewsLogic'
import { materializationJobsLogic } from './materializationJobsLogic'
+import { computeJobDuration, jobLogsWindow } from './materializationJobUtils'
const LOG_LEVELS: LogEntryLevel[] = ['LOG', 'INFO', 'WARN', 'WARNING', 'ERROR']
@@ -413,23 +412,12 @@ export function MaterializationStatusPanel({ viewId, kind = 'view' }: Materializ
{
title: 'Updated',
dataIndex: 'last_run_at',
- render: (_, { last_run_at }: DataModelingJob) => humanFriendlyDetailedTime(last_run_at),
+ render: (_, { last_run_at }: DataModelingJob) =>
+ last_run_at ? humanFriendlyDetailedTime(last_run_at) : '-',
},
{
title: 'Duration',
- render: (_, job: DataModelingJob) => {
- if (job.status === 'Running') {
- return 'In progress'
- }
- const start = new Date(job.created_at).getTime()
- const end = new Date(job.last_run_at).getTime()
-
- if (start > end) {
- return 'N/A'
- }
-
- return humanFriendlyDuration((end - start) / 1000)
- },
+ render: (_, job: DataModelingJob) => computeJobDuration(job),
},
]}
expandable={
@@ -446,15 +434,8 @@ export function MaterializationStatusPanel({ viewId, kind = 'view' }: Materializ
hideLevelsFilter
hideInstanceIdColumn
defaultFilters={{
- instanceId: job.workflow_run_id,
- dateFrom: dayjsUtcToTimezone(job.created_at, timezone).format(
- 'YYYY-MM-DD HH:mm:ss'
- ),
- dateTo: job.last_run_at
- ? dayjsUtcToTimezone(job.last_run_at, timezone)
- .add(1, 'hour')
- .format('YYYY-MM-DD HH:mm:ss')
- : undefined,
+ instanceId: job.workflow_run_id ?? undefined,
+ ...jobLogsWindow(job, timezone),
levels: showDebugLogs ? ['DEBUG', ...LOG_LEVELS] : LOG_LEVELS,
}}
/>
diff --git a/frontend/src/scenes/data-warehouse/saved_queries/materializationJobUtils.test.ts b/frontend/src/scenes/data-warehouse/saved_queries/materializationJobUtils.test.ts
new file mode 100644
index 000000000000..3f9c3ae9c70a
--- /dev/null
+++ b/frontend/src/scenes/data-warehouse/saved_queries/materializationJobUtils.test.ts
@@ -0,0 +1,77 @@
+import { DataModelingJob } from '~/types'
+
+import { computeJobDuration, jobLogsWindow } from './materializationJobUtils'
+
+const START = '2024-01-10T10:00:00Z'
+const START_PLUS_90S = '2024-01-10T10:01:30Z'
+
+function makeJob(overrides: Partial<DataModelingJob>): DataModelingJob {
+ return {
+ id: 'job-1',
+ saved_query_id: 'view-1',
+ status: 'Completed',
+ rows_materialized: 10,
+ rows_expected: 10,
+ error: null,
+ created_at: START,
+ last_run_at: START_PLUS_90S,
+ workflow_id: 'wf',
+ workflow_run_id: 'run-1',
+ ...overrides,
+ }
+}
+
+describe('materializationJobUtils', () => {
+ // humanFriendlyDuration joins units with a non-breaking space
+ const NINETY_SECONDS = '1m\u00a030s'
+
+ test.each<[string, Partial<DataModelingJob>, string]>([
+ ['running job', { status: 'Running' }, 'In progress'],
+ ['completed job', {}, NINETY_SECONDS],
+ [
+ 'failed job with stale last_run_at falls back to updated_at',
+ { status: 'Failed', last_run_at: START, updated_at: START_PLUS_90S },
+ NINETY_SECONDS,
+ ],
+ [
+ 'cancelled job stamped by a bulk update, where updated_at is the stale one',
+ { status: 'Cancelled', last_run_at: START_PLUS_90S, updated_at: START },
+ NINETY_SECONDS,
+ ],
+ ['failed job with stale last_run_at and no updated_at', { status: 'Failed', last_run_at: START }, '-'],
+ ['unparseable created_at', { created_at: 'not-a-date' }, '-'],
+ ['missing last_run_at on completed job', { last_run_at: undefined as unknown as string }, '-'],
+ ])('computeJobDuration: %s', (_name, overrides, expected) => {
+ expect(computeJobDuration(makeJob(overrides))).toEqual(expected)
+ })
+
+ test.each<[string, Partial<DataModelingJob>, string | undefined, string | undefined]>([
+ [
+ 'completed job closes the window an hour after the run ended',
+ {},
+ '2024-01-10 10:00:00',
+ '2024-01-10 11:01:30',
+ ],
+ ['running job leaves the window open', { status: 'Running' }, '2024-01-10 10:00:00', undefined],
+ [
+ 'failed job uses updated_at for the window end',
+ { status: 'Failed', last_run_at: START, updated_at: START_PLUS_90S },
+ '2024-01-10 10:00:00',
+ '2024-01-10 11:01:30',
+ ],
+ [
+ 'cancelled job stamped by a bulk update uses the fresher last_run_at',
+ { status: 'Cancelled', last_run_at: START_PLUS_90S, updated_at: START },
+ '2024-01-10 10:00:00',
+ '2024-01-10 11:01:30',
+ ],
+ [
+ 'unparseable created_at omits dateFrom instead of sending "Invalid Date"',
+ { created_at: 'not-a-date' },
+ undefined,
+ '2024-01-10 11:01:30',
+ ],
+ ])('jobLogsWindow: %s', (_name, overrides, expectedFrom, expectedTo) => {
+ expect(jobLogsWindow(makeJob(overrides), 'UTC')).toEqual({ dateFrom: expectedFrom, dateTo: expectedTo })
+ })
+})
diff --git a/frontend/src/scenes/data-warehouse/saved_queries/materializationJobUtils.ts b/frontend/src/scenes/data-warehouse/saved_queries/materializationJobUtils.ts
new file mode 100644
index 000000000000..e675339d15f5
--- /dev/null
+++ b/frontend/src/scenes/data-warehouse/saved_queries/materializationJobUtils.ts
@@ -0,0 +1,61 @@
+import { dayjs, dayjsUtcToTimezone } from 'lib/dayjs'
+import { humanFriendlyDuration } from 'lib/utils/durations'
+
+import { DataModelingJob } from '~/types'
+
+const LOGS_FILTER_FORMAT = 'YYYY-MM-DD HH:mm:ss'
+
+function isParseableDate(value: string | null | undefined): value is string {
+ return !!value && dayjs(value).isValid()
+}
+
+function latestOf(...values: (string | null | undefined)[]): string | null {
+ const parseable = values.filter(isParseableDate)
+ if (!parseable.length) {
+ return null
+ }
+ return parseable.reduce((latest, value) => (dayjs(value).isAfter(dayjs(latest)) ? value : latest))
+}
+
+/** When the run ended, best-effort. Running jobs have no end yet. Failed and cancelled runs are
+ * stamped by different write paths: a model save advances both fields, a bulk `QuerySet.update()`
+ * skips the `auto_now` on `updated_at`, and rows predating the failure stamp still carry the run's
+ * start time in `last_run_at`. Whichever is later is the end in all three cases. */
+function jobEndTimestamp(job: DataModelingJob): string | null {
+ if (job.status === 'Running') {
+ return null
+ }
+ if (job.status === 'Completed') {
+ return isParseableDate(job.last_run_at) ? job.last_run_at : null
+ }
+ return latestOf(job.updated_at, job.last_run_at)
+}
+
+export function computeJobDuration(job: DataModelingJob): string {
+ if (job.status === 'Running') {
+ return 'In progress'
+ }
+ const end = jobEndTimestamp(job)
+ if (!isParseableDate(job.created_at) || !isParseableDate(end)) {
+ return '-'
+ }
+ const durationSeconds = (new Date(end).getTime() - new Date(job.created_at).getTime()) / 1000
+ if (durationSeconds <= 0) {
+ return '-'
+ }
+ return humanFriendlyDuration(durationSeconds)
+}
+
+/** Time window for the run's log search. An open `dateTo` (still running, or no usable end
+ * timestamp) lets the LogsViewer default to "now" instead of silently cutting off logs. */
+export function jobLogsWindow(job: DataModelingJob, timezone: string): { dateFrom?: string; dateTo?: string } {
+ const end = jobEndTimestamp(job)
+ return {
+ dateFrom: isParseableDate(job.created_at)
+ ? dayjsUtcToTimezone(job.created_at, timezone).format(LOGS_FILTER_FORMAT)
+ : undefined,
+ dateTo: isParseableDate(end)
+ ? dayjsUtcToTimezone(end, timezone).add(1, 'hour').format(LOGS_FILTER_FORMAT)
+ : undefined,
+ }
+}
diff --git a/frontend/src/scenes/models/NodeDetailMaterialization.tsx b/frontend/src/scenes/models/NodeDetailMaterialization.tsx
index 4415e82ea7c2..9addda15761f 100644
--- a/frontend/src/scenes/models/NodeDetailMaterialization.tsx
+++ b/frontend/src/scenes/models/NodeDetailMaterialization.tsx
@@ -3,8 +3,7 @@ import { useActions, useValues } from 'kea'
import { LemonTable, LemonTag, Tooltip } from '@posthog/lemon-ui'
import { TZLabel } from 'lib/components/TZLabel'
-import { dayjsUtcToTimezone } from 'lib/dayjs'
-import { humanFriendlyDuration } from 'lib/utils/durations'
+import { computeJobDuration, jobLogsWindow } from 'scenes/data-warehouse/saved_queries/materializationJobUtils'
import { LogsViewer } from 'scenes/hog-functions/logs/LogsViewer'
import { teamLogic } from 'scenes/teamLogic'
import { userLogic } from 'scenes/userLogic'
@@ -16,19 +15,6 @@ import { nodeDetailSceneLogic } from './nodeDetailSceneLogic'
const LOG_LEVELS: LogEntryLevel[] = ['LOG', 'INFO', 'WARN', 'WARNING', 'ERROR']
-function computeDuration(job: DataModelingJob): string {
- if (!job.created_at || !job.last_run_at) {
- return '-'
- }
- const start = new Date(job.created_at).getTime()
- const end = new Date(job.last_run_at).getTime()
- const durationSeconds = (end - start) / 1000
- if (durationSeconds <= 0) {
- return '-'
- }
- return humanFriendlyDuration(durationSeconds)
-}
-
export function NodeDetailMaterialization({ id }: { id: string }): JSX.Element | null {
const { materializationJobs, materializationJobsLoading, jobsOffset, savedQuery } = useValues(
nodeDetailSceneLogic({ id })
@@ -74,7 +60,7 @@ export function NodeDetailMaterialization({ id }: { id: string }): JSX.Element |
{
title: 'Duration',
key: 'duration',
- render: (_, job: DataModelingJob) => computeDuration(job),
+ render: (_, job: DataModelingJob) => computeJobDuration(job),
},
{
title: 'Rows',
@@ -112,15 +98,8 @@ export function NodeDetailMaterialization({ id }: { id: string }): JSX.Element |
hideLevelsFilter
hideInstanceIdColumn
defaultFilters={{
- instanceId: job.workflow_run_id,
- dateFrom: dayjsUtcToTimezone(job.created_at, timezone).format(
- 'YYYY-MM-DD HH:mm:ss'
- ),
- dateTo: job.last_run_at
- ? dayjsUtcToTimezone(job.last_run_at, timezone)
- .add(1, 'hour')
- .format('YYYY-MM-DD HH:mm:ss')
- : undefined,
+ instanceId: job.workflow_run_id ?? undefined,
+ ...jobLogsWindow(job, timezone),
levels: showDebugLogs ? ['DEBUG', ...LOG_LEVELS] : LOG_LEVELS,
}}
/>
diff --git a/frontend/src/types.ts b/frontend/src/types.ts
index 31b2bcb294e9..47a3bf5b5db2 100644
--- a/frontend/src/types.ts
+++ b/frontend/src/types.ts
@@ -6333,8 +6333,10 @@ export interface DataModelingJob {
error: string | null
created_at: string
last_run_at: string
+ /** When the job row last changed; for finished jobs this approximates when the run ended. */
+ updated_at?: string
workflow_id: string
- workflow_run_id: string
+ workflow_run_id: string | null
}
export interface SimpleExternalDataSourceSchema {