Skip to content

Commit

Permalink
fix: notification filters (#1453)
Browse files Browse the repository at this point in the history
* fix: notification filters

* always populate the cel variables for notification
  • Loading branch information
adityathebe authored Sep 25, 2024
1 parent 79906c4 commit bd1edbf
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 144 deletions.
53 changes: 14 additions & 39 deletions notification/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/samber/lo"
)

type celVariables struct {
Expand Down Expand Up @@ -67,52 +68,26 @@ func (t *celVariables) AsMap() map[string]any {
output := map[string]any{
"permalink": t.Permalink,
"silenceURL": t.SilenceURL,
}

if t.Agent != nil {
output["agent"] = t.Agent.AsMap()
}
"agent": lo.FromPtr(t.Agent).AsMap(),
"status": lo.FromPtr(t.CheckStatus).AsMap(),
"check": lo.FromPtr(t.Check).AsMap("spec"),
"canary": lo.FromPtr(t.Canary).AsMap("spec"),
"component": lo.ToPtr(lo.FromPtr(t.Component)).AsMap("checks", "incidents", "analysis", "components", "order", "relationship_id", "children", "parents"),
"config": lo.FromPtr(t.ConfigItem).AsMap("last_scraped_time", "path", "parent_id"),

if t.ConfigItem != nil {
output["config"] = t.ConfigItem.AsMap("last_scraped_time", "path", "parent_id")
"evidence": lo.FromPtr(t.Evidence).AsMap(),
"hypothesis": lo.FromPtr(t.Hypothesis).AsMap(),
"incident": lo.FromPtr(t.Incident).AsMap(),
"responder": lo.FromPtr(t.Responder).AsMap(),

"comment": lo.FromPtr(t.Comment).AsMap(),
"author": lo.FromPtr(t.Author).AsMap(),
}

if t.NewState != "" {
output["new_state"] = t.NewState
}
if t.Component != nil {
output["component"] = t.Component.AsMap("checks", "incidents", "analysis", "components", "order", "relationship_id", "children", "parents")
}

if t.CheckStatus != nil {
output["status"] = t.CheckStatus.AsMap()
}
if t.Check != nil {
output["check"] = t.Check.AsMap("spec")
}
if t.Canary != nil {
output["canary"] = t.Canary.AsMap("spec")
}

if t.Incident != nil {
output["incident"] = t.Incident.AsMap()
}
if t.Responder != nil {
output["responder"] = t.Responder.AsMap()
}
if t.Evidence != nil {
output["evidence"] = t.Evidence.AsMap()
}
if t.Hypothesis != nil {
output["hypothesis"] = t.Hypothesis.AsMap()
}

if t.Comment != nil {
output["comment"] = t.Comment.AsMap()
}
if t.Author != nil {
output["author"] = t.Author.AsMap()
}

return output
}
16 changes: 9 additions & 7 deletions notification/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query"
"github.com/flanksource/duty/types"
"github.com/flanksource/gomplate/v3"
"github.com/flanksource/incident-commander/api"
"github.com/flanksource/incident-commander/db"
"github.com/flanksource/incident-commander/events"
"github.com/flanksource/incident-commander/incidents/responder"
"github.com/flanksource/incident-commander/logs"
"github.com/flanksource/incident-commander/utils/expression"
"github.com/google/uuid"
"github.com/samber/lo"
"gorm.io/gorm/clause"
Expand Down Expand Up @@ -163,12 +163,14 @@ func addNotificationEvent(ctx context.Context, id string, celEnv map[string]any,
return nil
}

if valid, err := expression.Eval(n.Filter, celEnv, allEnvVars); err != nil {
// On invalid spec error, we store the error on the notification itself and exit out.
logs.IfError(db.UpdateNotificationError(ctx, id, err.Error()), "failed to update notification")
return nil
} else if !valid {
return nil
if n.Filter != "" {
if valid, err := gomplate.RunTemplateBool(celEnv, gomplate.Template{Expression: n.Filter}); err != nil {
// On invalid spec error, we store the error on the notification itself and exit out.
logs.IfError(db.UpdateNotificationError(ctx, id, err.Error()), "failed to update notification")
return nil
} else if !valid {
return nil
}
}

payloads, err := CreateNotificationSendPayloads(ctx, event, n, celEnv)
Expand Down
27 changes: 15 additions & 12 deletions notification/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ import (
"github.com/flanksource/commons/utils"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/gomplate/v3"
"github.com/google/uuid"

"github.com/flanksource/incident-commander/api"

"github.com/flanksource/incident-commander/db"
"github.com/flanksource/incident-commander/logs"
"github.com/flanksource/incident-commander/teams"
"github.com/flanksource/incident-commander/utils/expression"
)

//go:embed templates/*
var templates embed.FS

// List of all possible variables for any expression related to notifications
var allEnvVars = []string{"agent", "config", "check", "canary", "component", "incident", "team", "responder", "comment", "evidence", "hypothesis", "permalink"}

// NotificationTemplate holds in data for notification
// that'll be used by struct templater.
type NotificationTemplate struct {
Expand Down Expand Up @@ -278,10 +275,13 @@ func CreateNotificationSendPayloads(ctx context.Context, event models.Event, n *
}

for _, cn := range teamSpec.Notifications {
if valid, err := expression.Eval(cn.Filter, celEnv, allEnvVars); err != nil {
logs.IfError(db.UpdateNotificationError(ctx, n.ID.String(), err.Error()), "failed to update notification")
} else if !valid {
continue
if cn.Filter != "" {
if valid, err := gomplate.RunTemplateBool(celEnv, gomplate.Template{Expression: cn.Filter}); err != nil {
logs.IfError(db.UpdateNotificationError(ctx, n.ID.String(), err.Error()), "failed to update notification")
continue
} else if !valid {
continue
}
}

payload := NotificationEventPayload{
Expand All @@ -299,10 +299,13 @@ func CreateNotificationSendPayloads(ctx context.Context, event models.Event, n *
}

for _, cn := range n.CustomNotifications {
if valid, err := expression.Eval(cn.Filter, celEnv, allEnvVars); err != nil {
logs.IfError(db.UpdateNotificationError(ctx, n.ID.String(), err.Error()), "failed to update notification")
} else if !valid {
continue
if cn.Filter != "" {
if valid, err := gomplate.RunTemplateBool(celEnv, gomplate.Template{Expression: cn.Filter}); err != nil {
logs.IfError(db.UpdateNotificationError(ctx, n.ID.String(), err.Error()), "failed to update notification")
continue
} else if !valid {
continue
}
}

payload := NotificationEventPayload{
Expand Down
86 changes: 0 additions & 86 deletions utils/expression/expression.go

This file was deleted.

0 comments on commit bd1edbf

Please sign in to comment.