Skip to content

Commit

Permalink
chore: put dependent views in the same file
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Jan 13, 2024
1 parent ee8049a commit 47e66b9
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 266 deletions.
2 changes: 0 additions & 2 deletions views/000_drop.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
DROP VIEW IF EXISTS topology; -- has dependencies on other views so we need to drop it first

DROP TRIGGER if exists check_statuses_change_to_event_queue on check_statuses;

CREATE OR REPLACE FUNCTION drop_push_queue_triggers () returns void as $$
Expand Down
134 changes: 0 additions & 134 deletions views/003_analysis_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,137 +14,3 @@ CREATE OR REPLACE TRIGGER reset_is_pushed_before_update
BEFORE UPDATE ON config_analysis
FOR EACH ROW
EXECUTE PROCEDURE reset_is_pushed_before_update();

-- analysis_by_config
DROP VIEW IF EXISTS analysis_by_config;
CREATE OR REPLACE VIEW
analysis_by_config AS
WITH
type_summary AS (
SELECT
summary.id,
summary.type,
json_object_agg(f.k, f.v) as json
FROM
(
SELECT
config_analysis.config_id AS id,
analysis_type as
type,
json_build_object(severity, count(*)) AS severity_agg
FROM
config_analysis
WHERE
status != 'resolved'
GROUP BY
severity,
analysis_type,
config_id
) AS summary,
json_each(summary.severity_agg) AS f (k, v)
GROUP BY
summary.type,
summary.id
)
SELECT
id,
jsonb_object_agg(key, value) as analysis
FROM
(
SELECT
id,
json_object_agg(
type,
json
) analysis
from
type_summary
group by
id,
type
) i,
json_each(analysis)
GROUP BY
id;

-- analysis_by_component
DROP VIEW IF EXISTS analysis_by_component;

CREATE OR REPLACE VIEW
analysis_by_component AS
SELECT
config_analysis.config_id,
configs.name,
configs.config_class,
configs.type,
analysis_type,
config_analysis.first_observed,
config_analysis.last_observed,
config_analysis.created_by,
config_analysis.id as analysis_id,
config_analysis.severity,
component_id
FROM
config_analysis
INNER JOIN config_component_relationships relations on relations.config_id = config_analysis.config_id
INNER JOIN config_items configs on configs.id = config_analysis.config_id
WHERE
configs.deleted_at IS NULL AND config_analysis.status = 'open'
ORDER BY
ARRAY_POSITION(ARRAY['critical', 'blocker', 'high', 'medium', 'low', 'info'], config_analysis.severity),
configs.name;

-- analysis_summary_by_component
DROP VIEW IF EXISTS analysis_summary_by_component CASCADE;
CREATE OR REPLACE VIEW
analysis_summary_by_component AS
WITH
type_summary AS (
SELECT
summary.id,
summary.type,
json_object_agg(f.k, f.v) as json
FROM
(
SELECT
config_component_relationships.component_id AS id,
config_analysis.analysis_type AS
type,
json_build_object(severity, count(*)) AS severity_agg
FROM
config_analysis
LEFT JOIN config_component_relationships ON config_analysis.config_id = config_component_relationships.config_id
INNER JOIN config_items configs ON configs.id = config_analysis.config_id
WHERE
config_component_relationships.deleted_at IS NULL
AND configs.deleted_at IS NULL
GROUP BY
config_analysis.severity,
config_analysis.analysis_type,
config_component_relationships.component_id
) AS summary,
json_each(summary.severity_agg) AS f (k, v)
GROUP BY
summary.type,
summary.id
)
SELECT
id,
jsonb_object_agg(key, value) AS analysis
FROM
(
SELECT
id,
json_object_agg(
type,
json
) analysis
FROM
type_summary
GROUP BY
id,
type
) i,
json_each(analysis)
GROUP BY
id;
29 changes: 0 additions & 29 deletions views/004_check_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,3 @@ GROUP BY
ORDER BY
key,
value;

CREATE OR REPLACE VIEW
checks_by_component AS
SELECT
check_component_relationships.component_id,
checks.id,
checks.type,
checks.name,
checks.severity,
checks.status
from
check_component_relationships
INNER JOIN checks ON checks.id = check_component_relationships.check_id
WHERE
check_component_relationships.deleted_at is null;

-- check_summary_by_component
CREATE OR REPLACE VIEW
check_summary_by_component AS
WITH cte as (
SELECT
component_id, status, COUNT(*) AS count
FROM
checks_by_component
GROUP BY
component_id, status
)
SELECT component_id, json_object_agg(status, count) AS checks
FROM cte GROUP BY component_id;
18 changes: 0 additions & 18 deletions views/009_incident_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,6 @@ CREATE OR REPLACE VIEW incidents_by_config AS
INNER JOIN incidents on hypotheses.incident_id = incidents.id
WHERE evidences.config_id is not null;

-- incident_summary_by_component
DROP VIEW IF EXISTS incident_summary_by_component;
CREATE OR REPLACE VIEW incident_summary_by_component AS
WITH type_summary AS (
SELECT summary.id, summary.type, json_object_agg(f.k, f.v) as json
FROM (
SELECT evidences.component_id AS id, incidents.type, json_build_object(severity, count(*)) AS severity_agg
FROM incidents
INNER JOIN hypotheses ON hypotheses.incident_id = incidents.id
INNER JOIN evidences ON evidences.hypothesis_id = hypotheses.id
WHERE (incidents.resolved IS NULL AND incidents.closed IS NULL and evidences.component_id IS NOT NULL
)
GROUP BY incidents.severity, incidents.type, evidences.component_id)
AS summary, json_each(summary.severity_agg) AS f(k,v) GROUP BY summary.type, summary.id
)

SELECT id, jsonb_object_agg(key, value) as incidents FROM (select id, json_object_agg(type,json) incidents from type_summary group by id, type) i, json_each(incidents) group by id;

-- incident_summary VIEW
DROP VIEW IF EXISTS incident_summary;
CREATE OR REPLACE VIEW incident_summary AS
Expand Down
182 changes: 182 additions & 0 deletions views/010_topology.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,190 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

-- Drop first
DROP VIEW IF EXISTS topology;

CREATE OR REPLACE VIEW
checks_by_component AS
SELECT
check_component_relationships.component_id,
checks.id,
checks.type,
checks.name,
checks.severity,
checks.status
from
check_component_relationships
INNER JOIN checks ON checks.id = check_component_relationships.check_id
WHERE
check_component_relationships.deleted_at is null;

-- check_summary_by_component
CREATE OR REPLACE VIEW
check_summary_by_component AS
WITH cte as (
SELECT
component_id, status, COUNT(*) AS count
FROM
checks_by_component
GROUP BY
component_id, status
)
SELECT component_id, json_object_agg(status, count) AS checks
FROM cte GROUP BY component_id;

-- analysis_by_config
DROP VIEW IF EXISTS analysis_by_config;
CREATE OR REPLACE VIEW
analysis_by_config AS
WITH
type_summary AS (
SELECT
summary.id,
summary.type,
json_object_agg(f.k, f.v) as json
FROM
(
SELECT
config_analysis.config_id AS id,
analysis_type as
type,
json_build_object(severity, count(*)) AS severity_agg
FROM
config_analysis
WHERE
status != 'resolved'
GROUP BY
severity,
analysis_type,
config_id
) AS summary,
json_each(summary.severity_agg) AS f (k, v)
GROUP BY
summary.type,
summary.id
)
SELECT
id,
jsonb_object_agg(key, value) as analysis
FROM
(
SELECT
id,
json_object_agg(
type,
json
) analysis
from
type_summary
group by
id,
type
) i,
json_each(analysis)
GROUP BY
id;

-- analysis_by_component
DROP VIEW IF EXISTS analysis_by_component;

CREATE OR REPLACE VIEW
analysis_by_component AS
SELECT
config_analysis.config_id,
configs.name,
configs.config_class,
configs.type,
analysis_type,
config_analysis.first_observed,
config_analysis.last_observed,
config_analysis.created_by,
config_analysis.id as analysis_id,
config_analysis.severity,
component_id
FROM
config_analysis
INNER JOIN config_component_relationships relations on relations.config_id = config_analysis.config_id
INNER JOIN config_items configs on configs.id = config_analysis.config_id
WHERE
configs.deleted_at IS NULL AND config_analysis.status = 'open'
ORDER BY
ARRAY_POSITION(ARRAY['critical', 'blocker', 'high', 'medium', 'low', 'info'], config_analysis.severity),
configs.name;

-- analysis_summary_by_component
DROP VIEW IF EXISTS analysis_summary_by_component CASCADE;
CREATE OR REPLACE VIEW
analysis_summary_by_component AS
WITH
type_summary AS (
SELECT
summary.id,
summary.type,
json_object_agg(f.k, f.v) as json
FROM
(
SELECT
config_component_relationships.component_id AS id,
config_analysis.analysis_type AS
type,
json_build_object(severity, count(*)) AS severity_agg
FROM
config_analysis
LEFT JOIN config_component_relationships ON config_analysis.config_id = config_component_relationships.config_id
INNER JOIN config_items configs ON configs.id = config_analysis.config_id
WHERE
config_component_relationships.deleted_at IS NULL
AND configs.deleted_at IS NULL
GROUP BY
config_analysis.severity,
config_analysis.analysis_type,
config_component_relationships.component_id
) AS summary,
json_each(summary.severity_agg) AS f (k, v)
GROUP BY
summary.type,
summary.id
)
SELECT
id,
jsonb_object_agg(key, value) AS analysis
FROM
(
SELECT
id,
json_object_agg(
type,
json
) analysis
FROM
type_summary
GROUP BY
id,
type
) i,
json_each(analysis)
GROUP BY
id;

-- incident_summary_by_component
DROP VIEW IF EXISTS incident_summary_by_component;
CREATE OR REPLACE VIEW incident_summary_by_component AS
WITH type_summary AS (
SELECT summary.id, summary.type, json_object_agg(f.k, f.v) as json
FROM (
SELECT evidences.component_id AS id, incidents.type, json_build_object(severity, count(*)) AS severity_agg
FROM incidents
INNER JOIN hypotheses ON hypotheses.incident_id = incidents.id
INNER JOIN evidences ON evidences.hypothesis_id = hypotheses.id
WHERE (incidents.resolved IS NULL AND incidents.closed IS NULL and evidences.component_id IS NOT NULL
)
GROUP BY incidents.severity, incidents.type, evidences.component_id)
AS summary, json_each(summary.severity_agg) AS f(k,v) GROUP BY summary.type, summary.id
)

SELECT id, jsonb_object_agg(key, value) as incidents FROM (select id, json_object_agg(type,json) incidents from type_summary group by id, type) i, json_each(incidents) group by id;

CREATE OR REPLACE VIEW
topology AS
WITH
Expand Down
Loading

0 comments on commit 47e66b9

Please sign in to comment.