Skip to content

Commit

Permalink
fix: add deduplication in topology
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Nov 3, 2023
1 parent 008fc27 commit b71c49e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 37 deletions.
21 changes: 19 additions & 2 deletions query/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type TopologyOptions struct {
// when set to true, only the children (except the direct children) are returned.
// when set to false, the direct children & the parent itself is fetched.
nonDirectChildrenOnly bool

// when set to true, only the children (except the direct children) are returned.
// when set to false, the direct children & the parent itself is fetched.
heirsOnly bool

Check failure on line 35 in query/topology.go

View workflow job for this annotation

GitHub Actions / lint

field `heirsOnly` is unused (unused)
}

func (opt TopologyOptions) String() string {
Expand Down Expand Up @@ -82,6 +86,7 @@ func (opt TopologyOptions) componentRelationWhereClause() string {
if opt.Labels != nil {
s += ` AND (parent.labels @> @labels)`
}

if opt.ID != "" {
if !opt.nonDirectChildrenOnly {
s += " AND (component_relationships.relationship_id = @id OR parent.parent_id = @id)"
Expand All @@ -90,7 +95,7 @@ func (opt TopologyOptions) componentRelationWhereClause() string {
}
} else {
if !opt.nonDirectChildrenOnly {
s += " AND component_relationships.component_id = NULL"
s += " AND component_relationships.component_id IS NULL"
} else {
s += " AND component_relationships.component_id IS NOT NULL"
}
Expand Down Expand Up @@ -201,7 +206,17 @@ func fetchAllComponents(ctx context.Context, params TopologyOptions) (TopologyRe
}

if len(nonDirectChildren.Components) > 0 {
response.Components = append(response.Components, nonDirectChildren.Components...)

compMap := make(map[string]bool)
for _, c := range response.Components {
compMap[c.ID.String()] = true
}
for _, c := range nonDirectChildren.Components {
if _, exists := compMap[c.ID.String()]; !exists {
response.Components = append(response.Components, c)
}
}

response.HealthStatuses = append(response.HealthStatuses, nonDirectChildren.HealthStatuses...)
response.Teams = append(response.Teams, nonDirectChildren.Teams...)
response.Types = append(response.Types, nonDirectChildren.Types...)
Expand All @@ -224,6 +239,7 @@ func Topology(ctx context.Context, params TopologyOptions) (*TopologyResponse, e
if err != nil {
return nil, err
}

response.Components = applyTypeFilter(response.Components, params.Types...)

if !params.Flatten {
Expand Down Expand Up @@ -306,6 +322,7 @@ func createComponentTree(params TopologyOptions, components models.Components) [
compChildrenMap[c.ParentId.String()] = append(compChildrenMap[c.ParentId.String()], c)
}
}

for _, parentID := range c.Parents {
compChildrenMap[parentID] = append(compChildrenMap[parentID], c)
}
Expand Down
34 changes: 1 addition & 33 deletions tests/fixtures/expectations/topology_child_tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,6 @@
},
"tooltip": "Logistic UI Pod",
"type": "KubernetesPod"
},
{
"agent_id": "00000000-0000-0000-0000-000000000000",
"icon": "icon-kubernetes-pod",
"id": "018681ff-80ed-d10d-21ef-c74f152b085b",
"is_leaf": false,
"name": "logistics-api-574dc95b5d-mp64w",
"parent_id": "018681ff-559f-7183-19d1-7d898b4e1413",
"parents": [
"018681fe-f5aa-37e9-83f7-47b5b0232d5e"
],
"status": "healthy",
"summary": {
"healthy": 1
},
"type": "KubernetesPod"
},
{
"agent_id": "00000000-0000-0000-0000-000000000000",
"icon": "icon-kubernetes-pod",
"id": "018681ff-b6c1-a14d-2fd4-8c7dac94cddd",
"is_leaf": false,
"name": "logistics-ui-676b85b87c-tjjcp",
"parent_id": "018681ff-559f-7183-19d1-7d898b4e1413",
"parents": [
"018681fe-f5aa-37e9-83f7-47b5b0232d5e"
],
"status": "healthy",
"summary": {
"healthy": 1
},
"type": "KubernetesPod"
}
],
"external_id": "dummy/node-a",
Expand All @@ -87,7 +55,7 @@
"path": "018681fe-8156-4b91-d178-caf8b3c2818c.018681fe-b27e-7627-72c2-ad18e93f72f4",
"status": "healthy",
"summary": {
"healthy": 4
"healthy": 2
},
"tooltip": "Node A",
"type": "KubernetesNode"
Expand Down
2 changes: 1 addition & 1 deletion tests/matcher/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func MatchFixture(path string, result any, jqFilter string) {

writeTestResult(path, resultJSON)
expected := readTestFile(path)
CompareJSON([]byte(expected), resultJSON, &jqFilter)
CompareJSON(resultJSON, []byte(expected), &jqFilter)
}

func readTestFile(p string) string {
Expand Down
2 changes: 1 addition & 1 deletion tests/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func prettytree(mytree []*models.Component) {
func testTopologyJSON(opts query.TopologyOptions, path string) {
tree, err := query.Topology(testutils.DefaultContext, opts)
Expect(err).ToNot(HaveOccurred())
matcher.MatchFixture(path, tree, `del(.. | .created_at?, .updated_at?)`)
matcher.MatchFixture(path, tree, `del(.. | .created_at?, .updated_at?, .path?, .external_id?, .tooltip?)`)
}

var _ = ginkgo.Describe("Topology behavior", func() {
Expand Down

0 comments on commit b71c49e

Please sign in to comment.