Skip to content

Commit

Permalink
chore: optimize config_detail view using materialized views
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Oct 23, 2024
1 parent 9e33010 commit cc6ec23
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions views/006_config_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ CREATE OR REPLACE VIEW config_detail AS
json_build_object(
'relationships', COALESCE(related.related_count, 0) + COALESCE(reverse_related.related_count, 0),
'analysis', COALESCE(analysis.analysis_count, 0),
'changes', COALESCE(config_changes.changes_count, 0),
'changes', COALESCE(change_summary.changes_count, 0),
'playbook_runs', COALESCE(playbook_runs.playbook_runs_count, 0),
'checks', COALESCE(config_checks.checks_count, 0)
) as summary,
Expand All @@ -851,17 +851,14 @@ CREATE OR REPLACE VIEW config_detail AS
(SELECT related_id, count(*) as related_count FROM config_relationships GROUP BY related_id) as reverse_related
ON ci.id = reverse_related.related_id
LEFT JOIN
(SELECT config_id, count(*) as analysis_count FROM config_analysis
WHERE last_observed > NOW() - interval '2 days'
(SELECT config_id, SUM(value::INT) as analysis_count FROM config_item_summary_7d
CROSS JOIN LATERAL jsonb_each_text(config_analysis_type_counts)
GROUP BY config_id) as analysis
ON ci.id = analysis.config_id
LEFT JOIN
(SELECT config_items.id as config_id, count(rcr.id) as changes_count
FROM config_items
LEFT JOIN LATERAL related_changes_recursive(config_items.id) rcr ON true
WHERE config_items.deleted_at IS NULL AND rcr.created_at > NOW() - interval '2 days'
GROUP BY config_items.id) as config_changes
ON ci.id = config_changes.config_id
(SELECT config_item_summary_7d.config_id, config_item_summary_7d.config_changes_count AS changes_count
FROM config_item_summary_7d) AS change_summary
ON ci.path LIKE '%' || change_summary.config_id || '%'
LEFT JOIN
(SELECT config_id, count(*) as playbook_runs_count FROM playbook_runs
WHERE start_time > NOW() - interval '30 days'
Expand Down

0 comments on commit cc6ec23

Please sign in to comment.