Skip to content

Commit

Permalink
modify related configs views to return configs as columns instead of
Browse files Browse the repository at this point in the history
json
  • Loading branch information
adityathebe committed Mar 5, 2024
1 parent 4bfb448 commit e9f64f7
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 47 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.bin
*.out.json
ginkgo.report
.vscode
.vscode
tests/
18 changes: 15 additions & 3 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,19 @@ const (
)

type RelatedConfig struct {
Relation string `json:"relation"`
Type RelatedConfigType `json:"relation_type" gorm:"column:relation_type"`
Config types.JSONMap `json:"config"`
Relation string `json:"relation"`
RelationType RelatedConfigType `json:"relation_type"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Tags types.JSONStringMap `json:"tags"`
Changes *types.JSONMap `json:"changes,omitempty"`
Analysis *types.JSONMap `json:"analysis,omitempty"`
CostPerMinute *float64 `json:"cost_per_minute,omitempty"`
CostTotal1d *float64 `json:"cost_total_1d,omitempty"`
CostTotal7d *float64 `json:"cost_total_7d,omitempty"`
CostTotal30d *float64 `json:"cost_total_30d,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
AgentID uuid.UUID `json:"agent_id"`
}
16 changes: 8 additions & 8 deletions tests/config_relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var _ = ginkgo.Describe("Config relationship recursive", ginkgo.Ordered, func()
Expect(err).To(BeNil())
Expect(len(relatedConfigs)).To(Equal(1))

Expect(relatedConfigs[0].Config["id"].(string)).To(Equal(F.ID.String()))
Expect(relatedConfigs[0].ID.String()).To(Equal(F.ID.String()))
})

ginkgo.It("should correctly return zero relationships for leaf nodes", func() {
Expand All @@ -120,7 +120,7 @@ var _ = ginkgo.Describe("Config relationship recursive", ginkgo.Ordered, func()
Expect(err).To(BeNil())
Expect(len(relatedConfigs)).To(Equal(7))

relatedIDs := lo.Map(relatedConfigs, func(rc models.RelatedConfig, _ int) uuid.UUID { return uuid.MustParse(rc.Config["id"].(string)) })
relatedIDs := lo.Map(relatedConfigs, func(rc models.RelatedConfig, _ int) uuid.UUID { return rc.ID })
Expect(relatedIDs).To(HaveExactElements([]uuid.UUID{B.ID, C.ID, D.ID, E.ID, F.ID, G.ID, H.ID}))
})
})
Expand All @@ -132,7 +132,7 @@ var _ = ginkgo.Describe("Config relationship recursive", ginkgo.Ordered, func()
Expect(err).To(BeNil())

Expect(len(relatedConfigs)).To(Equal(5))
relatedIDs := lo.Map(relatedConfigs, func(rc models.RelatedConfig, _ int) uuid.UUID { return uuid.MustParse(rc.Config["id"].(string)) })
relatedIDs := lo.Map(relatedConfigs, func(rc models.RelatedConfig, _ int) uuid.UUID { return rc.ID })
Expect(relatedIDs).To(HaveExactElements([]uuid.UUID{C.ID, A.ID, H.ID, D.ID, B.ID}))
})

Expand All @@ -141,7 +141,7 @@ var _ = ginkgo.Describe("Config relationship recursive", ginkgo.Ordered, func()
err := DefaultContext.DB().Raw("SELECT * FROM related_configs_recursive(?, 'incoming', false)", G.ID).Find(&relatedConfigs).Error
Expect(err).To(BeNil())

relatedIDs := lo.Map(relatedConfigs, func(rc models.RelatedConfig, _ int) uuid.UUID { return uuid.MustParse(rc.Config["id"].(string)) })
relatedIDs := lo.Map(relatedConfigs, func(rc models.RelatedConfig, _ int) uuid.UUID { return rc.ID })
Expect(relatedIDs).To(HaveExactElements([]uuid.UUID{D.ID, B.ID, A.ID, H.ID}))
})
})
Expand All @@ -155,7 +155,7 @@ var _ = ginkgo.Describe("Config relationship recursive", ginkgo.Ordered, func()
Expect(err).To(BeNil())
Expect(len(relatedConfigs)).To(Equal(5))

relatedIDs := lo.Map(relatedConfigs, func(rc models.RelatedConfig, _ int) uuid.UUID { return uuid.MustParse(rc.Config["id"].(string)) })
relatedIDs := lo.Map(relatedConfigs, func(rc models.RelatedConfig, _ int) uuid.UUID { return rc.ID })
Expect(relatedIDs).To(HaveExactElements([]uuid.UUID{V.ID, W.ID, X.ID, Y.ID, Z.ID}))
})
})
Expand All @@ -174,7 +174,7 @@ var _ = ginkgo.Describe("Config relationship recursive", ginkgo.Ordered, func()
Expect(err).To(BeNil())
Expect(len(relatedConfigs)).To(Equal(3))

relatedIDs := lo.Map(relatedConfigs, func(rc models.RelatedConfig, _ int) uuid.UUID { return uuid.MustParse(rc.Config["id"].(string)) })
relatedIDs := lo.Map(relatedConfigs, func(rc models.RelatedConfig, _ int) uuid.UUID { return rc.ID })
Expect(relatedIDs).To(HaveExactElements([]uuid.UUID{X.ID, V.ID, U.ID}))
})
})
Expand All @@ -191,7 +191,7 @@ var _ = ginkgo.Describe("Config relationship", ginkgo.Ordered, func() {
for _, rc := range relatedConfigs {
Expect(rc.Relation).To(Equal("ClusterNode"))
Expect(rc.Type).To(Equal(models.RelatedConfigTypeOutgoing))
Expect(rc.Config["id"]).To(BeElementOf([]string{dummy.KubernetesNodeA.ID.String(), dummy.KubernetesNodeB.ID.String()}))
Expect(rc.ID.String()).To(BeElementOf([]string{dummy.KubernetesNodeA.ID.String(), dummy.KubernetesNodeB.ID.String()}))
}
})

Expand All @@ -203,6 +203,6 @@ var _ = ginkgo.Describe("Config relationship", ginkgo.Ordered, func() {
Expect(len(relatedConfigs)).To(Equal(1))
Expect(relatedConfigs[0].Relation).To(Equal("ClusterNode"))
Expect(relatedConfigs[0].Type).To(Equal(models.RelatedConfigTypeIncoming))
Expect(relatedConfigs[0].Config["id"]).To(Equal(dummy.KubernetesCluster.ID.String()))
Expect(relatedConfigs[0].ID.String()).To(Equal(dummy.KubernetesCluster.ID.String()))
})
})
96 changes: 61 additions & 35 deletions views/006_config_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -491,33 +491,44 @@ CREATE FUNCTION related_configs (
config_id UUID,
type_filter TEXT DEFAULT 'all',
include_deleted_configs BOOLEAN DEFAULT FALSE
)
RETURNS TABLE (
) RETURNS TABLE (
relation TEXT,
relation_type TEXT,
config JSONB
) AS $$
id uuid,
NAME TEXT,
TYPE TEXT,
tags jsonb,
changes json,
analysis json,
cost_per_minute NUMERIC(16, 4),
cost_total_1d NUMERIC(16, 4),
cost_total_7d NUMERIC(16, 4),
cost_total_30d NUMERIC(16, 4),
created_at TIMESTAMP WITH TIME ZONE,
updated_at TIMESTAMP WITH TIME ZONE,
agent_id uuid
)
AS $$
BEGIN
RETURN query
SELECT
r.relation,
r.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
configs.id,
configs.name,
configs.type,
configs.tags,
configs.changes,
configs.analysis,
configs.cost_per_minute,
configs.cost_total_1d,
configs.cost_total_7d,
configs.cost_total_30d,
configs.created_at,
configs.updated_at,
configs.agent_id
FROM related_config_ids($1, $2, $3) as r
LEFT JOIN configs AS c ON r.id = c.id;
LEFT JOIN configs ON r.id = configs.id;
END;
$$ LANGUAGE plpgsql;

Expand Down Expand Up @@ -559,28 +570,43 @@ CREATE FUNCTION related_configs_recursive (
config_id UUID,
type_filter TEXT DEFAULT 'outgoing',
include_deleted_configs BOOLEAN DEFAULT FALSE
) RETURNS TABLE (relation TEXT, relation_type TEXT, config JSONB) AS $$
) RETURNS TABLE (
relation TEXT,
relation_type TEXT,
id uuid,
name TEXT,
type TEXT,
tags jsonb,
changes json,
analysis json,
cost_per_minute NUMERIC(16, 4),
cost_total_1d NUMERIC(16, 4),
cost_total_7d NUMERIC(16, 4),
cost_total_30d NUMERIC(16, 4),
created_at TIMESTAMP WITH TIME ZONE,
updated_at TIMESTAMP WITH TIME ZONE,
agent_id uuid
) AS $$
BEGIN
RETURN query
SELECT
r.relation,
r.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
configs.id,
configs.name,
configs.type,
configs.tags,
configs.changes,
configs.analysis,
configs.cost_per_minute,
configs.cost_total_1d,
configs.cost_total_7d,
configs.cost_total_30d,
configs.created_at,
configs.updated_at,
configs.agent_id
FROM related_config_ids_recursive($1, $2, $3) as r
LEFT JOIN configs AS c ON r.id = c.id;
LEFT JOIN configs ON r.id = configs.id;
END;
$$ LANGUAGE plpgsql;

Expand Down

0 comments on commit e9f64f7

Please sign in to comment.