Skip to content

Commit

Permalink
feat: return the config name also
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Mar 12, 2024
1 parent 501d091 commit 0a9aec8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
19 changes: 12 additions & 7 deletions query/config_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,14 @@ func (t *CatalogChangesSearchRequest) Validate() error {
return nil
}

type ConfigChangeRow struct {
models.ConfigChange `json:",inline"`
CatalogName string `json:"catalog_name"`
}

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

func (t *CatalogChangesSearchResponse) Summarize() {
Expand All @@ -128,15 +133,15 @@ func FindCatalogChanges(ctx context.Context, req CatalogChangesSearchRequest) (*
}

var clauses []string
query := "SELECT cc.* FROM related_changes_recursive(@catalog_id, @recursive, @include_deleted_configs) cc"
query := "SELECT cc.*, config_items.name as catalog_name FROM related_changes_recursive(@catalog_id, @recursive, @include_deleted_configs) cc"
if req.Recursive == "" {
query = "SELECT cc.* FROM config_changes cc"
query = "SELECT cc.*, config_items.name as catalog_name FROM config_changes cc"
clauses = append(clauses, "cc.config_id = @catalog_id")
}

if req.ConfigType != "" {
query += " LEFT JOIN config_items ON cc.config_id = config_items.id"
query += " LEFT JOIN config_items ON cc.config_id = config_items.id"

if req.ConfigType != "" {
_clauses, _args := parseAndBuildFilteringQuery(req.ConfigType, "config_items.type")
clauses = append(clauses, _clauses...)
args = collections.MergeMap(args, _args)
Expand Down Expand Up @@ -177,7 +182,7 @@ func FindCatalogChanges(ctx context.Context, req CatalogChangesSearchRequest) (*
args["offset"] = (req.Page - 1) * req.PageSize

var output CatalogChangesSearchResponse
if err := ctx.DB().Debug().Raw(query, args).Find(&output.Changes).Error; err != nil {
if err := ctx.DB().Raw(query, args).Find(&output.Changes).Error; err != nil {
return nil, err
}

Expand Down
8 changes: 4 additions & 4 deletions tests/config_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
})
Expect(err).To(BeNil())
Expect(len(response.Changes)).To(Equal(2))
changes := lo.Map(response.Changes, func(c models.ConfigChange, _ int) string { return c.Summary })
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 @@ -252,7 +252,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
})
Expect(err).To(BeNil())
Expect(len(response.Changes)).To(Equal(2))
changes := lo.Map(response.Changes, func(c models.ConfigChange, _ int) string { return c.Summary })
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 @@ -266,7 +266,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
})
Expect(err).To(BeNil())
Expect(len(response.Changes)).To(Equal(6))
changes := lo.Map(response.Changes, func(c models.ConfigChange, _ int) string { return c.ChangeType })
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,7 +278,7 @@ var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
})
Expect(err).To(BeNil())
Expect(len(response.Changes)).To(Equal(6))
changes := lo.Map(response.Changes, func(c models.ConfigChange, _ int) string { return c.ChangeType })
changes := lo.Map(response.Changes, func(c query.ConfigChangeRow, _ int) string { return c.ChangeType })
Expect(changes).To(Equal([]string{"RegisterNode", "Pulled", "Pulled", "diff", "diff", "diff"}))
})
})
Expand Down

0 comments on commit 0a9aec8

Please sign in to comment.