This repository was archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_pulling_tools.js
More file actions
164 lines (144 loc) · 4.86 KB
/
data_pulling_tools.js
File metadata and controls
164 lines (144 loc) · 4.86 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
const fs = require('fs');
const moment = require('moment');
const folderPath = "../appsody_reports/"
module.exports = {
createLogFile: function(extension, array, cb) {
ensurePathExists(folderPath, 0744, function(err) {
if (err) {
cb(err)
}
});
d = new Date().toISOString().slice(0, 10);
var filePath = `${folderPath}${d}/`
ensurePathExists(filePath, 0744, function(err) {
if (err) {
cb(err)
} else {
var filename = `${filePath}${extension}`
writeFile(filename, array, function(err) {
if (err) {
cb(err)
}
});
}
})
},
addZeroPrefix: function(num) {
if (num.toString().length < 2) {
return `0${num}`;
} else {
return num;
}
},
createComparisonFile: function(filename, newRes, id, previous) {
oldResPath = `../appsody_reports/${previous}/${filename}.json`;
fs.access(oldResPath, fs.F_OK, (err) => {
if (err) {
console.error("No file found to make comparison.")
return
}
let rawdata = fs.readFileSync(oldResPath);
let oldRes = JSON.parse(rawdata);
var comparison = compareToLast(oldRes, newRes, id);
this.createLogFile(`${filename}_comparison.json`, comparison, function(err) {
console.log(err);
});
})
}
};
function ensurePathExists(path, mask, cb) {
if (typeof mask == 'function') {
cb = mask;
mask = 0777;
}
fs.mkdir(path, mask, function(err) {
if (err) {
if (err.code == 'EEXIST') {
cb(null);
} else {
cb(err);
}
} else {
cb(null);
}
});
}
function writeFile(filename, array, cb) {
fs.writeFile(filename, JSON.stringify(array, null, 4), 'utf8', (err) => {
if (err) {
console.log("An error occured while writing JSON Object to File.");
cb(err);
} else {
cb(null);
}
});
}
function compareToLast(oldRes, newRes, id) {
comparisons = [];
for (let i = 0; i < newRes.length; i++) {
let single = {};
switch (id) {
case "repo":
var noMatch = true;
for (let j = 0; j < oldRes.length; j++) {
if (newRes[i].repo === oldRes[j].repo) {
noMatch = false;
single = {
"repo": newRes[i].repo,
"stars": newRes[i].stars - oldRes[j].stars,
"watchers": newRes[i].watchers - oldRes[j].watchers,
"forks": newRes[i].forks - oldRes[j].forks
}
}
}
if (noMatch) {
single = newRes[i];
}
comparisons.push(single);
break;
case "name":
var noMatch = true;
for (let j = 0; j < oldRes.length; j++) {
if (newRes[i].name === oldRes[j].name) {
noMatch = false;
single = {
"name": newRes[i].name,
"pull_count": newRes[i].pull_count - oldRes[j].pull_count,
"last_updated": newRes[i].last_updated
}
}
}
if (noMatch) {
single = {
"name": newRes[i].name,
"pull_count": newRes[i].pull_count,
"last_updated": newRes[i].last_updated
}
}
comparisons.push(single);
break;
case "cli_binary":
var noMatch = true;
for (let j = 0; j < oldRes.length; j++) {
if (newRes[i].cli_binary === oldRes[j].cli_binary && newRes[i].release === oldRes[j].release) {
noMatch = false;
single = {
"release": newRes[i].release,
"cli_binary": newRes[i].cli_binary,
"download_count": newRes[i].download_count - oldRes[j].download_count
}
}
}
if (noMatch) {
single = {
"release": newRes[i].release,
"cli_binary": newRes[i].cli_binary,
"download_count": newRes[i].download_count
}
}
comparisons.push(single);
break;
}
}
return comparisons;
}