stats

package
v1.28.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package stats reads cheap per-container resource usage via `podman stats` and exposes a structured view both lerd-ui (for the dashboard widget) and the TUI (for its Dashboard pane) can share. Lives outside internal/ui so the TUI can call it in-process without pulling in the HTTP server stack.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatBytes

func FormatBytes(b int64) string

FormatBytes turns a byte count into a short human string ("128MB", "2.4GB"). Used by both the TUI dashboard and lerd-ui's resources widget so the units they show match.

func SetHostReader added in v1.25.0

func SetHostReader(fn func() ([]ContainerStat, error)) (restore func())

SetHostReader swaps the host-process reader for tests so Read doesn't shell out to systemctl (which would also add the ~1s CPU-sample sleep).

func SetReader

func SetReader(fn func() ([]ContainerStat, error)) (restore func())

SetReader swaps the underlying container reader for tests so callers can drive Read from a fixture without shelling out to podman.

Types

type ContainerStat

type ContainerStat struct {
	Name       string  `json:"name"`
	CPUPercent float64 `json:"cpu_percent"`
	MemBytes   int64   `json:"mem_bytes"`
	MemLimit   int64   `json:"mem_limit_bytes"`
	MemPercent float64 `json:"mem_percent"`
}

ContainerStat is one row of resource usage for a single lerd-prefixed container. Mirrors the JSON the web UI consumes so callers can serialize directly without an adapter struct.

func ParseRows

func ParseRows(text string) []ContainerStat

ParseRows turns multi-line `podman stats --format …` output into a slice of ContainerStat. Exported so tests for either caller can build inputs without a real podman.

type Snapshot

type Snapshot struct {
	Containers      []ContainerStat `json:"containers"`
	TotalCPUPercent float64         `json:"total_cpu_percent"`
	TotalMemBytes   int64           `json:"total_mem_bytes"`
	HostMemBytes    int64           `json:"host_mem_bytes"`
	UpdatedAt       time.Time       `json:"updated_at"`
	Available       bool            `json:"available"`
}

Snapshot is the aggregated view returned by Read. Totals are summed server-side so multiple subscribers don't each re-aggregate.

func Cached

func Cached(ttl time.Duration) Snapshot

Cached returns Read's result, refreshing at most once per ttl across all concurrent callers. Safe for concurrent use: the first caller after the TTL expires runs Read; later callers wait for that single Read to finish (or read the now-fresh cached value on retry).

func Read

func Read() Snapshot

Read returns a fresh snapshot. Callers that need caching wrap this with their own TTL (lerd-ui caches for 3s in handleStats; the TUI dashboard holds the last snapshot between frames).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL