优化节点列表

This commit is contained in:
GoEdgeLab
2022-04-07 18:31:38 +08:00
parent 63d070f7e0
commit cc1e806cee
7 changed files with 329 additions and 33 deletions

View File

@@ -19,6 +19,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/andybalholm/brotli"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/logs"
"github.com/iwind/TeaGo/types"
stringutil "github.com/iwind/TeaGo/utils/string"
@@ -181,19 +182,21 @@ func (this *NodeService) ListEnabledNodesMatch(ctx context.Context, req *pb.List
tx := this.NullTx()
clusterDNS, err := models.SharedNodeClusterDAO.FindClusterDNSInfo(tx, req.NodeClusterId, nil)
if err != nil {
return nil, err
}
var dnsDomainId = int64(0)
var domainRoutes = []*dnstypes.Route{}
dnsDomainId := int64(0)
domainRoutes := []*dnstypes.Route{}
if clusterDNS != nil {
dnsDomainId = int64(clusterDNS.DnsDomainId)
if clusterDNS.DnsDomainId > 0 {
domainRoutes, err = dns.SharedDNSDomainDAO.FindDomainRoutes(tx, dnsDomainId)
if err != nil {
return nil, err
if req.NodeClusterId > 0 {
clusterDNS, err := models.SharedNodeClusterDAO.FindClusterDNSInfo(tx, req.NodeClusterId, nil)
if err != nil {
return nil, err
}
if clusterDNS != nil {
dnsDomainId = int64(clusterDNS.DnsDomainId)
if clusterDNS.DnsDomainId > 0 {
domainRoutes, err = dns.SharedDNSDomainDAO.FindDomainRoutes(tx, dnsDomainId)
if err != nil {
return nil, err
}
}
}
}
@@ -216,13 +219,18 @@ func (this *NodeService) ListEnabledNodesMatch(ctx context.Context, req *pb.List
order = "trafficOutAsc"
} else if req.TrafficOutDesc {
order = "trafficOutDesc"
} else if req.LoadAsc {
order = "loadAsc"
} else if req.LoadDesc {
order = "loadDesc"
}
nodes, err := models.SharedNodeDAO.ListEnabledNodesMatch(tx, req.NodeClusterId, configutils.ToBoolState(req.InstallState), configutils.ToBoolState(req.ActiveState), req.Keyword, req.NodeGroupId, req.NodeRegionId, req.Level, true, order, req.Offset, req.Size)
if err != nil {
return nil, err
}
result := []*pb.Node{}
var result = []*pb.Node{}
var cacheMap = utils.NewCacheMap()
for _, node := range nodes {
// 主集群信息
clusterName, err := models.SharedNodeClusterDAO.FindNodeClusterName(tx, int64(node.ClusterId))
@@ -277,19 +285,54 @@ func (this *NodeService) ListEnabledNodesMatch(ctx context.Context, req *pb.List
}
// DNS线路
routeCodes, err := node.DNSRouteCodesForDomainId(dnsDomainId)
if err != nil {
return nil, err
}
pbRoutes := []*pb.DNSRoute{}
for _, routeCode := range routeCodes {
for _, route := range domainRoutes {
if route.Code == routeCode {
pbRoutes = append(pbRoutes, &pb.DNSRoute{
Name: route.Name,
Code: route.Code,
})
break
var pbRoutes = []*pb.DNSRoute{}
if dnsDomainId > 0 {
routeCodes, err := node.DNSRouteCodesForDomainId(dnsDomainId)
if err != nil {
return nil, err
}
for _, routeCode := range routeCodes {
for _, route := range domainRoutes {
if route.Code == routeCode {
pbRoutes = append(pbRoutes, &pb.DNSRoute{
Name: route.Name,
Code: route.Code,
})
break
}
}
}
} else if req.NodeClusterId == 0 {
var clusterDomainIds = []int64{}
for _, clusterId := range node.AllClusterIds() {
clusterDNSInfo, err := models.SharedNodeClusterDAO.FindClusterDNSInfo(tx, clusterId, cacheMap)
if err != nil {
return nil, err
}
if clusterDNSInfo != nil && clusterDNSInfo.DnsDomainId > 0 {
clusterDomainIds = append(clusterDomainIds, int64(clusterDNSInfo.DnsDomainId))
}
}
for domainId, routeCodes := range node.DNSRouteCodes() {
if domainId == 0 {
continue
}
if !lists.ContainsInt64(clusterDomainIds, domainId) {
continue
}
for _, routeCode := range routeCodes {
routeName, err := dns.SharedDNSDomainDAO.FindDomainRouteName(tx, domainId, routeCode)
if err != nil {
return nil, err
}
if len(routeName) > 0 {
pbRoutes = append(pbRoutes, &pb.DNSRoute{
Name: routeName,
Code: routeCode,
})
}
}
}
}
@@ -310,12 +353,18 @@ func (this *NodeService) ListEnabledNodesMatch(ctx context.Context, req *pb.List
}
}
// 状态
statusJSON, err := models.SharedNodeValueDAO.ComposeNodeStatusJSON(tx, nodeconfigs.NodeRoleNode, int64(node.Id), node.Status)
if err != nil {
return nil, err
}
result = append(result, &pb.Node{
Id: int64(node.Id),
Name: node.Name,
Version: int64(node.Version),
IsInstalled: node.IsInstalled,
StatusJSON: node.Status,
StatusJSON: statusJSON,
NodeCluster: &pb.NodeCluster{
Id: int64(node.ClusterId),
Name: clusterName,
@@ -455,6 +504,7 @@ func (this *NodeService) FindEnabledNode(ctx context.Context, req *pb.FindEnable
if err != nil {
return nil, err
}
var clusterIds = []int64{int64(node.ClusterId)}
// 从集群信息
var secondaryPBClusters []*pb.NodeCluster
@@ -471,6 +521,7 @@ func (this *NodeService) FindEnabledNode(ctx context.Context, req *pb.FindEnable
IsOn: cluster.IsOn,
Name: cluster.Name,
})
clusterIds = append(clusterIds, int64(cluster.Id))
}
// 认证信息
@@ -556,10 +607,49 @@ func (this *NodeService) FindEnabledNode(ctx context.Context, req *pb.FindEnable
}
}
// 线路
var pbRoutes = []*pb.DNSRoute{}
var clusterDomainIds = []int64{}
for _, clusterId := range node.AllClusterIds() {
clusterDNSInfo, err := models.SharedNodeClusterDAO.FindClusterDNSInfo(tx, clusterId, nil)
if err != nil {
return nil, err
}
if clusterDNSInfo != nil && clusterDNSInfo.DnsDomainId > 0 {
clusterDomainIds = append(clusterDomainIds, int64(clusterDNSInfo.DnsDomainId))
}
}
for domainId, routeCodes := range node.DNSRouteCodes() {
if domainId == 0 {
continue
}
if !lists.ContainsInt64(clusterDomainIds, domainId) {
continue
}
for _, routeCode := range routeCodes {
routeName, err := dns.SharedDNSDomainDAO.FindDomainRouteName(tx, domainId, routeCode)
if err != nil {
return nil, err
}
if len(routeName) > 0 {
pbRoutes = append(pbRoutes, &pb.DNSRoute{
Name: routeName,
Code: routeCode,
})
}
}
}
// 监控状态
statusJSON, err := models.SharedNodeValueDAO.ComposeNodeStatusJSON(tx, nodeconfigs.NodeRoleNode, int64(node.Id), node.Status)
if err != nil {
return nil, err
}
return &pb.FindEnabledNodeResponse{Node: &pb.Node{
Id: int64(node.Id),
Name: node.Name,
StatusJSON: node.Status,
StatusJSON: statusJSON,
UniqueId: node.UniqueId,
Version: int64(node.Version),
LatestVersion: int64(node.LatestVersion),
@@ -582,6 +672,7 @@ func (this *NodeService) FindEnabledNode(ctx context.Context, req *pb.FindEnable
MaxCacheMemoryCapacity: pbMaxCacheMemoryCapacity,
CacheDiskDir: node.CacheDiskDir,
Level: int32(node.Level),
DnsRoutes: pbRoutes,
}}, nil
}

View File

@@ -6,7 +6,9 @@ import (
"context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/types"
)
type NodeValueService struct {
@@ -60,7 +62,7 @@ func (this *NodeValueService) ListNodeValues(ctx context.Context, req *pb.ListNo
if err != nil {
return nil, err
}
pbValues := []*pb.NodeValue{}
var pbValues = []*pb.NodeValue{}
for _, value := range values {
pbValues = append(pbValues, &pb.NodeValue{
ValueJSON: value.Value,
@@ -70,3 +72,80 @@ func (this *NodeValueService) ListNodeValues(ctx context.Context, req *pb.ListNo
return &pb.ListNodeValuesResponse{NodeValues: pbValues}, nil
}
// SumAllNodeValueStats 读取所有节点的最新数据
func (this *NodeValueService) SumAllNodeValueStats(ctx context.Context, req *pb.SumAllNodeValueStatsRequest) (*pb.SumAllNodeValueStatsResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
var result = &pb.SumAllNodeValueStatsResponse{}
// traffic
{
total, _, _, err := models.SharedNodeValueDAO.SumAllNodeValues(tx, nodeconfigs.NodeRoleNode, nodeconfigs.NodeValueItemTrafficOut, "total", 1, nodeconfigs.NodeValueDurationUnitMinute)
if err != nil {
return nil, err
}
result.TotalTrafficBytesPerSecond = types.Int64(total) / 60
}
// cpu
{
_, avg, max, err := models.SharedNodeValueDAO.SumAllNodeValues(tx, nodeconfigs.NodeRoleNode, nodeconfigs.NodeValueItemCPU, "usage", 1, nodeconfigs.NodeValueDurationUnitMinute)
if err != nil {
return nil, err
}
result.AvgCPUUsage = types.Float32(avg)
result.MaxCPUUsage = types.Float32(max)
}
{
total, _, _, err := models.SharedNodeValueDAO.SumAllNodeValues(tx, nodeconfigs.NodeRoleNode, nodeconfigs.NodeValueItemCPU, "cores", 1, nodeconfigs.NodeValueDurationUnitMinute)
if err != nil {
return nil, err
}
result.TotalCPUCores = types.Int32(total)
}
// memory
{
_, avg, max, err := models.SharedNodeValueDAO.SumAllNodeValues(tx, nodeconfigs.NodeRoleNode, nodeconfigs.NodeValueItemMemory, "usage", 1, nodeconfigs.NodeValueDurationUnitMinute)
if err != nil {
return nil, err
}
result.AvgMemoryUsage = types.Float32(avg)
result.MaxMemoryUsage = types.Float32(max)
}
{
total, _, _, err := models.SharedNodeValueDAO.SumAllNodeValues(tx, nodeconfigs.NodeRoleNode, nodeconfigs.NodeValueItemMemory, "total", 1, nodeconfigs.NodeValueDurationUnitMinute)
if err != nil {
return nil, err
}
result.TotalMemoryBytes = types.Int64(total)
}
// load
{
_, avg, max, err := models.SharedNodeValueDAO.SumAllNodeValues(tx, nodeconfigs.NodeRoleNode, nodeconfigs.NodeValueItemLoad, "load1m", 1, nodeconfigs.NodeValueDurationUnitMinute)
if err != nil {
return nil, err
}
result.AvgLoad1Min = types.Float32(avg)
result.MaxLoad1Min = types.Float32(max)
}
{
_, avg, _, err := models.SharedNodeValueDAO.SumAllNodeValues(tx, nodeconfigs.NodeRoleNode, nodeconfigs.NodeValueItemLoad, "load5m", 1, nodeconfigs.NodeValueDurationUnitMinute)
if err != nil {
return nil, err
}
result.AvgLoad5Min = types.Float32(avg)
}
return result, nil
}

View File

@@ -433,6 +433,7 @@ func (this *ServerGroupService) FindEnabledServerGroupConfigInfo(ctx context.Con
ServerGroupId: int64(group.Id),
}
// http
if len(group.HttpReverseProxy) > 0 {
var ref = &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal(group.HttpReverseProxy, ref)
@@ -442,6 +443,7 @@ func (this *ServerGroupService) FindEnabledServerGroupConfigInfo(ctx context.Con
result.HasHTTPReverseProxy = ref.IsPrior
}
// tcp
if len(group.TcpReverseProxy) > 0 {
var ref = &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal(group.TcpReverseProxy, ref)
@@ -451,6 +453,7 @@ func (this *ServerGroupService) FindEnabledServerGroupConfigInfo(ctx context.Con
result.HasTCPReverseProxy = ref.IsPrior
}
// udp
if len(group.UdpReverseProxy) > 0 {
var ref = &serverconfigs.ReverseProxyRef{}
err = json.Unmarshal(group.UdpReverseProxy, ref)