Files
supabase/apps/studio/data/warehouse/warehouse-metrics-query.ts
Danny WhiteandJoshen Lim 48c90094d9 feat(studio): show Warehouse query activity in Observability (#53495)
## Problem

Warehouse query activity is not available in Observability.

## Solution

Add a Warehouse report showing query rate over the last hour, with
loading, setup, no-data and request-error states. The report uses live
project metrics and appears in navigation for projects with Warehouse
access.

| After |
| --- |
| <img width="2548" height="1416" alt="CleanShot 2026-11-17 at 16 55
33@2x"
src="https://github.com/user-attachments/assets/4d6796e0-72f1-45d5-b6b7-2cd4a3577496"
/> |
| <img width="2552" height="1390" alt="CleanShot 2026-11-17 at 16 56
18@2x"
src="https://github.com/user-attachments/assets/d96efdfd-4ca9-42fe-ac30-9cb4667c95d7"
/> |
| <img width="2544" height="1388" alt="CleanShot 2026-11-17 at 16 32
11@2x"
src="https://github.com/user-attachments/assets/bc80af8d-0468-4229-98d0-ac2681823059"
/> |
| <img width="2560" height="1344" alt="CleanShot 2026-11-17 at 16 59
53@2x"
src="https://github.com/user-attachments/assets/1ed70348-c2fc-4673-85a7-530d9734886c"
/> |


Visitor | What they see
-- | --
Signed-in customer without Warehouse access | No Warehouse entry in
Observability
Customer with Warehouse access, not configured | “Warehouse is not set
up” empty state and setup button
Customer with Warehouse disabled | “Warehouse is disabled” empty state
and setup button
Customer with active Warehouse | Query rate chart, or the relevant
loading/empty/error state



## Review instructions

1. Open the [Studio staging
preview](https://studio-staging-git-dnywh-warehouse-metrics-client-supabase.vercel.app)
with a staging project that has Warehouse access.
2. Go to **Observability > Warehouse**, or
`/project/<ref>/observability/warehouse`
([example](https://studio-staging-git-dnywh-warehouse-metrics-client-supabase.vercel.app/dashboard/project/yirnfrgkxmnqgexikhfc/observability/warehouse)).
3. Check the query-rate chart after running Warehouse queries. A project
without measurements shows an explicit last-hour empty state.
4. Open the report on a project where Warehouse is not set up and check
the setup link.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-11-19 14:57:39 +11:00

78 lines
2.4 KiB
TypeScript

import { queryOptions } from '@tanstack/react-query'
import { warehouseKeys } from './keys'
import type {
WarehouseMetricName,
WarehouseMetricsInterval,
WarehouseMetricsResponse,
} from './warehouse-metrics-api-types'
import { get, handleError } from '@/data/fetchers'
import { IS_PLATFORM } from '@/lib/constants'
import type { StudioError } from '@/lib/errors/studio-errors'
const MAX_ATTRIBUTES = 8
const MAX_RETRIES = 2
export type WarehouseMetricsVariables = {
projectRef?: string
attributes: readonly WarehouseMetricName[]
startDate: string
endDate: string
interval: WarehouseMetricsInterval
enabled?: boolean
}
export type WarehouseMetricsData = WarehouseMetricsResponse
export type WarehouseMetricsError = StudioError
async function getWarehouseMetrics(
{ projectRef, attributes, startDate, endDate, interval }: WarehouseMetricsVariables,
signal?: AbortSignal
): Promise<WarehouseMetricsData> {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await get('/platform/warehouse/{ref}/metrics', {
params: {
path: { ref: projectRef },
query: { attributes: [...attributes], startDate, endDate, interval },
},
signal,
})
if (error) handleError(error)
return data
}
/** Retry throttling and server errors at most twice. */
export const shouldRetryWarehouseMetrics = (failureCount: number, error: StudioError) =>
failureCount < MAX_RETRIES &&
(error.code === 429 || (typeof error.code === 'number' && error.code >= 500))
export const warehouseMetricsQueryOptions = ({
projectRef,
attributes,
startDate,
endDate,
interval,
enabled = true,
}: WarehouseMetricsVariables) => {
return queryOptions({
queryKey: warehouseKeys.metrics(projectRef, { attributes, startDate, endDate, interval }),
queryFn: ({ signal }) =>
getWarehouseMetrics({ projectRef, attributes, startDate, endDate, interval }, signal),
enabled:
enabled &&
IS_PLATFORM &&
!!projectRef &&
attributes.length > 0 &&
attributes.length <= MAX_ATTRIBUTES,
retry: (failureCount: number, error: WarehouseMetricsError) =>
shouldRetryWarehouseMetrics(failureCount, error),
retryDelay: (attemptIndex: number, error: WarehouseMetricsError) => {
if (error.retryAfter !== undefined) return error.retryAfter * 1000
return Math.min(1000 * 2 ** attemptIndex, 8000) + Math.random() * 500
},
refetchOnWindowFocus: false,
})
}