Skip to content

Commit 2deb3d5

Browse files
committed
feat: calculate the unit from aggregation name [ma-3976]
1 parent a796b87 commit 2deb3d5

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { describe, it, expect } from 'vitest'
2+
import { getUnitFromAggregationName, AggregationUnit } from './getUnitFromAggregationName'
3+
import { type AllAggregations } from '../types/explore/all'
4+
import { basicExploreAggregations, exploreAggregations, aiExploreAggregations } from '../'
5+
6+
describe('getUnitFromAggregationName', () => {
7+
it('returns a value for every aggregation name', () => {
8+
const validUnits = Object.values(AggregationUnit)
9+
10+
const allAggregations = [
11+
...basicExploreAggregations,
12+
...exploreAggregations,
13+
...aiExploreAggregations,
14+
]
15+
16+
allAggregations.forEach((name) => {
17+
const unit = getUnitFromAggregationName(name)
18+
expect(validUnits).toContain(unit)
19+
})
20+
})
21+
22+
it.each([
23+
[AggregationUnit.ms, 'latency'],
24+
[AggregationUnit.bytes, 'size'],
25+
[AggregationUnit.countPerMinute, 'perminute'],
26+
[AggregationUnit.countPerMinute, 'per_minute'],
27+
[AggregationUnit.tokenCount, 'token'],
28+
[AggregationUnit.usd, 'cost'],
29+
[AggregationUnit.count, 'count'],
30+
])('returns \'%s\' when metric includes \'%s\'', (unit, metric) => {
31+
const mockAggregationName = `Mock ${metric} metric` as AllAggregations
32+
expect(getUnitFromAggregationName(mockAggregationName)).toEqual(unit)
33+
})
34+
})
35+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { AllAggregations } from '../types/explore/all'
2+
3+
export enum AggregationUnit {
4+
ms = 'ms',
5+
bytes = 'bytes',
6+
countPerMinute = 'count/minute',
7+
tokenCount = 'token count',
8+
usd = 'usd',
9+
count = 'count',
10+
}
11+
12+
export const getUnitFromAggregationName = (name: AllAggregations): AggregationUnit => {
13+
const lower = name.toLowerCase()
14+
15+
if (lower.includes('latency')) {
16+
return AggregationUnit.ms
17+
}
18+
19+
if (lower.includes('size')) {
20+
return AggregationUnit.bytes
21+
}
22+
23+
if (lower.includes('perminute') || lower.includes('per_minute')) {
24+
return AggregationUnit.countPerMinute
25+
}
26+
27+
if (lower.includes('token')) {
28+
return AggregationUnit.tokenCount
29+
}
30+
31+
if (lower.includes('cost')) {
32+
return AggregationUnit.usd
33+
}
34+
35+
return AggregationUnit.count
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './chartDataGenerator'
22
export * from './helpers'
33
export * from './SeedRandom'
4+
export * from './getUnitFromAggregationName'

0 commit comments

Comments
 (0)