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
- Variables
- func Catalogue(s *site.Site) map[string]Formula
- func GaugeCatalogue(s *site.Site) map[string]GaugeFormula
- func Slot(t time.Time) int64
- type Agg
- type Cache
- type Formula
- type GaugeFormula
- type GaugePoint
- type GaugeResult
- type Level
- type Period
- type PeriodValue
- type Reader
- type Result
- type Service
- type Term
- type UnknownSeriesError
- type Value
Constants ¶
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).
const ( GaugeSitePV = "site/pv" GaugeSiteSoC = "site/soc" )
SiteGauges are the site quantities of the gauge endpoint.
const ( MaxDays = 3660 MaxBuckets = 10000 )
Range limits (spec 9.1).
Variables ¶
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 ¶
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).
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).
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 ¶
Formula is a linear combination of stored series; a ratio has a denominator.
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 Period ¶
Period is a named slot range [From, To).
type PeriodValue ¶
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 ¶
GaugeNames returns every name Gauge accepts, sorted.
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