Skip to content

Commit

Permalink
fix(graph): sort graph nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
davydkov committed Dec 9, 2023
1 parent e7ec019 commit 3c8adf7
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 187 deletions.
15 changes: 10 additions & 5 deletions packages/core/src/utils/fqn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,16 @@ export function ancestorsFqn(fqn: Fqn): Fqn[] {
export function compareFqnHierarchically(a: string, b: string): number {
const depthA = a.split('.').length
const depthB = b.split('.').length
if (depthA === depthB) {
// return a.localeCompare(b)
return 0
} else {
return depthA - depthB
switch (true) {
case depthA > depthB: {
return 1
}
case depthA < depthB: {
return -1
}
default: {
return 0
}
}
}

Expand Down
13 changes: 8 additions & 5 deletions packages/core/src/utils/relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ export const compareRelations = <T extends { source: string; target: string }>(a
return -1
}
const compareParents = parentA && parentB ? compareFqnHierarchically(parentA, parentB) : 0
return (
compareParents ||
compareFqnHierarchically(a.source, b.source) ||
compareFqnHierarchically(a.target, b.target)
)
if (compareParents === 0) {
const compareSource = compareFqnHierarchically(a.source, b.source)
if (compareSource !== 0) {
return compareSource
}
return compareFqnHierarchically(a.target, b.target)
}
return compareParents
}

const isInside = (parent: Fqn): RelationPredicate => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,70 +88,70 @@ exports[`compute-element-view > view of cloud 1`] = `
"title": "support",
},
{
"children": [],
"children": [
"cloud.frontend",
"cloud.backend",
],
"color": "primary",
"depth": 1,
"description": null,
"id": "cloud.frontend",
"id": "cloud",
"inEdges": [
"support:cloud.frontend",
"customer:cloud.frontend",
],
"kind": "container",
"level": 1,
"kind": "system",
"level": 0,
"links": null,
"outEdges": [
"cloud.frontend:cloud.backend",
"cloud.backend:amazon",
],
"parent": "cloud",
"shape": "browser",
"parent": null,
"shape": "rectangle",
"tags": null,
"technology": null,
"title": "frontend",
"title": "cloud",
},
{
"children": [],
"color": "primary",
"description": null,
"id": "cloud.backend",
"id": "cloud.frontend",
"inEdges": [
"cloud.frontend:cloud.backend",
"support:cloud.frontend",
"customer:cloud.frontend",
],
"kind": "container",
"level": 1,
"links": null,
"outEdges": [
"cloud.backend:amazon",
"cloud.frontend:cloud.backend",
],
"parent": "cloud",
"shape": "rectangle",
"shape": "browser",
"tags": null,
"technology": null,
"title": "backend",
"title": "frontend",
},
{
"children": [
"cloud.frontend",
"cloud.backend",
],
"children": [],
"color": "primary",
"depth": 1,
"description": null,
"id": "cloud",
"id": "cloud.backend",
"inEdges": [
"support:cloud.frontend",
"customer:cloud.frontend",
"cloud.frontend:cloud.backend",
],
"kind": "system",
"level": 0,
"kind": "container",
"level": 1,
"links": null,
"outEdges": [
"cloud.backend:amazon",
],
"parent": null,
"parent": "cloud",
"shape": "rectangle",
"tags": null,
"technology": null,
"title": "cloud",
"title": "backend",
},
{
"children": [],
Expand Down
58 changes: 31 additions & 27 deletions packages/graph/src/compute-view/__test__/element-expr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ describe('element-expr', () => {
$include('support'),
$exclude('cloud.backend')
])
expect(nodeIds).toEqual(['customer', 'support', 'cloud.frontend', 'cloud'])
expect(edgeIds).toEqual(['customer:cloud.frontend', 'customer:cloud', 'support:cloud.frontend'])
expect(nodeIds).toEqual(['support', 'customer', 'cloud', 'cloud.frontend'])
expect(edgeIds).to.have.same.members([
'customer:cloud.frontend',
'customer:cloud',
'support:cloud.frontend'
])
})

describe('view of cloud', () => {
Expand All @@ -55,9 +59,9 @@ describe('element-expr', () => {
expect(nodeIds).toEqual([
'customer',
'support',
'cloud',
'cloud.frontend',
'cloud.backend',
'cloud',
'amazon'
])
for (const node of nodes) {
Expand All @@ -84,7 +88,7 @@ describe('element-expr', () => {

it('include *, exclude support', () => {
const { nodeIds, edgeIds } = computeView('cloud', [$include('*'), $exclude('support')])
expect(nodeIds).toEqual(['customer', 'cloud.frontend', 'cloud.backend', 'cloud', 'amazon'])
expect(nodeIds).toEqual(['customer', 'cloud', 'cloud.frontend', 'cloud.backend', 'amazon'])
expect(edgeIds).to.have.same.members([
'cloud.frontend:cloud.backend',
'cloud.backend:amazon',
Expand Down Expand Up @@ -113,12 +117,12 @@ describe('element-expr', () => {
])
expect(nodeIds).toEqual([
'support',
'cloud.frontend.adminPanel',
'customer',
'cloud.frontend.dashboard',
'cloud',
'cloud.frontend',
'cloud.frontend.adminPanel',
'cloud.frontend.dashboard',
'cloud.backend',
'cloud',
'amazon'
])
expect(edgeIds).to.have.same.members([
Expand All @@ -143,29 +147,29 @@ describe('element-expr', () => {
const { nodeIds, edgeIds } = computeView('cloud.frontend', [$include('*')])
expect(nodeIds).toEqual([
'support',
'cloud.frontend.adminPanel',
'customer',
'cloud.frontend.dashboard',
'cloud.frontend',
'cloud.frontend.adminPanel',
'cloud.frontend.dashboard',
'cloud.backend'
])
expect(edgeIds).toEqual([
'support:cloud.frontend.adminPanel',
'cloud.frontend.adminPanel:cloud.backend',
'customer:cloud.frontend.dashboard',
'cloud.frontend.adminPanel:cloud.backend',
'cloud.frontend.dashboard:cloud.backend'
])
})
it('include *, cloud', () => {
const { nodeIds, edgeIds } = computeView('cloud.frontend', [$include('*'), $include('cloud')])
expect(nodeIds).toEqual([
'support',
'cloud.frontend.adminPanel',
'customer',
'cloud.frontend.dashboard',
'cloud',
'cloud.frontend',
'cloud.backend',
'cloud'
'cloud.frontend.dashboard',
'cloud.frontend.adminPanel',
'cloud.backend'
])
expect(edgeIds).to.have.same.members([
'support:cloud.frontend.adminPanel',
Expand All @@ -182,15 +186,15 @@ describe('element-expr', () => {
])
expect(nodeIds).toEqual([
'customer',
'cloud.frontend.dashboard',
'cloud.frontend.adminPanel',
'cloud.frontend',
'cloud.frontend.adminPanel',
'cloud.frontend.dashboard',
'cloud.backend'
])
expect(edgeIds).toEqual([
'customer:cloud.frontend.dashboard',
'cloud.frontend.dashboard:cloud.backend',
'cloud.frontend.adminPanel:cloud.backend'
'cloud.frontend.adminPanel:cloud.backend',
'cloud.frontend.dashboard:cloud.backend'
])
})
it('include *, cloud, exclude cloud.backend', () => {
Expand All @@ -201,11 +205,11 @@ describe('element-expr', () => {
])
expect(nodeIds).toEqual([
'support',
'cloud.frontend.adminPanel',
'customer',
'cloud.frontend.dashboard',
'cloud',
'cloud.frontend',
'cloud'
'cloud.frontend.dashboard',
'cloud.frontend.adminPanel'
])
expect(edgeIds).to.have.same.members([
'support:cloud.frontend.adminPanel',
Expand All @@ -220,9 +224,9 @@ describe('element-expr', () => {
const { nodeIds, edgeIds } = computeView('cloud.backend', [$include('*')])
expect(nodeIds).toEqual([
'cloud.frontend',
'cloud.backend',
'cloud.backend.graphql',
'cloud.backend.storage',
'cloud.backend',
'amazon'
])
expect(edgeIds).toEqual([
Expand All @@ -239,9 +243,9 @@ describe('element-expr', () => {
expect(nodeIds).toEqual([
'cloud.frontend.adminPanel',
'cloud.frontend.dashboard',
'cloud.backend',
'cloud.backend.graphql',
'cloud.backend.storage',
'cloud.backend',
'amazon'
])
expect(edgeIds).toEqual([
Expand All @@ -258,12 +262,12 @@ describe('element-expr', () => {
$include('cloud.frontend.* -> cloud.backend')
])
expect(nodeIds).toEqual([
'cloud.frontend',
'cloud.frontend.adminPanel',
'cloud.frontend.dashboard',
'cloud.frontend',
'cloud.backend',
'cloud.backend.graphql',
'cloud.backend.storage',
'cloud.backend',
'amazon'
])
expect(edgeIds).toEqual([
Expand All @@ -277,11 +281,11 @@ describe('element-expr', () => {
it('include *, cloud', () => {
const { nodeIds, edgeIds } = computeView('cloud.backend', [$include('*'), $include('cloud')])
expect(nodeIds).toEqual([
'cloud',
'cloud.frontend',
'cloud.backend',
'cloud.backend.graphql',
'cloud.backend.storage',
'cloud.backend',
'cloud',
'amazon'
])
expect(edgeIds).toEqual([
Expand Down
18 changes: 9 additions & 9 deletions packages/graph/src/compute-view/__test__/incoming-expr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ describe('incoming-expr', () => {
])
expect(nodeIds).toEqual([
'customer',
'cloud.frontend.dashboard',
'support',
'cloud.frontend.adminPanel',
'cloud',
'cloud.frontend',
'cloud.frontend.dashboard',
'cloud.backend',
'cloud',
'amazon'
'amazon',
'cloud.frontend.adminPanel'
])
expect(edgeIds).toEqual([
'customer:cloud.frontend.dashboard',
Expand All @@ -50,7 +50,7 @@ describe('incoming-expr', () => {

it('exclude -> amazon', () => {
const { nodeIds, edgeIds } = computeView('cloud', [$include('*'), $exclude('-> amazon')])
expect(nodeIds).toEqual(['customer', 'support', 'cloud.frontend', 'cloud.backend', 'cloud'])
expect(nodeIds).toEqual(['customer', 'support', 'cloud', 'cloud.frontend', 'cloud.backend'])
expect(edgeIds).toEqual([
'customer:cloud.frontend',
'support:cloud.frontend',
Expand All @@ -60,7 +60,7 @@ describe('incoming-expr', () => {

it('exclude -> *', () => {
const { nodeIds, edgeIds } = computeView('cloud', [$include('*'), $exclude('-> *')])
expect(nodeIds).toEqual(['cloud.frontend', 'cloud.backend', 'cloud', 'amazon'])
expect(nodeIds).toEqual(['cloud', 'cloud.frontend', 'cloud.backend', 'amazon'])
expect(edgeIds).to.have.same.members(['cloud.frontend:cloud.backend', 'cloud.backend:amazon'])
})

Expand All @@ -69,7 +69,7 @@ describe('incoming-expr', () => {
$include('*'),
$exclude('-> cloud.frontend.dashboard')
])
expect(nodeIds).toEqual(['support', 'cloud.frontend', 'cloud.backend', 'cloud', 'amazon'])
expect(nodeIds).toEqual(['support', 'cloud', 'cloud.frontend', 'cloud.backend', 'amazon'])
expect(edgeIds).to.have.same.members([
'cloud.frontend:cloud.backend',
'cloud.backend:amazon',
Expand All @@ -86,10 +86,10 @@ describe('incoming-expr', () => {
])
expect(nodeIds).toEqual([
'support',
'cloud.frontend.adminPanel',
'customer',
'cloud.frontend.dashboard',
'cloud.frontend',
'cloud.frontend.adminPanel',
'cloud.frontend.dashboard',
// 'cloud.backend', // implicit is removed, due to `-> cloud.backend.*`
'cloud.backend.graphql'
])
Expand Down
Loading

0 comments on commit 3c8adf7

Please sign in to comment.