mirror of
https://github.com/grafana/grafana.git
synced 2024-11-26 02:40:26 -06:00
4eb8e4ff66
* Define EvaluationContext * Refactor ConditionEval to use new context struct * Refactor QueriesAndExpressionsEval to use EvaluationContext * Remove dead field from AlertExecCtx * Refactor Validate to use EvaluationContext * Get rid of privately used AlertExecCtx * Move EvaluationContext to new file and add helper * Add builder pattern and bind rule info to context * Extract header logic and add rule UID header * Fix missing call
44 lines
932 B
Go
44 lines
932 B
Go
package eval
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
"github.com/grafana/grafana/pkg/services/user"
|
|
)
|
|
|
|
// EvaluationContext represents the context in which a condition is evaluated.
|
|
type EvaluationContext struct {
|
|
Ctx context.Context
|
|
User *user.SignedInUser
|
|
At time.Time
|
|
RuleUID string
|
|
}
|
|
|
|
func Context(ctx context.Context, user *user.SignedInUser) EvaluationContext {
|
|
return EvaluationContext{
|
|
Ctx: ctx,
|
|
User: user,
|
|
At: time.Now(),
|
|
}
|
|
}
|
|
|
|
func (c EvaluationContext) When(t time.Time) EvaluationContext {
|
|
c.At = t
|
|
return c
|
|
}
|
|
|
|
func (c EvaluationContext) WithRule(r *models.AlertRule) EvaluationContext {
|
|
if r != nil {
|
|
c.RuleUID = r.UID
|
|
}
|
|
return c
|
|
}
|
|
|
|
func (c EvaluationContext) WithTimeout(timeout time.Duration) (EvaluationContext, context.CancelFunc) {
|
|
timeoutCtx, cancel := context.WithTimeout(c.Ctx, timeout)
|
|
c.Ctx = timeoutCtx
|
|
return c, cancel
|
|
}
|