代码拉取完成,页面将自动刷新
package oncall
import (
"time"
"github.com/target/goalert/assignment"
)
// SingleRuleCalculator will calculate the currently active user.
type SingleRuleCalculator struct {
*TimeIterator
act *ActiveCalculator
rot *UserCalculator
loc *time.Location
rule ResolvedRule
userID string
changed bool
}
// NewSingleRuleCalculator will create a new SingleRuleCalculator bound to the TimeIterator.
func (t *TimeIterator) NewSingleRuleCalculator(loc *time.Location, rule ResolvedRule) *SingleRuleCalculator {
calc := &SingleRuleCalculator{
TimeIterator: t,
rule: rule,
loc: loc,
act: t.NewActiveCalculator(),
}
loopLimit := 100000
limit := func() bool {
if loopLimit <= 0 {
panic("infinite loop")
}
loopLimit--
return true
}
if rule.AlwaysActive() {
// always active so just add one span for the entire duration +1 step
calc.act.SetSpan(t.Start(), t.End().Add(t.Step()))
} else if !rule.NeverActive() {
cur := rule.StartTime(t.Start().In(loc))
// loop through rule active times
for cur.Before(t.End()) && limit() {
end := rule.EndTime(cur)
calc.act.SetSpan(cur, end)
cur = rule.StartTime(end)
}
}
calc.act.Init()
if rule.Rotation != nil {
calc.rot = t.NewUserCalculator()
switch len(rule.Rotation.Users) {
case 0:
// nothing, no on-call
case 1:
// always same user
calc.rot.SetSpan(t.Start(), t.End().Add(t.Step()), rule.Rotation.UserID(t.Start()))
default:
cur := t.Start().In(loc)
// loop through rotations
for cur.Before(t.End()) && limit() {
userID := rule.Rotation.UserID(cur)
calc.rot.SetSpan(rule.Rotation.CurrentStart, rule.Rotation.CurrentEnd, userID)
cur = rule.Rotation.CurrentEnd
}
}
calc.rot.Init()
}
t.Register(calc)
return calc
}
// Process implements the SubIterator.Process method.
func (rCalc *SingleRuleCalculator) Process(int64) int64 {
var newUserID string
if rCalc.act.Active() {
if rCalc.rot != nil {
usrs := rCalc.rot.ActiveUsers()
if len(usrs) > 0 {
// rotation will only ever have 1 active user
newUserID = usrs[0]
}
} else if rCalc.rule.Target.TargetType() == assignment.TargetTypeUser {
newUserID = rCalc.rule.Target.TargetID()
}
}
rCalc.changed = rCalc.userID != newUserID
rCalc.userID = newUserID
return 0
}
// Done implements the SubIterator.Done method.
func (rCalc *SingleRuleCalculator) Done() {}
// ActiveUser returns the currently active UserID or an empty string.
func (rCalc *SingleRuleCalculator) ActiveUser() string { return rCalc.userID }
// Changed will return true if the ActiveUser has changed this tick.
func (rCalc *SingleRuleCalculator) Changed() bool { return rCalc.changed }
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。