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

100% Statements 186/186
80% Branches 20/25
100% Functions 5/5
100% Lines 186/186

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 1871x 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 30x 30x 30x 9x 9x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 1x 1x 1x 1x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 1x 1x 1x 1x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 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 30x 30x 30x 30x 30x 30x 30x 30x 30x 10x 10x 10x 30x 30x 10x 10x 10x 30x 30x 30x 30x 30x 30x 1x 1x 1x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 12x 12x 14x 2x 2x 2x 14x 14x 14x 14x  
'use client';
 
import React from 'react';
import { formatDuration } from '@/lib/monitoring/percentiles';
 
export interface SlowRequest {
  id: string | number;
  type: string;
  name: string;
  duration: number;
  method?: string | null;
  statusCode?: number | null;
  createdAt: Date | string;
  metadata?: Record<string, unknown> | null;
}
 
export interface SlowestRequestsTableProps {
  /** Slow requests to display */
  requests: SlowRequest[];
  /** Loading state */
  isLoading?: boolean;
  /** Maximum rows to display */
  maxRows?: number;
  /** Threshold for highlighting (ms) */
  criticalThreshold?: number;
  /** Show metadata column */
  showMetadata?: boolean;
}
 
/**
 * Get severity color based on duration
 */
function getSeverityColor(duration: number, threshold: number): string {
  if (duration >= threshold * 2) return 'text-red-600 dark:text-red-400 bg-red-50 dark:bg-red-900/20';
  if (duration >= threshold) return 'text-orange-600 dark:text-orange-400 bg-orange-50 dark:bg-orange-900/20';
  return 'text-yellow-600 dark:text-yellow-400 bg-yellow-50 dark:bg-yellow-900/20';
}
 
/**
 * Get status code color
 */
function getStatusColor(statusCode: number | null | undefined): string {
  if (!statusCode) return 'text-gray-500';
  if (statusCode >= 500) return 'text-red-600 dark:text-red-400';
  if (statusCode >= 400) return 'text-yellow-600 dark:text-yellow-400';
  return 'text-green-600 dark:text-green-400';
}
 
/**
 * Format timestamp for display
 */
function formatTimestamp(date: Date | string): string {
  const d = typeof date === 'string' ? new Date(date) : date;
  return d.toLocaleString([], {
    month: 'short',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
  });
}
 
/**
 * TypeBadge component - styled metric type indicator
 */
function TypeBadge({ type }: { type: string }) {
  const colors: Record<string, string> = {
    api: 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200',
    database: 'bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-200',
    render: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200',
  };
 
  return (
    <span
      className={`px-2 py-0.5 text-xs font-medium rounded ${colors[type] || 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-200'}`}
    >
      {type}
    </span>
  );
}
 
/**
 * SlowestRequestsTable displays the slowest requests/queries
 */
export function SlowestRequestsTable({
  requests,
  isLoading = false,
  maxRows = 10,
  criticalThreshold = 1000,
  showMetadata = false,
}: SlowestRequestsTableProps) {
  const displayedRequests = requests.slice(0, maxRows);
 
  if (isLoading) {
    return (
      <div
        className="bg-white dark:bg-gray-800 rounded-lg shadow p-4"
        data-testid="slowest-requests-table"
      >
        <h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
          Slowest Requests
        </h3>
        <div className="animate-pulse space-y-3">
          {[1, 2, 3, 4, 5].map((i) => (
            <div key={i} className="h-12 bg-gray-200 dark:bg-gray-700 rounded" />
          ))}
        </div>
      </div>
    );
  }
 
  if (requests.length === 0) {
    return (
      <div
        className="bg-white dark:bg-gray-800 rounded-lg shadow p-4"
        data-testid="slowest-requests-table"
      >
        <h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
          Slowest Requests
        </h3>
        <p className="text-gray-500 dark:text-gray-400 text-center py-8">
          No slow requests detected
        </p>
      </div>
    );
  }
 
  return (
    <div
      className="bg-white dark:bg-gray-800 rounded-lg shadow p-4"
      data-testid="slowest-requests-table"
    >
      <div className="flex items-center justify-between mb-4">
        <h3 className="text-lg font-semibold text-gray-900 dark:text-white">Slowest Requests</h3>
        <span className="text-sm text-gray-500 dark:text-gray-400">
          Threshold: {formatDuration(criticalThreshold)}
        </span>
      </div>
      <div className="space-y-2">
        {displayedRequests.map((request) => (
          <div
            key={request.id}
            className={`p-3 rounded-lg ${getSeverityColor(request.duration, criticalThreshold)}`}
          >
            <div className="flex items-start justify-between gap-4">
              <div className="flex-1 min-w-0">
                <div className="flex items-center gap-2 mb-1">
                  <TypeBadge type={request.type} />
                  {request.method && (
                    <span className="text-xs font-medium text-gray-600 dark:text-gray-400">
                      {request.method}
                    </span>
                  )}
                  {request.statusCode && (
                    <span className={`text-xs font-medium ${getStatusColor(request.statusCode)}`}>
                      {request.statusCode}
                    </span>
                  )}
                </div>
                <p className="font-mono text-sm truncate" title={request.name}>
                  {request.name}
                </p>
                {showMetadata && request.metadata && (
                  <pre className="text-xs text-gray-500 dark:text-gray-400 mt-1 truncate">
                    {JSON.stringify(request.metadata)}
                  </pre>
                )}
              </div>
              <div className="text-right flex-shrink-0">
                <p className="text-lg font-bold">{formatDuration(request.duration)}</p>
                <p className="text-xs text-gray-500 dark:text-gray-400">
                  {formatTimestamp(request.createdAt)}
                </p>
              </div>
            </div>
          </div>
        ))}
      </div>
      {requests.length > maxRows && (
        <p className="text-sm text-gray-500 dark:text-gray-400 mt-4 text-center">
          Showing {maxRows} of {requests.length} slow requests
        </p>
      )}
    </div>
  );
}