All files / src/components/features/admin/monitoring/EndpointPerformanceTable index.tsx

98.99% Statements 197/199
74.28% Branches 26/35
100% Functions 5/5
98.99% Lines 197/199

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 2001x 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 31x 31x 31x 31x 7x     1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 7x 7x 7x 31x 31x 31x 31x 31x 31x 31x 31x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 13x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 14x 2x 2x 2x 2x 14x 14x 14x 14x 14x 14x 14x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 4x 4x 4x 4x 4x 4x 4x 4x 31x 31x 31x 31x 31x 31x 31x 31x 14x 14x 14x 14x 14x 2x 2x 2x 14x 14x 14x 14x  
'use client';
 
import React from 'react';
import { formatDuration } from '@/lib/monitoring/percentiles';
 
export interface EndpointStats {
  name: string;
  method: string | null;
  avgDuration: number;
  count: number;
  p95Duration?: number;
  p99Duration?: number;
}
 
export interface EndpointPerformanceTableProps {
  /** Endpoint statistics */
  endpoints: EndpointStats[];
  /** Show percentiles columns */
  showPercentiles?: boolean;
  /** Loading state */
  isLoading?: boolean;
  /** Maximum rows to display */
  maxRows?: number;
  /** Callback when clicking on an endpoint */
  onEndpointClick?: (name: string) => void;
}
 
/**
 * Get color class based on response time
 */
function getPerformanceColor(duration: number): string {
  if (duration < 100) return 'text-green-600 dark:text-green-400';
  if (duration < 300) return 'text-green-500 dark:text-green-500';
  if (duration < 500) return 'text-yellow-600 dark:text-yellow-400';
  if (duration < 1000) return 'text-orange-600 dark:text-orange-400';
  return 'text-red-600 dark:text-red-400';
}
 
/**
 * PerformanceBar component - visual indicator of response time
 */
function PerformanceBar({ duration }: { duration: number }) {
  // Scale: 0-2000ms maps to 0-100%
  const percentage = Math.min((duration / 2000) * 100, 100);
  const color =
    duration < 200
      ? 'bg-green-500'
      : duration < 500
        ? 'bg-yellow-500'
        : duration < 1000
          ? 'bg-orange-500'
          : 'bg-red-500';
 
  return (
    <div className="w-24 h-2 bg-gray-200 dark:bg-gray-700 rounded overflow-hidden">
      <div className={`h-full rounded ${color} transition-all`} style={{ width: `${percentage}%` }} />
    </div>
  );
}
 
/**
 * MethodBadge component - styled HTTP method indicator
 */
function MethodBadge({ method }: { method: string | null }) {
  if (!method) return <span className="text-gray-400">-</span>;
 
  const colors: Record<string, string> = {
    GET: 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200',
    POST: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200',
    PUT: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200',
    PATCH: 'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200',
    DELETE: 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200',
  };
 
  return (
    <span
      className={`px-2 py-0.5 text-xs font-medium rounded ${colors[method] || 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-200'}`}
    >
      {method}
    </span>
  );
}
 
/**
 * EndpointPerformanceTable displays performance metrics grouped by endpoint
 */
export function EndpointPerformanceTable({
  endpoints,
  showPercentiles = false,
  isLoading = false,
  maxRows,
  onEndpointClick,
}: EndpointPerformanceTableProps) {
  const displayedEndpoints = maxRows ? endpoints.slice(0, maxRows) : endpoints;
 
  if (isLoading) {
    return (
      <div
        className="bg-white dark:bg-gray-800 rounded-lg shadow p-4"
        data-testid="endpoint-performance-table"
      >
        <h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
          Endpoints by Response Time
        </h3>
        <div className="animate-pulse space-y-3">
          {[1, 2, 3, 4, 5].map((i) => (
            <div key={i} className="h-10 bg-gray-200 dark:bg-gray-700 rounded" />
          ))}
        </div>
      </div>
    );
  }
 
  if (endpoints.length === 0) {
    return (
      <div
        className="bg-white dark:bg-gray-800 rounded-lg shadow p-4"
        data-testid="endpoint-performance-table"
      >
        <h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
          Endpoints by Response Time
        </h3>
        <p className="text-gray-500 dark:text-gray-400 text-center py-8">
          No endpoint data available
        </p>
      </div>
    );
  }
 
  return (
    <div
      className="bg-white dark:bg-gray-800 rounded-lg shadow p-4"
      data-testid="endpoint-performance-table"
    >
      <h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
        Endpoints by Response Time
      </h3>
      <div className="overflow-x-auto">
        <table className="w-full">
          <thead>
            <tr className="text-left text-gray-500 dark:text-gray-400 text-sm">
              <th className="pb-3 font-medium">Endpoint</th>
              <th className="pb-3 font-medium">Method</th>
              <th className="pb-3 font-medium text-right">Avg Time</th>
              {showPercentiles && (
                <>
                  <th className="pb-3 font-medium text-right">P95</th>
                  <th className="pb-3 font-medium text-right">P99</th>
                </>
              )}
              <th className="pb-3 font-medium text-right">Requests</th>
              <th className="pb-3 font-medium">Performance</th>
            </tr>
          </thead>
          <tbody>
            {displayedEndpoints.map((endpoint, i) => (
              <tr
                key={`${endpoint.name}-${endpoint.method}-${i}`}
                className={`border-t border-gray-200 dark:border-gray-700 ${onEndpointClick ? 'cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-700/50' : ''}`}
                onClick={() => onEndpointClick?.(endpoint.name)}
              >
                <td className="py-3 font-mono text-sm text-gray-900 dark:text-gray-100 max-w-xs truncate">
                  {endpoint.name}
                </td>
                <td className="py-3">
                  <MethodBadge method={endpoint.method} />
                </td>
                <td className={`py-3 text-right font-medium ${getPerformanceColor(endpoint.avgDuration)}`}>
                  {formatDuration(endpoint.avgDuration)}
                </td>
                {showPercentiles && (
                  <>
                    <td className="py-3 text-right text-gray-600 dark:text-gray-400">
                      {endpoint.p95Duration ? formatDuration(endpoint.p95Duration) : '-'}
                    </td>
                    <td className="py-3 text-right text-gray-600 dark:text-gray-400">
                      {endpoint.p99Duration ? formatDuration(endpoint.p99Duration) : '-'}
                    </td>
                  </>
                )}
                <td className="py-3 text-right text-gray-600 dark:text-gray-400">
                  {endpoint.count.toLocaleString()}
                </td>
                <td className="py-3">
                  <PerformanceBar duration={endpoint.avgDuration} />
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
      {maxRows && endpoints.length > maxRows && (
        <p className="text-sm text-gray-500 dark:text-gray-400 mt-4 text-center">
          Showing {maxRows} of {endpoints.length} endpoints
        </p>
      )}
    </div>
  );
}