All files / web/src/components/agents performance-dashboard.tsx

94.71% Statements 215/227
73.68% Branches 14/19
100% Functions 3/3
94.71% Lines 215/227

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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307    1x 1x               1x 1x             1x                       1x 13x 13x 13x     13x   13x                             13x 13x 39x   39x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x   39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x   39x 39x 13x 13x   13x 13x 13x   13x 13x 13x 13x 13x 13x 13x 13x   13x 13x   13x 13x 13x 13x 13x 13x 13x   13x 13x 13x 13x 13x 13x 1x 1x   13x   13x 13x 13x 13x 13x   13x 13x 13x 13x   13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x   13x 13x 13x 13x   13x 13x 13x 13x 13x 13x 13x 9x 9x   9x   13x 13x   13x 13x 13x 13x   13x 13x 13x 13x 13x 13x 13x 9x 9x   9x   13x 13x 13x     13x 13x 13x 13x 13x   13x 13x 13x 13x 13x 13x 13x   13x 13x 13x 13x   13x 13x 13x 13x   13x 13x 13x 13x   13x 13x 13x       13x 3x 3x   3x 3x 3x 3x 3x 3x   3x 3x   3x 3x   3x 3x   3x 3x   3x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 3x 3x 3x       13x 4x 4x 4x   4x 4x 4x 4x 4x     4x 4x         4x 4x 4x 4x 4x   13x 13x 13x   13x  
'use client';
 
import { Button } from '@web/components/ui/button';
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from '@web/components/ui/card';
import type { NavigationMetrics } from '@web/hooks/use-navigation-performance';
import { useNavigationPerformance } from '@web/hooks/use-navigation-performance';
import {
  BarChart,
  Clock,
  Trash2,
  TrendingDown,
  TrendingUp,
} from 'lucide-react';
import { useState } from 'react';
 
interface RouteMetrics {
  route: string;
  count: number;
  totalLoadTime: number;
  avgLoadTime: number;
  minLoadTime: number;
  maxLoadTime: number;
  metrics: NavigationMetrics[];
}
 
export function PerformanceDashboard() {
  const { metrics, getPerformanceReport, clearMetrics, currentMetric } =
    useNavigationPerformance();
  const [showDetails, setShowDetails] = useState(false);
 
  // Calculate report directly from metrics instead of using state
  const report = getPerformanceReport();
 
  if (!report) {
    return (
      <Card>
        <CardHeader>
          <CardTitle>Navigation Performance</CardTitle>
          <CardDescription>
            No performance data available yet. Navigate through the app to
            collect metrics.
          </CardDescription>
        </CardHeader>
      </Card>
    );
  }
 
  // Group metrics by route
  const routeMetrics = metrics.reduce(
    (acc, metric) => {
      if (!metric.loadTime) return acc;
 
      if (!acc[metric.route]) {
        acc[metric.route] = {
          route: metric.route,
          count: 0,
          totalLoadTime: 0,
          avgLoadTime: 0,
          minLoadTime: Infinity,
          maxLoadTime: 0,
          metrics: [],
        };
      }
 
      acc[metric.route].count += 1;
      acc[metric.route].totalLoadTime += metric.loadTime;
      acc[metric.route].avgLoadTime =
        acc[metric.route].totalLoadTime / acc[metric.route].count;
      acc[metric.route].minLoadTime = Math.min(
        acc[metric.route].minLoadTime,
        metric.loadTime,
      );
      acc[metric.route].maxLoadTime = Math.max(
        acc[metric.route].maxLoadTime,
        metric.loadTime,
      );
      acc[metric.route].metrics.push(metric);
 
      return acc;
    },
    {} as Record<string, RouteMetrics>,
  );
 
  const sortedRoutes = Object.values(routeMetrics).sort(
    (a, b) => b.avgLoadTime - a.avgLoadTime,
  );
 
  return (
    <div className="space-y-4">
      <Card>
        <CardHeader>
          <div className="flex justify-between items-start">
            <div>
              <CardTitle className="flex items-center gap-2">
                <BarChart className="h-5 w-5" />
                Navigation Performance Dashboard
              </CardTitle>
              <CardDescription>
                Real-time performance metrics for agent navigation
              </CardDescription>
            </div>
            <div className="flex gap-2">
              <Button
                variant="outline"
                size="sm"
                onClick={() => setShowDetails(!showDetails)}
              >
                {showDetails ? 'Hide' : 'Show'} Details
              </Button>
              <Button
                variant="outline"
                size="sm"
                onClick={() => {
                  clearMetrics();
                }}
              >
                <Trash2 className="h-4 w-4 mr-1" />
                Clear
              </Button>
            </div>
          </div>
        </CardHeader>
        <CardContent className="space-y-6">
          {/* Summary Stats */}
          <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
            <Card>
              <CardHeader className="pb-2">
                <CardTitle className="text-sm font-medium">
                  Average Load Time
                </CardTitle>
              </CardHeader>
              <CardContent>
                <div className="text-2xl font-bold">
                  {report.averageLoadTime.toFixed(0)}ms
                </div>
                <p className="text-xs text-muted-foreground">
                  Across {metrics.length} navigations
                </p>
              </CardContent>
            </Card>
 
            <Card>
              <CardHeader className="pb-2">
                <CardTitle className="text-sm font-medium flex items-center gap-1">
                  <TrendingUp className="h-4 w-4 text-red-500" />
                  Slowest Route
                </CardTitle>
              </CardHeader>
              <CardContent>
                <div className="text-sm font-medium truncate">
                  {report.slowestRoute || 'N/A'}
                </div>
                {report.slowestRoute && routeMetrics[report.slowestRoute] && (
                  <p className="text-xs text-muted-foreground">
                    {routeMetrics[report.slowestRoute].maxLoadTime.toFixed(0)}ms
                    max
                  </p>
                )}
              </CardContent>
            </Card>
 
            <Card>
              <CardHeader className="pb-2">
                <CardTitle className="text-sm font-medium flex items-center gap-1">
                  <TrendingDown className="h-4 w-4 text-green-500" />
                  Fastest Route
                </CardTitle>
              </CardHeader>
              <CardContent>
                <div className="text-sm font-medium truncate">
                  {report.fastestRoute || 'N/A'}
                </div>
                {report.fastestRoute && routeMetrics[report.fastestRoute] && (
                  <p className="text-xs text-muted-foreground">
                    {routeMetrics[report.fastestRoute].minLoadTime.toFixed(0)}ms
                    min
                  </p>
                )}
              </CardContent>
            </Card>
          </div>
 
          {/* Current Navigation */}
          {currentMetric && (
            <Card className="border-blue-200 bg-blue-50/50 dark:border-blue-900 dark:bg-blue-950/20">
              <CardHeader className="pb-2">
                <CardTitle className="text-sm font-medium flex items-center gap-2">
                  <Clock className="h-4 w-4" />
                  Current Navigation
                </CardTitle>
              </CardHeader>
              <CardContent>
                <div className="text-sm space-y-1">
                  <div>Route: {currentMetric.route}</div>
                  {currentMetric.loadTime && (
                    <div>Load Time: {currentMetric.loadTime.toFixed(0)}ms</div>
                  )}
                  {currentMetric.renderTime && (
                    <div>
                      Render Time: {currentMetric.renderTime.toFixed(0)}ms
                    </div>
                  )}
                  {currentMetric.fcp && (
                    <div>
                      First Contentful Paint: {currentMetric.fcp.toFixed(0)}ms
                    </div>
                  )}
                  {currentMetric.lcp && (
                    <div>
                      Largest Contentful Paint: {currentMetric.lcp.toFixed(0)}ms
                    </div>
                  )}
                </div>
              </CardContent>
            </Card>
          )}
 
          {/* Route Performance Table */}
          {showDetails && sortedRoutes.length > 0 && (
            <div className="space-y-2">
              <h3 className="text-sm font-semibold">
                Route Performance Details
              </h3>
              <div className="rounded-md border">
                <table className="w-full">
                  <thead>
                    <tr className="border-b bg-muted/50">
                      <th className="p-2 text-left text-xs font-medium">
                        Route
                      </th>
                      <th className="p-2 text-right text-xs font-medium">
                        Count
                      </th>
                      <th className="p-2 text-right text-xs font-medium">
                        Avg (ms)
                      </th>
                      <th className="p-2 text-right text-xs font-medium">
                        Min (ms)
                      </th>
                      <th className="p-2 text-right text-xs font-medium">
                        Max (ms)
                      </th>
                    </tr>
                  </thead>
                  <tbody>
                    {sortedRoutes.map((route) => (
                      <tr key={route.route} className="border-b">
                        <td className="p-2 text-xs font-mono truncate max-w-xs">
                          {route.route}
                        </td>
                        <td className="p-2 text-right text-xs">
                          {route.count}
                        </td>
                        <td className="p-2 text-right text-xs font-medium">
                          {route.avgLoadTime.toFixed(0)}
                        </td>
                        <td className="p-2 text-right text-xs text-green-600">
                          {route.minLoadTime.toFixed(0)}
                        </td>
                        <td className="p-2 text-right text-xs text-red-600">
                          {route.maxLoadTime.toFixed(0)}
                        </td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            </div>
          )}
 
          {/* Performance Tips */}
          {report.averageLoadTime > 1000 && (
            <Card className="border-yellow-200 bg-yellow-50/50 dark:border-yellow-900 dark:bg-yellow-950/20">
              <CardHeader className="pb-2">
                <CardTitle className="text-sm font-medium">
                  Performance Tips
                </CardTitle>
              </CardHeader>
              <CardContent>
                <ul className="text-xs space-y-1 list-disc list-inside">
                  <li>
                    Average load time is above 1 second - consider optimizing
                    data fetching
                  </li>
                  {sortedRoutes[0]?.avgLoadTime > 1500 && (
                    <li>
                      Route "{sortedRoutes[0].route}" is particularly slow
                    </li>
                  )}
                  <li>Check network tab for slow API calls</li>
                  <li>Consider implementing pagination or lazy loading</li>
                </ul>
              </CardContent>
            </Card>
          )}
        </CardContent>
      </Card>
    </div>
  );
}