-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContributions.js
576 lines (531 loc) · 16.6 KB
/
Contributions.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import Mongoose from "mongoose";
import express from "express";
import User from "../models/user.js";
import Organization from "../models/organization.js";
import Stat from "../models/stat.js";
const router = express.Router();
const getDaysInAMonth = (month, year) => {
return new Date(year, month + 1, 0).getDate();
};
const getLast30DaysContributions = _contributionsList => {
const currentDate = new Date();
const currentDateString = currentDate.toISOString();
const last30Days = currentDate.setDate(currentDate.getDate() - 30);
const last30DaysString = new Date(last30Days).toISOString();
const last30DaysContributions = _contributionsList.filter(contribution => {
if (
new Date(contribution.occurredAt) >= new Date(last30DaysString) &&
new Date(contribution.occurredAt) <= new Date(currentDateString)
) {
return true;
}
return false;
});
return last30DaysContributions;
};
const countLast30DaysContributions = async () => {
let users = await User.find(
{},
"commit_contributions issue_contributions pr_contributions code_review_contributions"
);
let contributionsList = [];
for (const user of users) {
for (const repo of user.commit_contributions) {
for (const commit of repo.commits) {
contributionsList.push(commit);
}
}
for (const repo of user.issue_contributions) {
for (const issue of repo.issues) {
contributionsList.push(issue);
}
}
for (const repo of user.pr_contributions) {
for (const pullRequest of repo.pullRequests) {
contributionsList.push(pullRequest);
}
}
for (const repo of user.code_review_contributions) {
for (const codeReview of repo.codeReviews) {
contributionsList.push(codeReview);
}
}
}
const last30DaysContributions = getLast30DaysContributions(contributionsList);
let count = 0;
for (const contribution of last30DaysContributions) {
if (contribution?.commitCount) {
count += contribution.commitCount;
} else {
count += 1;
}
}
return count;
};
const countUsersCreatedBeforeDate = async date => {
return await User.find({
user_createdAt: {
$lt: date,
},
}).countDocuments();
};
const getUsersCreatedBetweenTwoDates = async (startDate, endDate) => {
return await User.find(
{
user_createdAt: {
$gte: startDate + "T00:00:00.000Z",
$lte: endDate + "T23:59:59.999Z",
},
},
"user_createdAt"
);
};
const getOrganizationsCreatedBetweenTwoDates = async (startDate, endDate) => {
return await Organization.find(
{
organization_createdAt: {
$gte: startDate + "T00:00:00.000Z",
$lte: endDate + "T23:59:59.999Z",
},
},
"organization_createdAt"
);
};
const countUsersCreatedBetweenTwoDates = async (startDate, endDate) => {
return await User.find({
user_createdAt: {
$gte: startDate + "T00:00:00.000Z",
$lte: endDate + "T23:59:59.999Z",
},
}).countDocuments();
};
const countOrganizationsCreatedBeforeDate = async date => {
return await Organization.find({
organization_createdAt: {
$lt: date,
},
}).countDocuments();
};
const accumulatedTotalOrganizationsByMonth = async periodArray => {
let organizations = await getOrganizationsCreatedBetweenTwoDates(
periodArray[0],
periodArray[1]
);
let organizationsCreatedByMonth = {};
for (const organization of organizations) {
let date = new Date(organization.organization_createdAt);
let month = date.getMonth();
let year = date.getFullYear();
if (organizationsCreatedByMonth[year] === undefined) {
organizationsCreatedByMonth[year] = {};
}
if (organizationsCreatedByMonth[year][month] === undefined) {
organizationsCreatedByMonth[year][month] = 0;
}
organizationsCreatedByMonth[year][month]++;
}
let organizationsCreatedAccumulationByMonth = {};
for (const year in organizationsCreatedByMonth) {
organizationsCreatedAccumulationByMonth[year] = [];
let accumulation = await countOrganizationsCreatedBeforeDate(year);
for (const month in organizationsCreatedByMonth[year]) {
accumulation += organizationsCreatedByMonth[year][month];
organizationsCreatedAccumulationByMonth[year][month] = accumulation;
}
}
return organizationsCreatedAccumulationByMonth;
};
const accumulatedTotalOrganizationsByDay = async periodArray => {
let organizations = await getOrganizationsCreatedBetweenTwoDates(
periodArray[0],
periodArray[1]
);
let organizationsCreatedByDay = {};
for (const organization of organizations) {
let date = new Date(organization.organization_createdAt);
let day = date.getDate();
let month = date.getMonth();
let year = date.getFullYear();
if (organizationsCreatedByDay[year] === undefined) {
organizationsCreatedByDay[year] = {};
}
if (organizationsCreatedByDay[year][month] === undefined) {
organizationsCreatedByDay[year][month] = new Array(
Number(getDaysInAMonth(month, year))
).fill(0);
}
if (organizationsCreatedByDay[year][month][day] === undefined) {
organizationsCreatedByDay[year][month][day] = 0;
}
organizationsCreatedByDay[year][month][day]++;
}
let organizationsCreatedAccumulationByDay = {};
for (const year in organizationsCreatedByDay) {
organizationsCreatedAccumulationByDay[year] = {};
for (const month in organizationsCreatedByDay[year]) {
organizationsCreatedAccumulationByDay[year][month] = [];
let accumulation = await countOrganizationsCreatedBeforeDate(
new Date(year, month, 1).toISOString()
);
for (const day in organizationsCreatedByDay[year][month]) {
accumulation += organizationsCreatedByDay[year][month][day];
organizationsCreatedAccumulationByDay[year][month][day] = accumulation;
}
}
}
return organizationsCreatedAccumulationByDay;
};
const accumulatedTotalUsersByMonth = async periodArray => {
let users = await getUsersCreatedBetweenTwoDates(
periodArray[0],
periodArray[1]
);
let usersCreatedByMonth = {};
for (const user of users) {
let date = new Date(user.user_createdAt);
let month = date.getMonth();
let year = date.getFullYear();
if (usersCreatedByMonth[year] === undefined) {
usersCreatedByMonth[year] = {};
}
if (usersCreatedByMonth[year][month] === undefined) {
usersCreatedByMonth[year][month] = 0;
}
usersCreatedByMonth[year][month]++;
}
let usersCreatedAccumulationByMonth = {};
for (const year in usersCreatedByMonth) {
usersCreatedAccumulationByMonth[year] = [];
let accumulation = await countUsersCreatedBeforeDate(year);
for (const month in usersCreatedByMonth[year]) {
accumulation += usersCreatedByMonth[year][month];
usersCreatedAccumulationByMonth[year][month] = accumulation;
}
}
return usersCreatedAccumulationByMonth;
};
const accumulatedTotalUsersByDay = async periodArray => {
let users = await getUsersCreatedBetweenTwoDates(
periodArray[0],
periodArray[1]
);
let usersCreatedByDay = {};
for (const user of users) {
let date = new Date(user.user_createdAt);
let day = date.getDate();
let month = date.getMonth();
let year = date.getFullYear();
if (usersCreatedByDay[year] === undefined) {
usersCreatedByDay[year] = {};
}
if (usersCreatedByDay[year][month] === undefined) {
usersCreatedByDay[year][month] = new Array(
Number(getDaysInAMonth(month, year))
).fill(0);
}
if (usersCreatedByDay[year][month][day] === undefined) {
usersCreatedByDay[year][month][day] = 0;
}
usersCreatedByDay[year][month][day]++;
}
let usersCreatedAccumulationByDay = {};
for (const year in usersCreatedByDay) {
usersCreatedAccumulationByDay[year] = {};
for (const month in usersCreatedByDay[year]) {
usersCreatedAccumulationByDay[year][month] = [];
let accumulation = await countUsersCreatedBeforeDate(
new Date(year, month, 1).toISOString()
);
for (const day in usersCreatedByDay[year][month]) {
accumulation += usersCreatedByDay[year][month][day];
usersCreatedAccumulationByDay[year][month][day] = accumulation;
}
}
}
return usersCreatedAccumulationByDay;
};
const accumulatedTotalContributionsByMonth = async (from, to) => {
let users = await User.find(
{},
"commit_contributions issue_contributions pr_contributions code_review_contributions"
);
let contributionsList = [];
for (const user of users) {
for (const repo of user.commit_contributions) {
for (const commit of repo.commits) {
if (commit.occurredAt >= from && commit.occurredAt <= to) {
contributionsList.push(commit);
}
}
}
for (const repo of user.issue_contributions) {
for (const issue of repo.issues) {
if (issue.occurredAt >= from && issue.occurredAt <= to) {
contributionsList.push(issue);
}
}
}
for (const repo of user.pr_contributions) {
for (const pullRequest of repo.pullRequests) {
if (pullRequest.occurredAt >= from && pullRequest.occurredAt <= to) {
contributionsList.push(pullRequest);
}
}
}
for (const repo of user.code_review_contributions) {
for (const codeReview of repo.codeReviews) {
if (codeReview.occurredAt >= from && codeReview.occurredAt <= to) {
contributionsList.push(codeReview);
}
}
}
}
let contributionsByMonth = {};
for (const contribution of contributionsList) {
let date = new Date(contribution.occurredAt);
let month = date.getMonth();
let year = date.getFullYear();
if (contributionsByMonth[year] === undefined) {
contributionsByMonth[year] = [];
}
if (contributionsByMonth[year][month] === undefined) {
contributionsByMonth[year][month] = 0;
}
contributionsByMonth[year][month]++;
}
return contributionsByMonth;
};
const accumulatedTotalContributionsByDay = async (from, to) => {
let users = await User.find(
{},
"commit_contributions issue_contributions pr_contributions code_review_contributions"
);
let contributionsList = [];
for (const user of users) {
for (const repo of user.commit_contributions) {
for (const commit of repo.commits) {
if (commit.occurredAt >= from && commit.occurredAt <= to) {
contributionsList.push(commit);
}
}
}
for (const repo of user.issue_contributions) {
for (const issue of repo.issues) {
if (issue.occurredAt >= from && issue.occurredAt <= to) {
contributionsList.push(issue);
}
}
}
for (const repo of user.pr_contributions) {
for (const pullRequest of repo.pullRequests) {
if (pullRequest.occurredAt >= from && pullRequest.occurredAt <= to) {
contributionsList.push(pullRequest);
}
}
}
for (const repo of user.code_review_contributions) {
for (const codeReview of repo.codeReviews) {
if (codeReview.occurredAt >= from && codeReview.occurredAt <= to) {
contributionsList.push(codeReview);
}
}
}
}
let contributionsByDay = {};
for (const contribution of contributionsList) {
let date = new Date(contribution.occurredAt);
let day = date.getDate();
let month = date.getMonth();
let year = date.getFullYear();
if (contributionsByDay[year] === undefined) {
contributionsByDay[year] = {};
}
if (contributionsByDay[year][month] === undefined) {
contributionsByDay[year][month] = new Array(
Number(getDaysInAMonth(month, year))
).fill(0);
}
if (contributionsByDay[year][month][day] === undefined) {
contributionsByDay[year][month][day] = 0;
}
contributionsByDay[year][month][day]++;
}
return contributionsByDay;
};
/**
* @swagger
* tags:
* name: Contributions
* description: The Contributions Routes
*/
/**
* @swagger
* /v1/contributions:
* get:
* summary: Returns information about the contributions
* tags: [Contributions]
* responses:
* 200:
* description: The request was successful
* 404:
* description: Check your internet connection and try again
*/
router.get("/contributions", async (req, res) => {
let contributions_last_30_days = await countLast30DaysContributions();
res.status(200).json({
success: true,
contributions_last_30_days,
});
});
/**
* @swagger
* tags:
* name: Stats
* description: The stats routes
*/
/**
* @swagger
* /v1/stats/contributors:
* get:
* summary: Returns the stats for the contributors
* tags: [Stats]
* parameters:
* - in: query
* name: type
* schema:
* type: string
* description: The type of the data to return users or commits, default is users
* - in: query
* name: period
* schema:
* type: string
* required: true
* description: The two dates to return the data between eg.. 2019-01-01_2019-01-31 (the two dates separated by underscore)
* - in: query
* name: aggregation
* schema:
* type: string
* description: Specifies to group and returns the data by month or day, default is month
* responses:
* 200:
* description: The request was successful
* 404:
* description: Check your internet connection and try again
*/
router.get("/contributors/stats", async (req, res) => {
let { type, period, aggregation } = req.query;
type = !type ? "users" : type;
aggregation = !aggregation ? "month" : aggregation;
if (period) {
const periodArray = period.split("_");
switch (type) {
case "users":
switch (aggregation) {
case "month":
const usersByMonth = await accumulatedTotalUsersByMonth(
periodArray
);
res.status(200).json({
success: true,
usersStats: usersByMonth,
});
break;
case "day":
const usersByDay = await accumulatedTotalUsersByDay(periodArray);
res.status(200).json({
success: true,
usersStats: usersByDay,
});
break;
default:
res.status(400).json({
success: false,
message: "Invalid aggregation type",
});
break;
}
break;
case "contributions":
switch (aggregation) {
case "month":
const commitsByMonth = await accumulatedTotalContributionsByMonth(
periodArray[0],
periodArray[1]
);
res.status(200).json({
success: true,
commitsStats: commitsByMonth,
});
break;
case "day":
const commitsByDay = await accumulatedTotalContributionsByDay(
periodArray[0],
periodArray[1]
);
res.status(200).json({
success: true,
commitsStats: commitsByDay,
});
break;
default:
res.status(400).json({
success: false,
message: "Invalid aggregation type",
});
break;
}
break;
default:
res.status(400).json({
success: false,
message: "Invalid type",
});
break;
}
} else {
res.status(400).json({
success: false,
message: "Please specifiy a period of time",
});
}
});
router.get("/organizations/stats", async (req, res) => {
let { period, aggregation } = req.query;
aggregation = !aggregation ? "month" : aggregation;
if (period) {
const periodArray = period.split("_");
switch (aggregation) {
case "day":
const organizationsByDay = await accumulatedTotalOrganizationsByDay(
periodArray
);
res.status(200).json({
success: true,
organizationsStats: organizationsByDay,
});
break;
default:
const organizationsByMonth = await accumulatedTotalOrganizationsByMonth(
periodArray
);
res.status(200).json({
success: true,
organizationsStats: organizationsByMonth,
});
break;
}
} else {
res.status(400).json({
success: false,
message: "Please specifiy a period of time",
});
}
});
router.get("/stats/updated", async (req, res) => {
const stat = await Stat.findOne({}, {}, { sort: { "createdAt": -1 } });
res.status(200).json({
success: true,
lastUpdated: stat.createdAt,
});
});
export default router;