move unit related constants and functions to common lib
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -256,33 +255,6 @@ func GetInstancePathFromRequest(c *gin.Context) (paas.InstancePath, error) {
|
|||||||
return vm_path, nil
|
return vm_path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FormatNumber(val int64, base int64) (string, string) {
|
|
||||||
valf := float64(val)
|
|
||||||
basef := float64(base)
|
|
||||||
steps := 0
|
|
||||||
for math.Abs(valf) > basef && steps < 4 {
|
|
||||||
valf /= basef
|
|
||||||
steps++
|
|
||||||
}
|
|
||||||
|
|
||||||
switch base {
|
|
||||||
case 1000:
|
|
||||||
s := fmt.Sprintf("%.4f", valf)
|
|
||||||
s = strings.TrimRight(s, "0")
|
|
||||||
s = strings.TrimRight(s, ".")
|
|
||||||
prefixes := []string{"", "K", "M", "G", "T"}
|
|
||||||
return s, prefixes[steps]
|
|
||||||
case 1024:
|
|
||||||
s := fmt.Sprintf("%.4f", valf)
|
|
||||||
s = strings.TrimRight(s, "0")
|
|
||||||
s = strings.TrimRight(s, ".")
|
|
||||||
prefixes := []string{"", "Ki", "Mi", "Gi", "Ti"}
|
|
||||||
return s, prefixes[steps]
|
|
||||||
default:
|
|
||||||
return "0", ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetRequestContextFromCookies(auth paas.Auth) map[string]string {
|
func GetRequestContextFromCookies(auth paas.Auth) map[string]string {
|
||||||
return map[string]string{
|
return map[string]string{
|
||||||
"username": auth.Username,
|
"username": auth.Username,
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ func HandleGETAccount(c *gin.Context) {
|
|||||||
// create a resource chart for resource depending on resource type
|
// create a resource chart for resource depending on resource type
|
||||||
switch t := v.(type) {
|
switch t := v.(type) {
|
||||||
case NumericResource:
|
case NumericResource:
|
||||||
avail, prefix := common.FormatNumber(t.Total.Avail*t.Multiplier, t.Base)
|
avail, prefix := paas.FormatNumber(t.Total.Avail*t.Multiplier, t.Base)
|
||||||
pools[poolname].Resources[category].(map[string]any)[resource] = ResourceChart{
|
pools[poolname].Resources[category].(map[string]any)[resource] = ResourceChart{
|
||||||
Type: t.Type,
|
Type: t.Type,
|
||||||
Display: t.Display,
|
Display: t.Display,
|
||||||
@@ -132,7 +132,7 @@ func HandleGETAccount(c *gin.Context) {
|
|||||||
ColorHex: InterpolateColorHSV(Green, Red, float64(t.Total.Used)/float64(t.Total.Max)).ToHTML(),
|
ColorHex: InterpolateColorHSV(Green, Red, float64(t.Total.Used)/float64(t.Total.Max)).ToHTML(),
|
||||||
}
|
}
|
||||||
case StorageResource:
|
case StorageResource:
|
||||||
avail, prefix := common.FormatNumber(t.Total.Avail*t.Multiplier, t.Base)
|
avail, prefix := paas.FormatNumber(t.Total.Avail*t.Multiplier, t.Base)
|
||||||
pools[poolname].Resources[category].(map[string]any)[resource] = ResourceChart{
|
pools[poolname].Resources[category].(map[string]any)[resource] = ResourceChart{
|
||||||
Type: t.Type,
|
Type: t.Type,
|
||||||
Display: t.Display,
|
Display: t.Display,
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ func GetInstanceBackups(vm paas.InstancePath, auth paas.Auth) ([]InstanceBackup,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := range backups {
|
for i := range backups {
|
||||||
size, prefix := common.FormatNumber(backups[i].Size, 1024)
|
size, prefix := paas.FormatNumber(backups[i].Size, 1024)
|
||||||
backups[i].SizeFormatted = fmt.Sprintf("%s %sB", size, prefix)
|
backups[i].SizeFormatted = fmt.Sprintf("%s %sB", size, prefix)
|
||||||
|
|
||||||
t := time.Unix(backups[i].CTime, 0)
|
t := time.Unix(backups[i].CTime, 0)
|
||||||
|
|||||||
+1
-1
Submodule proxmoxaas-common-lib updated: ad6b8211b3...372fd452c7
Reference in New Issue
Block a user