AverageRate returns the arithmetic mean of rates in [now, now+d), or nil if unavailable.
(t api.Tariff, d time.Duration)
| 46 | |
| 47 | // AverageRate returns the arithmetic mean of rates in [now, now+d), or nil if unavailable. |
| 48 | func AverageRate(t api.Tariff, d time.Duration) *float64 { |
| 49 | rr := Rates(t) |
| 50 | if rr == nil { |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | now := time.Now() |
| 55 | end := now.Add(d) |
| 56 | |
| 57 | var sum float64 |
| 58 | var count int |
| 59 | for _, r := range rr { |
| 60 | if r.Start.Before(end) && r.End.After(now) { |
| 61 | sum += r.Value |
| 62 | count++ |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if count == 0 { |
| 67 | return nil |
| 68 | } |
| 69 | |
| 70 | return new(sum / float64(count)) |
| 71 | } |
| 72 | |
| 73 | func (t *Tariffs) Get(u api.TariffUsage) api.Tariff { |
| 74 | // ensure tariff is not a wrapper |
no test coverage detected