Skip to content

Commit

Permalink
fix: 💡 add log, fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
bugaolengdeyuxiaoer committed Nov 25, 2021
1 parent 72403c2 commit b0c0d74
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 23 deletions.
2 changes: 1 addition & 1 deletion conf/cmp/i18n/cp/scenarios/cmp-dashboard-nodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ zh:
Drain following nodes: 驱散以下节点:
Offline following nodes: 下线以下节点:
Online following nodes: 上线以下节点:
show: 查看
show: 查看pods
undrained: 此节点中还存在未驱散的Pod!
isOffline: 已下线
no select: 未选择
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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" {
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"sync"

Expand Down Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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}
}
}

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -312,7 +375,7 @@ func TestTable_GetUnusedRate(t1 *testing.T) {
name string
fields fields
args args
want string
want DistributionRate
}{
// TODO: Add test cases.
{
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions modules/cmp/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const (
Pod = "pod"
Node = "node"

NodeAll = "nodeall"

syncKey = "metricsCacheSync"

queryTimeout = 30 * time.Second
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b0c0d74

Please sign in to comment.