Skip to content

Commit

Permalink
feat: new config relationship function
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Feb 9, 2024
1 parent a15a9fc commit 009a16e
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion views/006_config_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,65 @@ CREATE OR REPLACE VIEW config_detail AS
ON ci.id = config_changes.config_id
LEFT JOIN
(SELECT config_id, count(*) as playbook_runs_count FROM playbook_runs GROUP BY config_id) as playbook_runs
ON ci.id = playbook_runs.config_id;
ON ci.id = playbook_runs.config_id;

DROP FUNCTION IF EXISTS related_configs(UUID, BOOLEAN);

CREATE FUNCTION related_configs (
config_id UUID,
include_deleted_configs BOOLEAN
)
RETURNS TABLE (
relation TEXT,
relation_type TEXT,
config JSONB
) AS $$
BEGIN
RETURN query
SELECT
config_relationships.relation,
'INCOMING' AS relation_type,
jsonb_build_object(
'id', c.id,
'name', c.name,
'type', c.type,
'tags', c.tags,
'changes', c.changes,
'analysis', c.analysis,
'cost_per_minute', c.cost_per_minute,
'cost_total_1d', c.cost_total_1d,
'cost_total_7d', c.cost_total_7d,
'cost_total_30d', c.cost_total_30d,
'created_at', c.created_at,
'updated_at', c.updated_at
) AS config
FROM config_relationships
INNER JOIN configs AS c ON config_relationships.related_id = c.id AND ($2 OR c.deleted_at IS NULL)
WHERE
config_relationships.deleted_at IS NULL
AND config_relationships.config_id = $1
UNION
SELECT
config_relationships.relation,
'OUTGOING' AS relation_type,
jsonb_build_object(
'id', c.id,
'name', c.name,
'type', c.type,
'tags', c.tags,
'changes', c.changes,
'analysis', c.analysis,
'cost_per_minute', c.cost_per_minute,
'cost_total_1d', c.cost_total_1d,
'cost_total_7d', c.cost_total_7d,
'cost_total_30d', c.cost_total_30d,
'created_at', c.created_at,
'updated_at', c.updated_at
) AS config
FROM config_relationships
INNER JOIN configs AS c ON config_relationships.config_id = c.id AND ($2 OR c.deleted_at IS NULL)
WHERE
config_relationships.deleted_at IS NULL
AND config_relationships.related_id = $1;
END;
$$ LANGUAGE plpgsql;

0 comments on commit 009a16e

Please sign in to comment.