Session: add reference price and CO2 (#28712)

This commit is contained in:
Michael Geers 2026-04-06 16:47:01 +02:00 • committed by GitHub
parent f00cde4972
commit 188a5d8216
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 15 deletions

View file

@ -44,6 +44,32 @@ func Rates(t api.Tariff) api.Rates {
return rr
}
// AverageRate returns the arithmetic mean of rates in [now, now+d), or nil if unavailable.
func AverageRate(t api.Tariff, d time.Duration) *float64 {
rr := Rates(t)
if rr == nil {
return nil
}
now := time.Now()
end := now.Add(d)
var sum float64
var count int
for _, r := range rr {
if r.Start.Before(end) && r.End.After(now) {
sum += r.Value
count++
}
}
if count == 0 {
return nil
}
return new(sum / float64(count))
}
func (t *Tariffs) Get(u api.TariffUsage) api.Tariff {
// ensure tariff is not a wrapper
exists := func(t api.Tariff) bool {