Files
EdgeAdmin/internal/web/actions/default/nodes/index.go

71 lines
1.8 KiB
Go
Raw Normal View History

2020-07-22 22:19:39 +08:00
package nodes
2020-07-29 19:34:54 +08:00
import (
2020-08-21 12:32:16 +08:00
"encoding/json"
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/configs/nodes"
2020-07-29 19:34:54 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
2020-08-21 12:32:16 +08:00
"github.com/iwind/TeaGo/logs"
2020-07-29 19:34:54 +08:00
"github.com/iwind/TeaGo/maps"
2020-08-21 12:32:16 +08:00
"time"
2020-07-29 19:34:54 +08:00
)
2020-07-22 22:19:39 +08:00
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
2020-07-29 19:34:54 +08:00
this.Nav("", "node", "index")
2020-07-22 22:19:39 +08:00
}
func (this *IndexAction) RunGet(params struct{}) {
2020-07-29 19:34:54 +08:00
countResp, err := this.RPC().NodeRPC().CountAllEnabledNodes(this.AdminContext(), &pb.CountAllEnabledNodesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
page := this.NewPage(countResp.Count)
this.Data["page"] = page.AsHTML()
nodesResp, err := this.RPC().NodeRPC().ListEnabledNodes(this.AdminContext(), &pb.ListEnabledNodesRequest{
Offset: page.Offset,
Size: page.Size,
})
nodeMaps := []maps.Map{}
for _, node := range nodesResp.Nodes {
2020-08-21 12:32:16 +08:00
// 状态
status := &nodes.NodeStatus{}
if len(node.Status) > 0 && node.Status != "null" {
err = json.Unmarshal([]byte(node.Status), &status)
if err != nil {
logs.Error(err)
continue
}
status.IsActive = time.Now().Unix()-status.UpdatedAt < 120 // 2分钟之内认为活跃
}
2020-07-29 19:34:54 +08:00
nodeMaps = append(nodeMaps, maps.Map{
"id": node.Id,
"name": node.Name,
2020-08-21 12:32:16 +08:00
"status": maps.Map{
"isActive": status.IsActive,
"updatedAt": status.UpdatedAt,
"hostname": status.Hostname,
"cpuUsage": status.CPUUsage,
"cpuUsageText": fmt.Sprintf("%.2f%%", status.CPUUsage*100),
"memUsage": status.MemoryUsage,
"memUsageText": fmt.Sprintf("%.2f%%", status.MemoryUsage*100),
},
2020-07-29 19:34:54 +08:00
"cluster": maps.Map{
"id": node.Cluster.Id,
"name": node.Cluster.Name,
},
})
}
this.Data["nodes"] = nodeMaps
2020-07-22 22:19:39 +08:00
this.Show()
}