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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 1x 1x 7x 7x 7x 7x 7x 7x 1x 1x 1x 19x 19x 19x 19x 19x 19x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 19x 19x 19x 19x 19x 10x 10x 10x 19x 19x 19x 19x 19x 19x 19x 19x 7x 9x 9x 9x 9x 9x 9x 9x 9x 7x 19x 19x 19x 19x 19x 19x 19x 9x 9x 19x 19x 19x 19x 19x 19x 19x 19x 19x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 19x 19x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 19x 19x 19x 19x 19x 19x 19x 19x 19x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 19x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 19x 19x 19x 19x 19x 19x 19x 7x 9x 9x 7x 19x 19x 19x 19x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 2x 17x 19x 19x 19x 19x 19x 19x 19x 19x 12x 12x 12x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 1x | import { supplierDisplayNames } from '@/constants/supplierMeta';
import { useAppContext } from '@/context';
import { i18n } from '@/helpers/i18n';
import { hexToRgba, SUPPLIER_COLORS } from '@/theme/colors';
import {
classifySupplierHealth,
filterStatsByRange,
STATS_RANGES,
statusLabelKey,
type StatsRange,
type SupplierHealthStatus,
} from '@/helpers/supplierStats';
import { clearStats, getStats } from '@/utils/SupplierStatsStore';
import { IDB_SUPPLIER_STATS_UPDATED } from '@/utils/idbCache';
import DeleteIcon from '@mui/icons-material/Delete';
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
import {
Box,
IconButton,
MenuItem,
Paper,
Tab,
Tabs,
TextField,
ToggleButton,
ToggleButtonGroup,
Tooltip,
Typography,
} from '@mui/material';
import { alpha, styled, type Theme } from '@mui/material/styles';
import { LineChart } from '@mui/x-charts/LineChart';
import { PieChart } from '@mui/x-charts/PieChart';
import { useDrawingArea } from '@mui/x-charts/hooks';
import { DataGrid, type GridColDef } from '@mui/x-data-grid';
import { FC, ReactNode, useEffect, useMemo, useState } from 'react';
import styles from './StatsPanel.module.scss';
import { BackButton } from './StyledComponents';
import { Logger } from '@/utils/Logger';
import { getErrorMessage } from '@/helpers/exceptions';
const logger = new Logger('StatsPanel');
/** Center label for the pie chart */
const StyledText = styled('text')(({ theme }) => ({
fill: theme.palette.text.primary,
textAnchor: 'middle',
dominantBaseline: 'central',
fontSize: 16,
fontWeight: 500,
}));
/**
* Renders text centered in the hole of a MUI X pie chart, positioned via the
* chart's drawing area. Used to show a total or label inside the pie.
* @param props - Component props.
* - `children` - The content to render at the pie's center.
* @returns An SVG text element centered in the chart.
* @example
* ```tsx
* <PieChart series={...}>
* <PieCenterLabel>1,234</PieCenterLabel>
* </PieChart>
* ```
* @source
*/
function PieCenterLabel({ children }: { children: ReactNode }) {
const { width, height, left, top } = useDrawingArea();
return (
<StyledText x={left + width / 2} y={top + height / 2}>
{children}
</StyledText>
);
}
interface PieDatum {
id: string;
value: number;
label: string;
percentage: number;
color: string;
}
type PieView = 'http' | 'parsed';
/**
* StatsPanel displays per-supplier search statistics as a full panel view.
* Three tabs: By Supplier (pie), Daily (bar), Totals (text).
* The pie chart has two views toggled like the Titanic example:
* - "HTTP Calls": inner = supplier totals, outer = success vs failure
* - "Parsed Data": inner = supplier totals, outer = products vs parse errors
* @category Components
* @source
*/
const StatsPanel: FC = () => {
const appContext = useAppContext();
const [stats, setStats] = useState<SupplierStatsData>({});
const [activeTab, setActiveTab] = useState(0);
const [pieView, setPieView] = useState<PieView>('http');
const [range, setRange] = useState<StatsRange>('all');
useEffect(() => {
const loadStats = async () => {
try {
const data = await getStats();
setStats(data);
} catch (error) {
logger.warn(`Failed to load supplier stats: ${getErrorMessage(error)}`, { error });
}
};
loadStats();
// Re-read stats when supplier stats are updated in IndexedDB (live updates during search)
const handler = () => loadStats();
window.addEventListener(IDB_SUPPLIER_STATS_UPDATED, handler);
return () => window.removeEventListener(IDB_SUPPLIER_STATS_UPDATED, handler);
}, []);
const handleClear = async () => {
try {
await clearStats();
setStats({});
} catch (error) {
logger.warn(`Failed to clear supplier stats: ${getErrorMessage(error)}`, { error });
}
};
// Stats are keyed by supplierName ("Carolina"), but userSettings.disabledSuppliers
// holds class names ("SupplierCarolina"), so translate before comparing.
const disabledSupplierNames = useMemo(() => {
const disabledClassNames = appContext?.userSettings?.suppliers?.disabled ?? [];
const displayNames = supplierDisplayNames();
return new Set(disabledClassNames.map((className) => displayNames[className] ?? className));
}, [appContext?.userSettings?.suppliers?.disabled]);
// Every view (pies, daily chart, totals) reflects the selected range.
const rangedStats = useMemo(() => filterStatsByRange(stats, range), [stats, range]);
// Aggregate stats across the days in range
const aggregatedBySupplier = useMemo(() => {
const agg: Record<
string,
{ success: number; failure: number; products: number; queries: number; parseErrors: number }
> = {};
for (const dayStats of Object.values(rangedStats)) {
for (const [supplier, s] of Object.entries(dayStats)) {
if (!agg[supplier])
agg[supplier] = { success: 0, failure: 0, products: 0, queries: 0, parseErrors: 0 };
agg[supplier].success += s.successCount;
agg[supplier].failure += s.failureCount;
agg[supplier].products += s.uniqueProductCount;
agg[supplier].queries += s.searchQueryCount ?? 0;
agg[supplier].parseErrors += s.parseErrorCount ?? 0;
}
}
return agg;
}, [rangedStats]);
// Build supplier color map
const supplierColorMap = useMemo(() => {
const suppliers = Object.keys(aggregatedBySupplier);
const colorMap: Record<string, string> = {};
suppliers.forEach((s, i) => {
colorMap[s] = SUPPLIER_COLORS[i % SUPPLIER_COLORS.length];
});
return colorMap;
}, [aggregatedBySupplier]);
// ── HTTP Calls view: inner = total HTTP calls per supplier, outer = success vs failure ──
const httpPieData = useMemo(() => {
const suppliers = Object.keys(aggregatedBySupplier);
const totalCalls = suppliers.reduce(
(sum, s) => sum + aggregatedBySupplier[s].success + aggregatedBySupplier[s].failure,
0,
);
const inner: PieDatum[] = suppliers.map((supplier) => {
const { success, failure } = aggregatedBySupplier[supplier];
const total = success + failure;
return {
id: supplier,
value: total,
label: `${supplier}:`,
percentage: totalCalls > 0 ? (total / totalCalls) * 100 : 0,
color: supplierColorMap[supplier],
};
});
// Exactly 2 entries per supplier (Yes/No pattern from Titanic example) so outer aligns with inner
const outer: PieDatum[] = suppliers.flatMap((supplier) => {
const { success, failure } = aggregatedBySupplier[supplier];
const supplierTotal = success + failure;
const baseColor = supplierColorMap[supplier];
return [
{
id: `${supplier}-success`,
label: i18n('stats_successes'),
value: success,
percentage: supplierTotal > 0 ? (success / supplierTotal) * 100 : 0,
color: baseColor,
},
{
id: `${supplier}-failure`,
label: i18n('stats_fails'),
value: failure,
percentage: supplierTotal > 0 ? (failure / supplierTotal) * 100 : 0,
color: hexToRgba(baseColor, 0.4),
},
];
});
return { inner, outer, totalCalls };
}, [aggregatedBySupplier, supplierColorMap]);
// ── Parsed Data view: inner = total products + parse errors per supplier, outer = products vs parseErrors ──
const parsedPieData = useMemo(() => {
const suppliers = Object.keys(aggregatedBySupplier);
const totalParsed = suppliers.reduce(
(sum, s) => sum + aggregatedBySupplier[s].products + aggregatedBySupplier[s].parseErrors,
0,
);
const inner: PieDatum[] = suppliers.map((supplier) => {
const { products, parseErrors } = aggregatedBySupplier[supplier];
const total = products + parseErrors;
return {
id: supplier,
value: total,
label: `${supplier}:`,
percentage: totalParsed > 0 ? (total / totalParsed) * 100 : 0,
color: supplierColorMap[supplier],
};
});
const outer: PieDatum[] = suppliers.flatMap((supplier) => {
const { products, parseErrors } = aggregatedBySupplier[supplier];
const supplierTotal = products + parseErrors;
const baseColor = supplierColorMap[supplier];
return [
{
id: `${supplier}-parsed`,
label: i18n('stats_successes'),
value: products,
percentage: supplierTotal > 0 ? (products / supplierTotal) * 100 : 0,
color: baseColor,
},
{
id: `${supplier}-parseError`,
label: i18n('stats_fails'),
value: parseErrors,
percentage: supplierTotal > 0 ? (parseErrors / supplierTotal) * 100 : 0,
color: hexToRgba(baseColor, 0.4),
},
];
});
return { inner, outer, totalParsed };
}, [aggregatedBySupplier, supplierColorMap]);
// Line chart data — one line per supplier showing total calls over time
const { lineSeries, lineDates } = useMemo(() => {
const sortedDates = Object.keys(rangedStats).sort();
const allSuppliers = new Set<string>();
for (const dayStats of Object.values(rangedStats)) {
for (const supplier of Object.keys(dayStats)) {
allSuppliers.add(supplier);
}
}
const series: Array<{
data: number[];
label: string;
color: string;
showMark: boolean;
valueFormatter: (v: number | null) => string;
}> = [];
let colorIdx = 0;
for (const supplier of allSuppliers) {
const color = SUPPLIER_COLORS[colorIdx % SUPPLIER_COLORS.length];
series.push({
data: sortedDates.map((date) => {
const s = stats[date]?.[supplier];
return s ? s.successCount + s.failureCount : 0;
}),
label: supplier,
color,
showMark: true,
valueFormatter: (v) => i18n('stats_calls', [String(v ?? 0)]),
});
colorIdx++;
}
return { lineSeries: series, lineDates: sortedDates.map((d) => d.slice(5)) };
}, [rangedStats]);
// Totals table
const totalsColumns: GridColDef[] = [
{ field: 'supplier', headerName: i18n('stats_col_supplier'), flex: 1, minWidth: 130 },
{ field: 'queries', headerName: i18n('stats_col_queries'), width: 80, type: 'number' },
{ field: 'success', headerName: i18n('stats_col_success'), width: 80, type: 'number' },
{ field: 'failure', headerName: i18n('stats_col_failure'), width: 80, type: 'number' },
{ field: 'products', headerName: i18n('stats_col_products'), width: 85, type: 'number' },
{
field: 'parseErrors',
headerName: i18n('stats_col_parse_errors'),
width: 105,
type: 'number',
},
{
field: 'status',
headerName: i18n('stats_col_status'),
width: 150,
valueFormatter: (value: SupplierHealthStatus) => i18n(statusLabelKey(value)),
},
];
// Dim, theme-aware row tints. Deliberately low-alpha: these mark rows worth a
// look, they shouldn't shout over the numbers themselves.
const statusRowSx = {
'& .stats-row--noSuccess': {
backgroundColor: (theme: Theme) => alpha(theme.palette.error.main, 0.16),
},
'& .stats-row--connectionErrors': {
backgroundColor: (theme: Theme) => alpha(theme.palette.error.main, 0.09),
},
'& .stats-row--parseFailure': {
backgroundColor: (theme: Theme) => alpha(theme.palette.error.main, 0.16),
},
'& .stats-row--parseErrors': {
backgroundColor: (theme: Theme) => alpha(theme.palette.warning.main, 0.12),
},
// Not a fault — just greyed back so it reads as inactive rather than broken.
'& .stats-row--disabled': {
backgroundColor: (theme: Theme) => alpha(theme.palette.text.disabled, 0.08),
color: 'text.disabled',
},
};
const totalsRows = useMemo(
() =>
Object.entries(aggregatedBySupplier).map(([supplier, s]) => {
const { status } = classifySupplierHealth(s, disabledSupplierNames.has(supplier));
return {
id: supplier,
supplier,
queries: s.queries,
success: s.success,
failure: s.failure,
products: s.products,
parseErrors: s.parseErrors,
status,
};
}),
[aggregatedBySupplier, disabledSupplierNames],
);
const hasData = Object.keys(stats).length > 0;
const hasRangedData = Object.keys(rangedStats).length > 0;
const totalCalls = Object.values(aggregatedBySupplier).reduce(
(sum, s) => sum + s.success + s.failure,
0,
);
const innerRadius = 50;
const middleRadius = 120;
// Select the right pie data based on the toggle
const activePie = pieView === 'http' ? httpPieData : parsedPieData;
const centerLabel = pieView === 'http' ? i18n('stats_center_http') : i18n('stats_center_parsed');
const activeTotalForTooltip =
pieView === 'http' ? httpPieData.totalCalls : parsedPieData.totalParsed;
return (
<div className={styles['stats-panel']}>
{/* Header */}
<div className={styles['stats-panel__top-header']}>
<div className={styles['header-left']}>
{appContext?.setPanel && (
<BackButton
onClick={() => appContext.setPanel!(0)}
size="small"
aria-label={i18n('common_back_to_search')}
>
<ArrowBackIcon />
</BackButton>
)}
<Typography variant="subtitle2">{i18n('stats_title')}</Typography>
</div>
<div className={styles['header-right']}>
<TextField
select
size="small"
variant="standard"
value={range}
onChange={(event) => setRange(event.target.value as StatsRange)}
aria-label={i18n('stats_range_label')}
slotProps={{ input: { disableUnderline: true } }}
sx={{ minWidth: 110, '& .MuiSelect-select': { fontSize: '0.75rem', py: 0.25 } }}
>
{STATS_RANGES.map(({ value, labelKey }) => (
<MenuItem key={value} value={value} sx={{ fontSize: '0.75rem' }}>
{i18n(labelKey)}
</MenuItem>
))}
</TextField>
<Typography variant="caption" color="text.secondary">
{totalCalls === 1
? i18n('stats_call_single', [String(totalCalls)])
: i18n('stats_calls', [String(totalCalls)])}
</Typography>
{hasData && (
<Tooltip title={i18n('stats_clear')}>
<IconButton
size="small"
onClick={handleClear}
className={styles['stats-panel__clear-btn']}
>
<DeleteIcon className={styles['stats-panel__clear-icon']} />
</IconButton>
</Tooltip>
)}
</div>
</div>
{!hasData ? (
<Typography variant="body2" color="text.secondary" className={styles['stats-panel__empty']}>
{i18n('stats_empty')}
</Typography>
) : (
<>
<Tabs
value={activeTab}
onChange={(_e, v) => setActiveTab(v)}
variant="fullWidth"
className={styles['stats-panel__tabs']}
>
<Tab label={i18n('stats_tab_by_supplier')} />
<Tab label={i18n('stats_tab_daily')} />
<Tab label={i18n('stats_tab_totals')} />
</Tabs>
<Paper className={styles['stats-panel__content']} elevation={2}>
{/* A range with no recorded days would otherwise render empty charts. */}
{!hasRangedData && (
<Typography
variant="body2"
color="text.secondary"
className={styles['stats-panel__empty']}
>
{i18n('stats_empty_range')}
</Typography>
)}
{/* Tab 0: Sunburst pie with toggle */}
{hasRangedData && activeTab === 0 && (
<>
<Box className={styles['stats-panel__toggle-container']}>
<ToggleButtonGroup
color="primary"
size="small"
value={pieView}
exclusive
onChange={(_e, v) => {
if (v !== null) setPieView(v);
}}
>
<ToggleButton value="http">{i18n('stats_toggle_http')}</ToggleButton>
<ToggleButton value="parsed">{i18n('stats_toggle_parsed')}</ToggleButton>
</ToggleButtonGroup>
</Box>
<Box className={styles['stats-panel__chart-container']}>
<PieChart
series={[
{
innerRadius,
outerRadius: middleRadius,
data: activePie.inner.map((d) => ({ ...d, label: d.id })),
valueFormatter: ({ value }) =>
i18n('stats_pie_inner_tooltip', [
String(value),
String(activeTotalForTooltip),
String(
activeTotalForTooltip > 0
? ((value / activeTotalForTooltip) * 100).toFixed(0)
: 0,
),
]),
highlightScope: { fade: 'global', highlight: 'item' },
highlighted: { additionalRadius: 2 },
cornerRadius: 3,
paddingAngle: 2,
},
{
innerRadius: middleRadius,
outerRadius: middleRadius + 20,
data: activePie.outer,
valueFormatter: (item) =>
// MUI's pie item type omits our custom PieDatum fields; the runtime
// data is the PieDatum[] we passed, so reading `percentage` is safe.
i18n('stats_pie_outer_tooltip', [
String(item.value),
String((item as unknown as PieDatum).percentage?.toFixed(0) ?? 0),
]),
highlightScope: { fade: 'global', highlight: 'item' },
highlighted: { additionalRadius: 2 },
cornerRadius: 3,
paddingAngle: 1,
},
]}
width={500}
height={300}
hideLegend
>
<PieCenterLabel>{centerLabel}</PieCenterLabel>
</PieChart>
</Box>
{/* Custom legend for supplier colors only */}
<Box className={styles['stats-panel__legend']}>
{activePie.inner.map((d) => (
<Box key={d.id} className={styles['stats-panel__legend-item']}>
<Box
className={styles['stats-panel__legend-dot']}
style={{ backgroundColor: d.color }}
/>
<span>{d.id}</span>
</Box>
))}
</Box>
</>
)}
{/* Tab 1: Line chart — daily calls per supplier */}
{hasRangedData && activeTab === 1 && (
<Box className={styles['stats-panel__chart-container']}>
{lineDates.length > 0 && lineSeries.length > 0 && (
<LineChart
xAxis={[{ scaleType: 'point', data: lineDates }]}
series={lineSeries}
width={500}
height={320}
hideLegend
/>
)}
{/* Custom legend */}
<Box className={styles['stats-panel__legend']}>
{lineSeries.map((s) => (
<Box key={s.label} className={styles['stats-panel__legend-item']}>
<Box
className={styles['stats-panel__legend-dot']}
style={{ backgroundColor: s.color }}
/>
<span>{s.label}</span>
</Box>
))}
</Box>
</Box>
)}
{/* Tab 2: Totals table */}
{hasRangedData && activeTab === 2 && (
<div className={styles['stats-panel__table-container']}>
<DataGrid
rows={totalsRows}
columns={totalsColumns}
density="compact"
disableColumnMenu
hideFooter={totalsRows.length <= 25}
initialState={{
sorting: { sortModel: [{ field: 'success', sort: 'desc' }] },
}}
getRowClassName={({ row }) => `stats-row--${row.status}`}
sx={statusRowSx}
className={styles['stats-panel__table']}
/>
</div>
)}
</Paper>
</>
)}
</div>
);
};
export default StatsPanel;
|