improve account resource chart creation,

now allows categories to have the same name as another resource without collision issue
This commit is contained in:
alu
2026-07-22 18:17:20 +00:00
parent 7809e6adb1
commit ca386adc26
3 changed files with 103 additions and 85 deletions
+133 -115
View File
@@ -12,8 +12,13 @@ import (
) )
type Account struct { type Account struct {
paas.User 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
@@ -98,29 +103,147 @@ var Green = color.RGB{
func HandleGETAccount(c *gin.Context) { func HandleGETAccount(c *gin.Context) {
auth, err := common.GetAuthFromRequest(c) auth, err := common.GetAuthFromRequest(c)
if err == nil { if err == nil {
account := Account{}
account, err := GetUser(auth) user, err := GetUserBasic(auth)
if err != nil { if err != nil {
common.HandleNonFatalError(c, err) common.HandleNonFatalError(c, err)
return return
} }
account.User = user
pools, err := GetUserPools(auth) pools, err := GetUserPools(auth)
if err != nil { if err != nil {
common.HandleNonFatalError(c, err) common.HandleNonFatalError(c, err)
return return
} }
account.Pools = pools
account.FormatPoolResourceCharts()
c.HTML(http.StatusOK, "html/account.html", gin.H{
"global": common.Global,
"page": "account",
"account": account,
})
} else {
c.Redirect(http.StatusFound, "/login") // if user is not authed, redirect user to login page
}
}
func GetUserBasic(auth paas.Auth) (paas.User, error) {
user := paas.User{}
body := map[string]any{}
res, code, err := common.RequestGetAPI(fmt.Sprintf("/access/users/%s", auth.Username), &auth, &body)
if err != nil {
return user, err
}
if code != 200 {
return user, fmt.Errorf("request to /access/pools resulted in %+v", res)
}
err = mapstructure.Decode(body["user"], &user)
return user, err
}
func GetUserPools(auth paas.Auth) (map[string]Pool, error) {
pools := map[string]Pool{}
// get all pools
body := map[string]any{}
res, code, err := common.RequestGetAPI("/access/pools", &auth, &body)
if err != nil {
return pools, err
}
if code != 200 {
return pools, fmt.Errorf("request to /access/pools resulted in %+v", res)
}
err = mapstructure.Decode(body["pools"].(map[string]any), &pools)
if err != nil {
return pools, err
}
// get global config for resource type metadata
body = map[string]any{}
// get resource meta data
res, code, err = common.RequestGetAPI("/global/config/resources", &auth, &body)
if err != nil {
return pools, err
}
if code != 200 {
return pools, fmt.Errorf("request to /global/config/resources resulted in %+v", res)
}
meta := body["resources"].(map[string]any)
// for each pool
for poolname, pool := range pools {
// for each resource in pool data
// create pool charts map
pool.ResourceCharts = make(map[string]map[string]any)
for k, v := range pool.Resources {
m := meta[k].(map[string]any)
t := m["type"].(string)
r := v.(map[string]any)
category := m["category"].(string)
// create a category if it does not already exist
if _, ok := pool.ResourceCharts[category]; !ok {
pool.ResourceCharts[category] = map[string]any{}
}
// depending on type, decode the apool data into the corresponding resource type
switch t {
case "numeric":
n := NumericResource{}
n.Type = t
err_m := mapstructure.Decode(m, &n)
err_r := mapstructure.Decode(r, &n)
if err_m != nil || err_r != nil {
return pools, fmt.Errorf("%s\n%s", err_m.Error(), err_r.Error())
}
pool.ResourceCharts[category][k] = n
case "storage":
n := StorageResource{}
n.Type = t
err_m := mapstructure.Decode(m, &n)
err_r := mapstructure.Decode(r, &n)
if err_m != nil || err_r != nil {
return pools, fmt.Errorf("%s\n%s", err_m.Error(), err_r.Error())
}
pool.ResourceCharts[category][k] = n
case "list":
n := ListResource{}
n.Type = t
err_m := mapstructure.Decode(m, &n)
err_r := mapstructure.Decode(r, &n)
if err_m != nil || err_r != nil {
return pools, fmt.Errorf("%s\n%s", err_m.Error(), err_r.Error())
}
pool.ResourceCharts[category][k] = n
}
// delete the old entry, only categories should be left at the end of the loop
//delete(pools[poolname].Resources, k)
}
pools[poolname] = pool
}
return pools, nil
}
func (account *Account) FormatPoolResourceCharts() error {
pools := account.Pools
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 +256,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,118 +291,13 @@ 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 return nil
c.HTML(http.StatusOK, "html/account.html", gin.H{
"global": common.Global,
"page": "account",
"account": account,
})
} else {
c.Redirect(http.StatusFound, "/login") // if user is not authed, redirect user to login page
}
}
func GetUser(auth paas.Auth) (Account, error) {
account := Account{}
body := map[string]any{}
res, code, err := common.RequestGetAPI(fmt.Sprintf("/access/users/%s", auth.Username), &auth, &body)
if err != nil {
return account, err
}
if code != 200 {
return account, fmt.Errorf("request to /access/pools resulted in %+v", res)
}
err = mapstructure.Decode(body, &account)
return account, err
}
func GetUserPools(auth paas.Auth) (map[string]paas.Pool, error) {
pools := map[string]paas.Pool{}
// get all pools
body := map[string]any{}
res, code, err := common.RequestGetAPI("/access/pools", &auth, &body)
if err != nil {
return pools, err
}
if code != 200 {
return pools, fmt.Errorf("request to /access/pools resulted in %+v", res)
}
err = mapstructure.Decode(body["pools"].(map[string]any), &pools)
if err != nil {
return pools, err
}
// get global config for resource type metadata
body = map[string]any{}
// get resource meta data
res, code, err = common.RequestGetAPI("/global/config/resources", &auth, &body)
if err != nil {
return pools, err
}
if code != 200 {
return pools, fmt.Errorf("request to /global/config/resources resulted in %+v", res)
}
meta := body["resources"].(map[string]any)
// for each pool
for poolname, pool := range pools {
// for each resource in pool data
for k, v := range pool.Resources {
m := meta[k].(map[string]any)
t := m["type"].(string)
r := v.(map[string]any)
category := m["category"].(string)
// create a category if it does not already exist
if _, ok := pool.Resources[category]; !ok {
pool.Resources[category] = map[string]any{}
}
// depending on type, decode the pool data into the corresponding resource type
switch t {
case "numeric":
n := NumericResource{}
n.Type = t
err_m := mapstructure.Decode(m, &n)
err_r := mapstructure.Decode(r, &n)
if err_m != nil || err_r != nil {
return pools, fmt.Errorf("%s\n%s", err_m.Error(), err_r.Error())
}
pools[poolname].Resources[category].(map[string]any)[k] = n
case "storage":
n := StorageResource{}
n.Type = t
err_m := mapstructure.Decode(m, &n)
err_r := mapstructure.Decode(r, &n)
if err_m != nil || err_r != nil {
return pools, fmt.Errorf("%s\n%s", err_m.Error(), err_r.Error())
}
pools[poolname].Resources[category].(map[string]any)[k] = n
case "list":
n := ListResource{}
n.Type = t
err_m := mapstructure.Decode(m, &n)
err_r := mapstructure.Decode(r, &n)
if err_m != nil || err_r != nil {
return pools, fmt.Errorf("%s\n%s", err_m.Error(), err_r.Error())
}
pools[poolname].Resources[category].(map[string]any)[k] = n
}
// delete the old entry, only categories should be left at the end of the loop
delete(pools[poolname].Resources, k)
}
}
return pools, nil
} }
// interpolate between min and max by normalized (0 - 1) val // interpolate between min and max by normalized (0 - 1) val
+2 -2
View File
@@ -39,8 +39,8 @@
<h2>Account</h2> <h2>Account</h2>
<section class="w3-card w3-padding"> <section class="w3-card w3-padding">
<h3>Account Details</h3> <h3>Account Details</h3>
<p id="username">Username: {{.account.Username.UserID}}@{{.account.Username.Realm}}</p> <p id="username">Username: {{.account.User.Username.UserID}}@{{.account.User.Username.Realm}}</p>
<p id="email">Email: {{.account.Mail}}</p> <p id="email">Email: {{.account.User.Mail}}</p>
<p>Password: <button class="w3-button" id="change-password" type="button" style="padding: 0em; height: 1.5em; line-height: 1.5em;">Change Password</button></p> <p>Password: <button class="w3-button" id="change-password" type="button" style="padding: 0em; height: 1.5em; line-height: 1.5em;">Change Password</button></p>
</section> </section>
{{range $poolname, $pool := .account.Pools}} {{range $poolname, $pool := .account.Pools}}
+1 -1
View File
@@ -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}}