Skip to content

Commit

Permalink
fix: group by for catalog summary
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Nov 22, 2024
1 parent e1f78a1 commit 761f5c8
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions query/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
"github.com/google/uuid"
"github.com/samber/lo"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
Expand Down Expand Up @@ -273,6 +272,7 @@ func ConfigSummary(ctx context.Context, req ConfigSummaryRequest) (types.JSON, e
_ = ctx.DB().Use(extraClausePlugin.New())

groupBy := strings.Join(req.groupBy(), ",")
plainGroupBy := strings.Join(req.GroupBy, ",")

healthGrouped := exclause.NewWith(
"health_grouped",
Expand All @@ -289,7 +289,7 @@ func ConfigSummary(ctx context.Context, req ConfigSummaryRequest) (types.JSON, e
"aggregated_health_count",
ctx.DB().Select(req.plainSelectClause("jsonb_object_agg(health_grouped.health, count)::jsonb AS health")).
Table("health_grouped").
Group(strings.Join(req.GroupBy, ",")),
Group(plainGroupBy),
)

// Keep track of all the ctes in this query (in order)
Expand Down Expand Up @@ -321,15 +321,11 @@ func ConfigSummary(ctx context.Context, req ConfigSummaryRequest) (types.JSON, e
Group(groupBy).Group("kv_pair.key"),
)

// Replace config_items. prefix as either sleect with tag or type will come from the changes_analysis_grouped view
selectClause := lo.Map(req.baseSelectClause("COALESCE(jsonb_object_agg(key, value_sum), '{}'::jsonb) AS total_analysis"), func(item string, _ int) string {
return strings.ReplaceAll(item, "config_items.", "")
})
aggregatedAnalysis := exclause.NewWith(
"aggregated_analysis",
ctx.DB().Select(selectClause).
ctx.DB().Select(req.plainSelectClause("COALESCE(jsonb_object_agg(key, value_sum), '{}'::jsonb) AS total_analysis")).
Table("changes_analysis_grouped").
Group(strings.ReplaceAll(groupBy, "config_items.", "")),
Group(plainGroupBy),
)

withClauses = append(withClauses, changesAnalysisGrouped, aggregatedAnalysis)
Expand Down Expand Up @@ -370,7 +366,7 @@ func ConfigSummary(ctx context.Context, req ConfigSummaryRequest) (types.JSON, e
"aggregated_analysis_count",
ctx.DB().Select(req.plainSelectClause("json_object_agg(analysis_type, count)::jsonb AS analysis")).
Table("analysis_grouped").
Group(strings.Join(req.GroupBy, ",")),
Group(plainGroupBy),
)

summaryQuery = summaryQuery.Joins(req.analysisJoin()).Group("aggregated_analysis_count.analysis")
Expand Down

0 comments on commit 761f5c8

Please sign in to comment.