: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { useMemo } from 'react'
import { LemonSkeleton } from '@posthog/lemon-ui'
import {
BoxPlot,
type BoxPlotConfig,
type BoxPlotDatum,
type BoxPlotSeries,
ChartLegend,
type ChartTheme,
type Series,
type TimeInterval,
TimeSeriesLineChart,
type TimeSeriesLineChartConfig,
createXAxisTickCallback,
legendItemsFromSeries,
} from '@posthog/quill-charts'
import { Button, Toggle, ToggleGroup, ToggleGroupItem } from '@posthog/quill-primitives'
import { useChartConfig } from 'lib/charts/hooks'
import { formatPercentage } from 'lib/utils/numbers'
import { Card, CardState } from '../dashboard/Card'
import { formatMsAsSeconds } from '../dashboard/formatters'
import { type ChartViewChange, type ChartViews, type DailyChartData } from '../mcpAnalyticsToolQualityLogic'
function formatCompactPercentage(value: number): string {
return formatPercentage(value, { compact: true })
}
function buildConfig(
timezone: string,
interval: TimeInterval,
yAxis?: TimeSeriesLineChartConfig['yAxis']
): TimeSeriesLineChartConfig {
return {
curve: 'monotone',
showAxisLines: true,
showTickMarks: true,
showCrosshair: true,
showGrid: true,
yAxis,
xAxis: { interval, timezone },
tooltip: { placement: 'cursor' },
}
}
type ToggleGroupChart = Exclude<ChartViewChange['chart'], 'successRate'>
const CHART_VIEW_ARIA_LABELS: Record<ToggleGroupChart, string> = {
calls: 'Calls and errors chart view',
latency: 'Latency chart view',
}
function ChartViewToggle<C extends Extract<ChartViewChange, { chart: ToggleGroupChart }>>({
chart,
view,
options,
onChange,
}: {
chart: C['chart']
view: C['view']
options: { value: C['view']; label: string }[]
onChange: (change: C) => void
}): JSX.Element {
return (
<ToggleGroup
value={[view]}
onValueChange={(next) => {
const picked = options.find(({ value }) => value === next[0])
if (picked) {
onChange({ chart, view: picked.value } as C)
}
}}
aria-label={CHART_VIEW_ARIA_LABELS[chart]}
>
{options.map(({ value, label }) => (
<ToggleGroupItem
key={value}
value={value}
size="sm"
data-attr={`mcp-tool-quality-${chart}-chart-view-${value}`}
>
{label}
</ToggleGroupItem>
))}
</ToggleGroup>
)
}
function ChartCard({
title,
action,
loading,
isEmpty,
children,
}: {
title: string
action: React.ReactNode
loading: boolean
isEmpty: boolean
children: React.ReactNode
}): JSX.Element {
return (
<Card title={title} action={action}>
<CardState
loading={loading}
isEmpty={isEmpty}
skeleton={<LemonSkeleton className="min-h-[220px] flex-1" />}
empty={
<div className="flex min-h-[220px] flex-1 items-center justify-center text-center text-[12px] text-secondary">
No tool calls for this selection.
</div>
}
>
{/* Dim while refreshing so date/tool changes give visible feedback. */}
<div className={`flex min-h-[220px] flex-1 flex-col transition-opacity ${loading ? 'opacity-50' : ''}`}>
{children}
</div>
</CardState>
</Card>
)
}
export function ToolQualityCharts({
data,
loading,
theme,
timezone,
interval,
incompleteTail,
views,
latencyBoxes,
onViewChange,
}: {
data: DailyChartData
loading: boolean
theme: ChartTheme
timezone: string
interval: TimeInterval
// When true, the final bucket is the current in-progress interval — dash that segment so a
// partial period doesn't read as a fall in calls or an improvement in latency. Required rather
// than optional: an omitted prop silently renders the partial bucket as settled data.
incompleteTail: boolean
views: ChartViews
latencyBoxes: (BoxPlotDatum | null)[]
onViewChange: (change: ChartViewChange) => void
}): JSX.Element {
const isEmpty = data.labels.length === 0
// `incompleteTail` is only true when the window has ≥2 buckets (lastBucketIsInProgress owns that
// rule), and every series is filled to the bucket count, so the final segment exists.
const partialStroke = useMemo(
() => (incompleteTail ? { partial: { fromIndex: data.labels.length - 1 } } : undefined),
[incompleteTail, data.labels.length]
)
const showErrorRate = views.calls === 'percentage'
const callsSeries = useMemo<Series[]>(
() =>
showErrorRate
? [
{
key: 'errorRate',
label: 'Error rate',
color: theme.colors[4],
data: data.errorRate,
stroke: partialStroke,
},
]
: [
{ key: 'calls', label: 'Calls', color: theme.colors[0], data: data.calls, stroke: partialStroke },
{
key: 'errors',
label: 'Errors',
color: theme.colors[4],
data: data.errors,
stroke: partialStroke,
},
],
[data, theme, partialStroke, showErrorRate]
)
const successSeries = useMemo<Series[]>(
() => [
{
key: 'successRate',
label: 'Success rate',
color: theme.colors[2],
data: data.successRate,
stroke: partialStroke,
},
],
[data, theme, partialStroke]
)
const latencySeries = useMemo<Series[]>(
() => [
{ key: 'p50', label: 'p50', color: theme.colors[1], data: data.p50, stroke: partialStroke },
{ key: 'p95', label: 'p95', color: theme.colors[0], data: data.p95, stroke: partialStroke },
{ key: 'p99', label: 'p99', color: theme.colors[4], data: data.p99, stroke: partialStroke },
],
[data, theme, partialStroke]
)
const latencyBoxSeries = useMemo<BoxPlotSeries[]>(
() => [{ key: 'latency', label: 'Latency', color: theme.colors[0], data: latencyBoxes }],
[latencyBoxes, theme]
)
const countsConfig = useChartConfig(() => buildConfig(timezone, interval), [timezone, interval])
const errorRateConfig = useChartConfig(
() => buildConfig(timezone, interval, { tickFormatter: formatCompactPercentage }),
[timezone, interval]
)
const successRateConfig = useChartConfig(
() =>
buildConfig(timezone, interval, {
format: 'percentage',
decimalPlaces: 3,
startAtZero: views.successRate !== 'zoom',
}),
[timezone, interval, views.successRate]
)
const latencyConfig = useChartConfig(
() => buildConfig(timezone, interval, { tickFormatter: formatMsAsSeconds }),
[timezone, interval]
)
const latencyBoxConfig = useChartConfig<BoxPlotConfig>(
() => ({
showGrid: true,
showAxisLines: true,
yTickFormatter: formatMsAsSeconds,
tooltip: { valueFormatter: formatMsAsSeconds },
xTickFormatter: createXAxisTickCallback({ allDays: data.labels, timezone, interval }),
}),
[data.labels, timezone, interval]
)
return (
<div className="@container">
<div className="grid grid-cols-1 gap-4 @5xl:grid-cols-3">
<ChartCard
title={showErrorRate ? 'Error rate' : 'Calls and errors'}
action={
<ChartViewToggle
chart="calls"
view={views.calls}
options={[
{ value: 'counts', label: 'Counts' },
{ value: 'percentage', label: 'Percent' },
]}
onChange={onViewChange}
/>
}
loading={loading}
isEmpty={isEmpty}
>
<TimeSeriesLineChart
series={callsSeries}
labels={data.labels}
config={showErrorRate ? errorRateConfig : countsConfig}
theme={theme}
dataAttr="mcp-tool-quality-calls-chart"
/>
</ChartCard>
<ChartCard
title="Success rate"
action={
<Toggle
size="sm"
pressed={views.successRate === 'zoom'}
onPressedChange={(pressed) =>
onViewChange({ chart: 'successRate', view: pressed ? 'zoom' : 'full' })
}
data-attr="mcp-tool-quality-successRate-chart-view-zoom"
render={(props) => (
<Button
variant="outline"
size="sm"
tooltip="Start the axis just below the lowest value instead of at zero, so small changes are easier to see."
{...props}
/>
)}
>
Zoom in
</Toggle>
}
loading={loading}
isEmpty={isEmpty}
>
<TimeSeriesLineChart
series={successSeries}
labels={data.labels}
config={successRateConfig}
theme={theme}
dataAttr="mcp-tool-quality-success-rate-chart"
/>
</ChartCard>
<ChartCard
title={views.latency === 'boxPlot' ? 'Latency' : 'Latency (p50 / p95 / p99)'}
action={
<ChartViewToggle
chart="latency"
view={views.latency}
options={[
{ value: 'lines', label: 'Trends' },
{ value: 'boxPlot', label: 'Box plot' },
]}
onChange={onViewChange}
/>
}
loading={loading}
isEmpty={isEmpty}
>
{views.latency === 'boxPlot' ? (
<BoxPlot
series={latencyBoxSeries}
labels={data.labels}
config={latencyBoxConfig}
theme={theme}
dataAttr="mcp-tool-quality-latency-box-plot"
/>
) : (
<ChartLegend items={legendItemsFromSeries(latencySeries, theme)} position="top" align="end">
<TimeSeriesLineChart
series={latencySeries}
labels={data.labels}
config={latencyConfig}
theme={theme}
dataAttr="mcp-tool-quality-latency-chart"
/>
</ChartLegend>
)}
</ChartCard>
</div>
</div>
)
}