From 68e269ea7db07d2b7b703bb3e09a4a66b7a9f996 Mon Sep 17 00:00:00 2001 From: tonysken Date: Fri, 24 Jul 2026 22:12:10 +0100 Subject: [PATCH] fix: fix test --- pkg/gui/presentation/container_stats.go | 38 ++++++++++++-------- pkg/gui/presentation/container_stats_test.go | 20 +++++------ pkg/utils/utils.go | 4 +-- pkg/utils/utils_test.go | 20 +++++------ 4 files changed, 46 insertions(+), 36 deletions(-) diff --git a/pkg/gui/presentation/container_stats.go b/pkg/gui/presentation/container_stats.go index f5e94fd9..534c9efa 100644 --- a/pkg/gui/presentation/container_stats.go +++ b/pkg/gui/presentation/container_stats.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "math" + "os" "reflect" "strconv" "strings" @@ -49,19 +50,19 @@ import ( // }` var PATHS_TO_CONVERT_BIGMETRICS = []map[string]string{ - {"client_stats.cpu_stats.cpu_usage.total_usage": "nanoseconds"}, - {"client_stats.cpu_stats.cpu_usage.percpu_usage": "nanoseconds"}, - {"client_stats.cpu_stats.cpu_usage.usage_in_kernelmode": "nanoseconds"}, - {"client_stats.cpu_stats.cpu_usage.usage_in_usermode": "nanoseconds"}, - {"client_stats.cpu_stats.system_cpu_usage": "nanoseconds"}, - {"client_stats.precpu_stats.cpu_usage.total_usage": "nanoseconds"}, - {"client_stats.precpu_stats.cpu_usage.percpu_usage": "nanoseconds"}, - {"client_stats.precpu_stats.cpu_usage.usage_in_kernelmode": "nanoseconds"}, - {"client_stats.precpu_stats.cpu_usage.usage_in_usermode": "nanoseconds"}, - {"client_stats.precpu_stats.system_cpu_usage": "nanoseconds"}, - {"client_stats.memory_stats.limit": "bytes"}, - {"client_stats.memory_stats.stats.hierarchical_memory_limit": "bytes"}, - {"client_stats.memory_stats.stats.hierarchical_memsw_limit": "bytes"}, + {"ClientStats.cpu_stats.cpu_usage.total_usage": "nanoseconds"}, + {"ClientStats.cpu_stats.cpu_usage.percpu_usage": "nanoseconds"}, + {"ClientStats.cpu_stats.cpu_usage.usage_in_kernelmode": "nanoseconds"}, + {"ClientStats.cpu_stats.cpu_usage.usage_in_usermode": "nanoseconds"}, + {"ClientStats.cpu_stats.system_cpu_usage": "nanoseconds"}, + {"ClientStats.precpu_stats.cpu_usage.total_usage": "nanoseconds"}, + {"ClientStats.precpu_stats.cpu_usage.percpu_usage": "nanoseconds"}, + {"ClientStats.precpu_stats.cpu_usage.usage_in_kernelmode": "nanoseconds"}, + {"ClientStats.precpu_stats.cpu_usage.usage_in_usermode": "nanoseconds"}, + {"ClientStats.precpu_stats.system_cpu_usage": "nanoseconds"}, + {"ClientStats.memory_stats.limit": "bytes"}, + {"ClientStats.memory_stats.stats.hierarchical_memory_limit": "bytes"}, + {"ClientStats.memory_stats.stats.hierarchical_memsw_limit": "bytes"}, } func RenderStats(userConfig *config.UserConfig, container *commands.Container, viewWidth int) (string, error) { @@ -91,11 +92,19 @@ func RenderStats(userConfig *config.UserConfig, container *commands.Container, v var statsMap map[string]interface{} err = json.Unmarshal(statsJsonBytes, &statsMap) + b, _ := json.MarshalIndent(statsMap, "", " ") + _ = os.WriteFile("in.json", b, 0644) if err != nil { return "", err } - convertBigMetricFromSchema(&statsMap) + // err = convertBigMetricFromSchema(&statsMap) + // b, _ = json.MarshalIndent(statsMap, "", " ") + // _ = os.WriteFile("out.json", b, 0644) + // if err != nil { + // _ = os.WriteFile("out", []byte(err.Error()), 0644) + // return "", err + // } originalStats, err := utils.MarshalIntoYaml(statsMap) if err != nil { @@ -111,6 +120,7 @@ func RenderStats(userConfig *config.UserConfig, container *commands.Container, v ) return contents, nil + } // plotGraph returns the plotted graph based on the graph spec and the stat history diff --git a/pkg/gui/presentation/container_stats_test.go b/pkg/gui/presentation/container_stats_test.go index b83777b2..6a0e93ee 100644 --- a/pkg/gui/presentation/container_stats_test.go +++ b/pkg/gui/presentation/container_stats_test.go @@ -18,16 +18,16 @@ func TestConvertBigMetricFromSchema(t *testing.T) { { name: "string schema: converts int64 bytes", data: map[string]interface{}{ - "client_stats": map[string]interface{}{ + "ClientStats": map[string]interface{}{ "memory_stats": map[string]interface{}{ "limit": int64(2048), }, }, }, expected: map[string]interface{}{ - "client_stats": map[string]interface{}{ + "ClientStats": map[string]interface{}{ "memory_stats": map[string]interface{}{ - "limit": "2.000 KB", + "limit": "2.000KB", }, }, }, @@ -35,7 +35,7 @@ func TestConvertBigMetricFromSchema(t *testing.T) { { name: "string schema: converts int64 nanoseconds", data: map[string]interface{}{ - "client_stats": map[string]interface{}{ + "ClientStats": map[string]interface{}{ "cpu_stats": map[string]interface{}{ "cpu_usage": map[string]interface{}{ "total_usage": int64(100000000000), @@ -48,15 +48,15 @@ func TestConvertBigMetricFromSchema(t *testing.T) { }, }, expected: map[string]interface{}{ - "client_stats": map[string]interface{}{ + "ClientStats": map[string]interface{}{ "cpu_stats": map[string]interface{}{ "cpu_usage": map[string]interface{}{ - "total_usage": "100.000 s", - "percpu_usage": []string{"50.000 s", "50.000 s"}, - "usage_in_kernelmode": "200.000 µs", - "usage_in_usermode": "200.000 µs", + "total_usage": "100.000s", + "percpu_usage": []string{"50.000s", "50.000s"}, + "usage_in_kernelmode": "200.000µs", + "usage_in_usermode": "200.000µs", }, - "system_cpu_usage": "100.000 s", + "system_cpu_usage": "100.000s", }, }, }, diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 0eae3fcc..3aed5c4f 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -433,9 +433,9 @@ func FormatBigMetric(number int64, baseUnitName string) string { } switch baseUnitName { case "bytes": - return fmt.Sprintf("%.3f %s", float64(number)/float64(div), memoryUnits[exp-1]) + return fmt.Sprintf("%.3f%s", float64(number)/float64(div), memoryUnits[exp-1]) case "nanoseconds": - return fmt.Sprintf("%.3f %s", float64(number)/float64(div), cpuUnits[exp-1]) + return fmt.Sprintf("%.3f%s", float64(number)/float64(div), cpuUnits[exp-1]) default: return fmt.Sprintf("%d", number) } diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 9a44f5c6..b6b4ea5a 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -505,25 +505,25 @@ func TestFormatBigMetric(t *testing.T) { name: "bytes: KB", number: 1500, baseUnitName: "bytes", - expected: "1.465 KB", + expected: "1.465KB", }, { name: "bytes: MB", number: 1500000, baseUnitName: "bytes", - expected: "1.431 MB", + expected: "1.431MB", }, { name: "bytes: GB", number: 1500000000, baseUnitName: "bytes", - expected: "1.397 GB", + expected: "1.397GB", }, { name: "bytes: TB", number: 1500000000000, baseUnitName: "bytes", - expected: "1.364 TB", + expected: "1.364TB", }, // Nanoseconds tests { @@ -536,38 +536,38 @@ func TestFormatBigMetric(t *testing.T) { name: "nanoseconds: µs", number: 1500, baseUnitName: "nanoseconds", - expected: "1.500 µs", + expected: "1.500µs", }, { name: "nanoseconds: ms", number: 1500000, baseUnitName: "nanoseconds", - expected: "1.500 ms", + expected: "1.500ms", }, { name: "nanoseconds: s", number: 1500000000, baseUnitName: "nanoseconds", - expected: "1.500 s", + expected: "1.500s", }, { name: "nanoseconds: m", number: 1500000000000, baseUnitName: "nanoseconds", - expected: "1.500 m", + expected: "1.500m", }, { name: "nanoseconds: h", number: 1500000000000000, baseUnitName: "nanoseconds", - expected: "1.500 h", + expected: "1.500h", }, // Exact boundaries { name: "bytes: exact 1024", number: 1024, baseUnitName: "bytes", - expected: "1.000 KB", + expected: "1.000KB", }, { name: "bytes: 1023",