Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Visualizer to use GraphViz #144

Merged
merged 5 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"testing.openTesting": "neverOpen",
"jest.outputConfig": {
"revealOn": "run",
"revealWithFocus": "none",
"clearOnRun": "none"
}
}
2 changes: 1 addition & 1 deletion calm/samples/traderx/traderx.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/finos-labs/architecture-as-code/main/calm/draft/2024-03/meta/calm.json",
"$schema": "https://raw.githubusercontent.com/finos-labs/architecture-as-code/main/calm/draft/2024-04/meta/calm.json",
"nodes": [
{
"unique-id": "traderx-system",
Expand Down
4 changes: 2 additions & 2 deletions cli/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export default {
testEnvironment: 'node',
testMatch: ['<rootDir>/src/**/*.spec.ts'],
moduleNameMapper: {
'(.+)\\.js': '$1'
},
'^(.+)\\.js$': '$1'
}
};
127 changes: 127 additions & 0 deletions cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"ajv": "^8.12.0",
"commander": "^12.0.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we removing Mermaid now that it's no longer needed? That should remove the dependency on Playwright/puppeteer

Copy link
Member Author

@aidanm3341 aidanm3341 Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, I'll do a follow up PR to remove all the old visualizer code once this is all merged in and done

"fetch-mock": "^9.11.0",
"graphviz-cli": "^2.0.0",
"mkdirp": "^3.0.1",
"ts-graphviz": "^2.1.1",
"tsconfig-paths": "^4.2.0",
"winston": "^3.13.0"
},
Expand Down
12 changes: 12 additions & 0 deletions cli/src/commands/validate/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ jest.mock('@stoplight/spectral-core', () => {
};
});

jest.mock('../helper.js', () => {
return {
initLogger: () => {
return {
info: jest.fn(),
debug: jest.fn(),
error: jest.fn()
};
}
};
});

const metaSchemaLocation = 'test_fixtures/calm';
const debugDisabled = false;

Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/validate/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { initLogger } from '../helper.js';

let logger: winston.Logger; // defined later at startup

export default async function validate(jsonSchemaInstantiationLocation: string, jsonSchemaLocation: string, metaSchemaPath: string, debug: boolean) {
export default async function validate(jsonSchemaInstantiationLocation: string, jsonSchemaLocation: string, metaSchemaPath: string, debug: boolean = false) {
logger = initLogger(debug);
let exitCode = 0;
try {
Expand Down Expand Up @@ -63,7 +63,7 @@ function loadMetaSchemas(ajv: Ajv2020, metaSchemaLocation: string) {

async function runSpectralValidations(jsonSchemaInstantiation: string) {
const spectral = new Spectral();
spectral.setRuleset(await getRuleset('../spectral/calm-validation-rules.yaml'));
spectral.setRuleset(await getRuleset('../spectral/instantiation/validation-rules.yaml'));
const issues = await spectral.run(jsonSchemaInstantiation);
if (issues && issues.length > 0) {
logger.info(`Spectral issues: ${prettifyJson(issues)}`);
Expand Down
61 changes: 61 additions & 0 deletions cli/src/commands/visualize/Types.d.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should definitely go into a shared module eventually. Other projects should be re-using this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. I'll just move it now so it's done

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
export interface CALMInstantiation {
nodes: CALMNode[],
relationships: CALMRelationship[]
}

export type NodeType = 'actor' | 'system' | 'service' | 'database' | 'internal-network' | 'ldap' | 'dataclient';

export interface CALMNode {
name: string,
class?: string,
'unique-id': string,
'node-type': NodeType,
description: string,
'data-classification'?: string,
'run-as'?: string,
instance?: string
}

export type CALMRelationship = CALMInteractsRelationship | CALMConnectsRelationship | CALMDeployedInRelationship | CALMComposedOfRelationship;

export interface CALMInteractsRelationship {
'relationship-type': {
willosborne marked this conversation as resolved.
Show resolved Hide resolved
'interacts': {
actor: string,
nodes: string[]
}
},
uniqueId: string,
}

export interface CALMConnectsRelationship {
'relationship-type': {
'connects': {
source: { node: string, interface?: string },
destination: { node: string, interface?: string }
}
},
uniqueId: string,
protocol?: string,
authentication?: string,
}

export interface CALMDeployedInRelationship {
'relationship-type': {
'deployed-in': {
container: string,
nodes: string[]
}
},
uniqueId: string,
}

export interface CALMComposedOfRelationship {
'relationship-type': {
'composed-of': {
container: string,
nodes: string[]
},
}
uniqueId: string,
}
93 changes: 93 additions & 0 deletions cli/src/commands/visualize/calmToDot.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { CALMInstantiation } from './Types';
import calmToDot from './calmToDot';

jest.mock('../helper.js', () => {
return {
initLogger: () => {
return {
info: jest.fn(),
debug: jest.fn(),
error: jest.fn()
};
}
};
});

describe('calmToDot', () => {
let calm: CALMInstantiation;

beforeEach(() => {
calm = {
nodes: [
{
'unique-id': 'node-1',
'name': 'Node 1',
'node-type': 'service',
'description': 'This is node 1'
},
{
'unique-id': 'node-2',
'name': 'Node 2',
'node-type': 'service',
'description': 'This is node 2'
}
],
relationships: [
{
'uniqueId': 'relationship-1',
'protocol': 'HTTPS',
'relationship-type': {
'connects': {
source: {
node: 'node-1'
},
destination: {
node: 'node-2'
}
}
}
}
]
};
});

it('creates a basic node', () => {
const actualDot = calmToDot(calm);
expect(actualDot).toContain('"node-1" [');
expect(actualDot).toContain('label = "Service: Node 1"');
expect(actualDot).toContain('shape = "box"');
});

it('creates a basic relationship', () => {
const actualDot = calmToDot(calm);
expect(actualDot).toContain('"node-1" -> "node-2" [');
expect(actualDot).toContain('label = "connects HTTPS "');
});

it('creates an empty graph when given empty calm', () => {
calm = { nodes: [], relationships: []};
const actualDot = calmToDot(calm);
expect(actualDot).toEqual('digraph { nodesep = 0.5; }');
});

it('creates a subgraph relationship', () => {
calm = {
...calm,
relationships: [
{
'uniqueId': 'subtest',
'relationship-type': {
'deployed-in': {
container: 'node-1',
nodes: ['node-2']
}
}
}
]
};
const actualDot = calmToDot(calm);
expect(actualDot).toContain('subgraph "clusternode-1" {');
expect(actualDot).toContain('label = "node-1"');
expect(actualDot).toContain('"node-2" [');
});
});
Loading
Loading