-
Notifications
You must be signed in to change notification settings - Fork 43
/
generate-stats.js
executable file
·160 lines (125 loc) · 3.6 KB
/
generate-stats.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
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
const https = require('https');
const fs = require('fs');
const maxRetry = 3;
let retries = 0;
var SI_SYMBOL = ["", "k", "M", "G", "T", "P", "E"];
function abbreviateNumber(number){
const tier = Math.log10(number) / 3 | 0;
if (tier == 0) {
return number;
}
const suffix = SI_SYMBOL[tier];
const scale = Math.pow(10, tier * 3);
const scaled = number / scale;
return scaled.toFixed(1) + suffix;
}
function httpsPost({body, ...options}) {
return new Promise((resolve,reject) => {
const req = https.request({
method: 'POST',
...options,
}, res => {
const chunks = [];
res.on('data', data => chunks.push(data))
res.on('end', () => {
let body = Buffer.concat(chunks);
switch(res.headers['content-type']) {
case 'application/json':
try {
body = JSON.parse(body);
} catch (e) {
console.log('Failed to parse response, retrying...');
if (retries < maxRetry) {
retries += 1;
console.log('Attempt ' + retries);
postRequest()
}
}
break;
}
resolve(body)
})
})
req.on('error', reject);
if (body) {
req.write(body);
}
req.end();
})
}
function generateTemplate(data) {
const templateString = `/* ------------------------
THIS FILE IS AUTOMATICALLY GENERATED
WITH THE node generate-stats COMMAND
!!!DO NOT EDIT IT HERE!!!
USE generate-stats.js for adjustments.
------------------------*/
var contributors = ${JSON.stringify(Object.entries(data))};
// Certain users may have inappropriate or offense imagery for their profile picture
var exclude = ["mjallday"]
var userRows;
var rows = [14, 14, 14, 14]; // users/row;
var maxRowSize = rows.slice().sort().reverse()[0];
var rowsString = '';
function filterContributors(arr) {
return arr.filter(function(value, index, arr) {
var userid = value[0]
return exclude.includes(userid) == false
});
}
function shuffle(arr) {
for (let i = arr.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
function userTemplate(userData) {
var link = 'https://github.com/' + userData[0];
var img = '<img alt="'+userData[0]+'" src="' + link + '.png">';
var figure = '<a id="'+userData[0]+'"><figure class="image">' + img + '</figure></a>';
return figure;
}
var filtered = filterContributors(contributors);
shuffle(filtered);
function buildRows(rowSet) {
rows.forEach(r => {
rowsString += '<div class="user-row">';
for (var i = 0; i < r; i++) {
rowsString += userTemplate(filtered.shift());
}
rowsString += '</div>';
});
return rowsString
}
document.addEventListener('DOMContentLoaded', function() {
userRows = document.getElementById('github-users');
userRows.innerHTML = buildRows(rows);
});`;
return templateString.replace(/^(\t\t)/gm, '');
}
function postRequest() {
console.log('Fetching data from kenfdev.o6s.io/github-stats via POST.');
httpsPost({
hostname: 'kenfdev.o6s.io',
port: 443,
path: '/github-stats',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
org: 'openfaas'
})
}).then(resp => {
resp.total.stars = abbreviateNumber(resp.total.stars);
fs.writeFile('_data/github_stats.json', JSON.stringify(resp), 'utf8', () => {
console.log('Github stats file generated');
});
fs.writeFile('js/contributors.js', generateTemplate(resp.byLogin), 'utf8', () => {
console.log('js/contributors.js file generated');
});
}).catch(err => {
console.log(err);
})
}
postRequest()