From 9b6192aaaab029b862706e9d563f961225120ad3 Mon Sep 17 00:00:00 2001 From: bugaolengdeyuxiaoer <937426472@qq.com> Date: Thu, 25 Nov 2021 10:47:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=92=A1=20add=20log,=20fix=20some?= =?UTF-8?q?=20bugs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cp/scenarios/cmp-dashboard-nodes.yaml | 2 +- .../cmp-cluster-list/list/render.go | 33 +++++--- .../cmp-dashboard-nodes/common/table/table.go | 40 +++++++-- .../common/table/table_test.go | 83 ++++++++++++++++++- modules/cmp/metrics/metrics.go | 6 ++ 5 files changed, 141 insertions(+), 23 deletions(-) diff --git a/conf/cmp/i18n/cp/scenarios/cmp-dashboard-nodes.yaml b/conf/cmp/i18n/cp/scenarios/cmp-dashboard-nodes.yaml index 85929f5420e..2519f15732e 100644 --- a/conf/cmp/i18n/cp/scenarios/cmp-dashboard-nodes.yaml +++ b/conf/cmp/i18n/cp/scenarios/cmp-dashboard-nodes.yaml @@ -98,7 +98,7 @@ zh: Drain following nodes: 驱散以下节点: Offline following nodes: 下线以下节点: Online following nodes: 上线以下节点: - show: 查看 + show: 查看pods undrained: 此节点中还存在未驱散的Pod! isOffline: 已下线 no select: 未选择 diff --git a/modules/cmp/component-protocol/components/cmp-cluster-list/list/render.go b/modules/cmp/component-protocol/components/cmp-cluster-list/list/render.go index c73521e9fc8..34fd1544942 100644 --- a/modules/cmp/component-protocol/components/cmp-cluster-list/list/render.go +++ b/modules/cmp/component-protocol/components/cmp-cluster-list/list/render.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "os" + "strconv" "strings" "sync" "time" @@ -33,6 +34,7 @@ import ( "github.com/erda-project/erda/apistructs" "github.com/erda-project/erda/bundle" "github.com/erda-project/erda/modules/cmp" + "github.com/erda-project/erda/modules/cmp/cache" "github.com/erda-project/erda/modules/cmp/component-protocol/components/cmp-cluster-list/common" "github.com/erda-project/erda/modules/cmp/component-protocol/types" "github.com/erda-project/erda/modules/cmp/metrics" @@ -62,8 +64,8 @@ func (l *List) Init(ctx servicehub.Context) error { func (l *List) Render(ctx context.Context, c *cptype.Component, scenario cptype.Scenario, event cptype.ComponentEvent, gs *cptype.GlobalStateData) error { var ( - err error - data map[string][]DataItem + err error + d map[string][]DataItem ) l.SDK = cputil.SDK(ctx) @@ -79,11 +81,11 @@ func (l *List) Render(ctx context.Context, c *cptype.Component, scenario cptype. return nil } - data, err = l.GetData(ctx) + d, err = l.GetData(ctx) if err != nil { return err } - l.Data = data + l.Data = d err = l.SetComponentValue(c) if err != nil { return err @@ -98,13 +100,18 @@ func (l *List) GetMetrics(ctx context.Context, clusterName string) map[string]*m OrgId: l.SDK.Identity.OrgID, Cluster: clusterName, Kind: metrics.Node, - Type: metrics.Disk, } metricsData, err := metricsServer.NodeMetrics(ctx, req) if err != nil { logrus.Error(err) return nil } + if metricsData == nil || len(metricsData) == 0 { + obj, _, _ := cache.GetFreeCache().Get(cache.GenerateKey(clusterName, metrics.NodeAll)) + if obj != nil { + metricsData = obj[0].Value().(map[string]*metrics.MetricsData) + } + } return metricsData } @@ -254,11 +261,12 @@ func (l *List) GetData(ctx context.Context) (map[string][]DataItem, error) { clusters []apistructs.ClusterInfo nodes []data.Object ) - //orgId, err := strconv.ParseUint(, 10, 64) - //if err != nil { - // logrus.Errorf("org id parse err :%v", err) - //} - clusters, err = l.Bdl.ListClusters("") + orgId, err := strconv.ParseUint(l.SDK.Identity.OrgID, 10, 64) + if err != nil { + logrus.Errorf("org id parse err :%v", err) + } + logrus.Infof("cluster start get data") + clusters, err = l.Bdl.ListClusters("", orgId) if err != nil { return nil, err } @@ -274,6 +282,7 @@ func (l *List) GetData(ctx context.Context) (map[string][]DataItem, error) { clusterInfos[clusters[i].Name] = &ClusterInfoDetail{} } go func() { + logrus.Infof("get nodes start") for i := 0; i < len(clusters); i++ { nodes, err = l.GetNodes(clusters[i].Name) usedData := res[clusters[i].Name] @@ -318,6 +327,7 @@ func (l *List) GetData(ctx context.Context) (map[string][]DataItem, error) { wg.Done() }() go func() { + logrus.Infof("start query cluster info") for _, c := range clusters { if ci, err := l.Bdl.QueryClusterInfo(c.Name); err != nil { errStr := fmt.Sprintf("failed to queryclusterinfo: %v, cluster: %v", err, c.Name) @@ -356,10 +366,12 @@ func (l *List) GetData(ctx context.Context) (map[string][]DataItem, error) { clusterInfos[c.Name].RawStatus = statusStr } } + logrus.Infof("query cluster info finished") wg.Done() }() wg.Wait() di := make([]DataItem, 0) + logrus.Infof("start set data") for _, c := range clusters { var bgImg = "" if c.Type == "k8s" { @@ -383,6 +395,7 @@ func (l *List) GetData(ctx context.Context) (map[string][]DataItem, error) { d := make(map[string][]DataItem) d["list"] = di + logrus.Infof("cluster get data finished") return d, nil } diff --git a/modules/cmp/component-protocol/components/cmp-dashboard-nodes/common/table/table.go b/modules/cmp/component-protocol/components/cmp-dashboard-nodes/common/table/table.go index bf69b7b65ea..101505b6609 100644 --- a/modules/cmp/component-protocol/components/cmp-dashboard-nodes/common/table/table.go +++ b/modules/cmp/component-protocol/components/cmp-dashboard-nodes/common/table/table.go @@ -21,6 +21,7 @@ import ( "fmt" "reflect" "sort" + "strconv" "strings" "sync" @@ -122,10 +123,10 @@ type RowItem struct { Role Role `json:"Role,omitempty"` Version string `json:"Version,omitempty"` // - Distribution Distribution `json:"Distribution,omitempty"` - Usage Distribution `json:"Usage,omitempty"` - DistributionRate string `json:"DistributionRate,omitempty"` - Operate Operate `json:"Operate,omitempty"` + Distribution Distribution `json:"Distribution,omitempty"` + Usage Distribution `json:"Usage,omitempty"` + DistributionRate DistributionRate `json:"DistributionRate,omitempty"` + Operate Operate `json:"Operate,omitempty"` // batchOperations for json BatchOperations []string `json:"batchOperations,omitempty"` } @@ -223,6 +224,12 @@ type Distribution struct { Tip string `json:"tip,omitempty"` } +type DistributionRate struct { + RenderType string `json:"renderType,omitempty"` + Value string `json:"value"` + DistributionValue float64 `json:"distributionValue"` +} + type DistributionValue struct { Text string `json:"text"` Percent string `json:"percent"` @@ -310,14 +317,15 @@ func (t *Table) GetDistributionValue(req, total float64, resourceType TableType) } } -func (t *Table) GetDistributionRate(allocate, request float64, resourceType TableType) string { +func (t *Table) GetDistributionRate(allocate, request float64, resourceType TableType) DistributionRate { rate := allocate / request + rate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", rate), 64) if rate <= 0.4 { - return t.SDK.I18n("Low") + return DistributionRate{RenderType: "text", Value: t.SDK.I18n("Low"), DistributionValue: rate} } else if rate <= 0.8 { - return t.SDK.I18n("Middle") + return DistributionRate{RenderType: "text", Value: t.SDK.I18n("Middle"), DistributionValue: rate} } else { - return t.SDK.I18n("High") + return DistributionRate{RenderType: "text", Value: t.SDK.I18n("High"), DistributionValue: rate} } } @@ -421,6 +429,8 @@ func (t *Table) RenderList(component *cptype.Component, tableType TableType, gs SortByRole(items, sortColumn, asc) case reflect.TypeOf(Distribution{}): SortByDistribution(items, sortColumn, asc) + case reflect.TypeOf(DistributionRate{}): + SortByDistributionRate(items, sortColumn, asc) case reflect.TypeOf(SteveStatus{}): SortByStatus(items, sortColumn, asc) default: @@ -814,6 +824,20 @@ func SortByDistribution(data []RowItem, sortColumn string, ascend bool) { }) } +// SortByDistributionRate sort by percent +func SortByDistributionRate(data []RowItem, sortColumn string, ascend bool) { + sort.Slice(data, func(i, j int) bool { + a := reflect.ValueOf(data[i]) + b := reflect.ValueOf(data[j]) + aValue := a.FieldByName(sortColumn).FieldByName("DistributionValue").Float() + bValue := b.FieldByName(sortColumn).FieldByName("DistributionValue").Float() + if ascend { + return aValue < bValue + } + return aValue > bValue + }) +} + // SortByStatus sort by percent func SortByStatus(data []RowItem, _ string, asc bool) { sort.Slice(data, func(i, j int) bool { diff --git a/modules/cmp/component-protocol/components/cmp-dashboard-nodes/common/table/table_test.go b/modules/cmp/component-protocol/components/cmp-dashboard-nodes/common/table/table_test.go index 4e7f7ffd73e..1aac502caad 100644 --- a/modules/cmp/component-protocol/components/cmp-dashboard-nodes/common/table/table_test.go +++ b/modules/cmp/component-protocol/components/cmp-dashboard-nodes/common/table/table_test.go @@ -173,6 +173,69 @@ func TestSortByDistribution(t *testing.T) { }) } } +func TestSortByDistributionRate(t *testing.T) { + type args struct { + data []RowItem + sortColumn string + asc bool + } + tests := []struct { + name string + args args + }{ + { + name: "testDistribution", + args: args{ + data: []RowItem{{ + DistributionRate: DistributionRate{ + RenderType: "", + DistributionValue: 1, + }, + }, { + DistributionRate: DistributionRate{ + RenderType: "", + DistributionValue: 2, + }, + }, { + DistributionRate: DistributionRate{ + RenderType: "", + DistributionValue: 3, + }, + }}, + sortColumn: "DistributionRate", + asc: true, + }, + }, + { + name: "testUsage", + args: args{ + data: []RowItem{{ + DistributionRate: DistributionRate{ + RenderType: "", + DistributionValue: 1, + }, + }, { + DistributionRate: DistributionRate{ + RenderType: "", + DistributionValue: 2, + }, + }, { + DistributionRate: DistributionRate{ + RenderType: "", + DistributionValue: 3, + }, + }}, + sortColumn: "DistributionRate", + asc: false, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + SortByDistributionRate(tt.args.data, tt.args.sortColumn, tt.args.asc) + }) + } +} func TestSortByString(t *testing.T) { type args struct { @@ -312,7 +375,7 @@ func TestTable_GetUnusedRate(t1 *testing.T) { name string fields fields args args - want string + want DistributionRate }{ // TODO: Add test cases. { @@ -323,7 +386,11 @@ func TestTable_GetUnusedRate(t1 *testing.T) { b: 1.2, resourceType: Memory, }, - want: "High", + want: DistributionRate{ + RenderType: "text", + Value: "High", + DistributionValue: 0.92, + }, }, { name: "text", @@ -333,7 +400,11 @@ func TestTable_GetUnusedRate(t1 *testing.T) { b: 1.2, resourceType: Memory, }, - want: "Middle", + want: DistributionRate{ + RenderType: "text", + Value: "Middle", + DistributionValue: 0.67, + }, }, { name: "text", @@ -343,7 +414,11 @@ func TestTable_GetUnusedRate(t1 *testing.T) { b: 1.2, resourceType: Memory, }, - want: "Low", + want: DistributionRate{ + RenderType: "text", + Value: "Low", + DistributionValue: 0.25, + }, }, } for _, tt := range tests { diff --git a/modules/cmp/metrics/metrics.go b/modules/cmp/metrics/metrics.go index bc169c5fc66..4de750e2732 100644 --- a/modules/cmp/metrics/metrics.go +++ b/modules/cmp/metrics/metrics.go @@ -48,6 +48,8 @@ const ( Pod = "pod" Node = "node" + NodeAll = "nodeall" + syncKey = "metricsCacheSync" queryTimeout = 30 * time.Second @@ -219,6 +221,9 @@ func (m *Metric) Store(response *pb.QueryWithInfluxFormatResponse, metricsReques } } } + if metricsRequest.resKind == Node { + SetCache(cache.GenerateKey(metricsRequest.rawReq.Params["cluster_name"].GetStringValue(), NodeAll), res) + } return res } return nil @@ -297,6 +302,7 @@ func (m *Metric) NodeMetrics(ctx context.Context, req *MetricsRequest) (map[stri case resp := <-c: for key, v := range resp { noNeed[key] = v + logrus.Info(key, "===", v.Used) } } return noNeed, nil