Files
supabase/apps/studio/components/interfaces/Observability/useServiceHealthMetrics.utils.ts
Jordi Enric 40bb24353d fix(studio): keep service health windows within DST limits FE-4928 (#53027)
## Problem

The home dashboard's 7-day charts can fail after daylight saving time
ends. Subtracting seven local calendar days produces a 169-hour request,
exceeding the service-health endpoint's 168-hour limit. The same
calculation also changes the 24-hour window across DST transitions.

## Fix

Calculate the shared service-health windows using 24 and 168 elapsed
hours. Add regression coverage for both daylight saving transitions in
America/New_York.

Fixes
[FE-4928](https://linear.app/supabase/issue/FE-4928/re-dashboard-is-not-showing-correct-data).

## How to test

- Run `pnpm --dir apps/studio exec vitest run --retry=0
components/interfaces/Observability/useServiceHealthMetrics.utils.test.ts`.
- In a browser using America/New_York around the November 1 DST
transition, open a project's home dashboard and select the 7-day view.
- Expected result: the service-health request spans exactly 168 hours
and the charts load. The 24-hour view spans exactly 24 hours across
either DST transition.

Local verification: Studio typecheck, focused ESLint, Prettier, and 41
affected tests pass. All four DST regressions fail against the original
helper and pass with the fix; shuffled tests also pass with seed 4928
and TZ=UTC.
2026-11-05 15:51:39 +01:00

105 lines
2.8 KiB
TypeScript

import dayjs from 'dayjs'
import type { LogsBarChartDatum } from '../ProjectHome/ProjectUsage.metrics'
import {
computeSuccessAndNonSuccessRates,
sumErrors,
sumTotal,
sumWarnings,
} from '../ProjectHome/ProjectUsage.metrics'
/**
* Calculates the date range for fetching service health metrics
* based on the selected interval
*/
export const calculateDateRange = (
interval: '1hr' | '1day' | '7day'
): { startDate: string; endDate: string } => {
const now = dayjs()
const end = now.toISOString()
let start: string
switch (interval) {
case '1hr':
start = now.subtract(1, 'hour').toISOString()
break
case '1day':
start = now.subtract(24, 'hour').toISOString()
break
case '7day':
start = now.subtract(168, 'hour').toISOString()
break
default:
start = now.subtract(1, 'hour').toISOString()
}
return { startDate: start, endDate: end }
}
export type RawChartData = {
timestamp: string | number
ok_count?: number | null
warning_count?: number | null
error_count?: number | null
[key: string]: string | number | null | undefined
}
/**
* Transforms raw chart query results to LogsBarChartDatum format
*/
export const transformToBarChartData = (data: RawChartData[]): LogsBarChartDatum[] => {
return data.map((row) => ({
timestamp: typeof row.timestamp === 'string' ? row.timestamp : String(row.timestamp),
ok_count: row.ok_count ?? 0,
warning_count: row.warning_count ?? 0,
error_count: row.error_count ?? 0,
}))
}
/**
* Calculates health metrics from bar chart data
*/
export const calculateHealthMetrics = (eventChartData: LogsBarChartDatum[]) => {
const total = sumTotal(eventChartData)
const errorCount = sumErrors(eventChartData)
const warningCount = sumWarnings(eventChartData)
const okCount = total - errorCount - warningCount
const errorRate = total > 0 ? (errorCount / total) * 100 : 0
const { successRate } = computeSuccessAndNonSuccessRates(total, warningCount, errorCount)
return {
total,
errorRate,
successRate,
errorCount,
warningCount,
okCount,
}
}
/**
* Calculates aggregated metrics across all services
*/
export const calculateAggregatedMetrics = (
services: {
total: number
errorCount: number
warningCount: number
}[]
) => {
const totalRequests = services.reduce((sum, s) => sum + s.total, 0)
const totalErrors = services.reduce((sum, s) => sum + s.errorCount, 0)
const totalWarnings = services.reduce((sum, s) => sum + s.warningCount, 0)
const { successRate: overallSuccessRate, nonSuccessRate: overallErrorRate } =
computeSuccessAndNonSuccessRates(totalRequests, totalWarnings, totalErrors)
return {
totalRequests,
totalErrors,
totalWarnings,
overallErrorRate,
overallSuccessRate,
}
}