improve account resource chart creation,
now allows categories to have the same name as another resource without collision issue
This commit is contained in:
+26
-17
@@ -13,7 +13,12 @@ import (
|
|||||||
|
|
||||||
type Account struct {
|
type Account struct {
|
||||||
paas.User
|
paas.User
|
||||||
Pools map[string]paas.Pool
|
Pools map[string]Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
type Pool struct {
|
||||||
|
paas.Pool `mapstructure:",squash"`
|
||||||
|
ResourceCharts map[string]map[string]any
|
||||||
}
|
}
|
||||||
|
|
||||||
// numerical constraint
|
// numerical constraint
|
||||||
@@ -113,14 +118,14 @@ func HandleGETAccount(c *gin.Context) {
|
|||||||
|
|
||||||
for poolname, pool := range pools {
|
for poolname, pool := range pools {
|
||||||
// for each resource category
|
// for each resource category
|
||||||
for category := range pool.Resources {
|
for categoryname, category := range pool.ResourceCharts {
|
||||||
// for each resource in each category
|
// for each resource in each category
|
||||||
for resource, v := range pool.Resources[category].(map[string]any) {
|
for resourcename, resource := range category {
|
||||||
// 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 := resource.(type) {
|
||||||
case NumericResource:
|
case NumericResource:
|
||||||
avail, prefix := paas.FormatNumber(paas.SafeUint64(t.Total.Avail*t.Multiplier), t.Base)
|
avail, prefix := paas.FormatNumber(paas.SafeUint64(t.Total.Avail*t.Multiplier), t.Base)
|
||||||
pools[poolname].Resources[category].(map[string]any)[resource] = ResourceChart{
|
pools[poolname].ResourceCharts[categoryname][resourcename] = ResourceChart{
|
||||||
Type: t.Type,
|
Type: t.Type,
|
||||||
Display: t.Display,
|
Display: t.Display,
|
||||||
Name: t.Name,
|
Name: t.Name,
|
||||||
@@ -133,7 +138,7 @@ func HandleGETAccount(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
case StorageResource:
|
case StorageResource:
|
||||||
avail, prefix := paas.FormatNumber(paas.SafeUint64(t.Total.Avail*t.Multiplier), t.Base)
|
avail, prefix := paas.FormatNumber(paas.SafeUint64(t.Total.Avail*t.Multiplier), t.Base)
|
||||||
pools[poolname].Resources[category].(map[string]any)[resource] = ResourceChart{
|
pools[poolname].ResourceCharts[categoryname][resourcename] = ResourceChart{
|
||||||
Type: t.Type,
|
Type: t.Type,
|
||||||
Display: t.Display,
|
Display: t.Display,
|
||||||
Name: t.Name,
|
Name: t.Name,
|
||||||
@@ -168,12 +173,11 @@ func HandleGETAccount(c *gin.Context) {
|
|||||||
ColorHex: InterpolateColorHSV(Green, Red, float64(r.Used)/float64(r.Max)).ToHTML(),
|
ColorHex: InterpolateColorHSV(Green, Red, float64(r.Used)/float64(r.Max)).ToHTML(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pools[poolname].Resources[category].(map[string]any)[resource] = l
|
pools[poolname].ResourceCharts[categoryname][resourcename] = l
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
account.Pools = pools
|
account.Pools = pools
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "html/account.html", gin.H{
|
c.HTML(http.StatusOK, "html/account.html", gin.H{
|
||||||
@@ -200,8 +204,8 @@ func GetUser(auth paas.Auth) (Account, error) {
|
|||||||
return account, err
|
return account, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUserPools(auth paas.Auth) (map[string]paas.Pool, error) {
|
func GetUserPools(auth paas.Auth) (map[string]Pool, error) {
|
||||||
pools := map[string]paas.Pool{}
|
pools := map[string]Pool{}
|
||||||
|
|
||||||
// get all pools
|
// get all pools
|
||||||
body := map[string]any{}
|
body := map[string]any{}
|
||||||
@@ -232,6 +236,10 @@ func GetUserPools(auth paas.Auth) (map[string]paas.Pool, error) {
|
|||||||
// for each pool
|
// for each pool
|
||||||
for poolname, pool := range pools {
|
for poolname, pool := range pools {
|
||||||
// for each resource in pool data
|
// for each resource in pool data
|
||||||
|
|
||||||
|
// create pool charts map
|
||||||
|
pool.ResourceCharts = make(map[string]map[string]any)
|
||||||
|
|
||||||
for k, v := range pool.Resources {
|
for k, v := range pool.Resources {
|
||||||
m := meta[k].(map[string]any)
|
m := meta[k].(map[string]any)
|
||||||
t := m["type"].(string)
|
t := m["type"].(string)
|
||||||
@@ -239,11 +247,11 @@ func GetUserPools(auth paas.Auth) (map[string]paas.Pool, error) {
|
|||||||
category := m["category"].(string)
|
category := m["category"].(string)
|
||||||
|
|
||||||
// create a category if it does not already exist
|
// create a category if it does not already exist
|
||||||
if _, ok := pool.Resources[category]; !ok {
|
if _, ok := pool.ResourceCharts[category]; !ok {
|
||||||
pool.Resources[category] = map[string]any{}
|
pool.ResourceCharts[category] = map[string]any{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// depending on type, decode the pool data into the corresponding resource type
|
// depending on type, decode the apool data into the corresponding resource type
|
||||||
switch t {
|
switch t {
|
||||||
case "numeric":
|
case "numeric":
|
||||||
n := NumericResource{}
|
n := NumericResource{}
|
||||||
@@ -253,7 +261,7 @@ func GetUserPools(auth paas.Auth) (map[string]paas.Pool, error) {
|
|||||||
if err_m != nil || err_r != nil {
|
if err_m != nil || err_r != nil {
|
||||||
return pools, fmt.Errorf("%s\n%s", err_m.Error(), err_r.Error())
|
return pools, fmt.Errorf("%s\n%s", err_m.Error(), err_r.Error())
|
||||||
}
|
}
|
||||||
pools[poolname].Resources[category].(map[string]any)[k] = n
|
pool.ResourceCharts[category][k] = n
|
||||||
case "storage":
|
case "storage":
|
||||||
n := StorageResource{}
|
n := StorageResource{}
|
||||||
n.Type = t
|
n.Type = t
|
||||||
@@ -262,7 +270,7 @@ func GetUserPools(auth paas.Auth) (map[string]paas.Pool, error) {
|
|||||||
if err_m != nil || err_r != nil {
|
if err_m != nil || err_r != nil {
|
||||||
return pools, fmt.Errorf("%s\n%s", err_m.Error(), err_r.Error())
|
return pools, fmt.Errorf("%s\n%s", err_m.Error(), err_r.Error())
|
||||||
}
|
}
|
||||||
pools[poolname].Resources[category].(map[string]any)[k] = n
|
pool.ResourceCharts[category][k] = n
|
||||||
case "list":
|
case "list":
|
||||||
n := ListResource{}
|
n := ListResource{}
|
||||||
n.Type = t
|
n.Type = t
|
||||||
@@ -271,12 +279,13 @@ func GetUserPools(auth paas.Auth) (map[string]paas.Pool, error) {
|
|||||||
if err_m != nil || err_r != nil {
|
if err_m != nil || err_r != nil {
|
||||||
return pools, fmt.Errorf("%s\n%s", err_m.Error(), err_r.Error())
|
return pools, fmt.Errorf("%s\n%s", err_m.Error(), err_r.Error())
|
||||||
}
|
}
|
||||||
pools[poolname].Resources[category].(map[string]any)[k] = n
|
pool.ResourceCharts[category][k] = n
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete the old entry, only categories should be left at the end of the loop
|
// delete the old entry, only categories should be left at the end of the loop
|
||||||
delete(pools[poolname].Resources, k)
|
//delete(pools[poolname].Resources, k)
|
||||||
}
|
}
|
||||||
|
pools[poolname] = pool
|
||||||
}
|
}
|
||||||
|
|
||||||
return pools, nil
|
return pools, nil
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<p id="nodes">Nodes: {{MapKeys .AllowedNodes ", "}}</p>
|
<p id="nodes">Nodes: {{MapKeys .AllowedNodes ", "}}</p>
|
||||||
<p id="backups">Max Backups Per Instance: {{.AllowedBackups.MaxPerInstance}} Max Backups Total: {{.AllowedBackups.MaxTotal}}</p>
|
<p id="backups">Max Backups Per Instance: {{.AllowedBackups.MaxPerInstance}} Max Backups Total: {{.AllowedBackups.MaxTotal}}</p>
|
||||||
<div>
|
<div>
|
||||||
{{range $category, $v := .Resources}}
|
{{range $category, $v := .ResourceCharts}}
|
||||||
{{if eq $category ""}}
|
{{if eq $category ""}}
|
||||||
<h4>Generic</h4>
|
<h4>Generic</h4>
|
||||||
{{else}}
|
{{else}}
|
||||||
|
|||||||
Reference in New Issue
Block a user