Files
7f7ebf527b feat(studio): use shared logs table for recent function errors (#50958)
The Overview's "Errors in the last 24h" section now uses the Logs tab's
queries (#50954) and the read-only `EmbeddedLogsTable` (#50953) instead
of its own SQL and table. It previews the latest five errors since the
last deploy, capped at 24h, shows "+N more" when there are more and
links to the rest. It's behind the `edgeFunctionsOverview` flag, like
the rest of the new Overview.

- Rows have no multi-select checkbox. Clicking one opens it in the Logs
tab with the same range and error filter, and "View logs" and "+N more"
open the filtered list.
- With the unified logs preview off, those links open the pre-unified
Invocations tab (for invocation rows) or Logs tab (for runtime rows),
using those tabs' own `its`/`ite`/`log` params.
- "Explain with AI" and "Copy summary" send the previewed errors to the
Assistant with the same prompt as selected logs on the Logs page. The
prompt also notes how many errors were left out.
- One count query, scoped to the function and unfiltered, gives both the
error count behind "+N more" and the invocations since the deploy, from
its level and log type facets. It replaces the hand-built invocation
count SQL, so the section runs two queries instead of three.
- The row query fetches only the five errors shown, through a new
`pageSize` option on `useUnifiedLogsInfiniteQuery`.
- The section uses the edge function preset from #50957, so it runs the
Logs tab's scoped queries and shares its saved columns.
- The custom error grouping and its SQL are removed. Net, the PR removes
about 390 lines.

### How to test

1. With `edgeFunctionsOverview` on, open a function that has recent
errors. You should see up to five rows without checkboxes, and "+N more"
when there are more.
2. Click a row with the unified logs preview on. It opens the Logs tab
with that log selected. Turn the preview off and click an invocation row
and a runtime row. They open Invocations and Logs respectively.
3. Use Explain with AI and Copy summary and check the prompt.
4. For a function with no errors since the last deploy, the section
shows the invocation count instead.

### Validation

- MSW component tests: preview rows and "+N more", no checkboxes, the
summary prompt, row links with the preview on and off, and the no-errors
state. They also check the row query asks for five rows and that a
single unfiltered count runs. `pageSize` and the facet count helper have
unit tests.
- Studio typecheck, lint ratchet, Prettier and knip pass on this layer.

### Stack

Merge in order:

1. #50953 — reusable logs table building blocks
2. #50954 — function-scoped unified logs queries
3. #50955 — slimmer function detail layout, header on Overview
4. #50957 — Invocations merged into a function-scoped Logs tab
5. #50958 — Overview errors in the shared logs table
6. #52281 — remove the legacy Overview

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Jordi Enric <jordi.err@gmail.com>
2026-10-27 16:47:51 +01:00

410 lines
12 KiB
TypeScript

import dayjs from 'dayjs'
import maxBy from 'lodash/maxBy'
import meanBy from 'lodash/meanBy'
import sumBy from 'lodash/sumBy'
import type { ChartConfig } from 'ui'
import { calculateBarClickTimeRange } from '@/components/interfaces/Settings/Logs/LogsBarChart.utils'
import type { UnifiedLogsScope } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.types'
import { serializeUnifiedLogsSearchParams } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.utils'
import type { ChartIntervals } from '@/types'
export type EdgeFunctionChartRawDatum = {
timestamp: string | number
success_count?: string | number
redirect_count?: string | number
client_err_count?: string | number
server_err_count?: string | number
avg_execution_time?: string | number
max_execution_time?: string | number
avg_cpu_time_used?: string | number
max_cpu_time_used?: string | number
avg_memory_used?: string | number
avg_heap_memory_used?: string | number
avg_external_memory_used?: string | number
}
export type EdgeFunctionChartDatum = {
timestamp: string
success_count: number
redirect_count: number
client_err_count: number
server_err_count: number
avg_execution_time: number
max_execution_time: number
avg_cpu_time_used: number
max_cpu_time_used: number
avg_memory_used: number
avg_heap_memory_used: number
avg_external_memory_used: number
}
export type InvocationChartDatum = {
timestamp: string
ok_count: number
warning_count: number
error_count: number
}
export type InvocationUpdateAnnotation = {
timestamp: string
position: number
updatedAt: Date
}
export const EDGE_FUNCTION_CHART_INTERVALS: ChartIntervals[] = [
{
key: '15min',
label: '15 min',
startValue: 15,
startUnit: 'minute',
format: 'MMM D, h:mm:ssa',
},
{
key: '1hr',
label: '1 hour',
startValue: 1,
startUnit: 'hour',
format: 'MMM D, h:mma',
},
{
key: '3hr',
label: '3 hours',
startValue: 3,
startUnit: 'hour',
format: 'MMM D, h:mma',
},
{
key: '1day',
label: '1 day',
startValue: 1,
startUnit: 'day',
format: 'MMM D, h:mma',
},
]
export const INVOCATION_CHART_CONFIG = {
ok_count: {
label: 'Ok',
color: 'var(--primary-bright)',
},
warning_count: {
label: 'Warnings',
color: 'var(--chart-status-warning)',
},
error_count: {
label: 'Errors',
color: 'hsl(var(--destructive-default))',
},
} satisfies ChartConfig
export const CPU_TIME_CHART_CONFIG = {
max_cpu_time_used: {
label: 'Max CPU Time',
color: 'var(--primary-bright)',
},
} satisfies ChartConfig
export const EXECUTION_TIME_CHART_CONFIG = {
avg_execution_time: {
label: 'Average Execution Time',
color: 'var(--foreground-default)',
},
max_execution_time: {
label: 'Max Execution Time',
color: 'var(--primary-bright)',
},
} satisfies ChartConfig
export const MEMORY_CHART_CONFIG = {
avg_memory_used: {
label: 'Memory Usage',
color: 'var(--primary-bright)',
},
} satisfies ChartConfig
const toManipulateUnit = (unit: ChartIntervals['startUnit']) => unit as dayjs.ManipulateType
const toNumber = (value: string | number | undefined) => Number(value ?? 0)
export const getRollingTimeRange = (
interval: ChartIntervals,
now: Date = new Date()
): [Date, Date] => {
const currentTime = dayjs(now)
const start = currentTime.subtract(interval.startValue, toManipulateUnit(interval.startUnit))
return [start.toDate(), currentTime.toDate()]
}
export const getInvocationChartNavigationUrl = ({
projectRef,
functionSlug,
isUnifiedLogsEnabled,
rangeStart,
rangeEnd,
clickedTimestamp,
}: {
projectRef: string
functionSlug: string
isUnifiedLogsEnabled: boolean
rangeStart: string
rangeEnd: string
clickedTimestamp: string
}) => {
const { start, end } = calculateBarClickTimeRange(rangeStart, rangeEnd, clickedTimestamp)
if (isUnifiedLogsEnabled) {
return getEdgeFunctionLogsUrl({ projectRef, functionSlug, start, end })
}
return getLegacyEdgeFunctionLogsUrl({ projectRef, functionSlug, tab: 'invocations', start, end })
}
export const getEdgeFunctionLogsScope = (functionId: string): UnifiedLogsScope => ({
filters: [
{ column: 'log_type', operator: '=', value: 'edge function' },
{ column: 'log_type', operator: '=', value: 'edge function runtime' },
{ column: 'function_id', operator: '=', value: functionId },
],
})
export const EDGE_FUNCTION_LOGS_VIEW = {
offeredFilters: ['date', 'log_type', 'level', 'event_message'],
columnPreferencesKey: 'edge-function',
} as const
export const getFunctionLogsDateRange = (start?: string, end?: string): Date[] | null => {
if (!start || !end) return null
const from = new Date(start)
const to = new Date(end)
if (!Number.isFinite(from.getTime()) || !Number.isFinite(to.getTime()) || from > to) return null
return [from, to]
}
export const getEdgeFunctionLogsUrl = ({
projectRef,
functionSlug,
start,
end,
filters,
logId,
}: {
projectRef: string
functionSlug: string
start?: string
end?: string
filters?: readonly string[]
logId?: string
}) =>
serializeUnifiedLogsSearchParams(`/project/${projectRef}/functions/${functionSlug}/logs`, {
filter: filters?.length ? [...filters] : null,
date: getFunctionLogsDateRange(start, end),
id: logId ?? null,
})
export const getLegacyEdgeFunctionLogsUrl = ({
projectRef,
functionSlug,
tab,
start,
end,
logId,
}: {
projectRef: string
functionSlug: string
tab: 'invocations' | 'logs'
start?: string
end?: string
logId?: string
}) => {
const params = new URLSearchParams()
if (start && end) {
params.set('its', start)
params.set('ite', end)
}
if (logId) params.set('log', logId)
const query = params.toString()
return `/project/${projectRef}/functions/${functionSlug}/${tab}${query ? `?${query}` : ''}`
}
export function getFunctionOverviewLogsUrl({
projectRef,
functionSlug,
start,
end,
logType,
isUnifiedLogsEnabled,
}: {
projectRef: string
functionSlug: string
start: string
end: string
logType: 'edge function' | 'edge function runtime'
isUnifiedLogsEnabled: boolean
}) {
if (isUnifiedLogsEnabled) {
return getEdgeFunctionLogsUrl({
projectRef,
functionSlug,
start,
end,
filters: [`log_type:eq:${logType}`],
})
}
const destination = logType === 'edge function' ? 'invocations' : 'logs'
return `/project/${projectRef}/functions/${functionSlug}/${destination}?its=${start}`
}
export const formatChartTimestamp = (value: Date | string | number | undefined, format: string) => {
return dayjs(value === undefined ? '' : value).format(format)
}
export const getChartTimeRangeLabels = (
data: Array<{ timestamp: string }>,
format: string
): { start: string; end: string } | undefined => {
if (data.length === 0) return undefined
return {
start: formatChartTimestamp(data[0]?.timestamp, format),
end: formatChartTimestamp(data[data.length - 1]?.timestamp, format),
}
}
export const toEdgeFunctionChartData = (
rows: EdgeFunctionChartRawDatum[] = []
): EdgeFunctionChartDatum[] =>
rows.map((row) => ({
timestamp: String(row.timestamp ?? ''),
success_count: toNumber(row.success_count),
redirect_count: toNumber(row.redirect_count),
client_err_count: toNumber(row.client_err_count),
server_err_count: toNumber(row.server_err_count),
avg_execution_time: toNumber(row.avg_execution_time),
max_execution_time: toNumber(row.max_execution_time),
avg_cpu_time_used: toNumber(row.avg_cpu_time_used),
max_cpu_time_used: toNumber(row.max_cpu_time_used),
avg_memory_used: toNumber(row.avg_memory_used),
avg_heap_memory_used: toNumber(row.avg_heap_memory_used),
avg_external_memory_used: toNumber(row.avg_external_memory_used),
}))
export const getInvocationChartData = (data: EdgeFunctionChartDatum[]): InvocationChartDatum[] =>
data.map((datum) => ({
timestamp: datum.timestamp,
ok_count: datum.success_count,
warning_count: datum.redirect_count + datum.client_err_count,
error_count: datum.server_err_count,
}))
export const getInvocationTotals = (data: InvocationChartDatum[]) => {
const totalInvocationCount = sumBy(data, (datum) => {
return datum.ok_count + datum.warning_count + datum.error_count
})
return {
totalInvocationCount,
totalWarningCount: sumBy(data, 'warning_count'),
totalErrorCount: sumBy(data, 'error_count'),
}
}
export const getExecutionMetrics = (data: EdgeFunctionChartDatum[]) => ({
averageExecutionTime: meanBy(data, 'avg_execution_time') ?? 0,
maxExecutionTime: maxBy(data, 'max_execution_time')?.max_execution_time ?? 0,
})
export const getUsageMetrics = (data: EdgeFunctionChartDatum[]) => {
const totalHeapMemory = sumBy(data, 'avg_heap_memory_used')
const totalExternalMemory = sumBy(data, 'avg_external_memory_used')
return {
averageCpuTime: meanBy(data, 'avg_cpu_time_used') ?? 0,
maxCpuTime: maxBy(data, 'max_cpu_time_used')?.max_cpu_time_used ?? 0,
averageMemoryUsage: meanBy(data, 'avg_memory_used') ?? 0,
totalHeapMemory,
totalExternalMemory,
totalMemoryByType: totalHeapMemory + totalExternalMemory,
}
}
export const getInvocationUpdateAnnotation = ({
updatedAt,
invocationChartData,
windowStart,
windowEnd,
}: {
updatedAt?: number | string
invocationChartData: InvocationChartDatum[]
windowStart: Date
windowEnd: Date
}): InvocationUpdateAnnotation | undefined => {
if (updatedAt === undefined || invocationChartData.length === 0) return undefined
const updatedAtDate = new Date(updatedAt)
const updatedAtValue = updatedAtDate.valueOf()
if (Number.isNaN(updatedAtValue)) return undefined
if (updatedAtValue < windowStart.valueOf() || updatedAtValue > windowEnd.valueOf()) {
return undefined
}
const closestTimestamp = invocationChartData.reduce((closest, datum) => {
const datumDistance = Math.abs(new Date(datum.timestamp).valueOf() - updatedAtValue)
const closestDistance = Math.abs(new Date(closest.timestamp).valueOf() - updatedAtValue)
return datumDistance < closestDistance ? datum : closest
}).timestamp
const markerIndex = invocationChartData.findIndex((datum) => datum.timestamp === closestTimestamp)
if (markerIndex < 0) return undefined
return {
timestamp: closestTimestamp,
position: ((markerIndex + 0.5) / invocationChartData.length) * 100,
updatedAt: updatedAtDate,
}
}
export const getSegmentedButtonClassName = (index: number, total: number) => {
if (index === 0) return 'rounded-tr-none rounded-br-none'
if (index === total - 1) return 'rounded-tl-none rounded-bl-none'
return 'rounded-none'
}
export const getChartEmptyStateCopy = (
subject: string,
isError: boolean,
errorMessage?: string
) => ({
title: isError ? `Unable to load ${subject}` : 'No data to show',
description: isError ? errorMessage : undefined,
})
export const formatMetric = (value?: number, unit?: string) => {
if (value === undefined || Number.isNaN(value)) return unit ? `0${unit}` : '0'
const formatted = unit === 'MB' ? value.toFixed(1) : Math.round(value).toLocaleString('en-US')
return unit ? `${formatted}${unit}` : formatted
}
export const formatRate = (count: number, total: number) =>
new Intl.NumberFormat('en-US', {
style: 'percent',
maximumFractionDigits: 1,
}).format(total === 0 ? 0 : count / total)
export const formatReferenceDelta = (value: number, reference: number, label = 'average') => {
const difference = value - reference
if (Math.abs(difference) < Number.EPSILON) return `At ${label}`
if (reference === 0) return `${difference > 0 ? 'Above' : 'Below'} ${label}`
const percentDifference = Math.round(Math.abs((difference / reference) * 100))
return `${percentDifference}% ${difference > 0 ? 'above' : 'below'} ${label}`
}
export const getMemoryTooltipDetail = (heapMemory: number, externalMemory: number) => {
return `Heap ${formatMetric(heapMemory, 'MB')} • External ${formatMetric(externalMemory, 'MB')}`
}