Skip to content

feat(#608): human-readable formatting for stats numbers - #824

Open
tkhhhh wants to merge 13 commits into
jesseduffield:masterfrom
tkhhhh:feat-human-readable-metric
Open

feat(#608): human-readable formatting for stats numbers#824
tkhhhh wants to merge 13 commits into
jesseduffield:masterfrom
tkhhhh:feat-human-readable-metric

Conversation

@tkhhhh

@tkhhhh tkhhhh commented Jul 24, 2026

Copy link
Copy Markdown

Summary

Enhancement for #608. The stats tab currently shows raw numeric metrics — nanosecond CPU counters and byte counts — that are hard to read at a glance (e.g. system_cpu_usage: 93579438000000, memory_stats.limit: 2052030464). This PR formats those values into human-readable units before rendering the YAML.

  • CPU / nanosecond fields → µs, ms, s, m, h (base 1000)
  • Byte fields → KB, MB, GB, TB (base 1024)
  • Values below the base unit are left as the raw integer.

Before / After

# before
memory_stats:
  limit: 2052030464
cpu_stats:                                                                                                                                                                               
  system_cpu_usage: 93579438000000
                                                                                                                                                                                         
# after                                                                                                                                                                                
memory_stats:
  limit: 1.911GB
cpu_stats:
  system_cpu_usage: 25.994h

Changes

pkg/utils/utils.go

  • FormatBigMetric — formats a big number given a base unit ("bytes" uses 1024, "nanoseconds" uses 1000).
  • SetObjectFieldByPath — sets a value at a dotted path inside a nested generic map.

pkg/gui/presentation/container_stats.go

  • Introduces PATHS_TO_CONVERT_BIGMETRICS, a whitelist mapping each path of stat with int64 type to its base unit. Covers cpu_stats / precpu_stats (cpu_usage.total_usage, usage_in_kernelmode,
    usage_in_usermode, percpu_usage, system_cpu_usage) and memory_stats (limit, stats.hierarchical_memory_limit, stats.hierarchical_memsw_limit).
  • RenderStats now marshals stats to JSON, decodes into a generic map, runs convertBigMetricFromSchema to rewrite the whitelisted fields, and renders the resulting map as YAML.
  • convertBigMetric handles both scalar values (float64 from JSON) and per-CPU arrays ([]float64). Missing keys are skipped so containers that don't emit every field still render.

Tests

  • pkg/utils/utils_test.go — table tests for FormatBigMetric (unit thresholds, sub-unit values, unknown unit) and SetObjectFieldByPath (nested writes, invalid path).
  • pkg/gui/presentation/container_stats_test.go — table tests covering scalar byte/nanosecond conversion, the percpu_usage array path, deeply nested hierarchical_*_limit fields, both cpu_stats and precpu_stats branches, preservation of non-whitelisted fields, and a realistic full-payload snapshot.
  • Run lazydocker against a live container, open the stats tab, and confirm whitelisted fields render with units while non-whitelisted numeric fields render unchanged.

Test Result

image image

@tkhhhh tkhhhh changed the title feat: human-readable formatting for stats numbers (closes #608) feat(#608): human-readable formatting for stats numbers Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant