-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-analyze-2.6.process.js
More file actions
380 lines (320 loc) · 10.9 KB
/
Copy path6-analyze-2.6.process.js
File metadata and controls
380 lines (320 loc) · 10.9 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// IMPORTS
import { Env } from '../helper/env.helper.js';
import { logger } from '../helper/logger.helper.js';
import { MongoDB } from '../dao/mongodb.dao.js';
import { Process } from './process.process.js';
import fs from 'fs';
import path from 'path';
import { DATABASES, DATABASE_COLORS } from '../constant/analyze.constant.js';
/**
* Represents a process to analyze the repositories.
*/
class ProcessAnalyze26 extends Process {
constructor() {
super();
// Dependencies.
this.env = new Env();
// Environment variables.
this.mongoDb = new MongoDB(
this.env.getMongoDbUrl(),
this.env.getMongoDbName(),
); // MongoDB connection.
// Constants.
this.SLICE = 5; // Top database categories to analyze while excluding others.
}
/**
* Executes the process to analyze repositories.
*/
process() {
const chart = (sets, intersections) =>
`
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Databases in Microservices</title>
<style>
body {
font-family: Arial, sans-serif; text-align: left;
}
svg {
width: 100%;
height: 100vh;
}
</style>
</head>
<body>
<h1>Database Categories Associations</h1>
<svg></svg>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-sankey@0.12.3/dist/d3-sankey.min.js"></script>
<script>
// Layout
const width = 500
const height = 1000
// Colors
const COLORS = ${JSON.stringify(DATABASE_COLORS)};
function mixColors(sets) {
let totalR = 0, totalG = 0, totalB = 0;
sets.forEach(c => {
c = COLORS[c].replace(/^#/, '');
if (c.length === 3) c = c.split('').map(x => x + x).join('');
totalR += parseInt(c.slice(0, 2), 16);
totalG += parseInt(c.slice(2, 4), 16);
totalB += parseInt(c.slice(4, 6), 16);
});
const avgR = Math.round(totalR / sets.length);
const avgG = Math.round(totalG / sets.length);
const avgB = Math.round(totalB / sets.length);
return \`#\${(1 << 24 | avgR << 16 | avgG << 8 | avgB).toString(16).slice(1).toUpperCase()}\`;
}
// Stankey
const sankey = d3.sankey()
.nodeWidth(20)
.nodePadding(15)
.size([width, height])
.nodeId(d => d.label);
const {nodes: sankeyNodes, links: sankeyLinks} = sankey({
nodes: ${JSON.stringify(sets)}.map(d => Object.assign({}, d)),
links: ${JSON.stringify(intersections)}.map(d => Object.assign({}, d))
});
// SVG
const svg = d3.select("svg")
.attr("viewBox", [width, 0, width, height + 10]);
// SVG node box
svg.append("g")
.selectAll("rect")
.data(sankeyNodes)
.join("rect")
.attr("x", d => d.x0)
.attr("y", d => d.y0)
.attr("height", d => d.y1 - d.y0)
.attr("width", d => d.x1 - d.x0)
.attr("fill", (d) => {
if (!d.label.includes("∩") && !d.label.includes("∪")) {
return COLORS[d.label];
} else if(d.label.includes("∪")) {
return COLORS[d.label.substring(0, d.label.indexOf(" "))]
} else {
const sets = d.label.split(" ∩ ");
return mixColors(sets);
}
})
//.attr("stroke", "#000")
.append("title")
.text(d => d.label);
// SVG node text
svg.append("g")
.selectAll("text")
.data(sankeyNodes)
.join("text")
.attr("x", d => d.x0 > (width / 2) ? d.x1 + 10 : d.x0 - 10)
.attr("y", d => (d.y1 + d.y0) / 2)
.attr("dy", "0.35em")
.attr("text-anchor", d => d.x0 > (width / 2) ? "start" : "end")
.text(d => d.label.split(' ').map(t => t.match(/w/) !== 0 ? t.substring(0,1) : t).join(' '));
// SVG links
svg.append("g")
.attr("fill", "none")
.selectAll("path")
.data(sankeyLinks)
.join("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", d => COLORS[d.source.label] || "#bbb")
.attr("stroke-width", d => Math.max(1, d.width))
.attr("stroke-opacity", 0.5)
.append("title")
.text(d => \`\${d.source.label} → \${d.target.label}\`);
</script>
</body>
`;
const analyze = async () => {
// ---
// ANALYZE: database co-usage in microservices as a Stankey diagram.
// ---
// Functions.
const getSubsets = (arr) => {
let subsets = [];
const totalSubsets = Math.pow(2, arr.length);
for (let i = 0; i < totalSubsets; i++) {
let subset = [];
for (let j = 0; j < arr.length; j++) {
if (i & (1 << j)) {
subset.push(arr[j]);
}
}
subsets.push(subset);
}
return subsets;
};
// Variables.
let microservices = [];
let sets = [];
let intersections = [];
let microservicesPerDatabaseCategory = {};
let databaseCategoriesCombinations = [];
let databaseCategoriesCombinationsLabels = [];
let microservicesPerDatabaseTechnology = [];
Object.keys(DATABASES).forEach((database) => {
microservicesPerDatabaseTechnology.push({
technology: database,
category: DATABASES[database],
count: 0,
});
});
// Data.
const cursor = await this.mongoDb.getRepositories(
'repositories_microservices',
);
while (await cursor.hasNext()) {
let microservice = await cursor.next();
let { databases } = microservice;
microservices.push(microservice); // Microservice.
databases.forEach((database) => {
microservicesPerDatabaseTechnology.filter(
(db) => db.technology === database,
)[0].count += 1;
}); // Database technology.
}
// Sorting.
microservicesPerDatabaseTechnology =
microservicesPerDatabaseTechnology.filter((d) => d.count !== 0); // Empty database technology removing.
microservicesPerDatabaseCategory =
microservicesPerDatabaseTechnology.reduce((acc, database) => {
if (acc[database.category]) {
acc[database.category] += database.count;
} else {
acc[database.category] = database.count;
}
return acc;
}, {}); // Database categories sum by total count of microservice occurences.
microservicesPerDatabaseCategory = Object.fromEntries(
Object.entries(microservicesPerDatabaseCategory).sort(
(a, b) => b[1] - a[1],
),
); // Database categories sorting by microservice occurences in descending order.
microservicesPerDatabaseCategory = Object.fromEntries(
Object.entries(microservicesPerDatabaseCategory).slice(0, this.SLICE),
); // Top database categories slicing.
databaseCategoriesCombinations = getSubsets(
Object.keys(microservicesPerDatabaseCategory),
); // Categories combinations generation.
databaseCategoriesCombinations.shift(); // Empty removing.
databaseCategoriesCombinationsLabels = databaseCategoriesCombinations.map(
(s) => {
if (s.length === 1) {
// Single database
return (
s[0] +
' \\ { ' +
Object.keys(microservicesPerDatabaseCategory)
.reduce((acc, database) => {
if (database !== s[0]) {
acc.push(database);
}
return acc;
}, [])
.join(' ∪ ') +
' }'
);
} else {
// Multiple databases
return s.join(' ∩ ');
}
},
); // Labels generation.
// Values.
Object.keys(microservicesPerDatabaseCategory).forEach((category) => {
sets.push({
label: category,
size: microservices.reduce((acc, microservice) => {
if (
microservice.databases
.map((dbName) => DATABASES[dbName])
.includes(category)
) {
acc += 1;
}
return acc;
}, 0),
});
}); // Sets
for (let i = 0; i < databaseCategoriesCombinations.length; i++) {
intersections.push({
label: databaseCategoriesCombinationsLabels[i],
size: microservices.reduce((acc, microservice) => {
if (
[
...new Set(
microservice.databases
.map((dbName) => DATABASES[dbName])
.filter(
(dbName) => microservicesPerDatabaseCategory[dbName],
),
),
]
.sort()
.toString() ===
databaseCategoriesCombinations[i].sort().toString()
) {
acc += 1; // The database list is strictly equals to the combination tested.
}
return acc;
}, 0),
}); // Intersections
}
intersections = intersections.filter((i) => i.size !== 0); // Empty set removing.
let setsStankey = [];
sets.forEach((s) =>
setsStankey.push({
label: s.label,
}),
);
let intersectionsStankey = [];
intersections.forEach((i) => {
if (i.label.includes(' \\')) {
// No intersection set, e.g., R \ { K ∪ D ∪ S ∪ C }.
intersectionsStankey.push({
source: i.label.substring(0, i.label.indexOf(' \\')),
target: i.label,
value: i.size,
});
setsStankey.push({
label: i.label,
});
} else {
// Intersection set, e.g., R ∩ K.
let setsInvolved = i.label.split(' ∩ ');
setsInvolved.forEach((s) => {
intersectionsStankey.push({
source: s,
target: i.label,
value: i.size / setsInvolved.length, // As the set size is used for the box height, it is divided by the number of sets to distribute the box height according to the number of sets involved.
});
});
setsStankey.push({
label: i.label,
});
}
});
console.log(setsStankey);
console.log(intersectionsStankey);
// HTML file.
let html = path.join('results', 'analyze-26.html');
fs.writeFileSync(html, chart(setsStankey, intersectionsStankey), 'utf8');
logger.info(`[analyze] Chart: ${html}`);
this.mongoDb.disconnect();
};
this.mongoDb
.connect()
.then(() => {
analyze(); // Process entry point.
})
.catch((error) => {
logger.error(`[analyze] ${error.message}`);
});
}
}
let process26 = new ProcessAnalyze26();
process26.process();