Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 27x 27x 27x 27x 27x 27x 25x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 27x 27x 27x 27x 27x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 27x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 56x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x | 'use client';
import React, { useMemo } from 'react';
import { PieChart, Pie, Cell, Tooltip, Legend, ResponsiveContainer, type PieLabelRenderProps } from 'recharts';
import { formatNumber } from '@/lib/monitoring/percentiles';
export interface EndpointDistribution {
path: string;
count: number;
percentage: number;
}
export interface EndpointDistributionChartProps {
/** Endpoint distribution data */
data: EndpointDistribution[];
/** Maximum number of slices before grouping into "Other" */
maxSlices?: number;
/** Chart height in pixels */
height?: number;
/** Loading state */
isLoading?: boolean;
}
// Color palette for pie slices
const COLORS = [
'#3B82F6', // blue
'#10B981', // green
'#F59E0B', // amber
'#EF4444', // red
'#8B5CF6', // purple
'#EC4899', // pink
'#14B8A6', // teal
'#F97316', // orange
'#6366F1', // indigo
'#84CC16', // lime
];
/**
* Custom tooltip component for the pie chart
*/
function CustomTooltip({
active,
payload,
}: {
active?: boolean;
payload?: Array<{ name: string; value: number; payload: { percent: number } }>;
}) {
if (!active || !payload?.length) return null;
const entry = payload[0];
return (
<div className="bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg shadow-lg p-3">
<p className="text-sm font-medium text-gray-900 dark:text-white mb-1">{entry.name}</p>
<p className="text-sm text-gray-600 dark:text-gray-400">
Requests: {formatNumber(entry.value)}
</p>
<p className="text-sm text-gray-600 dark:text-gray-400">
{(entry.payload.percent * 100).toFixed(1)}%
</p>
</div>
);
}
/**
* EndpointDistributionChart shows request distribution by endpoint
*/
export function EndpointDistributionChart({
data,
maxSlices = 8,
height = 300,
isLoading = false,
}: EndpointDistributionChartProps) {
// Group small slices into "Other"
const chartData = useMemo(() => {
if (data.length <= maxSlices) {
return data.map((d) => ({ name: d.path, value: d.count }));
}
const sorted = [...data].sort((a, b) => b.count - a.count);
const topSlices = sorted.slice(0, maxSlices - 1);
const otherSlices = sorted.slice(maxSlices - 1);
const otherCount = otherSlices.reduce((sum, d) => sum + d.count, 0);
return [
...topSlices.map((d) => ({ name: d.path, value: d.count })),
{ name: 'Other', value: otherCount },
];
}, [data, maxSlices]);
// Custom label
const renderLabel = (props: PieLabelRenderProps) => {
const { cx, cy, midAngle, innerRadius, outerRadius, percent } = props;
// Handle optional values - return null if required values are missing
if (
typeof cx !== 'number' ||
typeof cy !== 'number' ||
typeof midAngle !== 'number' ||
typeof innerRadius !== 'number' ||
typeof outerRadius !== 'number' ||
typeof percent !== 'number'
) {
return null;
}
if (percent < 0.05) return null; // Don't show labels for small slices
const RADIAN = Math.PI / 180;
const radius = innerRadius + (outerRadius - innerRadius) * 0.5;
const x = cx + radius * Math.cos(-midAngle * RADIAN);
const y = cy + radius * Math.sin(-midAngle * RADIAN);
return (
<text
x={x}
y={y}
fill="white"
textAnchor="middle"
dominantBaseline="central"
fontSize={12}
fontWeight="bold"
>
{`${(percent * 100).toFixed(0)}%`}
</text>
);
};
if (isLoading) {
return (
<div
className="bg-white dark:bg-gray-800 rounded-lg shadow p-4"
data-testid="endpoint-distribution-chart"
>
<h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
Endpoint Distribution
</h3>
<div
className="flex items-center justify-center bg-gray-100 dark:bg-gray-700 rounded animate-pulse"
style={{ height }}
>
<span className="text-gray-500 dark:text-gray-400">Loading chart...</span>
</div>
</div>
);
}
if (data.length === 0) {
return (
<div
className="bg-white dark:bg-gray-800 rounded-lg shadow p-4"
data-testid="endpoint-distribution-chart"
>
<h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
Endpoint Distribution
</h3>
<div
className="flex items-center justify-center bg-gray-50 dark:bg-gray-700 rounded"
style={{ height }}
>
<span className="text-gray-500 dark:text-gray-400">No data available</span>
</div>
</div>
);
}
return (
<div
className="bg-white dark:bg-gray-800 rounded-lg shadow p-4"
data-testid="endpoint-distribution-chart"
>
<h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
Endpoint Distribution
</h3>
<ResponsiveContainer width="100%" height={height}>
<PieChart>
<Pie
data={chartData}
cx="50%"
cy="50%"
labelLine={false}
label={renderLabel}
outerRadius={100}
innerRadius={40}
fill="#8884d8"
dataKey="value"
paddingAngle={2}
>
{chartData.map((_, index) => (
<Cell key={`cell-${index}`} fill={COLORS[index % COLORS.length]} />
))}
</Pie>
<Tooltip content={<CustomTooltip />} />
<Legend
layout="vertical"
align="right"
verticalAlign="middle"
formatter={(value: string) => (
<span className="text-sm text-gray-700 dark:text-gray-300">
{value.length > 30 ? `${value.substring(0, 30)}...` : value}
</span>
)}
/>
</PieChart>
</ResponsiveContainer>
</div>
);
}
|