2024-11-12 08:40:56 -05:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"dynatron.me/x/stillbox/internal/ruletime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AlertRules []AlertRule
|
|
|
|
|
2024-11-12 09:57:38 -05:00
|
|
|
func (ars AlertRules) Apply(t time.Time, coversOpts ...ruletime.CoversOption) float64 {
|
|
|
|
if ars == nil || len(ars) < 1 {
|
|
|
|
return 1.0
|
|
|
|
}
|
|
|
|
|
2024-11-12 08:40:56 -05:00
|
|
|
final := 1.0
|
|
|
|
|
2024-11-12 09:57:38 -05:00
|
|
|
for _, ar := range ars {
|
2024-11-12 08:40:56 -05:00
|
|
|
if ar.MatchTime(t, coversOpts...) {
|
|
|
|
final *= float64(ar.ScoreMultiplier)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return final
|
|
|
|
}
|
|
|
|
|
|
|
|
type AlertRule struct {
|
|
|
|
Times []ruletime.RuleTime `json:"times"`
|
|
|
|
ScoreMultiplier float32 `json:"mult"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ar *AlertRule) MatchTime(t time.Time, coversOpts ...ruletime.CoversOption) bool {
|
|
|
|
for _, at := range ar.Times {
|
|
|
|
if at.Covers(t, coversOpts...) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|