mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-17 22:19:42 +02:00
agent: add system-info endpoint
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user