Session: add reference price and CO2 (#28712)
This commit is contained in:
parent
f00cde4972
commit
188a5d8216
3 changed files with 50 additions and 15 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue