Skip to content

Commit

Permalink
feat: support severity filter
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Jun 14, 2024
1 parent 338c0a7 commit a627da9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
28 changes: 27 additions & 1 deletion query/config_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/flanksource/duty/api"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/pkg/kube/labels"
"github.com/flanksource/duty/types"
"github.com/google/uuid"
Expand Down Expand Up @@ -224,6 +225,31 @@ func (t *CatalogChangesSearchResponse) Summarize() {
}
}

func formSeverityQuery(severity string) string {
if strings.HasPrefix(severity, "!") {
// For `Not` queries, we don't need to make any changes.
return severity
}

var severities = []models.Severity{
models.SeverityCritical,
models.SeverityHigh,
models.SeverityMedium,
models.SeverityLow,
models.SeverityInfo,
}

var applicable []string
for _, s := range severities {
applicable = append(applicable, string(s))
if string(s) == severity {
break
}
}

return strings.Join(applicable, ",")
}

func FindCatalogChanges(ctx context.Context, req CatalogChangesSearchRequest) (*CatalogChangesSearchResponse, error) {
req.SetDefaults()
if err := req.Validate(); err != nil {
Expand All @@ -248,7 +274,7 @@ func FindCatalogChanges(ctx context.Context, req CatalogChangesSearchRequest) (*
}

if req.Severity != "" {
clauses = append(clauses, parseAndBuildFilteringQuery(req.Severity, "severity")...)
clauses = append(clauses, parseAndBuildFilteringQuery(formSeverityQuery(req.Severity), "severity")...)
}

if req.Summary != "" {
Expand Down
39 changes: 27 additions & 12 deletions tests/config_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
// Create changes for each config
var (
UChange = models.ConfigChange{ID: uuid.New().String(), CreatedAt: lo.ToPtr(time.Now()), Severity: "info", ConfigID: U.ID.String(), Summary: ".name.U", ChangeType: "RegisterNode", Source: "test-changes"}
VChange = models.ConfigChange{ID: uuid.New().String(), CreatedAt: lo.ToPtr(time.Now().Add(-time.Hour)), Severity: "warn", ConfigID: V.ID.String(), Summary: ".name.V", ChangeType: "diff", Source: "test-changes"}
WChange = models.ConfigChange{ID: uuid.New().String(), CreatedAt: lo.ToPtr(time.Now().Add(-time.Hour * 2)), Severity: "low", ConfigID: W.ID.String(), Summary: ".name.W", ChangeType: "Pulled", Source: "test-changes"}
VChange = models.ConfigChange{ID: uuid.New().String(), CreatedAt: lo.ToPtr(time.Now().Add(-time.Hour)), Severity: models.SeverityHigh, ConfigID: V.ID.String(), Summary: ".name.V", ChangeType: "diff", Source: "test-changes"}
WChange = models.ConfigChange{ID: uuid.New().String(), CreatedAt: lo.ToPtr(time.Now().Add(-time.Hour * 2)), Severity: models.SeverityCritical, ConfigID: W.ID.String(), Summary: ".name.W", ChangeType: "Pulled", Source: "test-changes"}
XChange = models.ConfigChange{ID: uuid.New().String(), CreatedAt: lo.ToPtr(time.Now().Add(-time.Hour * 3)), Severity: "info", ConfigID: X.ID.String(), Summary: ".name.X", ChangeType: "diff", Source: "test-changes"}
YChange = models.ConfigChange{ID: uuid.New().String(), CreatedAt: lo.ToPtr(time.Now().Add(-time.Hour * 4)), Severity: "warn", ConfigID: Y.ID.String(), Summary: ".name.Y", ChangeType: "diff", Source: "test-changes"}
ZChange = models.ConfigChange{ID: uuid.New().String(), CreatedAt: lo.ToPtr(time.Now().Add(-time.Hour * 5)), Severity: "info", ConfigID: Z.ID.String(), Summary: ".name.Z", ChangeType: "Pulled", Source: "test-changes"}
Expand Down Expand Up @@ -228,17 +228,32 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
})
})

ginkgo.It("Severity filter", func() {
response, err := query.FindCatalogChanges(DefaultContext, query.CatalogChangesSearchRequest{
CatalogID: U.ID.String(),
Recursive: query.CatalogChangeRecursiveDownstream,
Severity: "!info",
ginkgo.Context("Severity filter", func() {
ginkgo.It("NOT", func() {
response, err := query.FindCatalogChanges(DefaultContext, query.CatalogChangesSearchRequest{
CatalogID: U.ID.String(),
Recursive: query.CatalogChangeRecursiveDownstream,
Severity: "!info",
})
Expect(err).To(BeNil())
Expect(response.Total).To(Equal(int64(3)))
Expect(len(response.Changes)).To(Equal(3))
Expect(response.Summary["Pulled"]).To(Equal(1))
Expect(response.Summary["diff"]).To(Equal(2))
})

ginkgo.It("should return the given severity and higher", func() {
response, err := query.FindCatalogChanges(DefaultContext, query.CatalogChangesSearchRequest{
CatalogID: U.ID.String(),
Recursive: query.CatalogChangeRecursiveDownstream,
Severity: string(models.SeverityMedium),
})
Expect(err).To(BeNil())
Expect(response.Total).To(Equal(int64(2)))
Expect(len(response.Changes)).To(Equal(2))
Expect(response.Summary["Pulled"]).To(Equal(1))
Expect(response.Summary["diff"]).To(Equal(1))
})
Expect(err).To(BeNil())
Expect(response.Total).To(Equal(int64(3)))
Expect(len(response.Changes)).To(Equal(3))
Expect(response.Summary["Pulled"]).To(Equal(1))
Expect(response.Summary["diff"]).To(Equal(2))
})

ginkgo.Context("Pagination", func() {
Expand Down

0 comments on commit a627da9

Please sign in to comment.