From 278cc6c03d553389b03851414adc3a15379cc3d8 Mon Sep 17 00:00:00 2001 From: dimonl Date: Wed, 20 Aug 2025 19:24:40 +0200 Subject: [PATCH 1/2] Add memory used and limit to stat --- pkg/commands/container_stats.go | 2 ++ pkg/commands/docker.go | 2 ++ pkg/gui/presentation/container_stats.go | 30 +++++++++++++++++++------ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/pkg/commands/container_stats.go b/pkg/commands/container_stats.go index 419782dd..4fc9117b 100644 --- a/pkg/commands/container_stats.go +++ b/pkg/commands/container_stats.go @@ -16,6 +16,8 @@ type RecordedStats struct { type DerivedStats struct { CPUPercentage float64 MemoryPercentage float64 + MemoryUsed int + MemoryLimit int64 } // ContainerStats autogenerated at https://mholt.github.io/json-to-go/ diff --git a/pkg/commands/docker.go b/pkg/commands/docker.go index 28c12c23..b7dbb2b7 100644 --- a/pkg/commands/docker.go +++ b/pkg/commands/docker.go @@ -169,6 +169,8 @@ func (c *DockerCommand) CreateClientStatMonitor(container *Container) { DerivedStats: DerivedStats{ CPUPercentage: stats.CalculateContainerCPUPercentage(), MemoryPercentage: stats.CalculateContainerMemoryUsage(), + MemoryUsed: stats.MemoryStats.Usage, + MemoryLimit: stats.MemoryStats.Limit, }, RecordedAt: time.Now(), } diff --git a/pkg/gui/presentation/container_stats.go b/pkg/gui/presentation/container_stats.go index e3a9b3dc..27e20782 100644 --- a/pkg/gui/presentation/container_stats.go +++ b/pkg/gui/presentation/container_stats.go @@ -58,7 +58,8 @@ func plotGraph(container *commands.Container, spec config.GraphConfig, width int container.StatsMutex.Lock() defer container.StatsMutex.Unlock() - data := make([]float64, len(container.StatHistory)) + dataLength := len(container.StatHistory) + data := make([]float64, dataLength) for i, stats := range container.StatHistory { value, err := lookup.LookupString(stats, spec.StatPath) @@ -88,12 +89,27 @@ func plotGraph(container *commands.Container, spec config.GraphConfig, width int height = spec.Height } - caption := fmt.Sprintf( - "%s: %0.2f (%v)", - spec.Caption, - data[len(data)-1], - time.Since(container.StatHistory[0].RecordedAt).Round(time.Second), - ) + memoryUsed := utils.FormatDecimalBytes(container.StatHistory[dataLength-1].DerivedStats.MemoryUsed) + memoryLimit := utils.FormatDecimalBytes(int(container.StatHistory[dataLength-1].DerivedStats.MemoryLimit)) + var caption string + switch spec.Caption { + case "CPU (%)": + caption = fmt.Sprintf( + "%s: %0.2f (%v)", + spec.Caption, + data[dataLength-1], + time.Since(container.StatHistory[0].RecordedAt).Round(time.Second), + ) + case "Memory (%)": + caption = fmt.Sprintf( + "%s: %s / %s (%0.2f) (%v)", + spec.Caption, + memoryUsed, + memoryLimit, + data[dataLength-1], + time.Since(container.StatHistory[0].RecordedAt).Round(time.Second), + ) + } return asciigraph.Plot( data, From 68c2d3ad42fbb009403052ab954ccd978f7b3280 Mon Sep 17 00:00:00 2001 From: dimonl Date: Wed, 20 Aug 2025 21:59:27 +0200 Subject: [PATCH 2/2] extract captions to const --- pkg/config/app_config.go | 9 +++++++-- pkg/gui/presentation/container_stats.go | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index 9c0ad474..4abeb786 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -22,6 +22,11 @@ import ( "github.com/jesseduffield/yaml" ) +const ( + CPUCaption = "CPU (%)" + MemoryCaption = "Memory (%)" +) + // UserConfig holds all of the user-configurable options type UserConfig struct { // Gui is for configuring visual things like colors and whether we show or @@ -460,12 +465,12 @@ func GetDefaultConfig() UserConfig { MaxDuration: duration, Graphs: []GraphConfig{ { - Caption: "CPU (%)", + Caption: CPUCaption, StatPath: "DerivedStats.CPUPercentage", Color: "cyan", }, { - Caption: "Memory (%)", + Caption: MemoryCaption, StatPath: "DerivedStats.MemoryPercentage", Color: "green", }, diff --git a/pkg/gui/presentation/container_stats.go b/pkg/gui/presentation/container_stats.go index 27e20782..7806e1bb 100644 --- a/pkg/gui/presentation/container_stats.go +++ b/pkg/gui/presentation/container_stats.go @@ -93,14 +93,14 @@ func plotGraph(container *commands.Container, spec config.GraphConfig, width int memoryLimit := utils.FormatDecimalBytes(int(container.StatHistory[dataLength-1].DerivedStats.MemoryLimit)) var caption string switch spec.Caption { - case "CPU (%)": + case config.CPUCaption: caption = fmt.Sprintf( "%s: %0.2f (%v)", spec.Caption, data[dataLength-1], time.Since(container.StatHistory[0].RecordedAt).Round(time.Second), ) - case "Memory (%)": + case config.MemoryCaption: caption = fmt.Sprintf( "%s: %s / %s (%0.2f) (%v)", spec.Caption,