Files
supabase/apps/studio/components/ui/Charts/StackedBarChart.tsx
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

238 lines
7.4 KiB
TypeScript

import { useState } from 'react'
import { Bar, BarChart, Cell, ComposedChart, Legend, Line, Tooltip, XAxis } from 'recharts'
import { ChartHeader } from './ChartHeader'
import {
CHART_COLORS,
DateTimeFormats,
DEFAULT_STACK_COLORS,
genStackColorScales,
ValidStackColor,
} from './Charts.constants'
import type { CommonChartProps } from './Charts.types'
import { numberFormatter, precisionFormatter, useChartSize, useStacked } from './Charts.utils'
import { NoDataPlaceholder } from './NoDataPlaceholder'
import { useChartHoverState } from './useChartHoverState'
import { formatDateTime, useFormatDateTime } from '@/lib/datetime'
interface Props extends CommonChartProps<any> {
xAxisKey: string
yAxisKey: string
stackKey: string
onBarClick?: () => void
variant?: 'values' | 'percentages'
xAxisFormatAsDate?: boolean
displayDateInUtc?: boolean
hideLegend?: boolean
hideHeader?: boolean
stackColors?: ValidStackColor[]
chartStyle?: 'bar' | 'line'
syncId?: string
}
const StackedBarChart: React.FC<Props> = ({
size,
data,
xAxisKey,
stackKey,
yAxisKey,
customDateFormat = DateTimeFormats.FULL,
title,
highlightedValue,
highlightedLabel,
format,
minimalHeader = false,
valuePrecision,
onBarClick,
variant,
xAxisFormatAsDate = true,
displayDateInUtc,
hideLegend = false,
hideHeader = false,
stackColors = DEFAULT_STACK_COLORS,
syncId,
chartStyle = 'bar',
}) => {
const Chart = chartStyle === 'line' ? ComposedChart : BarChart
const { Container } = useChartSize(size)
const { hoveredIndex, syncTooltip, setHover, clearHover } = useChartHoverState(
syncId || 'default'
)
const { dataKeys, stackedData, percentagesStackedData } = useStacked({
data,
xAxisKey,
stackKey,
yAxisKey,
variant,
})
const [focusDataIndex, setFocusDataIndex] = useState<number | null>(null)
// When `displayDateInUtc` is set the chart explicitly wants UTC labels.
// Otherwise honour the user's selected timezone via the picker.
const formatPickerDate = useFormatDateTime()
const formatChartDate = (value: number | string) =>
displayDateInUtc
? formatDateTime(value, { tz: 'UTC', format: customDateFormat })
: formatPickerDate(value, customDateFormat)
const resolvedHighlightedLabel =
(focusDataIndex !== null &&
data &&
data[focusDataIndex] !== undefined &&
formatChartDate(data[focusDataIndex][xAxisKey])) ||
highlightedLabel
const resolvedHighlightedValue =
focusDataIndex !== null ? data[focusDataIndex]?.[yAxisKey] : highlightedValue
if (!data || data.length === 0) {
return (
<NoDataPlaceholder
description="It may take up to 24 hours for data to refresh"
size={size}
attribute={title}
format={format}
/>
)
}
const stackColorScales = genStackColorScales(stackColors)
return (
<div className="w-full">
{!hideHeader && (
<ChartHeader
title={title}
format={format}
customDateFormat={customDateFormat}
minimalHeader={minimalHeader}
highlightedValue={
typeof resolvedHighlightedValue === 'number'
? numberFormatter(resolvedHighlightedValue, valuePrecision)
: resolvedHighlightedValue
}
highlightedLabel={resolvedHighlightedLabel}
syncId={syncId}
data={data}
xAxisKey={xAxisKey}
yAxisKey={yAxisKey}
xAxisIsDate={xAxisFormatAsDate}
displayDateInUtc={displayDateInUtc}
valuePrecision={valuePrecision}
attributes={[]}
/>
)}
<Container>
<Chart
data={variant === 'percentages' ? percentagesStackedData : stackedData}
margin={{
top: 20,
right: 20,
left: 20,
bottom: 5,
}}
className="cursor-pointer overflow-visible"
// mouse hover focusing logic
onMouseMove={(e: any) => {
if (e.activeTooltipIndex !== focusDataIndex) {
setFocusDataIndex(e.activeTooltipIndex)
}
setHover(e.activeTooltipIndex)
}}
onMouseLeave={() => {
setFocusDataIndex(null)
clearHover()
}}
>
{!hideLegend && (
<Legend
wrapperStyle={{ top: -6, fontSize: '0.8rem' }}
iconSize={8}
iconType="circle"
verticalAlign="top"
/>
)}
<XAxis
dataKey={xAxisKey}
interval={data.length - 2}
angle={0}
tick={false}
axisLine={{ stroke: CHART_COLORS.AXIS }}
tickLine={{ stroke: CHART_COLORS.AXIS }}
/>
{chartStyle === 'line' &&
dataKeys.map((datum, stackIndex) => (
<Line
key={datum}
dataKey={datum}
type="linear"
legendType="circle"
stroke={stackColorScales[stackIndex].base}
dot={false}
animationDuration={300}
/>
))}
{chartStyle === 'bar' &&
dataKeys.map((datum, stackIndex) => (
<Bar
key={stackIndex}
dataKey={datum}
type="monotone"
legendType="circle"
fill={stackColorScales[stackIndex].base}
stackId={1}
animationDuration={300}
maxBarSize={48}
className={onBarClick ? 'cursor-pointer' : ''}
>
{stackedData?.map((_entry: unknown, index: any) => (
<Cell
key={`cell-${index}`}
className={`transition-all duration-300`}
opacity={focusDataIndex === index ? 0.85 : 1}
/>
))}
</Bar>
))}
<Tooltip
labelFormatter={
xAxisFormatAsDate ? (label) => formatChartDate(label as number | string) : undefined
}
formatter={(value, name, props) => {
const suffix = format || ''
if (variant === 'percentages' && percentagesStackedData) {
const index = percentagesStackedData.findIndex(
(pStack) => pStack === props.payload!
)
const val = stackedData[index][name]
const percentage = precisionFormatter(Number(value) * 100, 1) + '%'
return `${percentage} (${val}${suffix})`
}
return String(value) + suffix
}}
cursor={false}
labelClassName="text-white"
contentStyle={{
backgroundColor: '#444444',
borderColor: '#444444',
fontSize: '12px',
}}
wrapperClassName="bg-gray-600 rounded-sm min-w-md"
active={!!syncId && syncTooltip && hoveredIndex !== null}
/>
</Chart>
</Container>
{stackedData && stackedData[0] && (
<div className="text-foreground-lighter -mt-5 flex items-center justify-between text-xs">
<span>{formatChartDate(stackedData[0][xAxisKey] as number | string)}</span>
<span>
{formatChartDate(stackedData[stackedData?.length - 1][xAxisKey] as number | string)}
</span>
</div>
)}
</div>
)
}
export default StackedBarChart