view

package
v0.0.199 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package view serves a read-only dashboard of the usage snapshots written by `agent-pro usage collect`. It reads the store, never writes to it.

Index

Constants

View Source
const (
	// MetricPrimaryPercent overlays each provider's own quota percentage, which
	// every provider names differently.
	MetricPrimaryPercent = "primary_percent"
	// MetricSessionsTotal is the count of sessions found on disk per provider.
	MetricSessionsTotal = "sessions_total"
)

Metric names the dashboard adds to the keys stored in a snapshot.

View Source
const (
	DefaultPort = 8080
)

DefaultPort is the first port `usage view` tries. A busy port is skipped for up to viewPortAttempts ports.

View Source
const DefaultSince = 7 * 24 * time.Hour

DefaultSince is the chart range used when --since is not given.

View Source
const RecentLimit = 50

RecentLimit is how many newest snapshots /api/summary carries for its table.

View Source
const SchemaID = "agent-pro/usage-view/v1"

SchemaID is the schema of the dashboard's JSON API.

Variables

DefaultMetrics are charted when a request names no metric.

Functions

func Chartable

func Chartable(name string) bool

Chartable reports whether a metric name can be plotted.

func Handler

func Handler(store *usage.Store, opts Options) http.Handler

Handler serves the dashboard page and its read-only JSON API over one store. The store is re-read per request, so a new collect shows up on the next poll.

func ParseRange

func ParseRange(value string) (time.Duration, error)

ParseRange parses a chart range: a duration such as 90m, 24h, 7d or 2w.

func Serve

func Serve(ctx context.Context, store *usage.Store, opts Options) error

Serve starts the dashboard over store and blocks until ctx is cancelled, a signal arrives, or the listener fails. It only reads the store.

func Step

func Step(name string) bool

Step reports whether a metric accumulates, so it is drawn as a step line.

Types

type MetricInfo

type MetricInfo struct {
	Name string `json:"name"`
	// Kind is percent, counter or number; KindTime marks a timestamp.
	Kind string `json:"kind"`
	Unit string `json:"unit,omitempty"`
	// Providers are the providers that reported this metric, sorted.
	Providers []string `json:"providers"`
	// Count is how many stored records carry a value for it.
	Count int `json:"count"`
	// Chartable is false for values that are timestamps, not magnitudes.
	Chartable bool `json:"chartable"`
}

MetricInfo describes one metric the dashboard can chart.

func Metrics

func Metrics(records []usage.StoredRecord) []MetricInfo

Metrics lists every chartable metric found in the records, the two derived ones first, then the raw snapshot keys alphabetically.

type Options

type Options struct {
	// Port is the preferred listen port; zero or negative binds any free port.
	Port int
	// PortExplicit records that --port was given, so falling back to the next
	// free port is worth a warning.
	PortExplicit bool
	// Since is the default chart range.
	Since time.Duration
	// StaticDir serves dashboard.html from disk instead of the embedded copy.
	StaticDir string
	// Open opens the dashboard in a browser once it is listening.
	Open bool
	// JSON prints one machine-readable line for the bound URL instead of the
	// human summary.
	JSON bool
	// Stdout receives the result lines. Defaults to os.Stdout.
	Stdout io.Writer
	// Stderr receives warnings and the stop hint. Defaults to os.Stderr.
	Stderr io.Writer
	// Now overrides the clock, for tests.
	Now func() time.Time
}

Options configures the read-only dashboard server.

type Point

type Point struct {
	TS    time.Time `json:"ts"`
	Value float64   `json:"value"`
}

Point is one charted value.

type Series

type Series struct {
	Provider string  `json:"provider"`
	Points   []Point `json:"points"`
	Latest   float64 `json:"latest"`
	Min      float64 `json:"min"`
	Max      float64 `json:"max"`
}

Series is one provider's points for one metric, ascending by time.

func BuildSeries

func BuildSeries(records []usage.StoredRecord, metric string, since time.Time) []Series

BuildSeries pivots stored records into per-provider series for one metric. Records arrive newest first (store order) and each series is returned ascending by time, so a chart can draw it directly.

Jump to

Keyboard shortcuts

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