miaopanel
1export function bytes(n: number): string {
2 if (!n || n < 0) return '0 B'
3 const units = ['B', 'KB', 'MB', 'GB', 'TB']
4 let i = 0
5 let v = n
6 while (v >= 1024 && i < units.length - 1) {
7 v /= 1024
8 i++
9 }
10 return `${v >= 100 ? v.toFixed(0) : v.toFixed(1)} ${units[i]}`
11}
12
13export function duration(seconds: number): string {
14 if (!seconds) return '—'
15 const d = Math.floor(seconds / 86400)
16 const h = Math.floor((seconds % 86400) / 3600)
17 const m = Math.floor((seconds % 3600) / 60)
18 if (d > 0) return `${d} 天 ${h} 小时`
19 if (h > 0) return `${h} 小时 ${m} 分`
20 return `${m} 分钟`
21}
22
23export function time(unix: number): string {
24 if (!unix) return '—'
25 const d = new Date(unix * 1000)
26 const pad = (v: number) => String(v).padStart(2, '0')
27 return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(
28 d.getMinutes(),
29 )}:${pad(d.getSeconds())}`
30}
31
32export function ratio(used: number, total: number): number {
33 if (!total) return 0
34 return Math.min(100, Math.max(0, (used / total) * 100))
35}
36
37export function classOf(p: number): string {
38 if (p >= 90) return 'danger'
39 if (p >= 70) return 'warn'
40 return ''
41}