Harden core packages and add interactive CLI config builder (rud config/validate/test)

This commit is contained in:
tyuXX 2026-11-19 13:05:26 +00:00
parent d37503c863
commit 99e24be6c9
29 changed files with 6763 additions and 165 deletions

View file

@ -21,9 +21,11 @@ import (
)
type ruleState struct {
Active bool
ActiveSince time.Time
LastFiredAt time.Time
Active bool
Notified bool
ActiveSince time.Time
LastFiredAt time.Time
LastTriggeredIDs []string
}
// EventPayload is passed to webhook body templates and command templates when an event triggers or resolves.
@ -85,27 +87,35 @@ func (e *Engine) Evaluate(ctx context.Context, snap store.Snapshot) {
matched, matchedIDs := e.evalCondition(ev.ID, "root", ev.Condition, snap, now)
if matched {
cooldown := ev.Cooldown.Std()
canFire := !st.Active && (st.LastFiredAt.IsZero() || cooldown <= 0 || now.Sub(st.LastFiredAt) >= cooldown)
if !st.Active {
st.Active = true
st.ActiveSince = now
}
if canFire {
cooldownOK := st.LastFiredAt.IsZero() || cooldown <= 0 || now.Sub(st.LastFiredAt) >= cooldown
if !st.Notified && cooldownOK {
st.Notified = true
st.LastFiredAt = now
st.LastTriggeredIDs = append([]string(nil), matchedIDs...)
payload := buildPayload(ev, "TRIGGERED", matchedIDs, snap, now)
e.dispatchActions(ctx, ev.ID, payload, ev.Actions)
}
} else {
if st.Active {
st.Active = false
st.Active = false
if st.Notified {
st.Notified = false
if ev.TriggerOnRecovery {
actions := ev.RecoveryActions
if len(actions) == 0 {
actions = ev.Actions
}
payload := buildPayload(ev, "RESOLVED", matchedIDs, snap, now)
recoveryIDs := st.LastTriggeredIDs
if len(recoveryIDs) == 0 {
recoveryIDs = matchedIDs
}
payload := buildPayload(ev, "RESOLVED", recoveryIDs, snap, now)
e.dispatchActions(ctx, ev.ID, payload, actions)
}
st.LastTriggeredIDs = nil
}
}
}
@ -143,15 +153,14 @@ func (e *Engine) evalCondition(eventID, path string, cond config.EventCondition,
return anyMatched, collected
}
// Default: "and"
if len(cond.Conditions) == 0 {
return false, nil
}
// Default: "and" — evaluate all children so sibling metric 'for' timers reset when metrics recover
allMatched := true
for i, sub := range cond.Conditions {
subPath := fmt.Sprintf("%s.%d", path, i)
ok, ids := e.evalCondition(eventID, subPath, sub, snap, now)
if !ok {
return false, nil
allMatched = false
continue
}
for _, id := range ids {
if !seen[id] {
@ -160,6 +169,9 @@ func (e *Engine) evalCondition(eventID, path string, cond config.EventCondition,
}
}
}
if !allMatched {
return false, nil
}
return true, collected
}
@ -426,7 +438,7 @@ func executeCommand(ctx context.Context, act config.EventAction, payload EventPa
}
func renderTemplate(tmplStr string, data EventPayload) (string, error) {
t, err := template.New("action").Parse(tmplStr)
t, err := template.New("action").Funcs(config.ActionTemplateFuncMap()).Parse(tmplStr)
if err != nil {
return "", err
}