Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca386adc26 |
+80
-71
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
type Account struct {
|
||||
paas.User
|
||||
User paas.User
|
||||
Pools map[string]Pool
|
||||
}
|
||||
|
||||
@@ -103,83 +103,24 @@ var Green = color.RGB{
|
||||
func HandleGETAccount(c *gin.Context) {
|
||||
auth, err := common.GetAuthFromRequest(c)
|
||||
if err == nil {
|
||||
account := Account{}
|
||||
|
||||
account, err := GetUser(auth)
|
||||
user, err := GetUserBasic(auth)
|
||||
if err != nil {
|
||||
common.HandleNonFatalError(c, err)
|
||||
return
|
||||
}
|
||||
account.User = user
|
||||
|
||||
pools, err := GetUserPools(auth)
|
||||
if err != nil {
|
||||
common.HandleNonFatalError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
for poolname, pool := range pools {
|
||||
// for each resource category
|
||||
for categoryname, category := range pool.ResourceCharts {
|
||||
// for each resource in each category
|
||||
for resourcename, resource := range category {
|
||||
// create a resource chart for resource depending on resource type
|
||||
switch t := resource.(type) {
|
||||
case NumericResource:
|
||||
avail, prefix := paas.FormatNumber(paas.SafeUint64(t.Total.Avail*t.Multiplier), t.Base)
|
||||
pools[poolname].ResourceCharts[categoryname][resourcename] = ResourceChart{
|
||||
Type: t.Type,
|
||||
Display: t.Display,
|
||||
Name: t.Name,
|
||||
Used: t.Total.Used,
|
||||
Max: t.Total.Max,
|
||||
Avail: avail,
|
||||
Prefix: prefix,
|
||||
Unit: t.Unit,
|
||||
ColorHex: InterpolateColorHSV(Green, Red, float64(t.Total.Used)/float64(t.Total.Max)).ToHTML(),
|
||||
}
|
||||
case StorageResource:
|
||||
avail, prefix := paas.FormatNumber(paas.SafeUint64(t.Total.Avail*t.Multiplier), t.Base)
|
||||
pools[poolname].ResourceCharts[categoryname][resourcename] = ResourceChart{
|
||||
Type: t.Type,
|
||||
Display: t.Display,
|
||||
Name: t.Name,
|
||||
Used: t.Total.Used,
|
||||
Max: t.Total.Max,
|
||||
Avail: avail,
|
||||
Prefix: prefix,
|
||||
Unit: t.Unit,
|
||||
ColorHex: InterpolateColorHSV(Green, Red, float64(t.Total.Used)/float64(t.Total.Max)).ToHTML(),
|
||||
}
|
||||
case ListResource:
|
||||
l := struct {
|
||||
Type string
|
||||
Display bool
|
||||
Resources []ResourceChart
|
||||
}{
|
||||
Type: t.Type,
|
||||
Display: t.Display,
|
||||
Resources: []ResourceChart{},
|
||||
}
|
||||
|
||||
for _, r := range t.Total {
|
||||
avail := fmt.Sprintf("%d", r.Avail)
|
||||
l.Resources = append(l.Resources, ResourceChart{
|
||||
Type: t.Type,
|
||||
Display: t.Display,
|
||||
Name: r.Name,
|
||||
Used: r.Used,
|
||||
Max: r.Max,
|
||||
Avail: avail, // usually an int
|
||||
Unit: "",
|
||||
ColorHex: InterpolateColorHSV(Green, Red, float64(r.Used)/float64(r.Max)).ToHTML(),
|
||||
})
|
||||
}
|
||||
pools[poolname].ResourceCharts[categoryname][resourcename] = l
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
account.Pools = pools
|
||||
|
||||
account.FormatPoolResourceCharts()
|
||||
|
||||
c.HTML(http.StatusOK, "html/account.html", gin.H{
|
||||
"global": common.Global,
|
||||
"page": "account",
|
||||
@@ -190,18 +131,18 @@ func HandleGETAccount(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func GetUser(auth paas.Auth) (Account, error) {
|
||||
account := Account{}
|
||||
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 account, err
|
||||
return user, err
|
||||
}
|
||||
if code != 200 {
|
||||
return account, fmt.Errorf("request to /access/pools resulted in %+v", res)
|
||||
return user, fmt.Errorf("request to /access/pools resulted in %+v", res)
|
||||
}
|
||||
err = mapstructure.Decode(body, &account)
|
||||
return account, err
|
||||
err = mapstructure.Decode(body["user"], &user)
|
||||
return user, err
|
||||
}
|
||||
|
||||
func GetUserPools(auth paas.Auth) (map[string]Pool, error) {
|
||||
@@ -291,6 +232,74 @@ func GetUserPools(auth paas.Auth) (map[string]Pool, error) {
|
||||
return pools, nil
|
||||
}
|
||||
|
||||
func (account *Account) FormatPoolResourceCharts() error {
|
||||
pools := account.Pools
|
||||
for poolname, pool := range pools {
|
||||
// for each resource category
|
||||
for categoryname, category := range pool.ResourceCharts {
|
||||
// for each resource in each category
|
||||
for resourcename, resource := range category {
|
||||
// create a resource chart for resource depending on resource type
|
||||
switch t := resource.(type) {
|
||||
case NumericResource:
|
||||
avail, prefix := paas.FormatNumber(paas.SafeUint64(t.Total.Avail*t.Multiplier), t.Base)
|
||||
pools[poolname].ResourceCharts[categoryname][resourcename] = ResourceChart{
|
||||
Type: t.Type,
|
||||
Display: t.Display,
|
||||
Name: t.Name,
|
||||
Used: t.Total.Used,
|
||||
Max: t.Total.Max,
|
||||
Avail: avail,
|
||||
Prefix: prefix,
|
||||
Unit: t.Unit,
|
||||
ColorHex: InterpolateColorHSV(Green, Red, float64(t.Total.Used)/float64(t.Total.Max)).ToHTML(),
|
||||
}
|
||||
case StorageResource:
|
||||
avail, prefix := paas.FormatNumber(paas.SafeUint64(t.Total.Avail*t.Multiplier), t.Base)
|
||||
pools[poolname].ResourceCharts[categoryname][resourcename] = ResourceChart{
|
||||
Type: t.Type,
|
||||
Display: t.Display,
|
||||
Name: t.Name,
|
||||
Used: t.Total.Used,
|
||||
Max: t.Total.Max,
|
||||
Avail: avail,
|
||||
Prefix: prefix,
|
||||
Unit: t.Unit,
|
||||
ColorHex: InterpolateColorHSV(Green, Red, float64(t.Total.Used)/float64(t.Total.Max)).ToHTML(),
|
||||
}
|
||||
case ListResource:
|
||||
l := struct {
|
||||
Type string
|
||||
Display bool
|
||||
Resources []ResourceChart
|
||||
}{
|
||||
Type: t.Type,
|
||||
Display: t.Display,
|
||||
Resources: []ResourceChart{},
|
||||
}
|
||||
|
||||
for _, r := range t.Total {
|
||||
avail := fmt.Sprintf("%d", r.Avail)
|
||||
l.Resources = append(l.Resources, ResourceChart{
|
||||
Type: t.Type,
|
||||
Display: t.Display,
|
||||
Name: r.Name,
|
||||
Used: r.Used,
|
||||
Max: r.Max,
|
||||
Avail: avail, // usually an int
|
||||
Unit: "",
|
||||
ColorHex: InterpolateColorHSV(Green, Red, float64(r.Used)/float64(r.Max)).ToHTML(),
|
||||
})
|
||||
}
|
||||
pools[poolname].ResourceCharts[categoryname][resourcename] = l
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// interpolate between min and max by normalized (0 - 1) val
|
||||
func InterpolateColorHSV(min color.RGB, max color.RGB, val float64) color.RGB {
|
||||
minhsl := min.ToHSL()
|
||||
|
||||
@@ -39,8 +39,8 @@
|
||||
<h2>Account</h2>
|
||||
<section class="w3-card w3-padding">
|
||||
<h3>Account Details</h3>
|
||||
<p id="username">Username: {{.account.Username.UserID}}@{{.account.Username.Realm}}</p>
|
||||
<p id="email">Email: {{.account.Mail}}</p>
|
||||
<p id="username">Username: {{.account.User.Username.UserID}}@{{.account.User.Username.Realm}}</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>
|
||||
</section>
|
||||
{{range $poolname, $pool := .account.Pools}}
|
||||
|
||||
Reference in New Issue
Block a user