File size: 1,568 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
package oncall
import "time"
// RulesCalculator provides a single interface for calculating active users of a set of ResolvedRules.
type RulesCalculator struct {
*TimeIterator
rules []*SingleRuleCalculator
userIDs []string
changed bool
}
// NewRulesCalculator will create a new RulesCalculator bound to the TimeIterator.
func (t *TimeIterator) NewRulesCalculator(loc *time.Location, rules []ResolvedRule) *RulesCalculator {
calc := &RulesCalculator{
TimeIterator: t,
}
for _, r := range rules {
calc.rules = append(calc.rules, t.NewSingleRuleCalculator(loc, r))
}
t.Register(calc)
return calc
}
// Process implements the SubIterator.Process method.
func (rCalc *RulesCalculator) Process(int64) int64 {
rCalc.changed = false
for _, r := range rCalc.rules {
if !r.Changed() {
continue
}
rCalc.changed = true
break
}
// no rules changed, abort
if !rCalc.changed {
return 0
}
rCalc.userIDs = rCalc.userIDs[:0]
for _, r := range rCalc.rules {
id := r.ActiveUser()
if id == "" {
continue
}
rCalc.userIDs = append(rCalc.userIDs, id)
}
return 0
}
// Done implements the SubIterator.Done method.
func (rCalc *RulesCalculator) Done() {}
// ActiveUsers returns the current set of active users for the current timestamp.
// It is only valid until the following Next() call and should not be modified.
func (rCalc *RulesCalculator) ActiveUsers() []string { return rCalc.userIDs }
// Changed will return true if there has been any change this tick.
func (rCalc *RulesCalculator) Changed() bool { return rCalc.changed }
|