Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.9 KB
Raw Blame
import { useMemo } from 'react'

import { BarChart } from '@posthog/quill-charts'

import { useChartTheme } from 'lib/charts/hooks'
import { getColorVar } from 'lib/colors'

import { flattenSeriesRows } from './metricsReduce'
import { thresholdColor } from './metricsThresholds'
import { metricValuesFormatter } from './metricsUnits'
import type { MetricsPanelProps } from './registry'
import { resolveReducer } from './registry'

const FALLBACK_COLOR = 'data-color-1'

/** One horizontal bar per series, sorted descending, colored by threshold. Grafana's
 * "top N by value". Needs a groupBy to be meaningful; the picker gates on that. */
export function BarGaugePanel({ series, display, fallbackName }: MetricsPanelProps): JSX.Element {
    const theme = useChartTheme()
    const reducer = resolveReducer(display)

    const rows = useMemo(
        () =>
            flattenSeriesRows(series, [reducer])
                .filter((row) => row.values[reducer] !== null)
                .sort((a, b) => (b.values[reducer] ?? 0) - (a.values[reducer] ?? 0)),
        [series, reducer]
    )

    const chartSeries = useMemo(
        () => [
            {
                key: 'value',
                label: fallbackName,
                data: rows.map((row) => row.values[reducer] ?? 0),
                color: getColorVar(FALLBACK_COLOR),
                // Per-bar threshold colors.
                bars: rows.map((row) => ({
                    color: getColorVar(thresholdColor(row.values[reducer] ?? null, display.thresholds, FALLBACK_COLOR)),
                })),
            },
        ],
        [rows, reducer, fallbackName, display.thresholds]
    )
    // `MetricsPanel` gives every series the same resolved unit, so the first row speaks for the chart.
    const formatValue = useMemo(
        () =>
            metricValuesFormatter(
                rows[0]?.series.unit ?? undefined,
                rows.map((row) => row.values[reducer] ?? 0)
            ),
        [rows, reducer]
    )
    const labels = useMemo(
        () =>
            rows.map(
                (row) =>
                    Object.entries(row.labels)
                        .map(([k, v]) => `${k}=${v}`)
                        .join(', ') ||
                    row.metricName ||
                    fallbackName
            ),
        [rows, fallbackName]
    )

    if (rows.length === 0) {
        return <div className="flex h-full items-center justify-center text-secondary text-sm">No data</div>
    }

    return (
        <div className="relative flex h-full w-full min-h-0 flex-col">
            <BarChart
                series={chartSeries}
                labels={labels}
                theme={theme}
                tooltip={(ctx) => {
                    const row = rows[ctx.dataIndex]
                    if (!row) {
                        return null
                    }
                    return <span>{formatValue(row.values[reducer] ?? 0)}</span>
                }}
            />
        </div>
    )
}