|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { AlertTriangle, AlertOctagon, TrendingUp, Minus } from 'lucide-react'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | +import { CollapsibleCardHeader } from './CollapsibleCardHeader.jsx'; |
| 5 | +import { useCollapsedSection } from '../utils/useCollapsedSection.js'; |
| 6 | + |
| 7 | +const TYPE_META = { |
| 8 | + nan: { icon: AlertOctagon, color: 'text-red-600 dark:text-red-400', bg: 'bg-red-50 dark:bg-red-950' }, |
| 9 | + explosion: { icon: AlertTriangle, color: 'text-red-600 dark:text-red-400', bg: 'bg-red-50 dark:bg-red-950' }, |
| 10 | + spike: { icon: TrendingUp, color: 'text-amber-600 dark:text-amber-400', bg: 'bg-amber-50 dark:bg-amber-950' }, |
| 11 | + plateau: { icon: Minus, color: 'text-gray-500 dark:text-gray-400', bg: 'bg-gray-50 dark:bg-gray-800' } |
| 12 | +}; |
| 13 | + |
| 14 | +function TypeBadge({ type, count, t }) { |
| 15 | + if (count === 0) return null; |
| 16 | + const meta = TYPE_META[type]; |
| 17 | + const Icon = meta.icon; |
| 18 | + return ( |
| 19 | + <span className={`inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-[11px] font-medium ${meta.color} ${meta.bg}`}> |
| 20 | + <Icon size={11} aria-hidden="true" /> |
| 21 | + <span className="font-mono tabular">{count}</span> |
| 22 | + <span className="hidden sm:inline">{t(`anomalies.type.${type}`)}</span> |
| 23 | + </span> |
| 24 | + ); |
| 25 | +} |
| 26 | + |
| 27 | +function fileSummary(eventsByMetric) { |
| 28 | + const summary = { nan: 0, explosion: 0, spike: 0, plateau: 0, total: 0 }; |
| 29 | + Object.values(eventsByMetric).forEach(events => { |
| 30 | + events.forEach(e => { |
| 31 | + if (summary[e.type] !== undefined) summary[e.type]++; |
| 32 | + summary.total++; |
| 33 | + }); |
| 34 | + }); |
| 35 | + return summary; |
| 36 | +} |
| 37 | + |
| 38 | +export function AnomaliesPanel({ anomaliesByFile = {}, files = [], collapseId }) { |
| 39 | + const { t } = useTranslation(); |
| 40 | + const [open, setOpen] = useCollapsedSection(collapseId, false); |
| 41 | + const collapsible = Boolean(collapseId); |
| 42 | + |
| 43 | + // Stable list of file entries that actually have anomalies, in upload order. |
| 44 | + const entries = useMemo(() => { |
| 45 | + const result = []; |
| 46 | + files.forEach(file => { |
| 47 | + const eventsByMetric = anomaliesByFile[file.id]; |
| 48 | + if (!eventsByMetric) return; |
| 49 | + const summary = fileSummary(eventsByMetric); |
| 50 | + if (summary.total === 0) return; |
| 51 | + result.push({ file, eventsByMetric, summary }); |
| 52 | + }); |
| 53 | + return result; |
| 54 | + }, [anomaliesByFile, files]); |
| 55 | + |
| 56 | + const totalCount = useMemo( |
| 57 | + () => entries.reduce((s, e) => s + e.summary.total, 0), |
| 58 | + [entries] |
| 59 | + ); |
| 60 | + |
| 61 | + const titleNode = ( |
| 62 | + <h3 className="text-base font-semibold text-gray-800 dark:text-gray-100 flex items-center gap-2 truncate"> |
| 63 | + <span>{t('anomalies.title')}</span> |
| 64 | + {totalCount > 0 && ( |
| 65 | + <span className="inline-flex items-center justify-center min-w-[1.25rem] h-5 px-1.5 rounded-full text-[11px] font-semibold font-mono tabular bg-red-100 text-red-700 dark:bg-red-950 dark:text-red-300"> |
| 66 | + {totalCount} |
| 67 | + </span> |
| 68 | + )} |
| 69 | + </h3> |
| 70 | + ); |
| 71 | + |
| 72 | + return ( |
| 73 | + <section className="card" aria-labelledby="anomalies-heading"> |
| 74 | + <CollapsibleCardHeader |
| 75 | + titleNode={titleNode} |
| 76 | + titleId="anomalies-heading" |
| 77 | + icon={<AlertTriangle size={16} className="text-amber-600 dark:text-amber-400" />} |
| 78 | + collapsible={collapsible} |
| 79 | + open={open} |
| 80 | + onToggle={() => setOpen(o => !o)} |
| 81 | + /> |
| 82 | + {(!collapsible || open) && ( |
| 83 | + <div className="mt-2"> |
| 84 | + {entries.length === 0 ? ( |
| 85 | + <p className="text-xs text-gray-500 dark:text-gray-400"> |
| 86 | + {t('anomalies.empty')} |
| 87 | + </p> |
| 88 | + ) : ( |
| 89 | + <ul className="space-y-2" role="list"> |
| 90 | + {entries.map(({ file, eventsByMetric, summary }) => ( |
| 91 | + <li |
| 92 | + key={file.id} |
| 93 | + className="text-xs bg-gray-50 dark:bg-gray-700 rounded p-2 space-y-1" |
| 94 | + > |
| 95 | + <div className="flex items-center justify-between gap-2"> |
| 96 | + <span className="truncate font-medium text-gray-700 dark:text-gray-200" title={file.name}> |
| 97 | + {file.name} |
| 98 | + </span> |
| 99 | + </div> |
| 100 | + <div className="flex flex-wrap gap-1"> |
| 101 | + <TypeBadge type="nan" count={summary.nan} t={t} /> |
| 102 | + <TypeBadge type="explosion" count={summary.explosion} t={t} /> |
| 103 | + <TypeBadge type="spike" count={summary.spike} t={t} /> |
| 104 | + <TypeBadge type="plateau" count={summary.plateau} t={t} /> |
| 105 | + </div> |
| 106 | + {/* Per-metric breakdown: show metric name + count when >1 metric has anomalies */} |
| 107 | + {Object.keys(eventsByMetric).length > 1 && ( |
| 108 | + <ul className="text-[11px] text-gray-500 dark:text-gray-400 space-y-0.5 mt-1"> |
| 109 | + {Object.entries(eventsByMetric).map(([metric, events]) => ( |
| 110 | + <li key={metric} className="flex items-center justify-between"> |
| 111 | + <span className="font-mono truncate">{metric}</span> |
| 112 | + <span className="font-mono tabular">{events.length}</span> |
| 113 | + </li> |
| 114 | + ))} |
| 115 | + </ul> |
| 116 | + )} |
| 117 | + </li> |
| 118 | + ))} |
| 119 | + </ul> |
| 120 | + )} |
| 121 | + <p className="text-[11px] text-gray-400 dark:text-gray-500 mt-2 leading-relaxed"> |
| 122 | + {t('anomalies.hint')} |
| 123 | + </p> |
| 124 | + </div> |
| 125 | + )} |
| 126 | + </section> |
| 127 | + ); |
| 128 | +} |
0 commit comments