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 308 309 310 | 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 1x 1x 1x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 30x 30x 1x 1x 1x 1x 176x 176x 176x 30x 30x 1x 1x 1x 1x 58x 58x 58x 58x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 29x 29x 29x 29x 29x 29x 29x 29x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 29x 29x 29x 31x 31x 31x 31x 31x 31x 31x 31x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 87x 29x 29x 29x 29x 31x 2x 2x 2x 31x 31x 31x 31x 31x 31x 31x 31x 31x 29x 29x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 29x 29x 29x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 58x 29x 29x 29x 58x 58x 58x 58x 58x 58x 58x 58x 29x 29x 31x 2x 2x 2x 31x 31x 31x 31x 31x 1x 1x | 'use client';
import React from 'react';
/**
* Database metric from the API
*/
export interface DatabaseMetricData {
id: string;
name: string;
operation: string;
tableName: string | null;
duration: number;
rowCount: number | null;
success: boolean;
errorType: string | null;
timestamp: Date | string;
}
/**
* Query statistics by table
*/
export interface QueryStatByTable {
tableName: string;
avgDuration: number;
totalCalls: number;
slowCount: number;
}
/**
* Props for the DatabasePerformance component
*/
export interface DatabasePerformanceProps {
/** Total number of queries in the time period */
totalQueries: number;
/** Average duration of all queries (ms) */
avgDuration: number;
/** List of slow queries */
slowQueries: DatabaseMetricData[];
/** Statistics grouped by table */
queryStats: QueryStatByTable[];
/** Operation breakdown (operation -> count) */
operationBreakdown: Record<string, number>;
/** Threshold for slow queries (ms) */
slowThreshold?: number;
/** Time period label */
timePeriod?: string;
}
/**
* Metric card for summary statistics
*/
function MetricCard({
title,
value,
variant = 'default',
}: {
title: string;
value: string | number;
variant?: 'default' | 'warning' | 'success' | 'error';
}) {
const variantStyles = {
default: 'text-gray-900 dark:text-white',
warning: 'text-yellow-600 dark:text-yellow-400',
success: 'text-green-600 dark:text-green-400',
error: 'text-red-600 dark:text-red-400',
};
return (
<div className="bg-white dark:bg-gray-800 rounded-lg shadow p-4">
<p className="text-sm font-medium text-gray-500 dark:text-gray-400">
{title}
</p>
<p className={`text-2xl font-bold mt-1 ${variantStyles[variant]}`}>
{value}
</p>
</div>
);
}
/**
* Get variant based on duration
*/
function getDurationVariant(
duration: number,
threshold: number
): 'default' | 'warning' | 'success' | 'error' {
if (duration >= threshold) return 'error';
if (duration >= threshold * 0.5) return 'warning';
return 'success';
}
/**
* Format duration for display
*/
function formatDuration(ms: number): string {
if (ms < 1) return '<1ms';
if (ms < 1000) return `${ms.toFixed(0)}ms`;
return `${(ms / 1000).toFixed(2)}s`;
}
/**
* Format timestamp for display
*/
function formatTimestamp(timestamp: Date | string): string {
const date = typeof timestamp === 'string' ? new Date(timestamp) : timestamp;
return date.toLocaleString();
}
/**
* DatabasePerformance Component
*
* Displays database query performance metrics including slow queries,
* query breakdown by table/operation, and summary statistics.
*/
export function DatabasePerformance({
totalQueries,
avgDuration,
slowQueries,
queryStats,
operationBreakdown,
slowThreshold = 500,
timePeriod = '24h',
}: DatabasePerformanceProps) {
const slowCount = slowQueries.length;
const tablesHit = queryStats.length;
return (
<div className="space-y-6" data-testid="database-performance">
{/* Summary Cards */}
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
<MetricCard
title="Total Queries"
value={totalQueries.toLocaleString()}
/>
<MetricCard
title="Avg Duration"
value={formatDuration(avgDuration)}
variant={getDurationVariant(avgDuration, slowThreshold)}
/>
<MetricCard
title="Slow Queries"
value={slowCount}
variant={slowCount > 0 ? 'warning' : 'success'}
/>
<MetricCard title="Tables Hit" value={tablesHit} />
</div>
{/* Operation Breakdown */}
{Object.keys(operationBreakdown).length > 0 && (
<div className="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
<h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
Operations Breakdown ({timePeriod})
</h3>
<div className="flex flex-wrap gap-3">
{Object.entries(operationBreakdown)
.sort(([, a], [, b]) => b - a)
.map(([operation, count]) => (
<div
key={operation}
className="flex items-center gap-2 px-3 py-2 bg-gray-100 dark:bg-gray-700 rounded-lg"
>
<span className="font-medium text-gray-700 dark:text-gray-300">
{operation}
</span>
<span className="text-sm px-2 py-0.5 bg-blue-100 dark:bg-blue-900 text-blue-800 dark:text-blue-200 rounded">
{count}
</span>
</div>
))}
</div>
</div>
)}
{/* Queries by Table */}
<div className="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
<h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
Queries by Table ({timePeriod})
</h3>
{queryStats.length > 0 ? (
<div className="overflow-x-auto">
<table className="min-w-full">
<thead>
<tr className="border-b dark:border-gray-700">
<th className="text-left py-2 text-gray-700 dark:text-gray-300 font-medium">
Table
</th>
<th className="text-right py-2 text-gray-700 dark:text-gray-300 font-medium">
Total Calls
</th>
<th className="text-right py-2 text-gray-700 dark:text-gray-300 font-medium">
Avg Duration
</th>
<th className="text-right py-2 text-gray-700 dark:text-gray-300 font-medium">
Slow Queries
</th>
</tr>
</thead>
<tbody>
{queryStats.map((stat) => (
<tr
key={stat.tableName}
className="border-b dark:border-gray-700 hover:bg-gray-50 dark:hover:bg-gray-700/50"
>
<td className="py-3 text-gray-900 dark:text-gray-200">
<code className="px-2 py-1 bg-gray-100 dark:bg-gray-700 rounded text-sm">
{stat.tableName}
</code>
</td>
<td className="text-right py-3 text-gray-900 dark:text-gray-200">
{stat.totalCalls.toLocaleString()}
</td>
<td className="text-right py-3">
<span
className={
stat.avgDuration >= slowThreshold
? 'text-red-600 dark:text-red-400 font-medium'
: stat.avgDuration >= slowThreshold * 0.5
? 'text-yellow-600 dark:text-yellow-400'
: 'text-gray-900 dark:text-gray-200'
}
>
{formatDuration(stat.avgDuration)}
</span>
</td>
<td className="text-right py-3">
<span
className={
stat.slowCount > 0
? 'text-red-600 dark:text-red-400 font-medium'
: 'text-gray-500 dark:text-gray-400'
}
>
{stat.slowCount}
</span>
</td>
</tr>
))}
</tbody>
</table>
</div>
) : (
<p className="text-center py-8 text-gray-500 dark:text-gray-400">
No table statistics available
</p>
)}
</div>
{/* Slow Queries List */}
<div className="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
<h3 className="text-lg font-semibold mb-4 text-gray-900 dark:text-white">
Slow Database Queries ({timePeriod}, >{slowThreshold}ms)
</h3>
{slowQueries.length > 0 ? (
<div className="space-y-3">
{slowQueries.map((query) => (
<div
key={query.id}
className="p-4 bg-yellow-50 dark:bg-yellow-900/20 border-l-4 border-yellow-500 rounded"
>
<div className="flex flex-wrap justify-between items-start gap-2">
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1">
<span className="font-semibold text-red-600 dark:text-red-400">
{formatDuration(query.duration)}
</span>
<span className="text-xs px-2 py-0.5 bg-gray-200 dark:bg-gray-600 text-gray-700 dark:text-gray-300 rounded">
{query.operation}
</span>
{!query.success && (
<span className="text-xs px-2 py-0.5 bg-red-200 dark:bg-red-800 text-red-700 dark:text-red-300 rounded">
Failed
</span>
)}
</div>
<p className="font-mono text-sm text-gray-800 dark:text-gray-200 break-all">
{query.name}
</p>
<div className="flex flex-wrap gap-2 mt-2">
{query.tableName && (
<span className="text-xs px-2 py-0.5 bg-blue-100 dark:bg-blue-900 text-blue-700 dark:text-blue-300 rounded">
Table: {query.tableName}
</span>
)}
{query.rowCount !== null && (
<span className="text-xs px-2 py-0.5 bg-purple-100 dark:bg-purple-900 text-purple-700 dark:text-purple-300 rounded">
Rows: {query.rowCount}
</span>
)}
</div>
</div>
<span className="text-xs text-gray-500 dark:text-gray-400 whitespace-nowrap">
{formatTimestamp(query.timestamp)}
</span>
</div>
</div>
))}
</div>
) : (
<p className="text-center py-8 text-gray-500 dark:text-gray-400">
No slow queries detected
</p>
)}
</div>
</div>
);
}
export default DatabasePerformance;
|