Skip to content

Commit

Permalink
feat: added total results
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Mar 12, 2024
1 parent ba1526a commit 29d5378
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
25 changes: 21 additions & 4 deletions query/config_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type ConfigChangeRow struct {

type CatalogChangesSearchResponse struct {
Summary map[string]int `json:"summary,omitempty"`
Total int `json:"total,omitempty"`
Changes []ConfigChangeRow `json:"changes,omitempty"`
}

Expand All @@ -132,14 +133,18 @@ func FindCatalogChanges(ctx context.Context, req CatalogChangesSearchRequest) (*
"include_deleted_configs": req.IncludeDeletedConfigs,
}

var clauses []string
query := "SELECT cc.*, config_items.name as catalog_name FROM related_changes_recursive(@catalog_id, @recursive, @include_deleted_configs) cc"
var (
clauses []string
selectColumns = "cc.*, config_items.name as catalog_name"
from = "related_changes_recursive(@catalog_id, @recursive, @include_deleted_configs) cc"
)

if req.Recursive == "" {
query = "SELECT cc.*, config_items.name as catalog_name FROM config_changes cc"
from = "config_changes cc"
clauses = append(clauses, "cc.config_id = @catalog_id")
}

query += " LEFT JOIN config_items ON cc.config_id = config_items.id"
from += " LEFT JOIN config_items ON cc.config_id = config_items.id"

if req.ConfigType != "" {
_clauses, _args := parseAndBuildFilteringQuery(req.ConfigType, "config_items.type")
Expand Down Expand Up @@ -169,6 +174,7 @@ func FindCatalogChanges(ctx context.Context, req CatalogChangesSearchRequest) (*
args["to"] = req.toParsed
}

query := fmt.Sprintf(`SELECT %s FROM %s`, selectColumns, from)
if len(clauses) > 0 {
query += fmt.Sprintf(" WHERE %s", strings.Join(clauses, " AND "))
}
Expand All @@ -186,6 +192,17 @@ func FindCatalogChanges(ctx context.Context, req CatalogChangesSearchRequest) (*
return nil, err
}

{
totalQuery := fmt.Sprintf(`SELECT count(*) FROM %s`, from)
if len(clauses) > 0 {
totalQuery += fmt.Sprintf(" WHERE %s", strings.Join(clauses, " AND "))
}

if err := ctx.DB().Raw(totalQuery, args).Find(&output.Total).Error; err != nil {
return nil, err
}
}

output.Summarize()
return &output, nil
}
14 changes: 14 additions & 0 deletions tests/config_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
})
Expect(err).To(BeNil())
Expect(len(response.Changes)).To(Equal(1))
Expect(response.Total).To(Equal(1))
Expect(response.Summary[UChange.ChangeType]).To(Equal(1))
})

Expand All @@ -173,6 +174,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
ConfigType: "Kubernetes::Pod,Kubernetes::ReplicaSet",
})
Expect(err).To(BeNil())
Expect(response.Total).To(Equal(3))
Expect(len(response.Changes)).To(Equal(3))
Expect(response.Summary["Pulled"]).To(Equal(2))
Expect(response.Summary["diff"]).To(Equal(1))
Expand All @@ -185,6 +187,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
ConfigType: "!Kubernetes::ReplicaSet",
})
Expect(err).To(BeNil())
Expect(response.Total).To(Equal(5))
Expect(len(response.Changes)).To(Equal(5))
Expect(response.Summary["diff"]).To(Equal(2))
Expect(response.Summary["Pulled"]).To(Equal(2))
Expand All @@ -200,6 +203,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
ChangeType: "diff",
})
Expect(err).To(BeNil())
Expect(response.Total).To(Equal(2))
Expect(len(response.Changes)).To(Equal(2))
Expect(response.Summary["diff"]).To(Equal(2))
})
Expand All @@ -211,6 +215,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
ChangeType: "!diff,!Pulled",
})
Expect(err).To(BeNil())
Expect(response.Total).To(Equal(1))
Expect(len(response.Changes)).To(Equal(1))
Expect(response.Summary["RegisterNode"]).To(Equal(1))
})
Expand All @@ -223,6 +228,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
Severity: "!info",
})
Expect(err).To(BeNil())
Expect(response.Total).To(Equal(3))
Expect(len(response.Changes)).To(Equal(3))
Expect(response.Summary["Pulled"]).To(Equal(1))
Expect(response.Summary["diff"]).To(Equal(2))
Expand All @@ -237,6 +243,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
PageSize: 2,
})
Expect(err).To(BeNil())
Expect(response.Total).To(Equal(6))
Expect(len(response.Changes)).To(Equal(2))
changes := lo.Map(response.Changes, func(c query.ConfigChangeRow, _ int) string { return c.Summary })
Expect(changes).To(Equal([]string{".name.U", ".name.V"}))
Expand All @@ -251,6 +258,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
Page: 2,
})
Expect(err).To(BeNil())
Expect(response.Total).To(Equal(6))
Expect(len(response.Changes)).To(Equal(2))
changes := lo.Map(response.Changes, func(c query.ConfigChangeRow, _ int) string { return c.Summary })
Expect(changes).To(Equal([]string{".name.W", ".name.X"}))
Expand All @@ -265,6 +273,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
SortBy: "change_type",
})
Expect(err).To(BeNil())
Expect(response.Total).To(Equal(6))
Expect(len(response.Changes)).To(Equal(6))
changes := lo.Map(response.Changes, func(c query.ConfigChangeRow, _ int) string { return c.ChangeType })
Expect(changes).To(Equal([]string{"diff", "diff", "diff", "Pulled", "Pulled", "RegisterNode"}))
Expand All @@ -278,6 +287,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
})
Expect(err).To(BeNil())
Expect(len(response.Changes)).To(Equal(6))
Expect(response.Total).To(Equal(6))
changes := lo.Map(response.Changes, func(c query.ConfigChangeRow, _ int) string { return c.CatalogName })
Expect(changes).To(Equal([]string{"Z", "Y", "X", "W", "V", "U"}))
})
Expand All @@ -291,6 +301,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
})
Expect(err).To(BeNil())
Expect(len(response.Changes)).To(Equal(2))
Expect(response.Total).To(Equal(2))
Expect(response.Summary[UChange.ChangeType]).To(Equal(1))
Expect(response.Summary[WChange.ChangeType]).To(Equal(1))
})
Expand All @@ -302,6 +313,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
})
Expect(err).To(BeNil())
Expect(len(response.Changes)).To(Equal(4))
Expect(response.Total).To(Equal(4))
Expect(response.Summary["diff"]).To(Equal(3))
Expect(response.Summary["Pulled"]).To(Equal(1))
})
Expand All @@ -313,6 +325,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
})
Expect(err).To(BeNil())
Expect(len(response.Changes)).To(Equal(5))
Expect(response.Total).To(Equal(5))
Expect(response.Summary["diff"]).To(Equal(3))
Expect(response.Summary["Pulled"]).To(Equal(1))
Expect(response.Summary["RegisterNode"]).To(Equal(1))
Expand All @@ -328,6 +341,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
To: "now-1s",
})
Expect(err).To(BeNil())
Expect(response.Total).To(Equal(2))
Expect(len(response.Changes)).To(Equal(2))
Expect(response.Summary["diff"]).To(Equal(1))
Expect(response.Summary["RegisterNode"]).To(Equal(1))
Expand Down

0 comments on commit 29d5378

Please sign in to comment.