Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { describe, it, expect } from 'vitest'
import { getUnitFromAggregationName, AggregationUnit } from './getUnitFromAggregationName'
import { type AllAggregations } from '../types/explore/all'
import { basicExploreAggregations, exploreAggregations, aiExploreAggregations } from '../'

describe('getUnitFromAggregationName', () => {
it('returns a value for every aggregation name', () => {
const validUnits = Object.values(AggregationUnit)

const allAggregations = [
...basicExploreAggregations,
...exploreAggregations,
...aiExploreAggregations,
]

allAggregations.forEach((name) => {
const unit = getUnitFromAggregationName(name)
expect(validUnits).toContain(unit)
})
})

it.each([
[AggregationUnit.ms, 'latency'],
[AggregationUnit.bytes, 'size'],
[AggregationUnit.countPerMinute, 'perminute'],
[AggregationUnit.countPerMinute, 'per_minute'],
[AggregationUnit.tokenCount, 'token'],
[AggregationUnit.usd, 'cost'],
[AggregationUnit.count, 'count'],
])('returns \'%s\' when metric includes \'%s\'', (unit, metric) => {
const mockAggregationName = `Mock ${metric} metric` as AllAggregations
expect(getUnitFromAggregationName(mockAggregationName)).toEqual(unit)
})
})

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { AllAggregations } from '../types/explore/all'

export enum AggregationUnit {
ms = 'ms',
bytes = 'bytes',
countPerMinute = 'count/minute',
tokenCount = 'token count',
usd = 'usd',
count = 'count',
}

export const getUnitFromAggregationName = (name: AllAggregations): AggregationUnit => {
const lower = name.toLowerCase()

if (lower.includes('latency')) {
return AggregationUnit.ms
}

if (lower.includes('size')) {
return AggregationUnit.bytes
}

if (lower.includes('perminute') || lower.includes('per_minute')) {
return AggregationUnit.countPerMinute
}

if (lower.includes('token')) {
return AggregationUnit.tokenCount
}

if (lower.includes('cost')) {
return AggregationUnit.usd
}

return AggregationUnit.count
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './chartDataGenerator'
export * from './helpers'
export * from './SeedRandom'
export * from './getUnitFromAggregationName'
Loading