-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.ts
193 lines (179 loc) · 5.69 KB
/
graph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { isCoveredByStory, isIgnoredElement } from "./util";
function isRepository(element: string): boolean {
return element.indexOf("Repository") > -1;
}
function getOnlyOneRepoAsDependency(
dependencies: string[]
): string | undefined {
const relevantDependencies = dependencies.filter(
(dependency) => !isIgnoredElement(dependency)
);
return relevantDependencies.length === 1 &&
isRepository(relevantDependencies[0])
? relevantDependencies[0]
: undefined;
}
function isDependentRepoUsedOnlyByThisService(
repositoryMap: { [key: string]: string[] },
serviceName: string,
dependencies: string[]
) {
const repo = getOnlyOneRepoAsDependency(dependencies);
const relevantDependencies = repo
? repositoryMap[repo]?.filter((dependency) => !isIgnoredElement(dependency))
: [];
return (
relevantDependencies.length === 1 && relevantDependencies[0] === serviceName
);
}
function isMigrated(dependencies: string[]) {
return (
dependencies &&
dependencies.filter(
(dependency) =>
dependency === "wrapMigratedService" ||
dependency === "wrapFullyMigratedService" ||
dependency === "wrapCloudAgnosticService"
).length > 0
);
}
function isFullyMigrated(dependencies: string[]) {
return (
dependencies.filter(
(dependency) => dependency === "wrapFullyMigratedService"
).length > 0
);
}
export enum NodeColor {
DEFAULT_COLOR = "white",
MIGRATED_COLOR = "#00FF00",
FULLY_MIGRATED_COLOR = "#008800",
FULLY_MIGRATED_CANDIDATE_COLOR = "#008888",
NO_DEPENDENCIES_COLOR = "yellow",
CANDIDATE_COLOR = "orange",
REPO_MULTIPLE_OWNERS = "purple",
SERV_MULTIPLE_DEPENDENCIES = "cyan",
ERROR = "red",
}
export type Element = {
name: string;
color: NodeColor;
type: "service" | "repository";
isCoveredByStory: boolean;
};
export type Graph = {
nodes: { [name: string]: Element };
edges: { from: string; to: string }[];
};
export function createGraph(
serviceMap: { [key: string]: string[] },
repositoryMap: { [key: string]: string[] }
): Graph {
const elements: Graph["nodes"] = {};
const addElement = function (name: string, color: Element["color"]) {
if (elements[name]) {
return;
}
console.log(`Adding ${name} with color ${color}`)
elements[name] = {
name,
color,
isCoveredByStory: isCoveredByStory(name),
type: name.indexOf("Repository") > -1 ? "repository" : "service",
};
};
// Color the service nodes and dependencies
Object.keys(serviceMap)
.filter((dependency) => !isIgnoredElement(dependency))
.forEach((serviceName) => {
const dependencies = serviceMap[serviceName] ?? [];
console.log(serviceName, dependencies);
// Already migrated
if (isMigrated(dependencies)) {
const fullyMigrated = isFullyMigrated(dependencies);
const color = fullyMigrated
? NodeColor.FULLY_MIGRATED_COLOR
: NodeColor.MIGRATED_COLOR;
addElement(serviceName, color);
dependencies
.filter((dependency) => !isIgnoredElement(dependency))
.forEach((dependency) => {
// Mark repos of migrated services as migrated
if (isRepository(dependency)) {
addElement(dependency, NodeColor.MIGRATED_COLOR);
} else {
// A migrated service may not depend on a non-migrated service
if (!isMigrated(serviceMap[dependency])) {
addElement(dependency, NodeColor.ERROR);
}
}
});
} else if (
isMigrated(dependencies) &&
!isFullyMigrated(dependencies) &&
dependencies.filter(
(dependency) =>
!isIgnoredElement(dependency) &&
isFullyMigrated(serviceMap[dependency])
)
) {
addElement(serviceName, NodeColor.FULLY_MIGRATED_CANDIDATE_COLOR);
} else if (dependencies.length === 0) {
// No dependencies
addElement(serviceName, NodeColor.NO_DEPENDENCIES_COLOR);
} else if (
getOnlyOneRepoAsDependency(dependencies) &&
isDependentRepoUsedOnlyByThisService(
repositoryMap,
serviceName,
dependencies
)
) {
// Only one repository
addElement(serviceName, NodeColor.CANDIDATE_COLOR);
dependencies
.filter((dependency) => !isIgnoredElement(dependency))
.forEach((dependency) => {
addElement(dependency, NodeColor.CANDIDATE_COLOR);
});
}
});
Object.keys(repositoryMap).forEach((repositoryName) => {
if (isIgnoredElement(repositoryName)) {
return;
}
const dependencies = repositoryMap[repositoryName];
// Color repositories used by multiple services
const dependingOnRepository = dependencies.filter(
(d) => !isIgnoredElement(d)
);
if (dependingOnRepository.length > 1) {
addElement(repositoryName, NodeColor.REPO_MULTIPLE_OWNERS);
} else if (dependingOnRepository.length <= 1) {
dependingOnRepository.forEach((serviceName) => {
if (Object.keys(elements).indexOf(serviceName) === -1) {
addElement(serviceName, NodeColor.SERV_MULTIPLE_DEPENDENCIES);
}
});
}
addElement(repositoryName, NodeColor.DEFAULT_COLOR);
});
const edges: Graph["edges"] = [];
// Draw nodes and dependencies
Object.keys(serviceMap).forEach((serviceName) => {
if (isIgnoredElement(serviceName)) {
return;
}
const dependencies = serviceMap[serviceName];
dependencies.forEach((dependency) => {
if (serviceName === dependency || isIgnoredElement(dependency)) {
return;
}
edges.push({ from: serviceName, to: dependency });
});
});
return {
nodes: elements,
edges,
};
}