fix: add shared number formatting utilities (formatMetric, formatCount, formatPercent)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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 },
|
||||
];
|
||||
|
||||
@@ -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)}%`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user