import type { ProviderLatencyHistogramResponse } from "@/lib/types/logs";
import { useMemo } from "react";
import { Area, AreaChart, Bar, BarChart, CartesianGrid, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts";
import { formatFullTimestamp, formatLatency, formatTimestamp, getModelColor, LATENCY_COLORS } from "../../utils/chartUtils";
import { ChartErrorBoundary } from "./chartErrorBoundary";
import type { ChartType } from "./chartTypeToggle";
interface ProviderLatencyChartProps {
data: ProviderLatencyHistogramResponse | null;
chartType: ChartType;
startTime: number;
endTime: number;
selectedProvider: string;
}
function AllProvidersTooltip({ active, payload, providers }: any) {
if (!active || !payload || !payload.length) return null;
const data = payload[0]?.payload;
if (!data) return null;
return (
{formatFullTimestamp(data.timestamp)}
{providers.map((provider: string, idx: number) => {
const stats = data.by_provider?.[provider];
if (!stats || stats.avg_latency === 0) return null;
return (
{provider}
{formatLatency(stats.avg_latency)}
);
})}
);
}
function SingleProviderTooltip({ active, payload }: any) {
if (!active || !payload || !payload.length) return null;
const data = payload[0]?.payload;
if (!data) return null;
return (
{formatFullTimestamp(data.timestamp)}
Avg
{formatLatency(data.avg_latency)}
P90
{formatLatency(data.p90_latency)}
P95
{formatLatency(data.p95_latency)}
P99
{formatLatency(data.p99_latency)}
Requests
{data.total_requests?.toLocaleString() || 0}
);
}
export function ProviderLatencyChart({ data, chartType, startTime, endTime, selectedProvider }: ProviderLatencyChartProps) {
const { chartData, mode, displayProviders } = useMemo(() => {
if (!data?.buckets || !data.bucket_size_seconds) {
return { chartData: [], mode: "all" as const, displayProviders: [] };
}
const isSingleProvider = selectedProvider !== "all";
const providers = isSingleProvider ? [selectedProvider] : data.providers;
const processed = data.buckets.map((bucket, index) => {
const item: any = {
...bucket,
index,
formattedTime: formatTimestamp(bucket.timestamp, data.bucket_size_seconds),
};
if (isSingleProvider) {
const stats = bucket.by_provider?.[selectedProvider];
item.avg_latency = stats?.avg_latency || 0;
item.p90_latency = stats?.p90_latency || 0;
item.p95_latency = stats?.p95_latency || 0;
item.p99_latency = stats?.p99_latency || 0;
item.total_requests = stats?.total_requests || 0;
} else {
providers.forEach((provider, idx) => {
item[`provider_${idx}`] = bucket.by_provider?.[provider]?.avg_latency || 0;
});
}
return item;
});
return { chartData: processed, mode: isSingleProvider ? ("single" as const) : ("all" as const), displayProviders: providers };
}, [data, selectedProvider]);
if (!data?.buckets || chartData.length === 0) {
return No data available
;
}
const commonProps = {
data: chartData,
margin: { top: 6, right: 4, left: 4, bottom: 0 },
};
return (
{chartType === "bar" ? (
chartData[Math.round(idx)]?.formattedTime || ""}
interval="preserveStartEnd"
/>
Math.max(dataMax, 1)]}
allowDataOverflow={false}
/>
{mode === "single" ? (
<>
} cursor={{ fill: "#8c8c8f", fillOpacity: 0.15 }} />
>
) : (
<>
} cursor={{ fill: "#8c8c8f", fillOpacity: 0.15 }} />
{displayProviders.map((provider, idx) => (
))}
>
)}
) : (
chartData[Math.round(idx)]?.formattedTime || ""}
interval="preserveStartEnd"
/>
Math.max(dataMax, 1)]}
allowDataOverflow={false}
/>
{mode === "single" ? (
<>
} />
>
) : (
<>
} />
{displayProviders.map((provider, idx) => (
))}
>
)}
)}
);
}