fix: fix test

This commit is contained in:
tonysken 2026-07-24 22:12:10 +01:00
parent bab0dfed81
commit 68e269ea7d
No known key found for this signature in database
GPG key ID: 67D2B9EB6131A6CE
4 changed files with 46 additions and 36 deletions

View file

@ -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

View file

@ -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",
},
},
},

View file

@ -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)
}

View file

@ -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",