Skip to content

Commit a600ff6

Browse files
committed
Refactor metrics to generic configuration
1 parent 5371231 commit a600ff6

4 files changed

Lines changed: 326 additions & 1046 deletions

File tree

src/App.jsx

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,22 @@ function App() {
1212

1313
// 全局解析配置状态
1414
const [globalParsingConfig, setGlobalParsingConfig] = useState({
15-
loss: {
16-
mode: 'keyword', // 'keyword' | 'regex'
17-
keyword: 'loss:',
18-
regex: 'loss:\\s*([\\d.eE+-]+)'
19-
},
20-
gradNorm: {
21-
mode: 'keyword', // 'keyword' | 'regex'
22-
keyword: 'norm:',
23-
regex: 'grad[\\s_]norm:\\s*([\\d.eE+-]+)'
24-
},
25-
others: [] // 其他自定义指标解析配置
15+
metrics: [
16+
{
17+
name: 'Loss',
18+
mode: 'keyword', // 'keyword' | 'regex'
19+
keyword: 'loss:',
20+
regex: 'loss:\\s*([\\d.eE+-]+)'
21+
},
22+
{
23+
name: 'Grad Norm',
24+
mode: 'keyword',
25+
keyword: 'norm:',
26+
regex: 'grad[\\s_]norm:\\s*([\\d.eE+-]+)'
27+
}
28+
]
2629
});
2730

28-
// 兼容旧版本的正则表达式状态(供ChartContainer使用)
29-
const [lossRegex, setLossRegex] = useState('loss:\\s*([\\d.eE+-]+)');
30-
const [gradNormRegex, setGradNormRegex] = useState('grad[\\s_]norm:\\s*([\\d.eE+-]+)');
31-
3231
const [compareMode, setCompareMode] = useState('normal');
3332
const [relativeBaseline, setRelativeBaseline] = useState(0.002);
3433
const [absoluteBaseline, setAbsoluteBaseline] = useState(0.005);
@@ -47,9 +46,7 @@ function App() {
4746
enabled: true,
4847
config: {
4948
// 使用全局解析配置作为默认值
50-
loss: { ...globalParsingConfig.loss },
51-
gradNorm: { ...globalParsingConfig.gradNorm },
52-
others: globalParsingConfig.others.map(o => ({ ...o })),
49+
metrics: globalParsingConfig.metrics.map(m => ({ ...m })),
5350
dataRange: {
5451
start: 0, // 默认从第一个数据点开始
5552
end: undefined, // 默认到最后一个数据点
@@ -120,31 +117,17 @@ function App() {
120117
// 全局解析配置变更处理
121118
const handleGlobalParsingConfigChange = useCallback((newConfig) => {
122119
setGlobalParsingConfig(newConfig);
123-
124-
// 同步更新兼容的正则表达式状态
125-
setLossRegex(newConfig.loss.mode === 'regex' ? newConfig.loss.regex : 'loss:\\s*([\\d.eE+-]+)');
126-
setGradNormRegex(newConfig.gradNorm.mode === 'regex' ? newConfig.gradNorm.regex : 'grad[\\s_]norm:\\s*([\\d.eE+-]+)');
127-
120+
128121
// 同步所有文件的解析配置
129122
setUploadedFiles(prev => prev.map(file => ({
130123
...file,
131124
config: {
132125
...file.config,
133-
loss: { ...newConfig.loss },
134-
gradNorm: { ...newConfig.gradNorm },
135-
others: newConfig.others.map(o => ({ ...o }))
126+
metrics: newConfig.metrics.map(m => ({ ...m }))
136127
}
137128
})));
138129
}, []);
139130

140-
const handleRegexChange = useCallback((type, value) => {
141-
if (type === 'loss') {
142-
setLossRegex(value);
143-
} else {
144-
setGradNormRegex(value);
145-
}
146-
}, []);
147-
148131
// 全局拖拽事件处理
149132
const handleGlobalDragEnter = useCallback((e) => {
150133
e.preventDefault();
@@ -306,9 +289,6 @@ function App() {
306289
<RegexControls
307290
globalParsingConfig={globalParsingConfig}
308291
onGlobalParsingConfigChange={handleGlobalParsingConfigChange}
309-
lossRegex={lossRegex}
310-
gradNormRegex={gradNormRegex}
311-
onRegexChange={handleRegexChange}
312292
uploadedFiles={uploadedFiles}
313293
xRange={xRange}
314294
onXRangeChange={setXRange}
@@ -441,9 +421,7 @@ function App() {
441421
>
442422
<ChartContainer
443423
files={uploadedFiles}
444-
lossRegex={lossRegex}
445-
gradNormRegex={gradNormRegex}
446-
otherConfigs={globalParsingConfig.others}
424+
metrics={globalParsingConfig.metrics}
447425
compareMode={compareMode}
448426
relativeBaseline={relativeBaseline}
449427
absoluteBaseline={absoluteBaseline}

0 commit comments

Comments
 (0)