-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Collect and aggregate metrics from the evaluation workflo…
…w execution (no-changelog) (#11945) Co-authored-by: Tomi Turtiainen <[email protected]>
- Loading branch information
1 parent
be69f5c
commit b5b95ff
Showing
5 changed files
with
183 additions
and
11 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
packages/cli/src/evaluation/test-runner/__tests__/evaluation-metrics.ee.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { EvaluationMetrics } from '../evaluation-metrics.ee'; | ||
|
||
describe('EvaluationMetrics', () => { | ||
test('should aggregate metrics correctly', () => { | ||
const testMetricNames = new Set(['metric1', 'metric2']); | ||
const metrics = new EvaluationMetrics(testMetricNames); | ||
|
||
metrics.addResults({ metric1: 1, metric2: 0 }); | ||
metrics.addResults({ metric1: 0.5, metric2: 0.2 }); | ||
|
||
const aggregatedMetrics = metrics.getAggregatedMetrics(); | ||
|
||
expect(aggregatedMetrics).toEqual({ metric1: 0.75, metric2: 0.1 }); | ||
}); | ||
|
||
test('should aggregate only numbers', () => { | ||
const testMetricNames = new Set(['metric1', 'metric2']); | ||
const metrics = new EvaluationMetrics(testMetricNames); | ||
|
||
metrics.addResults({ metric1: 1, metric2: 0 }); | ||
metrics.addResults({ metric1: '0.5', metric2: 0.2 }); | ||
metrics.addResults({ metric1: 'not a number', metric2: [1, 2, 3] }); | ||
|
||
const aggregatedUpMetrics = metrics.getAggregatedMetrics(); | ||
|
||
expect(aggregatedUpMetrics).toEqual({ metric1: 1, metric2: 0.1 }); | ||
}); | ||
|
||
test('should handle missing values', () => { | ||
const testMetricNames = new Set(['metric1', 'metric2']); | ||
const metrics = new EvaluationMetrics(testMetricNames); | ||
|
||
metrics.addResults({ metric1: 1 }); | ||
metrics.addResults({ metric2: 0.2 }); | ||
|
||
const aggregatedMetrics = metrics.getAggregatedMetrics(); | ||
|
||
expect(aggregatedMetrics).toEqual({ metric1: 1, metric2: 0.2 }); | ||
}); | ||
|
||
test('should handle empty metrics', () => { | ||
const testMetricNames = new Set(['metric1', 'metric2']); | ||
const metrics = new EvaluationMetrics(testMetricNames); | ||
|
||
const aggregatedMetrics = metrics.getAggregatedMetrics(); | ||
|
||
expect(aggregatedMetrics).toEqual({}); | ||
}); | ||
|
||
test('should handle empty testMetrics', () => { | ||
const metrics = new EvaluationMetrics(new Set()); | ||
|
||
metrics.addResults({ metric1: 1, metric2: 0 }); | ||
metrics.addResults({ metric1: 0.5, metric2: 0.2 }); | ||
|
||
const aggregatedMetrics = metrics.getAggregatedMetrics(); | ||
|
||
expect(aggregatedMetrics).toEqual({}); | ||
}); | ||
|
||
test('should ignore non-relevant values', () => { | ||
const testMetricNames = new Set(['metric1']); | ||
const metrics = new EvaluationMetrics(testMetricNames); | ||
|
||
metrics.addResults({ metric1: 1, notRelevant: 0 }); | ||
metrics.addResults({ metric1: 0.5, notRelevant2: { foo: 'bar' } }); | ||
|
||
const aggregatedMetrics = metrics.getAggregatedMetrics(); | ||
|
||
expect(aggregatedMetrics).toEqual({ metric1: 0.75 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/cli/src/evaluation/test-runner/evaluation-metrics.ee.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { IDataObject } from 'n8n-workflow'; | ||
|
||
export class EvaluationMetrics { | ||
private readonly rawMetricsByName = new Map<string, number[]>(); | ||
|
||
constructor(private readonly metricNames: Set<string>) { | ||
for (const metricName of metricNames) { | ||
this.rawMetricsByName.set(metricName, []); | ||
} | ||
} | ||
|
||
addResults(result: IDataObject) { | ||
for (const [metricName, metricValue] of Object.entries(result)) { | ||
if (typeof metricValue === 'number' && this.metricNames.has(metricName)) { | ||
this.rawMetricsByName.get(metricName)!.push(metricValue); | ||
} | ||
} | ||
} | ||
|
||
getAggregatedMetrics() { | ||
const aggregatedMetrics: Record<string, number> = {}; | ||
|
||
for (const [metricName, metricValues] of this.rawMetricsByName.entries()) { | ||
if (metricValues.length > 0) { | ||
const metricSum = metricValues.reduce((acc, val) => acc + val, 0); | ||
aggregatedMetrics[metricName] = metricSum / metricValues.length; | ||
} | ||
} | ||
|
||
return aggregatedMetrics; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters