From 63b2fe0d76f094aad9c501b01168a1659168ff3b Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sat, 3 Jan 2026 22:45:48 -0800 Subject: [PATCH] fix: EC UI template error when viewing shard details (#7955) * fix: EC UI template error when viewing shard details Fixed field name mismatch in volume.html where it was using .ShardDetails instead of .Shards. Added a robust type conversion wrapper in templates.go to handle int64 to uint64 conversion for bytesToHumanReadable. Added regression test to ensure future stability. * refactor: improve bytesToHumanReadable and test robustness - Handled more integer types (uint32, int32, uint) in bytesToHumanReadable. - Improved volume_test.go to verify both shards are formatted correctly. * refactor: add bounds checking to bytesToHumanReadable Added checks for negative values in signed integer types to avoid incorrect formatting when converting to uint64. Addressed feedback from coderabbitai. --- weed/server/volume_server_ui/templates.go | 33 +- weed/server/volume_server_ui/volume.html | 329 ++++++++++---------- weed/server/volume_server_ui/volume_test.go | 76 +++++ 3 files changed, 274 insertions(+), 164 deletions(-) create mode 100644 weed/server/volume_server_ui/volume_test.go diff --git a/weed/server/volume_server_ui/templates.go b/weed/server/volume_server_ui/templates.go index a434c55cf..436e461fe 100644 --- a/weed/server/volume_server_ui/templates.go +++ b/weed/server/volume_server_ui/templates.go @@ -3,16 +3,45 @@ package volume_server_ui import ( _ "embed" "fmt" - "github.com/seaweedfs/seaweedfs/weed/util" "html/template" "strconv" "strings" + + "github.com/seaweedfs/seaweedfs/weed/util" ) func percentFrom(total uint64, part_of uint64) string { return fmt.Sprintf("%.2f", (float64(part_of)/float64(total))*100) } +func bytesToHumanReadable(b interface{}) string { + switch v := b.(type) { + case uint64: + return util.BytesToHumanReadable(v) + case int64: + if v < 0 { + return fmt.Sprintf("%d B", v) + } + return util.BytesToHumanReadable(uint64(v)) + case int: + if v < 0 { + return fmt.Sprintf("%d B", v) + } + return util.BytesToHumanReadable(uint64(v)) + case uint32: + return util.BytesToHumanReadable(uint64(v)) + case int32: + if v < 0 { + return fmt.Sprintf("%d B", v) + } + return util.BytesToHumanReadable(uint64(v)) + case uint: + return util.BytesToHumanReadable(uint64(v)) + default: + return fmt.Sprintf("%v", b) + } +} + func join(data []int64) string { var ret []string for _, d := range data { @@ -23,7 +52,7 @@ func join(data []int64) string { var funcMap = template.FuncMap{ "join": join, - "bytesToHumanReadable": util.BytesToHumanReadable, + "bytesToHumanReadable": bytesToHumanReadable, "percentFrom": percentFrom, "isNotEmpty": util.IsNotEmpty, } diff --git a/weed/server/volume_server_ui/volume.html b/weed/server/volume_server_ui/volume.html index 605eb52f0..28703ebd2 100644 --- a/weed/server/volume_server_ui/volume.html +++ b/weed/server/volume_server_ui/volume.html @@ -1,11 +1,12 @@ + SeaweedFS {{ .Version }} + src="/seaweedfsstatic/javascript/jquery-sparklines/2.1.2/jquery.sparkline.min.js">