Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/components/ChartContainer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,13 @@ export default function ChartContainer({
return results;
};

metrics.forEach(metric => {
metrics.forEach((metric, idx) => {
const fileMetric = file.config?.metrics?.[idx] || metric;
let points = [];
if (metric.mode === 'keyword') {
points = extractByKeyword(lines, metric.keyword);
} else if (metric.regex) {
const reg = new RegExp(metric.regex);
if (fileMetric.mode === 'keyword') {
points = extractByKeyword(lines, fileMetric.keyword);
} else if (fileMetric.regex) {
const reg = new RegExp(fileMetric.regex);
lines.forEach(line => {
reg.lastIndex = 0;
const m = reg.exec(line);
Expand All @@ -278,7 +279,20 @@ export default function ChartContainer({
}
});
}
metricsData[metric.name || metric.keyword] = points;

let key = '';
if (metric.name && metric.name.trim()) {
key = metric.name.trim();
} else if (metric.keyword) {
Comment on lines +283 to +286

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Align metric lookup with sanitized metric keys

The parsing loop now strips punctuation when storing metric data (key = metric.keyword.replace(/[::]/g, '').trim() and similar for regex). However, the chart still looks up datasets by the raw metric.name || metric.keyword string when building metricElements. For metrics without a name that contain punctuation (e.g. { mode: 'keyword', keyword: 'loss:' }), data are stored under "loss" but later retrieved using "loss:", so the rendered chart contains no series even though values were parsed. Apply the same normalization during lookup (or reuse the metricNames array) so metrics defined only by keyword/regex continue to display.

Useful? React with 👍 / 👎.

key = metric.keyword.replace(/[::]/g, '').trim();
} else if (metric.regex) {
const sanitized = metric.regex.replace(/[^a-zA-Z0-9_]/g, '').trim();
key = sanitized || `metric${idx + 1}`;
} else {
key = `metric${idx + 1}`;
}

metricsData[key] = points;
});

const range = file.config?.dataRange;
Expand Down
34 changes: 34 additions & 0 deletions src/components/__tests__/ChartContainer.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,38 @@ describe('ChartContainer', () => {
opts.plugins.zoom.pan.onPanComplete({ chart: { scales: { x: { min: 0, max: 10 } } } });
opts.plugins.zoom.zoom.onZoomComplete({ chart: { scales: { x: { min: 2, max: 4 } } } });
});

it('uses per-file metric configuration when provided', () => {
const onXRangeChange = vi.fn();
const onMaxStepChange = vi.fn();
const files = [
{ name: 'a.log', enabled: true, content: 'loss: 1\nloss: 2' },
{
name: 'b.log',
enabled: true,
content: 'train_loss: 3\ntrain_loss: 4',
config: { metrics: [{ mode: 'keyword', keyword: 'train_loss:' }] }
}
];
const metrics = [{ name: 'loss', mode: 'keyword', keyword: 'loss:' }];

render(
<ChartContainer
files={files}
metrics={metrics}
compareMode="normal"
onXRangeChange={onXRangeChange}
onMaxStepChange={onMaxStepChange}
/>
);

const mainChart = [...__lineProps].reverse().find(p =>
p.data.datasets && p.data.datasets.some(d => d.label === 'b')
);
const ds = mainChart.data.datasets.find(d => d.label === 'b');
expect(ds.data).toEqual([
{ x: 0, y: 3 },
{ x: 1, y: 4 }
]);
});
});