Files
EdgeAdmin/internal/web/actions/default/settings/user-nodes/index.go

83 lines
2.0 KiB
Go
Raw Normal View History

2020-12-14 21:24:21 +08:00
package usernodes
import (
2021-01-20 10:08:50 +08:00
"encoding/json"
"fmt"
2021-09-27 09:23:48 +08:00
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
2020-12-14 21:24:21 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
2021-01-20 10:08:50 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
2020-12-14 21:24:21 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
2021-01-20 10:08:50 +08:00
"github.com/iwind/TeaGo/logs"
2020-12-14 21:24:21 +08:00
"github.com/iwind/TeaGo/maps"
2021-01-20 10:08:50 +08:00
"time"
2020-12-14 21:24:21 +08:00
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "node", "index")
}
func (this *IndexAction) RunGet(params struct{}) {
2021-09-27 09:23:48 +08:00
if !teaconst.IsPlus {
this.RedirectURL("/")
return
}
2020-12-14 21:24:21 +08:00
countResp, err := this.RPC().UserNodeRPC().CountAllEnabledUserNodes(this.AdminContext(), &pb.CountAllEnabledUserNodesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
count := countResp.Count
page := this.NewPage(count)
this.Data["page"] = page.AsHTML()
nodeMaps := []maps.Map{}
if count > 0 {
nodesResp, err := this.RPC().UserNodeRPC().ListEnabledUserNodes(this.AdminContext(), &pb.ListEnabledUserNodesRequest{
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
for _, node := range nodesResp.UserNodes {
2021-01-20 10:08:50 +08:00
// 状态
status := &nodeconfigs.NodeStatus{}
if len(node.StatusJSON) > 0 {
err = json.Unmarshal(node.StatusJSON, &status)
if err != nil {
logs.Error(err)
continue
}
status.IsActive = status.IsActive && time.Now().Unix()-status.UpdatedAt <= 60 // N秒之内认为活跃
}
2020-12-14 21:24:21 +08:00
nodeMaps = append(nodeMaps, maps.Map{
"id": node.Id,
"isOn": node.IsOn,
"name": node.Name,
"accessAddrs": node.AccessAddrs,
2021-01-20 10:08:50 +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),
"buildVersion": status.BuildVersion,
},
2020-12-14 21:24:21 +08:00
})
}
}
this.Data["nodes"] = nodeMaps
this.Show()
}