agent: add system-info endpoint

This commit is contained in:
yusing
2025-02-10 12:34:08 +08:00
parent 9120bbea34
commit 73e2660e59
8 changed files with 151 additions and 18 deletions

View File

@@ -2,6 +2,7 @@ package strutils
import (
"fmt"
"math"
"strconv"
"strings"
"time"
@@ -65,6 +66,39 @@ func ParseBool(s string) bool {
}
}
func formatFloat(f float64) string {
f = math.Round(f*100) / 100
if f == 0 {
return "0"
}
return strconv.FormatFloat(f, 'f', -1, 64)
}
func FormatByteSize[T ~uint64](size T) string {
const (
_ = (1 << (10 * iota))
kb
mb
gb
tb
pb
)
switch {
case size < kb:
return fmt.Sprintf("%d B", size)
case size < mb:
return formatFloat(float64(size)/kb) + "KiB"
case size < gb:
return formatFloat(float64(size)/mb) + "MiB"
case size < tb:
return formatFloat(float64(size)/gb) + "GiB"
case size < pb:
return formatFloat(float64(size/gb)/kb) + "TiB" // prevent overflow
default:
return formatFloat(float64(size/tb)/kb) + "PiB" // prevent overflow
}
}
func PortString(port uint16) string {
return strconv.FormatUint(uint64(port), 10)
}