derive

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package derive turns 15-minute buckets into periods (day, month, year, total) and site quantities, caching closed periods in derived_cache (spec 5.1 step 8, 7.3). The period and aggregate math in period.go, agg.go and formula.go is pure: no I/O, clock or logging.

Index

Constants

View Source
const (
	QuantityGridNet         = "site/grid_net"
	QuantitySelfSufficiency = "site/self_sufficiency"
	QuantitySelfConsumption = "site/self_consumption"
)

Site quantity names that are not in the live set (spec 2).

View Source
const (
	GaugeSitePV  = "site/pv"
	GaugeSiteSoC = "site/soc"
)

SiteGauges are the site quantities of the gauge endpoint.

View Source
const (
	MaxDays    = 3660
	MaxBuckets = 10000
)

Range limits (spec 9.1).

Variables

View Source
var (
	ErrInvalidLevel = errors.New("derive: invalid level")
	ErrInvalidRange = errors.New("derive: to must not be before from")
	ErrRangeTooLong = errors.New("derive: range too long")
)

Errors of a request; the HTTP layer maps them to error codes.

Functions

func Catalogue

func Catalogue(s *site.Site) map[string]Formula

Catalogue returns every energy name a client may request: the visible stored energy series and the site quantities, as formulas over stored series.

func GaugeCatalogue

func GaugeCatalogue(s *site.Site) map[string]GaugeFormula

GaugeCatalogue returns every gauge name: the visible gauge series of the meters, the PV power of the site (Σ top-level PV meters) and the site SoC (weighted by capacity when every battery has one, else the mean).

func Slot

func Slot(t time.Time) int64

Slot returns the bucket slot of t.

Types

type Agg

type Agg struct {
	Wh        float64  `json:"wh"`
	Data      bool     `json:"data"`              // at least one bucket exists
	Estimated bool     `json:"estimated"`         // any estimated or gap bucket
	Missing   []string `json:"missing,omitempty"` // incomplete local days, sorted
}

Agg is the energy of one series over a period with its quality (spec 5.4).

func (Agg) Combine

func (a Agg) Combine(coef float64, b Agg) Agg

Combine adds coef × b as another term of a site quantity: data only if every term has data, incomplete where any term is.

func (Agg) Plus

func (a Agg) Plus(b Agg) Agg

Plus adds a later part of the same series (time sum): data if any part has data.

type Cache

type Cache interface {
	CacheGet(ctx context.Context, keys []string, configRev int64) (map[string]string, error)
	CacheGen() int64
	CachePut(ctx context.Context, configRev, gen int64, entries map[string]string) error
}

Cache is the derived cache (derive write domain).

type Formula

type Formula struct {
	Terms []Term
	Den   []Term // non-empty for ratios: 100 × Σ Terms / Σ Den
}

Formula is a linear combination of stored series; a ratio has a denominator.

func (Formula) Evaluate

func (f Formula) Evaluate(aggs map[string]Agg) Value

Evaluate applies f to the aggregates of its series (absent = no data).

func (Formula) Ratio

func (f Formula) Ratio() bool

Ratio reports whether f yields a percentage.

type GaugeFormula

type GaugeFormula struct {
	Terms []gaugeTerm
	Unit  string
}

GaugeFormula combines gauge series slot by slot: Σ weight × avg/min/max. The min and max of a sum are the sums of the parts' extremes, a band that contains the true one.

func (GaugeFormula) Combine

func (f GaugeFormula) Combine(buckets map[string][]storage.GaugeBucket) []GaugePoint

Combine applies f to the buckets of its terms; a slot is only present when every term has it (a missing part never counts as 0).

type GaugePoint

type GaugePoint struct {
	Slot          int64
	Avg, Min, Max float64
	Cover         int // seconds covered by every term
}

GaugePoint is one slot of a gauge series.

type GaugeResult

type GaugeResult struct {
	Name   string
	Unit   string
	Points []GaugePoint // slots with data only, oldest first
}

GaugeResult is one requested gauge series over the requested days.

type Level

type Level string

Level is the granularity of a request.

const (
	LevelBucket Level = "bucket"
	LevelDay    Level = "day"
	LevelMonth  Level = "month"
	LevelYear   Level = "year"
	LevelTotal  Level = "total"
)

Levels (spec 9.1 /api/v1/energy).

type Period

type Period struct {
	Key  string
	From int64
	To   int64
}

Period is a named slot range [From, To).

func Days

func Days(p Period, loc *time.Location) []Period

Days returns the day periods inside p (a day, month or year period).

func Months

func Months(p Period, loc *time.Location) []Period

Months returns the month periods inside a year period.

func Periods

func Periods(level Level, from, to time.Time, loc *time.Location) ([]Period, error)

Periods returns the periods of level covering the local days from..to (inclusive). Months and years are whole even when from/to fall inside them. Bucket periods are the slots of [from, to+1 day).

type PeriodValue

type PeriodValue struct {
	Period string
	Value
}

PeriodValue is the value of one period.

type Reader

type Reader interface {
	SeriesID(ctx context.Context, key storage.SeriesKey) (int64, error)
	SumEnergyMany(ctx context.Context, ids []int64, from, to, closedBefore int64,
	) (map[int64]storage.EnergySum, error)
	EnergyBuckets(ctx context.Context, seriesID, from, to int64) ([]storage.EnergyBucket, error)
	GaugeBuckets(ctx context.Context, seriesID, from, to int64) ([]storage.GaugeBucket, error)
	Carry(ctx context.Context, ids []int64) (map[int64]float64, error)
	FirstSlot(ctx context.Context) (int64, bool, error)
}

Reader is the part of the read store derive needs.

type Result

type Result struct {
	Name   string
	Ratio  bool // values are percent instead of Wh
	Values []PeriodValue
}

Result is one requested series over the requested periods.

type Service

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

Service answers energy requests from buckets and the derived cache.

func New

func New(s *site.Site, read Reader, cache Cache, clock util.Clock, loc *time.Location,
) (*Service, error)

New builds the service for site s.

func (*Service) Energy

func (s *Service) Energy(ctx context.Context, names []string, level Level, from, to time.Time,
) ([]Result, error)

Energy returns names per period of level for the local days from..to.

func (*Service) Gauge

func (s *Service) Gauge(ctx context.Context, names []string, from, to time.Time,
) ([]GaugeResult, error)

Gauge returns the 15-minute gauges of names for the local days from..to.

func (*Service) GaugeNames

func (s *Service) GaugeNames() []string

GaugeNames returns every name Gauge accepts, sorted.

func (*Service) Names

func (s *Service) Names() []string

Names returns every name Energy accepts, sorted.

type Term

type Term struct {
	Series string // "<meter>/<channel>"
	Coef   float64
}

Term is one stored series with its coefficient.

type UnknownSeriesError

type UnknownSeriesError struct{ Names []string }

UnknownSeriesError lists requested names that are not in the catalogue.

func (*UnknownSeriesError) Error

func (e *UnknownSeriesError) Error() string

type Value

type Value struct {
	Value     *float64 // Wh or percent; nil when there is no data
	Complete  bool
	Estimated bool
	Missing   []string // incomplete local days, sorted
}

Value is the result of a formula over one period.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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