Skip to content

Commit

Permalink
chore: optimize related_change_recursive_query
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Nov 1, 2024
1 parent 2d8bfcb commit 23f6b54
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion query/config_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func FindCatalogChanges(ctx context.Context, req CatalogChangesSearchRequest) (*

table := query.Table("catalog_changes")
if err := uuid.Validate(req.CatalogID); err == nil {
table = query.Table("related_changes_recursive(?,?,?,?)", req.CatalogID, req.Recursive, req.IncludeDeletedConfigs, req.Depth)
table = query.Table("related_changes_recursive(?,?,?,?, true)", req.CatalogID, req.Recursive, req.IncludeDeletedConfigs, req.Depth)
} else {
clause, err := parseAndBuildFilteringQuery(req.CatalogID, "config_id", false)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tests/config_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/samber/lo"
)

var _ = ginkgo.Describe("Config changes recursive", ginkgo.Ordered, func() {
var _ = ginkgo.FDescribe("Config changes recursive", ginkgo.Ordered, func() {
// Graph #1 (acyclic)
//
// U
Expand Down
27 changes: 25 additions & 2 deletions views/006_config_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,8 @@ CREATE OR REPLACE FUNCTION related_changes_recursive (
lookup_id UUID,
type_filter TEXT DEFAULT 'downstream', -- 'downstream', 'upstream', 'all', 'none' or ''
include_deleted_configs BOOLEAN DEFAULT FALSE,
max_depth INTEGER DEFAULT 5
max_depth INTEGER DEFAULT 5,
soft BOOLEAN DEFAULT FALSE
) RETURNS TABLE (
id uuid,
config_id uuid,
Expand Down Expand Up @@ -784,6 +785,29 @@ BEGIN
FROM config_changes cc
LEFT JOIN config_items on config_items.id = cc.config_id
WHERE cc.config_id = lookup_id;

ELSIF type_filter IN ('downstream') THEN
RETURN query
SELECT
cc.id, cc.config_id, config_items.name, config_items.type, config_items.tags, cc.external_created_by,
cc.created_at, cc.severity, cc.change_type, cc.source, cc.summary, cc.created_by, cc.count, cc.first_observed, config_items.agent_id
FROM config_changes cc
LEFT JOIN config_items on config_items.id = cc.config_id
LEFT JOIN config_relationships cr ON cr.config_id = cc.config_id
WHERE starts_with(config_items.path, (SELECT CONCAT(config_items.path, '.', config_items.id) FROM config_items WHERE config_items.id = lookup_id )) OR
(soft AND cc.config_id = lookup_id );

ELSIF type_filter IN ('upstream') THEN
RETURN query
SELECT
cc.id, cc.config_id, config_items.name, config_items.type, config_items.tags, cc.external_created_by,
cc.created_at, cc.severity, cc.change_type, cc.source, cc.summary, cc.created_by, cc.count, cc.first_observed, config_items.agent_id
FROM config_changes cc
LEFT JOIN config_items on config_items.id = cc.config_id
LEFT JOIN config_relationships cr ON cr.config_id = cc.config_id
WHERE cc.config_id IN (SELECT id FROM get_recursive_path(lookup_id)) OR
(soft AND cc.config_id = lookup_id );

ELSE
RETURN query
SELECT
Expand All @@ -798,7 +822,6 @@ BEGIN
lookup_id,
CASE
WHEN type_filter = 'upstream' THEN 'incoming'
WHEN type_filter = 'downstream' THEN 'outgoing'
ELSE type_filter
END,
max_depth
Expand Down

0 comments on commit 23f6b54

Please sign in to comment.