Passed
Pull Request — master (#7)
by Pawel
02:40
created

MappingHelpers.ts ➔ compareNodes   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
1
import { DependencyLink, DependencyNode, Network, Service } from '../../components/types';
2
import { ServiceDto } from '../../api/api.types';
3
4
export function mapServiceDtoToService(serviceDto: ServiceDto): Service {
5
    const service: Service = {
6
        name: serviceDto.name,
7
        version: serviceDto.version,
8
        capabilities: {},
9
        expectations: {},
10
    };
11
12
    const { expectations, capabilities } = serviceDto;
13
14
    const providerServices = Object.keys(expectations);
15
16
    providerServices.forEach((serviceName: string) => {
17
        service.expectations[serviceName] = {};
18
        const types = Object.keys(expectations[serviceName]);
19
20
        types.forEach((type: string) => {
21
            service.expectations[serviceName][type] = {
22
                ...expectations[serviceName][type],
23
                value: JSON.parse(expectations[serviceName][type].value),
24
            };
25
        });
26
    });
27
28
    const capabilityTypes = Object.keys(capabilities);
29
30
    capabilityTypes.forEach((type: string) => {
31
        service.capabilities[type] = {
32
            ...capabilities[type],
33
            value: JSON.parse(capabilities[type].value),
34
        };
35
    });
36
37
    return service;
38
}
39
40
export function createLinks(nodes: DependencyNode[], services: Service[]): DependencyLink[] {
41
    const mappedLinks = services.reduce((links: DependencyLink[], service: Service) => {
42
        const linksWithTypes: DependencyLink[] = [];
43
        const providerNames = Object.keys(service.expectations);
44
        const sourceNode = nodes.find((node: DependencyNode) => node.name === service.name && node.version === service.version);
45
        providerNames.forEach((name: string) => {
46
            const targetNode = nodes.find((node: DependencyNode) => node.name === name);
47
48
            Object.keys(service.expectations[name]).forEach((connectionType: string) => {
49
                if (sourceNode && targetNode) {
50
                    linksWithTypes.push({
51
                        source: sourceNode,
52
                        target: targetNode,
53
                        type: connectionType,
54
                    });
55
                }
56
            });
57
        });
58
59
        return [...links, ...linksWithTypes];
60
    }, []);
61
62
    mappedLinks.forEach((link: DependencyLink) => {
63
        const target = nodes.find(node => compareNodes(node, link.target));
64
        const source = nodes.find(node => compareNodes(node, link.source));
65
        if (target) {
66
            link.source.links.push(target);
67
        }
68
        if (source) {
69
            link.target.links.push(source);
70
        }
71
    });
72
73
    return mappedLinks;
74
}
75
76
export function createNetworkFromServices(services: Service[]): Network {
77
    const nodes = services.map((service: Service) => ({
78
        name: service.name,
79
        version: service.version,
80
        isProvider: Object.keys(service.capabilities).length > 0,
81
        isConsumer: Object.keys(service.expectations).length > 0,
82
        links: [],
83
        level: 0,
84
    }));
85
    return {
86
        nodes,
87
        links: createLinks(nodes, services),
88
    };
89
}
90
91
export function filterConnectedNodes(network: Network) {
92
    return network.nodes.filter(
93
        (node: DependencyNode) =>
94
            network.links.findIndex(
95
                (link: DependencyLink) =>
96
                    (link.source.name === node.name && link.source.version === node.version) ||
97
                    (link.target.name === node.name && link.target.version === node.version)
98
            ) >= 0
99
    );
100
}
101
102
function compareNodes(node1: DependencyNode, node2: DependencyNode) {
103
    return node1.name === node2.name && node1.version === node2.version;
104
}
105