Files
EdgeAdmin/internal/web/actions/default/clusters/cluster/index.go

225 lines
6.0 KiB
Go
Raw Normal View History

2020-09-06 16:19:34 +08:00
package cluster
2020-07-22 22:19:39 +08:00
2020-07-29 19:34:54 +08:00
import (
2020-08-21 12:32:16 +08:00
"encoding/json"
"fmt"
2020-07-29 19:34:54 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
2020-09-26 08:07:18 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
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-09-13 20:37:07 +08:00
"github.com/iwind/TeaGo/types"
2020-11-02 09:37:20 +08:00
"strconv"
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-09-13 20:37:07 +08:00
this.Nav("", "node", "index")
2020-09-06 16:19:34 +08:00
this.SecondMenu("nodes")
2020-07-22 22:19:39 +08:00
}
2020-09-06 16:19:34 +08:00
func (this *IndexAction) RunGet(params struct {
2020-09-13 20:37:07 +08:00
ClusterId int64
GroupId int64
2020-12-10 16:11:41 +08:00
RegionId int64
2020-09-13 20:37:07 +08:00
InstalledState int
2020-09-26 08:07:18 +08:00
ActiveState int
2020-10-28 18:21:11 +08:00
Keyword string
2020-09-06 16:19:34 +08:00
}) {
this.Data["groupId"] = params.GroupId
2020-12-10 16:11:41 +08:00
this.Data["regionId"] = params.RegionId
2020-09-13 20:37:07 +08:00
this.Data["installState"] = params.InstalledState
2020-09-26 08:07:18 +08:00
this.Data["activeState"] = params.ActiveState
2020-10-28 18:21:11 +08:00
this.Data["keyword"] = params.Keyword
2020-09-13 20:37:07 +08:00
2021-04-18 15:17:49 +08:00
countAllResp, err := this.RPC().NodeRPC().CountAllEnabledNodesMatch(this.AdminContext(), &pb.CountAllEnabledNodesMatchRequest{
NodeClusterId: params.ClusterId,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["countAll"] = countAllResp.Count
2020-09-06 16:19:34 +08:00
countResp, err := this.RPC().NodeRPC().CountAllEnabledNodesMatch(this.AdminContext(), &pb.CountAllEnabledNodesMatchRequest{
NodeClusterId: params.ClusterId,
2021-05-25 17:48:51 +08:00
NodeGroupId: params.GroupId,
NodeRegionId: params.RegionId,
InstallState: types.Int32(params.InstalledState),
ActiveState: types.Int32(params.ActiveState),
Keyword: params.Keyword,
2020-09-06 16:19:34 +08:00
})
2020-07-29 19:34:54 +08:00
if err != nil {
this.ErrorPage(err)
return
}
page := this.NewPage(countResp.Count)
this.Data["page"] = page.AsHTML()
2020-09-06 16:19:34 +08:00
nodesResp, err := this.RPC().NodeRPC().ListEnabledNodesMatch(this.AdminContext(), &pb.ListEnabledNodesMatchRequest{
Offset: page.Offset,
Size: page.Size,
NodeClusterId: params.ClusterId,
2021-05-25 17:48:51 +08:00
NodeGroupId: params.GroupId,
NodeRegionId: params.RegionId,
InstallState: types.Int32(params.InstalledState),
ActiveState: types.Int32(params.ActiveState),
Keyword: params.Keyword,
2020-07-29 19:34:54 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
2020-07-29 19:34:54 +08:00
nodeMaps := []maps.Map{}
for _, node := range nodesResp.Nodes {
2020-08-21 12:32:16 +08:00
// 状态
2020-09-26 08:07:18 +08:00
isSynced := false
status := &nodeconfigs.NodeStatus{}
if len(node.StatusJSON) > 0 {
err = json.Unmarshal(node.StatusJSON, &status)
2020-08-21 12:32:16 +08:00
if err != nil {
logs.Error(err)
continue
}
status.IsActive = status.IsActive && time.Now().Unix()-status.UpdatedAt <= 60 // N秒之内认为活跃
2020-09-26 08:07:18 +08:00
isSynced = status.ConfigVersion == node.Version
2020-08-21 12:32:16 +08:00
}
2020-08-21 21:09:42 +08:00
// IP
ipAddressesResp, err := this.RPC().NodeIPAddressRPC().FindAllEnabledIPAddressesWithNodeId(this.AdminContext(), &pb.FindAllEnabledIPAddressesWithNodeIdRequest{
NodeId: node.Id,
Role: nodeconfigs.NodeRoleNode,
})
2020-08-21 21:09:42 +08:00
if err != nil {
this.ErrorPage(err)
return
}
ipAddresses := []maps.Map{}
for _, addr := range ipAddressesResp.Addresses {
ipAddresses = append(ipAddresses, maps.Map{
"id": addr.Id,
"name": addr.Name,
"ip": addr.Ip,
"canAccess": addr.CanAccess,
2020-08-21 21:09:42 +08:00
})
}
2020-12-10 16:11:41 +08:00
// 分组
var groupMap maps.Map = nil
2021-05-25 17:48:51 +08:00
if node.NodeGroup != nil {
groupMap = maps.Map{
2021-05-25 17:48:51 +08:00
"id": node.NodeGroup.Id,
"name": node.NodeGroup.Name,
}
}
2020-12-10 16:11:41 +08:00
// 区域
var regionMap maps.Map = nil
2021-05-25 17:48:51 +08:00
if node.NodeRegion != nil {
2020-12-10 16:11:41 +08:00
regionMap = maps.Map{
2021-05-25 17:48:51 +08:00
"id": node.NodeRegion.Id,
"name": node.NodeRegion.Name,
2020-12-10 16:11:41 +08:00
}
}
2020-11-16 13:03:45 +08:00
// DNS
dnsRouteNames := []string{}
for _, route := range node.DnsRoutes {
dnsRouteNames = append(dnsRouteNames, route.Name)
}
2020-07-29 19:34:54 +08:00
nodeMaps = append(nodeMaps, maps.Map{
2020-09-06 16:19:34 +08:00
"id": node.Id,
"name": node.Name,
"isInstalled": node.IsInstalled,
"isOn": node.IsOn,
"isUp": node.IsUp,
2020-09-13 20:37:07 +08:00
"installStatus": maps.Map{
"isRunning": node.InstallStatus.IsRunning,
"isFinished": node.InstallStatus.IsFinished,
"isOk": node.InstallStatus.IsOk,
"error": node.InstallStatus.Error,
},
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{
2020-12-17 17:35:38 +08:00
"id": node.NodeCluster.Id,
"name": node.NodeCluster.Name,
2020-07-29 19:34:54 +08:00
},
2020-11-16 13:03:45 +08:00
"isSynced": isSynced,
"ipAddresses": ipAddresses,
"group": groupMap,
2020-12-10 16:11:41 +08:00
"region": regionMap,
2020-11-16 13:03:45 +08:00
"dnsRouteNames": dnsRouteNames,
2020-07-29 19:34:54 +08:00
})
}
this.Data["nodes"] = nodeMaps
// 所有分组
groupMaps := []maps.Map{}
2021-05-25 17:48:51 +08:00
groupsResp, err := this.RPC().NodeGroupRPC().FindAllEnabledNodeGroupsWithNodeClusterId(this.AdminContext(), &pb.FindAllEnabledNodeGroupsWithNodeClusterIdRequest{
NodeClusterId: params.ClusterId,
})
if err != nil {
this.ErrorPage(err)
return
}
2021-05-25 17:48:51 +08:00
for _, group := range groupsResp.NodeGroups {
2020-12-10 15:02:55 +08:00
countResp, err := this.RPC().NodeRPC().CountAllEnabledNodesWithNodeGroupId(this.AdminContext(), &pb.CountAllEnabledNodesWithNodeGroupIdRequest{NodeGroupId: group.Id})
if err != nil {
this.ErrorPage(err)
return
}
countNodes := countResp.Count
2020-11-02 09:37:20 +08:00
groupName := group.Name
if countNodes > 0 {
groupName += "(" + strconv.FormatInt(countNodes, 10) + ")"
}
groupMaps = append(groupMaps, maps.Map{
"id": group.Id,
2020-11-02 09:37:20 +08:00
"name": groupName,
"countNodes": countNodes,
})
}
this.Data["groups"] = groupMaps
2020-12-10 16:11:41 +08:00
// 所有区域
regionsResp, err := this.RPC().NodeRegionRPC().FindAllEnabledAndOnNodeRegions(this.AdminContext(), &pb.FindAllEnabledAndOnNodeRegionsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
regionMaps := []maps.Map{}
for _, region := range regionsResp.NodeRegions {
regionMaps = append(regionMaps, maps.Map{
"id": region.Id,
"name": region.Name,
})
}
this.Data["regions"] = regionMaps
2021-05-03 11:32:59 +08:00
// 记录最近访问
_, err = this.RPC().LatestItemRPC().IncreaseLatestItem(this.AdminContext(), &pb.IncreaseLatestItemRequest{
ItemType: "cluster",
ItemId: params.ClusterId,
})
if err != nil {
this.ErrorPage(err)
return
}
2020-07-22 22:19:39 +08:00
this.Show()
}