Skip to content

Commit 94128d2

Browse files
authored
fix: correct config detail change count (#2073)
1 parent a0acc75 commit 94128d2

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

tests/config_detail_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tests
2+
3+
import (
4+
"github.com/flanksource/duty/job"
5+
"github.com/flanksource/duty/tests/fixtures/dummy"
6+
ginkgo "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
var _ = ginkgo.Describe("Config detail", func() {
11+
ginkgo.It("reports only the config's own changes", func() {
12+
Expect(job.RefreshConfigItemSummary7d(DefaultContext)).To(Succeed())
13+
14+
var expectedChanges int
15+
err := DefaultContext.DB().Raw(
16+
"SELECT config_changes_count FROM config_item_summary_7d WHERE config_id = ?",
17+
dummy.NginxIngressPod.ID,
18+
).Scan(&expectedChanges).Error
19+
Expect(err).NotTo(HaveOccurred())
20+
21+
var changes int
22+
err = DefaultContext.DB().Raw(
23+
"SELECT (summary->>'changes')::int FROM config_detail WHERE id = ?",
24+
dummy.NginxIngressPod.ID,
25+
).Scan(&changes).Error
26+
Expect(err).NotTo(HaveOccurred())
27+
Expect(changes).To(Equal(expectedChanges))
28+
})
29+
})

views/006_config_views.sql

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ LEFT JOIN
6969
GROUP BY
7070
ci.id, ci.name;
7171

72+
CREATE UNIQUE INDEX IF NOT EXISTS config_item_summary_7d_config_id_idx
73+
ON config_item_summary_7d (config_id);
7274

7375
CREATE OR REPLACE FUNCTION refresh_config_item_summary_7d() RETURNS VOID AS $$
7476
BEGIN
@@ -1011,7 +1013,7 @@ CREATE OR REPLACE VIEW config_detail AS
10111013
json_build_object(
10121014
'relationships', COALESCE(related.related_count, 0) + COALESCE(reverse_related.related_count, 0),
10131015
'analysis', COALESCE(analysis.analysis_count, 0),
1014-
'changes', COALESCE(change_summary.total_changes_count, 0),
1016+
'changes', COALESCE(change_summary.config_changes_count, 0),
10151017
'playbook_runs', COALESCE(playbook_runs.playbook_runs_count, 0),
10161018
'checks', COALESCE(config_checks.checks_count, 0)
10171019
) as summary,
@@ -1034,11 +1036,7 @@ CREATE OR REPLACE VIEW config_detail AS
10341036
CROSS JOIN LATERAL jsonb_each_text(config_analysis_type_counts)
10351037
GROUP BY config_id) as analysis
10361038
ON ci.id = analysis.config_id
1037-
LEFT JOIN
1038-
(SELECT ci.id AS config_id, SUM(cs.config_changes_count) AS total_changes_count
1039-
FROM config_items ci
1040-
LEFT JOIN config_item_summary_7d cs ON ci.path LIKE '%' || cs.config_id || '%'
1041-
GROUP BY ci.id) AS change_summary
1039+
LEFT JOIN config_item_summary_7d AS change_summary
10421040
ON ci.id = change_summary.config_id
10431041
LEFT JOIN
10441042
(SELECT config_id, count(*) as playbook_runs_count FROM playbook_runs

0 commit comments

Comments
 (0)