import type { LatencyHistogramResponse } from "@/lib/types/logs";
import { useMemo } from "react";
import { Area, AreaChart, Bar, BarChart, CartesianGrid, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts";
import { formatFullTimestamp, formatLatency, formatTimestamp, LATENCY_COLORS } from "../../utils/chartUtils";
import { ChartErrorBoundary } from "./chartErrorBoundary";
import type { ChartType } from "./chartTypeToggle";
interface LatencyChartProps {
data: LatencyHistogramResponse | null;
chartType: ChartType;
startTime: number;
endTime: number;
}
function CustomTooltip({ 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()}
);
}
export function LatencyChart({ data, chartType, startTime, endTime }: LatencyChartProps) {
const chartData = useMemo(() => {
if (!data?.buckets || !data.bucket_size_seconds) {
return [];
}
return data.buckets.map((bucket, index) => ({
...bucket,
index,
formattedTime: formatTimestamp(bucket.timestamp, data.bucket_size_seconds),
}));
}, [data]);
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}
/>
} cursor={{ fill: "#8c8c8f", fillOpacity: 0.15 }} />
) : (
chartData[Math.round(idx)]?.formattedTime || ""}
interval="preserveStartEnd"
/>
Math.max(dataMax, 1)]}
allowDataOverflow={false}
/>
} />
{/* Render P99 first (behind), then overlay in descending order so Avg is in front */}
)}
);
}