Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/commands/container_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/config/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
},
Expand Down
30 changes: 23 additions & 7 deletions pkg/gui/presentation/container_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 config.CPUCaption:
caption = fmt.Sprintf(
"%s: %0.2f (%v)",
spec.Caption,
data[dataLength-1],
time.Since(container.StatHistory[0].RecordedAt).Round(time.Second),
)
case config.MemoryCaption:
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,
Expand Down