Skip to content

Commit 8773357

Browse files
committed
fix: respect per-file metric config
1 parent 9b8c680 commit 8773357

2 files changed

Lines changed: 54 additions & 6 deletions

File tree

src/components/ChartContainer.jsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,13 @@ export default function ChartContainer({
260260
return results;
261261
};
262262

263-
metrics.forEach(metric => {
263+
metrics.forEach((metric, idx) => {
264+
const fileMetric = file.config?.metrics?.[idx] || metric;
264265
let points = [];
265-
if (metric.mode === 'keyword') {
266-
points = extractByKeyword(lines, metric.keyword);
267-
} else if (metric.regex) {
268-
const reg = new RegExp(metric.regex);
266+
if (fileMetric.mode === 'keyword') {
267+
points = extractByKeyword(lines, fileMetric.keyword);
268+
} else if (fileMetric.regex) {
269+
const reg = new RegExp(fileMetric.regex);
269270
lines.forEach(line => {
270271
reg.lastIndex = 0;
271272
const m = reg.exec(line);
@@ -278,7 +279,20 @@ export default function ChartContainer({
278279
}
279280
});
280281
}
281-
metricsData[metric.name || metric.keyword] = points;
282+
283+
let key = '';
284+
if (metric.name && metric.name.trim()) {
285+
key = metric.name.trim();
286+
} else if (metric.keyword) {
287+
key = metric.keyword.replace(/[:]/g, '').trim();
288+
} else if (metric.regex) {
289+
const sanitized = metric.regex.replace(/[^a-zA-Z0-9_]/g, '').trim();
290+
key = sanitized || `metric${idx + 1}`;
291+
} else {
292+
key = `metric${idx + 1}`;
293+
}
294+
295+
metricsData[key] = points;
282296
});
283297

284298
const range = file.config?.dataRange;

src/components/__tests__/ChartContainer.test.jsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,38 @@ describe('ChartContainer', () => {
175175
opts.plugins.zoom.pan.onPanComplete({ chart: { scales: { x: { min: 0, max: 10 } } } });
176176
opts.plugins.zoom.zoom.onZoomComplete({ chart: { scales: { x: { min: 2, max: 4 } } } });
177177
});
178+
179+
it('uses per-file metric configuration when provided', () => {
180+
const onXRangeChange = vi.fn();
181+
const onMaxStepChange = vi.fn();
182+
const files = [
183+
{ name: 'a.log', enabled: true, content: 'loss: 1\nloss: 2' },
184+
{
185+
name: 'b.log',
186+
enabled: true,
187+
content: 'train_loss: 3\ntrain_loss: 4',
188+
config: { metrics: [{ mode: 'keyword', keyword: 'train_loss:' }] }
189+
}
190+
];
191+
const metrics = [{ name: 'loss', mode: 'keyword', keyword: 'loss:' }];
192+
193+
render(
194+
<ChartContainer
195+
files={files}
196+
metrics={metrics}
197+
compareMode="normal"
198+
onXRangeChange={onXRangeChange}
199+
onMaxStepChange={onMaxStepChange}
200+
/>
201+
);
202+
203+
const mainChart = [...__lineProps].reverse().find(p =>
204+
p.data.datasets && p.data.datasets.some(d => d.label === 'b')
205+
);
206+
const ds = mainChart.data.datasets.find(d => d.label === 'b');
207+
expect(ds.data).toEqual([
208+
{ x: 0, y: 3 },
209+
{ x: 1, y: 4 }
210+
]);
211+
});
178212
});

0 commit comments

Comments
 (0)