Compare commits

..

6 Commits

Author SHA1 Message Date
alu 05519694e1 add vmid to instance 2026-07-07 16:42:07 +00:00
alu 78e6de32d7 add uint to uint64 for SafeUint64 2026-07-07 03:36:32 +00:00
alu 24904dde66 improve FormatNumber logic 2026-06-26 20:57:49 +00:00
alu d6173c8675 make SafeUint64 generic 2026-06-26 20:38:01 +00:00
alu 41a06f6b45 add SafeUint64 to units 2026-06-26 20:31:56 +00:00
alu 372fd452c7 fix to uint 2026-06-26 19:05:06 +00:00
2 changed files with 36 additions and 28 deletions
+1
View File
@@ -21,6 +21,7 @@ const VM InstanceType = "VM"
const CT InstanceType = "CT" const CT InstanceType = "CT"
type Instance struct { type Instance struct {
VMID InstanceID `json:"vmid" mapstructure:"vmid"`
Type InstanceType `json:"type" mapstructure:"type"` Type InstanceType `json:"type" mapstructure:"type"`
Name string `json:"name" mapstructure:"name"` Name string `json:"name" mapstructure:"name"`
Proctype string `json:"cpu" mapstructure:"cpu"` Proctype string `json:"cpu" mapstructure:"cpu"`
+35 -28
View File
@@ -2,49 +2,56 @@ package proxmoxaas_common_lib
import ( import (
"fmt" "fmt"
"log"
"math" "math"
"strings" "strings"
) )
const Base1000 int64 = 1000 const Base1000 uint64 = 1000
const Base1024 int64 = 1024 const Base1024 uint64 = 1024
var prefixesBase1000 []string = []string{"", "K", "M", "G", "T"} var prefixesBase1000 []string = []string{"", "K", "M", "G", "T"}
var prefixesBase1024 []string = []string{"", "Ki", "Mi", "Gi", "Ti"} var prefixesBase1024 []string = []string{"", "Ki", "Mi", "Gi", "Ti"}
const KB int64 = Base1000 const KB uint64 = Base1000
const MB int64 = KB * Base1000 const MB uint64 = KB * Base1000
const GB int64 = MB * Base1000 const GB uint64 = MB * Base1000
const TB int64 = GB * Base1000 const TB uint64 = GB * Base1000
const KiB int64 = Base1024 const KiB uint64 = Base1024
const MiB int64 = KiB * Base1024 const MiB uint64 = KiB * Base1024
const GiB int64 = MiB * Base1024 const GiB uint64 = MiB * Base1024
const TiB int64 = GiB * Base1024 const TiB uint64 = GiB * Base1024
func FormatNumber(val uint64, base uint64) (string, string) {
prefixes := []string{""}
switch base {
case Base1000:
prefixes = prefixesBase1000
case Base1024:
prefixes = prefixesBase1024
default:
}
func FormatNumber(val int64, base int64) (string, string) {
valf := float64(val) valf := float64(val)
basef := float64(base) basef := float64(base)
steps := 0 steps := 0
for math.Abs(valf) > basef && steps < 4 { for math.Abs(valf) > basef && steps < len(prefixes)-1 {
valf /= basef valf /= basef
steps++ steps++
} }
switch base { s := fmt.Sprintf("%.4f", valf)
case Base1000: s = strings.TrimRight(s, "0")
s := fmt.Sprintf("%.4f", valf) s = strings.TrimRight(s, ".")
s = strings.TrimRight(s, "0") return s, prefixes[steps]
s = strings.TrimRight(s, ".") }
prefixes := prefixesBase1000
return s, prefixes[steps] // Converts int, int32, and int64 to uint64. Less than 0 check.
case Base1024: func SafeUint64[I int | int32 | int64 | uint | uint32](i I) uint64 {
s := fmt.Sprintf("%.4f", valf) if i < 0 {
s = strings.TrimRight(s, "0") log.Printf("Tried to cast %d to uint64", i)
s = strings.TrimRight(s, ".") return 0
prefixes := prefixesBase1024 }
return s, prefixes[steps] return uint64(i)
default:
return "0", ""
}
} }