forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.ts
More file actions
98 lines (83 loc) · 3.21 KB
/
Copy pathformat.ts
File metadata and controls
98 lines (83 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
export function formatThroughput(tps: number): string {
if (!Number.isFinite(tps) || tps <= 0) return '—'
if (tps >= 1_000) return `${(tps / 1_000).toFixed(1)}K t/s`
return `${tps.toFixed(tps < 10 ? 2 : 1)} t/s`
}
export function formatLatency(ms: number): string {
if (!Number.isFinite(ms) || ms <= 0) return '—'
if (ms >= 1_000) return `${(ms / 1_000).toFixed(2)}s`
return `${Math.round(ms)}ms`
}
export function formatUptimePct(pct: number): string {
if (!Number.isFinite(pct)) return '—'
return `${pct.toFixed(2)}%`
}
export type SuccessRateLevel =
| 'excellent'
| 'good'
| 'warning'
| 'critical'
| 'unknown'
const SUCCESS_RATE_EXCELLENT_MIN = 100
const SUCCESS_RATE_GOOD_MIN = 90
const SUCCESS_RATE_WARNING_MIN = 70
/**
* Single source of truth for grading a success rate (0-100).
* - excellent: 100% (full green)
* - good: >= 90% (slightly lighter green)
* - warning: >= 70%
* - critical: below 70%
* - unknown: non-finite values
*/
export function getSuccessRateLevel(rate: number): SuccessRateLevel {
if (!Number.isFinite(rate)) return 'unknown'
if (rate >= SUCCESS_RATE_EXCELLENT_MIN) return 'excellent'
if (rate >= SUCCESS_RATE_GOOD_MIN) return 'good'
if (rate >= SUCCESS_RATE_WARNING_MIN) return 'warning'
return 'critical'
}
const SUCCESS_RATE_TEXT_CLASS: Record<SuccessRateLevel, string> = {
excellent: 'text-emerald-600 dark:text-emerald-400',
good: 'text-emerald-500 dark:text-emerald-300',
warning: 'text-amber-600 dark:text-amber-400',
critical: 'text-red-600 dark:text-red-400',
unknown: 'text-muted-foreground',
}
const SUCCESS_RATE_DOT_CLASS: Record<SuccessRateLevel, string> = {
excellent: 'bg-emerald-500',
good: 'bg-emerald-400',
warning: 'bg-amber-500',
critical: 'bg-red-500',
unknown: 'bg-muted-foreground',
}
// Hex colors for non-CSS contexts (e.g. chart libraries that need raw values).
const SUCCESS_RATE_HEX_COLOR: Record<SuccessRateLevel, string> = {
excellent: '#10b981', // emerald-500 (full green)
good: '#34d399', // emerald-400 (slightly lighter green)
warning: '#f59e0b', // amber-500
critical: '#ef4444', // red-500
unknown: '#9ca3af', // gray-400
}
export function getSuccessRateTextClass(rate: number): string {
return SUCCESS_RATE_TEXT_CLASS[getSuccessRateLevel(rate)]
}
export function getSuccessRateDotClass(rate: number): string {
return SUCCESS_RATE_DOT_CLASS[getSuccessRateLevel(rate)]
}
export function getSuccessRateColor(rate: number): string {
return SUCCESS_RATE_HEX_COLOR[getSuccessRateLevel(rate)]
}