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 209 210 211 212 213 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 62x 62x 62x 62x 62x 1x 1x 62x 62x 62x 62x 6x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 30x 30x 30x 30x 28x 28x 28x 62x 62x 62x 62x 28x 30x 30x 30x 30x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 30x 30x 30x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 30x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 30x 27x 27x 27x 30x 30x 6x 6x 6x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 66x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x | 'use client';
import React, { useMemo } from 'react';
import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, Cell } from 'recharts';
import { formatNumber } from '@/lib/monitoring/percentiles';
export interface StatusCodeData {
statusCode: number;
count: number;
}
export interface StatusCodeChartProps {
/** Status code distribution data */
data: StatusCodeData[];
/** Chart height in pixels */
height?: number;
/** Loading state */
isLoading?: boolean;
}
// Color coding by status code range
function getStatusCodeColor(code: number): string {
if (code >= 200 && code < 300) return '#10B981'; // green - success
if (code >= 300 && code < 400) return '#3B82F6'; // blue - redirect
if (code >= 400 && code < 500) return '#F59E0B'; // amber - client error
if (code >= 500) return '#EF4444'; // red - server error
return '#6B7280'; // gray - other
}
// Get status code category label
function getStatusCategory(code: number): string {
if (code >= 200 && code < 300) return 'Success';
if (code >= 300 && code < 400) return 'Redirect';
if (code >= 400 && code < 500) return 'Client Error';
if (code >= 500) return 'Server Error';
return 'Other';
}
/**
* Custom tooltip component for the status code chart
*/
function CustomTooltip({
active,
payload,
}: {
active?: boolean;
payload?: Array<{ payload: { name: string; value: number; category: string } }>;
}) {
if (!active || !payload?.length) return null;
const entry = payload[0].payload;
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">
Status {entry.name}
</p>
<p className="text-sm text-gray-600 dark:text-gray-400">{entry.category}</p>
<p className="text-sm text-gray-600 dark:text-gray-400">
Count: {formatNumber(entry.value)}
</p>
</div>
);
}
/**
* StatusCodeChart shows distribution of HTTP status codes
*/
export function StatusCodeChart({
data,
height = 250,
isLoading = false,
}: StatusCodeChartProps) {
// Sort and format data
const chartData = useMemo(() => {
return [...data]
.sort((a, b) => a.statusCode - b.statusCode)
.map((d) => ({
name: d.statusCode.toString(),
value: d.count,
color: getStatusCodeColor(d.statusCode),
category: getStatusCategory(d.statusCode),
}));
}, [data]);
// Calculate summary stats
const summary = useMemo(() => {
const total = data.reduce((sum, d) => sum + d.count, 0);
const success = data
.filter((d) => d.statusCode >= 200 && d.statusCode < 300)
.reduce((sum, d) => sum + d.count, 0);
const clientErrors = data
.filter((d) => d.statusCode >= 400 && d.statusCode < 500)
.reduce((sum, d) => sum + d.count, 0);
const serverErrors = data
.filter((d) => d.statusCode >= 500)
.reduce((sum, d) => sum + d.count, 0);
return {
total,
successRate: total > 0 ? ((success / total) * 100).toFixed(1) : '0',
clientErrorRate: total > 0 ? ((clientErrors / total) * 100).toFixed(1) : '0',
serverErrorRate: total > 0 ? ((serverErrors / total) * 100).toFixed(1) : '0',
};
}, [data]);
if (isLoading) {
return (
<div
className="bg-white dark:bg-gray-800 rounded-lg shadow p-4"
data-testid="status-code-chart"
>
<h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
Status Code 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="status-code-chart"
>
<h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
Status Code 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="status-code-chart"
>
<div className="flex items-center justify-between mb-4">
<h3 className="text-lg font-semibold text-gray-900 dark:text-white">
Status Code Distribution
</h3>
<div className="flex gap-4 text-sm">
<span className="text-green-600 dark:text-green-400">
{summary.successRate}% Success
</span>
{parseFloat(summary.clientErrorRate) > 0 && (
<span className="text-amber-600 dark:text-amber-400">
{summary.clientErrorRate}% 4xx
</span>
)}
{parseFloat(summary.serverErrorRate) > 0 && (
<span className="text-red-600 dark:text-red-400">
{summary.serverErrorRate}% 5xx
</span>
)}
</div>
</div>
<ResponsiveContainer width="100%" height={height}>
<BarChart data={chartData} margin={{ top: 5, right: 30, left: 20, bottom: 5 }}>
<XAxis
dataKey="name"
className="text-gray-600 dark:text-gray-400"
tick={{ fill: 'currentColor', fontSize: 12 }}
/>
<YAxis
tickFormatter={(value) => formatNumber(value)}
className="text-gray-600 dark:text-gray-400"
tick={{ fill: 'currentColor', fontSize: 12 }}
/>
<Tooltip content={<CustomTooltip />} />
<Bar dataKey="value" radius={[4, 4, 0, 0]}>
{chartData.map((entry, index) => (
<Cell key={`cell-${index}`} fill={entry.color} />
))}
</Bar>
</BarChart>
</ResponsiveContainer>
{/* Legend */}
<div className="flex justify-center gap-6 mt-4 text-sm">
<div className="flex items-center gap-2">
<div className="w-3 h-3 rounded bg-green-500" />
<span className="text-gray-600 dark:text-gray-400">2xx Success</span>
</div>
<div className="flex items-center gap-2">
<div className="w-3 h-3 rounded bg-blue-500" />
<span className="text-gray-600 dark:text-gray-400">3xx Redirect</span>
</div>
<div className="flex items-center gap-2">
<div className="w-3 h-3 rounded bg-amber-500" />
<span className="text-gray-600 dark:text-gray-400">4xx Client</span>
</div>
<div className="flex items-center gap-2">
<div className="w-3 h-3 rounded bg-red-500" />
<span className="text-gray-600 dark:text-gray-400">5xx Server</span>
</div>
</div>
</div>
);
}
|