-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
203 lines (174 loc) · 5.69 KB
/
index.js
File metadata and controls
203 lines (174 loc) · 5.69 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
'use strict';
let cheerio = require('cheerio'),
moment = require('moment'),
request = require('request');
let GitActivityStats = function () {
this.giturl = 'https://github.com/users/';
this.GITHUB_MAGIC = 3.77972616981;
};
/**
* Callback for getContributions().
*
* @callback contributionsCallback
* @param {error} error - request error
* @param {Object} contrib
* @param {int[]} contrib.contributions - contributions array
* @param {string} contrib.startDate - first date of the contributions calendar
*/
/**
* Get the user contributions calendar
*
* @param {string} username Github username
* @param {contributionsCallback} callback see contributionsCallback
*/
GitActivityStats.prototype.getContributions = function (username, callback) {
let url = this.giturl + username + '/contributions';
request(url, function (error, response, body) {
let $ = cheerio.load(body);
let contributions = [];
let startDate = null;
$('rect').each(function (i) {
if (startDate === null)
startDate = $(this).data('date');
contributions[i] = $(this).data('count');
});
callback(error, {contributions, startDate});
});
};
/**
* Get the maximum number of contributions in a day
*
* @param {int[]} contributions - the contributions array
* @return {int} - maximum number of contributions in a day
*/
GitActivityStats.prototype.getMax = function (contributions) {
return Math.max.apply(null, contributions);
};
/**
* Get the mean of the contributions
*
* @param {int[]} contributions - the contributions array
* @return {int} - mean of the contributions
*/
GitActivityStats.prototype.getMean = function (contributions) {
let sum = contributions.reduce((pre, cur) => pre + cur);
return sum / contributions.length;
};
/**
* Get all streaks
*
* @param {int[]} contributions - the contributions array
* @param {string} startDate - first date of the contributions calendar
* @return {Object[]} - streaks
*/
GitActivityStats.prototype.getStreaks = function (contributions, startDate) {
let date = moment(startDate);
let streaks = [];
for (let i = 0; i < contributions.length; i++)
if (contributions[i] > 0) {
let streakStartIndex = i;
let streakStartDate = date.clone().add(i + 1, 'd').toDate();
while (contributions[i] > 0 && i < contributions.length)
i++;
let length = i - streakStartIndex;
if (i - streakStartIndex > 0) {
let endDate = date.clone().add(i, 'd').toDate();
streaks.push({startDate: streakStartDate, endDate, length});
}
}
return streaks;
};
/**
* Get the longest streak(s)
*
* @param {int[]} contributions - the contributions array
* @param {string} startDate - first date of the contributions calendar
* @return {Object[]} - longest streak(s)
*/
GitActivityStats.prototype.getMaxStreak = function (contributions, startDate) {
let streaks = this.getStreaks(contributions, startDate);
let maxLenght = Math.max.apply(Math, streaks.map(function (o) {
return o.length;
}));
let maxStreaks = [];
for (let i = 0; i < streaks.length; i++)
if (streaks[i].length === maxLenght)
maxStreaks.push(streaks[i]);
return maxStreaks;
};
/**
* Get the current streak
*
* @param {int[]} contributions - the contributions array
* @param {string} startDate - first date of the contributions calendar
* @return {Object[]} - current streak
*/
GitActivityStats.prototype.getCurrentStreak = function (contributions, startDate) {
let streaks = this.getStreaks(contributions, startDate);
let lastStreak = streaks[streaks.length - 1];
let lastStreakEnd = moment(lastStreak.endDate);
let yesteday = moment().subtract(1, 'd');
return lastStreakEnd.isSameOrAfter(yesteday) ? lastStreak : [];
};
/**
* get Outliers
*
* @param {int[]} contributions - the contributions array
* @return {int[]} - outliers
*/
GitActivityStats.prototype.getOutliers = function (contributions) {
let mean = this.getMean(contributions);
let firstPass = contributions.reduce(
(pre, cur) => Math.pow(cur - mean, 2) + pre
);
let stdvar = Math.sqrt(firstPass / (contributions.length - 1));
if (contributions.filter((v, i, a) => a.indexOf(v) === i).length < 5)
return [];
else {
let getOutliers = contributions.filter(
(v) => (Math.abs((mean - v) / stdvar) > this.GITHUB_MAGIC)
);
return getOutliers.filter((v, i, a) => a.indexOf(v) === i);
}
};
/**
* get Outliers - according to Github algo
*
* @param {int[]} contributions - the contributions array
* @return {int[]} - outliers
*/
GitActivityStats.prototype.getGithubOutliers = function (contributions) {
let getOutliers = this.getOutliers(contributions);
let max = this.getMax(contributions);
let mean = this.getMean(contributions);
let o = max - mean < 6 || max < 15 ? 1 : 3;
return getOutliers.slice(0, o);
};
/**
* Get quartile boundaries, with the index as the quartile number
* and the value as the upper bound of the quartile (inclusive)
*
* @param {int[]} contributions - the contributions array
* @return {int[]} - quartile boundaries
*/
GitActivityStats.prototype.getQuartileBoundaries = function (contributions) {
let getGithubOutliers = this.getGithubOutliers(contributions);
let top = this.getMax(contributions.filter((v) => (!this.contains(getGithubOutliers, v))));
let bounds = [];
for (let i = 1; i < 4; i++)
bounds.push(Math.floor(i * top / 4));
bounds.push(top);
return bounds;
};
/**
* helper function - check if an array contains an object
*/
GitActivityStats.prototype.contains = function (a, obj) {
let i = a.length;
while (i--)
if (a[i] === obj)
return true;
return false;
};
GitActivityStats = new GitActivityStats();
module.exports = GitActivityStats;