diff --git a/ui/src/components/TabKpis.tsx b/ui/src/components/TabKpis.tsx index 58041f46..edc197ed 100644 --- a/ui/src/components/TabKpis.tsx +++ b/ui/src/components/TabKpis.tsx @@ -2,6 +2,7 @@ import { useMemo } from 'react'; import { useGlobalFilters } from '@cameleer/design-system'; import { useExecutionStats } from '../api/queries/executions'; import type { Scope } from '../hooks/useScope'; +import { formatPercent } from '../utils/format-utils'; import styles from './TabKpis.module.css'; interface TabKpisProps { @@ -20,10 +21,6 @@ function formatMs(ms: number): string { return `${Math.round(ms)}ms`; } -function formatPct(pct: number): string { - return `${pct.toFixed(1)}%`; -} - type Trend = 'up' | 'down' | 'flat'; function trend(current: number, previous: number): Trend { @@ -74,7 +71,7 @@ export function TabKpis({ scope }: TabKpisProps) { return [ { label: 'Total', value: formatNum(total), trend: trend(total, prevTotal) }, - { label: 'Err%', value: formatPct(errorRate), trend: trend(errorRate, prevErrorRate), upIsBad: true }, + { label: 'Err%', value: formatPercent(errorRate), trend: trend(errorRate, prevErrorRate), upIsBad: true }, { label: 'Avg', value: formatMs(avgMs), trend: trend(avgMs, prevAvgMs), upIsBad: true }, { label: 'P99', value: formatMs(p99), trend: trend(p99, prevP99), upIsBad: true }, ]; diff --git a/ui/src/utils/format-utils.ts b/ui/src/utils/format-utils.ts index 04a07745..d05d5172 100644 --- a/ui/src/utils/format-utils.ts +++ b/ui/src/utils/format-utils.ts @@ -39,3 +39,20 @@ export function timeAgo(iso?: string): string { if (hours < 24) return `${hours}h ago`; return `${Math.floor(hours / 24)}d ago`; } + +export function formatMetric(value: number, unit: string, decimals = 1): string { + if (Math.abs(value) >= 1_000_000) return `${(value / 1_000_000).toFixed(decimals)}M ${unit}`; + if (Math.abs(value) >= 1_000) return `${(value / 1_000).toFixed(decimals)}K ${unit}`; + if (Number.isInteger(value)) return `${value} ${unit}`; + return `${value.toFixed(decimals)} ${unit}`; +} + +export function formatCount(value: number): string { + if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(1)}M`; + if (value >= 1_000) return `${(value / 1_000).toFixed(1)}K`; + return String(value); +} + +export function formatPercent(value: number, decimals = 1): string { + return `${value.toFixed(decimals)}%`; +}