-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (54 loc) · 2.27 KB
/
index.js
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
const fs = require('fs');
const path = require('path');
const graphs = {};
const fileNames = ['cpp', 'rust'];
const files_path = `./`;
const outputs_path = `./`;
function main() {
try {
for (let f = 0; f < fileNames.length; f++) {
const file_name = fileNames[f];
const file_path = path.join(files_path, `${file_name}.txt`);
const output_path = path.join(outputs_path, `${file_name}.json`);
const lines = fs.readFileSync(file_path, 'utf8').split('\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i].trim() == '') continue;
const line = lines[i].split(',');
const name = line[1];
const time = +line[0] || new Date(+line[0]);
const savingTime = line[2];
const graph = graphs[name] || { series: [], xaxis: { type: "datetime", categories: [] } };
const timings = line[3].split('|');
for (let t = 0; t < timings.length; t++) {
const [key, value] = timings[t].split(':');
const find = graph.series.find(r => r.name.toLowerCase() == key.toLowerCase());
if (find) {
find.data.push(value);
continue;
};
graph.series.push({
name: key,
data: [value]
});
};
graph.xaxis.categories.push(time);
graphs[name] = graph;
};
const array = Object.keys(graphs);
for (let i = 0; i < array.length; i++) {
const key = array[i];
const value = graphs[key];
for (let s = 0; s < value.series.length; s++) {
const series = value.series[s];
const sum = series.data.reduce((a, value) => parseFloat(a) + +value, 0);
if (!value.average) value.average = {};
value.average[series.name] = sum / series.data.length;
};
};
fs.writeFileSync(output_path, JSON.stringify(graphs), 'utf8');
};
} catch (error) {
console.log(error);
};
};
main();