diff --git a/internal/web/actions/default/clusters/cluster/init.go b/internal/web/actions/default/clusters/cluster/init.go index 49237a30..2ab975ba 100644 --- a/internal/web/actions/default/clusters/cluster/init.go +++ b/internal/web/actions/default/clusters/cluster/init.go @@ -44,6 +44,7 @@ func init() { Post("/node/monitor/load", new(monitor.LoadAction)). Post("/node/monitor/trafficIn", new(monitor.TrafficInAction)). Post("/node/monitor/trafficOut", new(monitor.TrafficOutAction)). + Post("/node/monitor/connections", new(monitor.ConnectionsAction)). // 分组相关 Get("/groups", new(groups.IndexAction)). diff --git a/internal/web/actions/default/clusters/cluster/node/monitor/connections.go b/internal/web/actions/default/clusters/cluster/node/monitor/connections.go new file mode 100644 index 00000000..f4aa111f --- /dev/null +++ b/internal/web/actions/default/clusters/cluster/node/monitor/connections.go @@ -0,0 +1,73 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package monitor + +import ( + "encoding/json" + "github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils" + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/maps" + timeutil "github.com/iwind/TeaGo/utils/time" + "time" +) + +type ConnectionsAction struct { + actionutils.ParentAction +} + +func (this *ConnectionsAction) RunPost(params struct { + NodeId int64 +}) { + resp, err := this.RPC().NodeValueRPC().ListNodeValues(this.AdminContext(), &pb.ListNodeValuesRequest{ + Role: "node", + NodeId: params.NodeId, + Item: nodeconfigs.NodeValueItemConnections, + Range: nodeconfigs.NodeValueRangeMinute, + }) + if err != nil { + this.ErrorPage(err) + return + } + valuesMap := map[string]int64{} // YmdHi => count + for _, v := range resp.NodeValues { + if len(v.ValueJSON) == 0 { + continue + } + + valueMap := maps.Map{} + err = json.Unmarshal(v.ValueJSON, &valueMap) + if err != nil { + this.ErrorPage(err) + return + } + + valuesMap[timeutil.FormatTime("YmdHi", v.CreatedAt)] = valueMap.GetInt64("total") + } + + // 过去一个小时 + result := []maps.Map{} + for i := 60; i >= 1; i-- { + timestamp := time.Now().Unix() - int64(i)*60 + minute := timeutil.FormatTime("YmdHi", timestamp) + total, ok := valuesMap[minute] + if ok { + result = append(result, maps.Map{ + "label": timeutil.FormatTime("H:i", timestamp), + "value": total, + "text": numberutils.FormatInt64(total), + }) + } else { + result = append(result, maps.Map{ + "label": timeutil.FormatTime("H:i", timestamp), + "value": 0, + "text": "0", + }) + } + } + + this.Data["values"] = result + + this.Success() +} diff --git a/web/views/@default/clusters/cluster/node/monitor/index.html b/web/views/@default/clusters/cluster/node/monitor/index.html index 615d7b37..37fbb5cf 100644 --- a/web/views/@default/clusters/cluster/node/monitor/index.html +++ b/web/views/@default/clusters/cluster/node/monitor/index.html @@ -12,6 +12,9 @@