-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-analyze-3.3.process.js
More file actions
264 lines (219 loc) · 7.77 KB
/
Copy path6-analyze-3.3.process.js
File metadata and controls
264 lines (219 loc) · 7.77 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
// 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';
/**
* Represents a process to analyze the repositories.
*/
class ProcessAnalyze33 extends Process {
constructor() {
super();
// Dependencies.
this.env = new Env();
// Environment variables.
this.mongoDb = new MongoDB(
this.env.getMongoDbUrl(),
this.env.getMongoDbName(),
); // MongoDB connection.
}
/**
* Executes the process to analyze repositories.
*/
process() {
const chart = (data) => `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Databases per Microservice</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstat/1.9.4/jstat.min.js"></script>
<style>
body { font-family: Arial, sans-serif; text-align: left; }
</style>
</head>
<body>
<h1>Microservices Size in Comparison with Database Technologies</h1>
<div id="histogram"></div>
<hr />
<div id="scatterPlot"></div>
<hr />
<div id="pValue"></div>
<hr />
<div id="details"></div>
<script>
// Data.
const xData = ${JSON.stringify(data.map((x) => x.size))};
const yData = ${JSON.stringify(data.map((x) => x.nbDatabases))};
const ids = ${JSON.stringify(data.map((x) => x.id))};
const databases = ${JSON.stringify(data.map((x) => x.databases))};
// Linear regression computing.
function linearRegressionWithStats(x, y) {
const n = x.length;
const sumX = x.reduce((a, b) => a + b, 0);
const sumY = y.reduce((a, b) => a + b, 0);
const meanX = sumX / n;
const meanY = sumY / n;
let Sxx = 0, Sxy = 0;
for (let i = 0; i < n; i++) {
Sxx += (x[i] - meanX) ** 2;
Sxy += (x[i] - meanX) * (y[i] - meanY);
}
const slope = Sxy / Sxx;
const intercept = meanY - slope * meanX;
let SSE = 0;
for (let i = 0; i < n; i++) {
const predicted = slope * x[i] + intercept;
SSE += (y[i] - predicted) ** 2;
}
const SE = Math.sqrt(SSE / (n - 2));
const SE_slope = SE / Math.sqrt(Sxx);
const t_stat = slope / SE_slope;
const p_value = 2 * (1 - jStat.studentt.cdf(Math.abs(t_stat), n - 2));
const residuals = yData.map((y, i) => y - (slope * xData[i] + intercept));
return { slope, intercept, residuals, p_value };
}
const { slope, intercept, residuals, p_value } = linearRegressionWithStats(xData, yData);
// Linear regression line.
const xMin = Math.min(...xData);
const xMax = Math.max(...xData);
const regressionLine = {
x: [xMin, xMax],
y: [slope * xMin + intercept, slope * xMax + intercept],
mode: 'lines',
type: 'scatter',
name: 'Regression Line',
line: { color: 'red' }
};
// Plots.
const scatterPlot = {
x: xData,
y: yData,
mode: 'markers',
type: 'scatter',
name: 'Repositories',
marker: {
opacity: 0.1,
color: 'black',
size: 10
},
customdata: [ids, databases],
hovertemplate: '# services: %{x}, # databases technologies: %{y}',
};
const histogram = {
x: residuals,
type: 'histogram',
name: 'Residuals Histogram',
opacity: 0.6,
marker: {color: 'blue'}
};
// Layouts.
const layoutScatterPlot = {
xaxis: {
title: { text: 'size (in KB)' },
tickformat: ',d'
},
yaxis: {
title: { text: '# databases technologies' }
},
hovermode: 'closest',
height: 500,
width: 500,
legend: {
x: 1,
xanchor: 'right',
y: 1
}
};
const layoutHistogram = {
title: 'Histogram of Residuals',
xaxis: {title: 'Residual'},
yaxis: {title: 'Count'},
height: 400,
width: 500
};
// Rendering.
Plotly.newPlot('scatterPlot', [scatterPlot, regressionLine], layoutScatterPlot).then(() => {
const scatterPlot = document.getElementById('scatterPlot');
const pValue = document.getElementById('pValue');
const details = document.getElementById('details');
pValue.innerHTML += '<p style="color: blue;"> p_value = ' + p_value + ' ; ' + (p_value < 0.05 ? 'p_value < 0.05, statistically significant' : 'p_value ≥ 0.05, no proof of statistical significance') + '</p>';
scatterPlot.on('plotly_click', function(data) {
const x = data.points[0].x;
const y = data.points[0].y;
while (details.firstChild) {
details.removeChild(details.firstChild);
}
let counter = 0;
${JSON.stringify(data)}
.filter(m => m.size === x && m.nbDatabases === y)
.forEach(m => {
counter++;
details.innerHTML += \`
<p>
<a href="https://www.github.com/\${m.id}" target="_blank">\${m.id}</a>
<ul>
<li>Size: \${m.size} KB</li>
<li># databases categories: \${m.nbDatabaseCategories}</li>
<li>Databases: \${m.databases.join(', ')}</li>
</ul>
</p><hr/>\`;
});
details.innerHTML += '<p>' + counter + ' matching repository(ies)</p>';
});
});
Plotly.newPlot('histogram', [histogram], layoutHistogram);
</script>
</body>
</html>
`;
const analyze = async () => {
// ---
// ANALYZE: microservice complexity in comparison with databases technologies / X = microservice disk size ; Y = database technologies (i.e., number of services in Docker Compose file excluding non-database services).
// ---
// Variables.
let data = [];
// Data.
const cursor = await this.mongoDb.getRepositories(
'repositories_microservices',
);
while (await cursor.hasNext()) {
let microservice = await cursor.next();
let { _id, databases, size } = microservice;
let nbDatabases = databases.length;
data.push({
id: _id,
size: size,
nbDatabases: nbDatabases,
databases: databases,
});
}
// Cleaning.
const values = data.map((d) => d['size']).sort((a, b) => a - b);
const q1 = values[Math.floor(values.length * 0.25)];
const q3 = values[Math.floor(values.length * 0.75)];
const iqr = q3 - q1;
const upper = q3 + 1.5 * iqr;
data = data.filter((d) => d['size'] <= upper); // Removing outliers via interquartile range.
// HTML file.
let html = path.join('results', 'analyze-33.html');
fs.writeFileSync(html, chart(data), '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 process33 = new ProcessAnalyze33();
process33.process();