From 1a642b98769ea604dfb74fa4d875bf42c24fda9f Mon Sep 17 00:00:00 2001 From: bingoohuang Date: Fri, 29 May 2020 15:37:58 +0800 Subject: [PATCH 1/2] add Volume Ids column only for max 100 volumes for convenience in the master ui. --- weed/server/master_ui/templates.go | 2 ++ weed/topology/data_node.go | 18 +++++++++++++ weed/util/inits.go | 43 ++++++++++++++++++++++++++++++ weed/util/inits_test.go | 14 ++++++++++ 4 files changed, 77 insertions(+) create mode 100644 weed/util/inits.go create mode 100644 weed/util/inits_test.go diff --git a/weed/server/master_ui/templates.go b/weed/server/master_ui/templates.go index b674e3f82..7189064d0 100644 --- a/weed/server/master_ui/templates.go +++ b/weed/server/master_ui/templates.go @@ -76,6 +76,7 @@ var StatusTpl = template.Must(template.New("status").Parse(` Rack RemoteAddr #Volumes + Volume Ids #ErasureCodingShards Max @@ -89,6 +90,7 @@ var StatusTpl = template.Must(template.New("status").Parse(` {{ $rack.Id }} {{ $dn.Url }} {{ $dn.Volumes }} + {{ $dn.VolumeIds}} {{ $dn.EcShards }} {{ $dn.Max }} diff --git a/weed/topology/data_node.go b/weed/topology/data_node.go index 617341e54..f6e96e235 100644 --- a/weed/topology/data_node.go +++ b/weed/topology/data_node.go @@ -2,6 +2,7 @@ package topology import ( "fmt" + "github.com/chrislusf/seaweedfs/weed/util" "strconv" "sync" @@ -166,6 +167,7 @@ func (dn *DataNode) ToMap() interface{} { ret := make(map[string]interface{}) ret["Url"] = dn.Url() ret["Volumes"] = dn.GetVolumeCount() + ret["VolumeIds"] = dn.GetVolumeIds() ret["EcShards"] = dn.GetEcShardCount() ret["Max"] = dn.GetMaxVolumeCount() ret["Free"] = dn.FreeSpace() @@ -190,3 +192,19 @@ func (dn *DataNode) ToDataNodeInfo() *master_pb.DataNodeInfo { } return m } + +// GetVolumeIds returns the human readable volume ids limited to count of max 100. +func (dn *DataNode) GetVolumeIds() string { + volumesLen := len(dn.volumes) + if volumesLen > 100 { + return "..." + } + + ids := make([]int, 0, volumesLen) + + for k := range dn.volumes { + ids = append(ids, int(k)) + } + + return util.HumanReadableInts(ids...) +} diff --git a/weed/util/inits.go b/weed/util/inits.go new file mode 100644 index 000000000..dfbf9da5f --- /dev/null +++ b/weed/util/inits.go @@ -0,0 +1,43 @@ +package util + +import ( + "fmt" + "sort" +) + +// HumanReadableInts joins a serials of inits into a smart one like 1-3 5 7-10 for human readable. +func HumanReadableInts(ids ...int) string { + sort.Ints(ids) + + s := "" + start := 0 + last := 0 + + for i, v := range ids { + if i == 0 { + start = v + last = v + s = fmt.Sprintf("%d", v) + continue + } + + if last+1 == v { + last = v + continue + } + + if last > start { + s += fmt.Sprintf("-%d", last) + } + + s += fmt.Sprintf(" %d", v) + start = v + last = v + } + + if last != start { + s += fmt.Sprintf("-%d", last) + } + + return s +} diff --git a/weed/util/inits_test.go b/weed/util/inits_test.go new file mode 100644 index 000000000..f1ccb4f7b --- /dev/null +++ b/weed/util/inits_test.go @@ -0,0 +1,14 @@ +package util + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestHumanReadableInts(t *testing.T) { + assert.Equal(t, "1-3", HumanReadableInts(1, 2, 3)) + assert.Equal(t, "1 3", HumanReadableInts(1, 3)) + assert.Equal(t, "1 3 5", HumanReadableInts(5, 1, 3)) + assert.Equal(t, "1-3 5", HumanReadableInts(1, 2, 3, 5)) + assert.Equal(t, "1-3 5 7-9", HumanReadableInts(7, 9, 8, 1, 2, 3, 5)) +} From 1f8782a1ed504e5f7e7b62218bcf4502903d350f Mon Sep 17 00:00:00 2001 From: bingoohuang Date: Fri, 29 May 2020 16:15:33 +0800 Subject: [PATCH 2/2] try showing the first 100 volume ids and an extra ... --- weed/topology/data_node.go | 9 ++------- weed/util/inits.go | 9 +++++++++ weed/util/inits_test.go | 5 +++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/weed/topology/data_node.go b/weed/topology/data_node.go index f6e96e235..0dccdd0f2 100644 --- a/weed/topology/data_node.go +++ b/weed/topology/data_node.go @@ -195,16 +195,11 @@ func (dn *DataNode) ToDataNodeInfo() *master_pb.DataNodeInfo { // GetVolumeIds returns the human readable volume ids limited to count of max 100. func (dn *DataNode) GetVolumeIds() string { - volumesLen := len(dn.volumes) - if volumesLen > 100 { - return "..." - } - - ids := make([]int, 0, volumesLen) + ids := make([]int, 0, len(dn.volumes)) for k := range dn.volumes { ids = append(ids, int(k)) } - return util.HumanReadableInts(ids...) + return util.HumanReadableIntsMax(100, ids...) } diff --git a/weed/util/inits.go b/weed/util/inits.go index dfbf9da5f..378878012 100644 --- a/weed/util/inits.go +++ b/weed/util/inits.go @@ -5,6 +5,15 @@ import ( "sort" ) +// HumanReadableIntsMax joins a serials of inits into a smart one like 1-3 5 ... for human readable. +func HumanReadableIntsMax(max int, ids ...int) string { + if len(ids) <= max { + return HumanReadableInts(ids...) + } + + return HumanReadableInts(ids[:max]...) + " ..." +} + // HumanReadableInts joins a serials of inits into a smart one like 1-3 5 7-10 for human readable. func HumanReadableInts(ids ...int) string { sort.Ints(ids) diff --git a/weed/util/inits_test.go b/weed/util/inits_test.go index f1ccb4f7b..f2c9b701f 100644 --- a/weed/util/inits_test.go +++ b/weed/util/inits_test.go @@ -5,6 +5,11 @@ import ( "testing" ) +func TestHumanReadableIntsMax(t *testing.T) { + assert.Equal(t, "1-2 ...", HumanReadableIntsMax(2, 1, 2, 3)) + assert.Equal(t, "1 3 ...", HumanReadableIntsMax(2, 1, 3, 5)) +} + func TestHumanReadableInts(t *testing.T) { assert.Equal(t, "1-3", HumanReadableInts(1, 2, 3)) assert.Equal(t, "1 3", HumanReadableInts(1, 3))