All files / src/components/features/admin/monitoring/charts/RequestVolumeChart index.tsx

79.42% Statements 166/209
78.57% Branches 11/14
25% Functions 1/4
79.42% Lines 166/209

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 2101x 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 1x                                                           1x 1x 1x 1x 28x 28x 28x 28x 28x 28x 28x 28x 28x 26x 29x 29x 29x 29x 29x 26x 28x 28x 28x 28x                             28x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x  
'use client';
 
import React, { useMemo } from 'react';
import {
  AreaChart,
  Area,
  XAxis,
  YAxis,
  CartesianGrid,
  Tooltip,
  Legend,
  ResponsiveContainer,
} from 'recharts';
import { formatNumber, type TimeRange } from '@/lib/monitoring/percentiles';
 
export interface VolumeDataPoint {
  timestamp: Date | string;
  total: number;
  successful: number;
  clientErrors: number; // 4xx
  serverErrors: number; // 5xx
}
 
export interface RequestVolumeChartProps {
  /** Volume data points */
  data: VolumeDataPoint[];
  /** Selected time range for formatting */
  timeRange: TimeRange;
  /** Show stacked area chart */
  stacked?: boolean;
  /** Chart height in pixels */
  height?: number;
  /** Loading state */
  isLoading?: boolean;
}
 
// Color palette
const COLORS = {
  successful: { stroke: '#10B981', fill: '#10B981' }, // green
  clientErrors: { stroke: '#F59E0B', fill: '#F59E0B' }, // amber
  serverErrors: { stroke: '#EF4444', fill: '#EF4444' }, // red
};
 
/**
 * Custom tooltip component for the volume chart
 */
function CustomTooltip({
  active,
  payload,
  label,
}: {
  active?: boolean;
  payload?: Array<{ name: string; value: number; color: string }>;
  label?: number;
}) {
  if (!active || !payload?.length || !label) return null;

  const total = payload.reduce((sum, entry) => sum + entry.value, 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-2">
        {new Date(label).toLocaleString()}
      </p>
      {payload.map((entry, index) => (
        <p key={index} className="text-sm" style={{ color: entry.color }}>
          {entry.name}: {formatNumber(entry.value)}
        </p>
      ))}
      <p className="text-sm font-medium text-gray-900 dark:text-white mt-2 pt-2 border-t border-gray-200 dark:border-gray-700">
        Total: {formatNumber(total)}
      </p>
    </div>
  );
}
 
/**
 * RequestVolumeChart displays request volume over time with success/error breakdown
 */
export function RequestVolumeChart({
  data,
  timeRange,
  stacked = true,
  height = 300,
  isLoading = false,
}: RequestVolumeChartProps) {
  // Format data for Recharts
  const chartData = useMemo(() => {
    return data.map((point) => ({
      ...point,
      timestamp:
        typeof point.timestamp === 'string'
          ? new Date(point.timestamp).getTime()
          : point.timestamp.getTime(),
    }));
  }, [data]);
 
  // Format timestamp based on time range
  const formatTimestamp = (timestamp: number): string => {
    const date = new Date(timestamp);
    switch (timeRange) {
      case '1h':
      case '6h':
        return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
      case '24h':
        return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
      case '7d':
        return date.toLocaleDateString([], { weekday: 'short', hour: '2-digit' });
      case '30d':
        return date.toLocaleDateString([], { month: 'short', day: 'numeric' });
      default:
        return date.toLocaleTimeString();
    }
  };
 
  if (isLoading) {
    return (
      <div
        className="bg-white dark:bg-gray-800 rounded-lg shadow p-4"
        data-testid="request-volume-chart"
      >
        <h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
          Request Volume
        </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="request-volume-chart"
      >
        <h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
          Request Volume
        </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="request-volume-chart"
    >
      <h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
        Request Volume ({timeRange})
      </h3>
      <ResponsiveContainer width="100%" height={height}>
        <AreaChart data={chartData} margin={{ top: 5, right: 30, left: 20, bottom: 5 }}>
          <CartesianGrid strokeDasharray="3 3" className="stroke-gray-200 dark:stroke-gray-700" />
          <XAxis
            dataKey="timestamp"
            tickFormatter={formatTimestamp}
            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 />} />
          <Legend />
          <Area
            type="monotone"
            dataKey="successful"
            name="Successful"
            stackId={stacked ? 'stack' : undefined}
            stroke={COLORS.successful.stroke}
            fill={COLORS.successful.fill}
            fillOpacity={0.6}
          />
          <Area
            type="monotone"
            dataKey="clientErrors"
            name="Client Errors (4xx)"
            stackId={stacked ? 'stack' : undefined}
            stroke={COLORS.clientErrors.stroke}
            fill={COLORS.clientErrors.fill}
            fillOpacity={0.6}
          />
          <Area
            type="monotone"
            dataKey="serverErrors"
            name="Server Errors (5xx)"
            stackId={stacked ? 'stack' : undefined}
            stroke={COLORS.serverErrors.stroke}
            fill={COLORS.serverErrors.fill}
            fillOpacity={0.6}
          />
        </AreaChart>
      </ResponsiveContainer>
    </div>
  );
}