diff --git a/query/config_changes.go b/query/config_changes.go index 590633e9..4b7e02e7 100644 --- a/query/config_changes.go +++ b/query/config_changes.go @@ -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"` } @@ -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") @@ -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 ")) } @@ -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 } diff --git a/tests/config_changes_test.go b/tests/config_changes_test.go index 4c991fcb..a3f157e7 100644 --- a/tests/config_changes_test.go +++ b/tests/config_changes_test.go @@ -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)) }) @@ -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)) @@ -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)) @@ -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)) }) @@ -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)) }) @@ -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)) @@ -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"})) @@ -251,38 +258,13 @@ 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"})) }) }) - ginkgo.Context("Sorting", func() { - ginkgo.It("Ascending", func() { - response, err := query.FindCatalogChanges(DefaultContext, query.CatalogChangesSearchRequest{ - CatalogID: U.ID, - Recursive: query.CatalogChangeRecursiveDownstream, - SortBy: "change_type", - }) - Expect(err).To(BeNil()) - 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"})) - }) - - ginkgo.It("Descending", func() { - response, err := query.FindCatalogChanges(DefaultContext, query.CatalogChangesSearchRequest{ - CatalogID: U.ID, - Recursive: query.CatalogChangeRecursiveDownstream, - SortBy: "-catalog_name", - }) - Expect(err).To(BeNil()) - Expect(len(response.Changes)).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"})) - }) - }) - ginkgo.Context("recursive mode", func() { ginkgo.It("upstream", func() { response, err := query.FindCatalogChanges(DefaultContext, query.CatalogChangesSearchRequest{ @@ -291,6 +273,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)) }) @@ -302,6 +285,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)) }) @@ -313,6 +297,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)) @@ -328,10 +313,39 @@ 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)) }) }) + + ginkgo.Context("Sorting", func() { + ginkgo.It("Descending", func() { + response, err := query.FindCatalogChanges(DefaultContext, query.CatalogChangesSearchRequest{ + CatalogID: U.ID, + Recursive: query.CatalogChangeRecursiveDownstream, + SortBy: "-catalog_name", + }) + 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"})) + }) + + ginkgo.It("Ascending", func() { + response, err := query.FindCatalogChanges(DefaultContext, query.CatalogChangesSearchRequest{ + CatalogID: U.ID, + Recursive: query.CatalogChangeRecursiveDownstream, + 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"})) + }) + }) }) })