usage

package
v0.39.15 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package usage keeps what each container costs this machine over time: CPU, memory and network traffic, per container per UTC hour, drawn as the three small line charts under the earnings chart. It follows app/usage.py and the /api/usage route of the web repository (GeiserX/CashPilot#515), with one machine instead of a fleet, so there is no server picker.

The network counters are CUMULATIVE since the container started, so traffic is the difference between two readings, with three rules:

  • A counter that went backwards is a restart. Docker resets the counters when the container restarts, so the new reading IS the traffic since the restart. Counting it keeps a busy service that restarts often from looking idle.
  • A baseline older than MaxBaselineAge is replaced, not differenced. After a long gap (the machine asleep, the app closed) the container may have restarted several times, and the difference would land hours of traffic on the hour the app came back. We start over and count nothing for that gap.
  • Missing counters are not zero. A host-network container (or a native program) has no counters of its own. Its traffic is unknown and stays unknown: its line has a gap, and a total without it is marked as a minimum.

Index

Constants

View Source
const MaxBaselineAge = 3600.0

MaxBaselineAge is how old a counter baseline may be and still be differenced. Readings arrive every few minutes; an hour covers a missed one or a short sleep, anything longer is a gap we refuse to put on one hour.

Variables

This section is empty.

Functions

func CounterDelta

func CounterDelta(previous, current int64) int64

CounterDelta is the bytes moved between two cumulative readings of the same container. A counter that went backwards means the container restarted, and the new reading is everything since the restart.

func Fold

func Fold(infos []runtime.ContainerInfo, baselines map[string]store.UsageBaseline, now time.Time) ([]store.UsageHour, map[string]store.UsageBaseline)

Fold turns one reading of every unit into this hour's rows (one per running container, by name, Beats 1) and the next counter baselines (per container id). It is the fold store.RecordUsage runs inside its transaction. Only running units count: a stopped one uses nothing, and draws a gap rather than a zero.

func Since

func Since(days int, now time.Time) int64

Since is the first second of the window Build draws, for the store query.

func Step

func Step(days int) int64

Step is the width of one point for a window of days.

Types

type Container

type Container struct {
	Name       string `json:"name"`
	Slug       string `json:"slug"`
	Service    string `json:"service"`
	NoCounters bool   `json:"noCounters"`
}

Container is one entry of the container picker: a container that has a reading in the window. NoCounters marks one whose traffic is unknown (host network).

type View

type View struct {
	Days       int         `json:"days"`
	Step       int64       `json:"step"`
	Hours      []int64     `json:"hours"`
	Containers []Container `json:"containers"`
	// Container is the picked container, "" for all of them (the machine total).
	Container string `json:"container"`
	// CPU where 100% is one core, memory in MB, traffic in bytes per second.
	CPUPercent []*float64 `json:"cpuPercent"`
	MemoryMB   []*float64 `json:"memoryMb"`
	RxRate     []*float64 `json:"rxRate"`
	TxRate     []*float64 `json:"txRate"`
	RxBytes    []*int64   `json:"rxBytes"`
	TxBytes    []*int64   `json:"txBytes"`
	// A point whose total leaves out a container it could not read: the value is
	// a minimum, shown with ≥.
	TrafficPartial []bool `json:"trafficPartial"`
	CPUPartial     []bool `json:"cpuPartial"`
	MemoryPartial  []bool `json:"memoryPartial"`
}

View is what the three charts draw. Hours is the start of every point in the window (Unix seconds), oldest first: one per hour on 7 days, one per Step (6 hours) on 30, where 720 hourly points would be noise on a small chart. Every series has one value per point; null is "no reading", drawn as a gap, never 0.

func Build

func Build(rows []store.UsageHour, days int, container string, now time.Time) View

Build draws the window of days ending now, for the picked container ("" or a name with no reading in the window means all of them).

Jump to

Keyboard shortcuts

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