stats

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package stats computes live per-tag rolling statistics (min/max/avg/count) over a core.TagDB stream — running KPIs for FameView 统计 dashboards.

A Collector subscribes to each tracked tag and folds every published sample into that tag's Stat. Because core.Tag.Subscribe PRIMES the callback once with the tag's current value at subscribe time, the first fold reflects the value present when Track was called: Count starts at 1 immediately after Track (the primed sample), before any further SetValue/Publish. Callers who want clean, predictable counts should set a known initial value before Track (or account for that one primed sample). Note also that core.Tag.Publish is notify-on-change: publishing a value equal to the current one is a no-op and is not folded.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector folds each tracked tag's value stream into a per-tag Stat. All methods are safe for concurrent use: samples fold on whatever goroutine drives SetValue/Publish, while readers call Get from any goroutine.

func NewCollector

func NewCollector(tags *core.TagDB) *Collector

NewCollector returns a Collector reading from tags.

func (*Collector) Get

func (c *Collector) Get(name string) (Stat, bool)

Get returns a snapshot copy of the named tag's Stat and whether it is tracked. The returned Stat is a value copy and never mutates afterward.

func (*Collector) Reset

func (c *Collector) Reset(name string)

Reset zeroes the named tag's Stat in place without dropping the subscription: subsequent values keep folding from a clean slate.

func (*Collector) StopAll

func (c *Collector) StopAll()

StopAll unsubscribes from every tracked tag. Accumulated Stats remain readable via Get; only the subscriptions are torn down.

func (*Collector) Track

func (c *Collector) Track(name string)

Track resolves (creating if needed) the named tag and subscribes to it, folding each value's Float() into the tag's Stat. Subscribe primes with the tag's current value, so that primed sample is the first fold (Count 1). Tracking an already-tracked name is a no-op (it does not re-subscribe).

type Stat

type Stat struct {
	Count               int
	Min, Max, Sum, Last float64
}

Stat is the running aggregate for one tag. Min/Max are only meaningful once Count > 0; the first folded sample initializes both.

func (Stat) Avg

func (s Stat) Avg() float64

Avg is Sum/Count, and 0 when Count == 0.

Jump to

Keyboard shortcuts

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