Files
supabase/apps/studio/components/interfaces/Observability/Warehouse/WarehouseMetrics.range.ts
Danny WhiteandJoshen Lim 8fcd5e7f65 feat(studio): add Warehouse report date controls (#53494)
## Problem

The Warehouse report only shows the last hour.

## Solution

Add date selection and refresh using the shared report controls. Respect
the organisation’s log retention limits and the metrics API’s 30-day
maximum.

| After |
| --- |
| <img width="1688" height="1050" alt="CleanShot 2026-11-18 at 11 35
24@2x"
src="https://github.com/user-attachments/assets/88267bf8-1054-480b-ac26-c0c2b58386bc"
/> |
| _The new controls._ |
| <img width="2420" height="1028" alt="CleanShot 2026-11-18 at 11 47
52@2x"
src="https://github.com/user-attachments/assets/383c06a0-5470-4748-b9a6-fe80647a1399"
/> |
| _The standard retention upgrade prompt follow tapping either of these
on a Pro plan._ |

## Review instructions

1. Open the [Studio staging
preview](https://studio-staging-git-dnywh-warehouse-metrics-range-supabase.vercel.app)
and select a staging project with Warehouse access.
2. Open **Observability > Warehouse**, or
`/project/<ref>/observability/warehouse`
([example](https://studio-staging-git-dnywh-warehouse-metrics-range-supabase.vercel.app/dashboard/project/yirnfrgkxmnqgexikhfc/observability/warehouse)).
Change Last 60 minutes to Last 24 hours and check that the chart or
no-data state reflects the selected period.
3. Refresh the report. Check that the selected period stays selected.
4. On a paid staging organisation, select a period beyond its log
retention (for example, Last 14 days with 7-day retention). Check that
the retention upgrade prompt appears and the current period stays
selected.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-11-19 15:08:18 +11:00

57 lines
1.7 KiB
TypeScript

import dayjs, { type Dayjs } from 'dayjs'
const WAREHOUSE_METRICS_MAX_RANGE_DAYS = 30
/** Keeps every series under ~720 points so a batch with many labeled series stays under the API's response cap. */
export const getWarehouseMetricsInterval = (
startIso: string,
endIso: string
): '1m' | '5m' | '30m' | '1h' => {
const hours = dayjs(endIso).diff(dayjs(startIso), 'hour', true)
if (hours <= 3) return '1m'
if (hours <= 24) return '5m'
if (hours <= 24 * 5) return '30m'
return '1h'
}
export type WarehouseMetricsRange =
| {
status: 'ok'
startDate: string
endDate: string
interval: ReturnType<typeof getWarehouseMetricsInterval>
isClamped: boolean
}
| { status: 'out_of_range' }
export const resolveWarehouseMetricsRange = ({
start,
end,
now = dayjs(),
}: {
start: string
end: string
now?: Dayjs
}): WarehouseMetricsRange => {
const latest = now.startOf('minute')
// Counted in hours, not calendar days, so a DST change doesn't shift the bound
// A few minutes of slack so the oldest bound doesn't fall out of range while the page is open
const earliest = latest.subtract(WAREHOUSE_METRICS_MAX_RANGE_DAYS * 24, 'hour').add(5, 'minute')
const isStartTooEarly = dayjs(start).isBefore(earliest)
const isEndInFuture = dayjs(end).isAfter(now)
const startDate = (isStartTooEarly ? earliest : dayjs(start)).toISOString()
const endDate = (isEndInFuture ? latest : dayjs(end)).toISOString()
if (!dayjs(endDate).isAfter(dayjs(startDate))) return { status: 'out_of_range' }
return {
status: 'ok',
startDate,
endDate,
interval: getWarehouseMetricsInterval(startDate, endDate),
isClamped: isStartTooEarly || isEndInFuture,
}
}