forked from akvelon/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatherMetrics.ts
528 lines (488 loc) · 16.5 KB
/
gatherMetrics.ts
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require("fs");
const github = require("./shared/githubUtils");
const {
REPO_OWNER,
REPO,
PATH_TO_METRICS_CSV,
BOT_NAME,
} = require("./shared/constants");
interface PrStats {
firstTimeContribution: boolean;
timeToFirstReview: number;
timeFromCreationToCompletion: number;
timeFromReviewersMentionedToCompletion: { [key: string]: number };
mergedDate: Date;
}
interface AggregatedMetrics {
prsCompleted: number;
prsCompletedByNewContributors: number;
averageTimeToFirstReview: number;
averageTimeToNewContributorFirstReview: number;
averageTimeCreationToCompletion: number;
numUsersPerformingReviews: number;
numCommittersPerformingReviews: number;
numNonCommittersPerformingReviews: number;
giniIndexCommittersPerformingReviews: number;
averageTimeFromCommitterAssignmentToPrMerge: number;
}
async function getCompletedPullsFromLastYear(): Promise<any[]> {
const cutoffDate = new Date();
cutoffDate.setFullYear(new Date().getFullYear() - 1);
console.log(`Getting PRs newer than ${cutoffDate}`);
const githubClient = github.getGitHubClient();
let result = await githubClient.rest.pulls.list({
owner: REPO_OWNER,
repo: REPO,
state: "closed",
per_page: 100, // max allowed
});
let page = 2;
let retries = 0;
let pulls = result.data;
while (
result.data.length > 0 &&
new Date(result.data[result.data.length - 1].created_at) > cutoffDate
) {
if (retries === 0) {
console.log(`Getting PRs, page: ${page}`);
console.log(
`Current oldest PR = ${new Date(
result.data[result.data.length - 1].created_at
)}`
);
}
try {
result = await githubClient.rest.pulls.list({
owner: REPO_OWNER,
repo: REPO,
state: "closed",
per_page: 100, // max allowed
page: page,
});
pulls = pulls.concat(result.data);
page++;
retries = 0;
} catch (err) {
if (retries >= 3) {
throw err;
}
retries++;
}
}
console.log("Got all PRs, moving to the processing stage");
return pulls;
}
// Rather than check the whole repo history (expensive),
// we'll just look at the last year's worth of contributions to check if they're a first time contributor.
// If they contributed > 1 year ago, their experience is probably similar to a first time contributor anyways.
function checkIfFirstTimeContributor(
pull: any,
pullsFromLastYear: any[]
): boolean {
return !pullsFromLastYear.some(
(pullFromLastYear) =>
pullFromLastYear.created_at < pull.created_at &&
pullFromLastYear.user.login === pull.user.login
);
}
// Get time between pr creation and the first comment, approval, or merge that isn't done by:
// (a) the author
// (b) automated tooling
function getTimeToFirstReview(
pull: any,
comments: any[],
reviews: any[],
creationDate: Date,
mergedDate: Date
): number {
let timeToFirstReview = mergedDate.getTime() - creationDate.getTime();
const firstReviewed = reviews.find(
(review) => review.user.login != pull.user.login
);
if (firstReviewed) {
const firstReviewDate = new Date(firstReviewed.submitted_at);
timeToFirstReview = Math.min(
timeToFirstReview,
firstReviewDate.getTime() - creationDate.getTime()
);
}
for (const comment of comments) {
if (
comment.user.login != pull.user.login &&
comment.user.login != BOT_NAME
) {
let commentTime = new Date(comment.created_at);
timeToFirstReview = Math.min(
timeToFirstReview,
commentTime.getTime() - creationDate.getTime()
);
}
}
return timeToFirstReview;
}
// Takes a R: @reviewer comment and extracts all reviewers tagged
// Returns an empty list if no reviewer can be extracted
function extractReviewersTaggedFromCommentBody(body: string): string[] {
if (!body) {
return [];
}
body = body.toLowerCase();
if (body.indexOf("r: @") < 0) {
return [];
}
let usernames: string[] = [];
const reviewerStrings = body.split(" @");
// Start at index 1 since we don't care about anything before the first @
for (let i = 1; i < reviewerStrings.length; i++) {
const curBlock = reviewerStrings[i];
let usernameIndex = 0;
let curUsername = "";
while (
usernameIndex < curBlock.length &&
curBlock[usernameIndex].match(/^[0-9a-z]+$/)
) {
curUsername += curBlock[usernameIndex];
usernameIndex += 1;
}
// Filter out username from PR template
if (curUsername && curUsername != "username") {
usernames.push(curUsername);
}
}
return usernames;
}
// Returns a dictionary mapping reviewers to the amount of time from their first comment to pr completion.
function getTimeFromReviewerMentionedToCompletion(
pull: any,
comments: any[],
reviewComments: any[],
mergedDate: Date
): { [key: string]: number } {
comments = comments.concat(reviewComments);
comments.push(pull);
let timeToCompletionPerReviewer = {};
for (const comment of comments) {
const reviewersTagged = extractReviewersTaggedFromCommentBody(comment.body);
const commentCreationDate = new Date(comment.created_at);
const timeToCompletion =
mergedDate.getTime() - commentCreationDate.getTime();
for (const reviewer of reviewersTagged) {
if (reviewer in timeToCompletionPerReviewer) {
timeToCompletionPerReviewer[reviewer] = Math.max(
timeToCompletion,
timeToCompletionPerReviewer[reviewer]
);
} else {
timeToCompletionPerReviewer[reviewer] = timeToCompletion;
}
}
}
return timeToCompletionPerReviewer;
}
async function extractPrStats(
pull: any,
pullsFromLastYear: any[]
): Promise<PrStats> {
const githubClient = github.getGitHubClient();
const creationDate = new Date(pull.created_at);
const mergedDate = new Date(pull.merged_at);
const reviews = (
await githubClient.rest.pulls.listReviews({
owner: REPO_OWNER,
repo: REPO,
pull_number: pull.number,
})
).data;
// GitHub has a concept of review comments (must be part of a review) and issue comments on a repo, so we need to look at both
const comments = (
await githubClient.rest.issues.listComments({
owner: REPO_OWNER,
repo: REPO,
issue_number: pull.number,
})
).data;
const reviewComments = (
await githubClient.rest.pulls.listReviewComments({
owner: REPO_OWNER,
repo: REPO,
pull_number: pull.number,
})
).data;
const prStats: PrStats = {
firstTimeContribution: checkIfFirstTimeContributor(pull, pullsFromLastYear),
timeToFirstReview: getTimeToFirstReview(
pull,
comments,
reviews,
creationDate,
mergedDate
),
timeFromCreationToCompletion: mergedDate.getTime() - creationDate.getTime(),
timeFromReviewersMentionedToCompletion:
getTimeFromReviewerMentionedToCompletion(
pull,
comments,
reviewComments,
mergedDate
),
mergedDate: mergedDate,
};
return prStats;
}
function getMetricBucketStartDate(pullStat: PrStats, bucketEnd: Date): number {
const bucketStart = bucketEnd;
while (bucketStart.getTime() > pullStat.mergedDate.getTime()) {
bucketStart.setDate(bucketStart.getDate() - 7);
}
return bucketStart.getTime();
}
function distinctReviewers(pullStats: PrStats[]): string[] {
let users: Set<string> = new Set();
for (const pullStat of pullStats) {
for (const user of Object.keys(
pullStat.timeFromReviewersMentionedToCompletion
)) {
users.add(user);
}
}
return Array.from(users);
}
async function committersFromReviewers(users: string[]): Promise<string[]> {
let committers: string[] = [];
for (const user of users) {
if (await github.checkIfCommitter(user)) {
committers.push(user);
}
}
return committers;
}
function averageTimeFromCommitterAssignmentToPrMerge(
pullStats: PrStats[],
committers: string[]
): number {
if (committers.length === 0) {
return 0;
}
let numCommitterReviews = 0;
let totalTimeFromAssignToMerge = 0;
for (const pullStat of pullStats) {
for (const reviewer of Object.keys(
pullStat.timeFromReviewersMentionedToCompletion
)) {
if (committers.indexOf(reviewer) > -1) {
numCommitterReviews++;
totalTimeFromAssignToMerge +=
pullStat.timeFromReviewersMentionedToCompletion[reviewer];
}
}
}
if (numCommitterReviews === 0) {
return 0;
}
return totalTimeFromAssignToMerge / numCommitterReviews;
}
// Calculates a gini index of inequality for reviews.
// 0 is perfectly equally distributed, 1 is inequally distributed (with 1 person having all reviews)
function getGiniIndexForCommitterReviews(
pullStats: PrStats[],
committers: string[]
) {
let reviewsPerCommitter: { [key: string]: number } = {};
for (const pullStat of pullStats) {
for (const reviewer of Object.keys(
pullStat.timeFromReviewersMentionedToCompletion
)) {
if (committers.indexOf(reviewer) > -1) {
if (reviewer in reviewsPerCommitter) {
reviewsPerCommitter[reviewer]++;
} else {
reviewsPerCommitter[reviewer] = 1;
}
}
}
}
let reviewCounts = Object.values(reviewsPerCommitter);
reviewCounts.sort();
let giniNumerator = 0;
let giniDenominator = 0;
const n = reviewCounts.length;
for (let i = 1; i <= reviewCounts.length; i++) {
let yi = reviewCounts[i - 1];
giniNumerator += (n + 1 - i) * yi;
giniDenominator += yi;
}
return (1 / n) * (n + 1 - (2 * giniNumerator) / giniDenominator);
}
async function aggregateStatsForBucket(
pullStats: PrStats[]
): Promise<AggregatedMetrics> {
const reviewers = distinctReviewers(pullStats);
const committers = await committersFromReviewers(reviewers);
const firstTimePrs = pullStats.filter(
(pullStat) => pullStat.firstTimeContribution
);
let averageTimeToNewContributorFirstReview = 0;
if (firstTimePrs.length > 0) {
averageTimeToNewContributorFirstReview =
firstTimePrs.reduce(
(sumTime, prStat) => sumTime + prStat.timeToFirstReview,
0
) / firstTimePrs.length;
}
return {
prsCompleted: pullStats.length,
prsCompletedByNewContributors: firstTimePrs.length,
averageTimeToFirstReview:
pullStats.reduce(
(sumTime, prStat) => sumTime + prStat.timeToFirstReview,
0
) / pullStats.length,
averageTimeToNewContributorFirstReview:
averageTimeToNewContributorFirstReview,
averageTimeCreationToCompletion:
pullStats.reduce(
(sumTime, prStat) => sumTime + prStat.timeFromCreationToCompletion,
0
) / pullStats.length,
numUsersPerformingReviews: reviewers.length,
numCommittersPerformingReviews: committers.length,
numNonCommittersPerformingReviews: reviewers.length - committers.length,
giniIndexCommittersPerformingReviews: getGiniIndexForCommitterReviews(
pullStats,
committers
),
averageTimeFromCommitterAssignmentToPrMerge:
averageTimeFromCommitterAssignmentToPrMerge(pullStats, committers),
};
}
function convertMsToRoundedMinutes(milliseconds: number): number {
return Math.floor(milliseconds / 60_000);
}
async function reportMetrics(statBuckets: { [key: number]: PrStats[] }) {
console.log("---------------------------------");
console.log("PR Stats");
console.log("---------------------------------");
let csvOutput = "";
csvOutput +=
"Bucket start (bucketed by merge time),PRs Completed,PRs completed by first time contributors,Average time in minutes to first review,Average time in minutes to first review for new contributors,Average time in minutes from PR creation to completion,Total number of reviewers,Total number of committers performing reviews,Total number of non-committers performing reviews,Gini index (fairness) of committers performing reviews,Average time in minutes from committer assignment to PR merge";
const startDates = Object.keys(statBuckets);
for (let i = 0; i < startDates.length; i++) {
let startDate = startDates[i];
let aggregatedStats = await aggregateStatsForBucket(statBuckets[startDate]);
console.log();
const bucketStart = new Date(parseInt(startDate));
console.log("Bucket start:", bucketStart);
csvOutput += `\n${bucketStart.toDateString()}`;
console.log("PRs completed:", aggregatedStats.prsCompleted);
csvOutput += `,${aggregatedStats.prsCompleted}`;
console.log(
"PRs completed by first time contributors:",
aggregatedStats.prsCompletedByNewContributors
);
csvOutput += `,${aggregatedStats.prsCompletedByNewContributors}`;
console.log(
"Average time in minutes to first review:",
convertMsToRoundedMinutes(aggregatedStats.averageTimeToFirstReview)
);
csvOutput += `,${convertMsToRoundedMinutes(
aggregatedStats.averageTimeToFirstReview
)}`;
console.log(
"Average time in minutes to first review for new contributors:",
convertMsToRoundedMinutes(
aggregatedStats.averageTimeToNewContributorFirstReview
)
);
csvOutput += `,${convertMsToRoundedMinutes(
aggregatedStats.averageTimeToNewContributorFirstReview
)}`;
console.log(
"Average time in minutes from PR creation to completion:",
convertMsToRoundedMinutes(aggregatedStats.averageTimeCreationToCompletion)
);
csvOutput += `,${convertMsToRoundedMinutes(
aggregatedStats.averageTimeCreationToCompletion
)}`;
console.log(
"Total number of reviewers:",
aggregatedStats.numUsersPerformingReviews
);
csvOutput += `,${aggregatedStats.numUsersPerformingReviews}`;
console.log(
"Total number of committers performing reviews:",
aggregatedStats.numCommittersPerformingReviews
);
csvOutput += `,${aggregatedStats.numCommittersPerformingReviews}`;
console.log(
"Total number of non-committers performing reviews:",
aggregatedStats.numNonCommittersPerformingReviews
);
csvOutput += `,${aggregatedStats.numNonCommittersPerformingReviews}`;
console.log(
"Gini index (fairness) of committers performing reviews:",
aggregatedStats.giniIndexCommittersPerformingReviews
);
csvOutput += `,${aggregatedStats.giniIndexCommittersPerformingReviews}`;
console.log(
"Average time in minutes from committer assignment to PR merge:",
convertMsToRoundedMinutes(
aggregatedStats.averageTimeFromCommitterAssignmentToPrMerge
)
);
csvOutput += `,${convertMsToRoundedMinutes(
aggregatedStats.averageTimeFromCommitterAssignmentToPrMerge
)}`;
}
fs.writeFileSync(PATH_TO_METRICS_CSV, csvOutput);
console.log(`Output written to ${PATH_TO_METRICS_CSV}`);
}
async function gatherMetrics() {
// We will only aggregate metrics from the last 90 days,
// but will look further back to determine if this is a user's first contribution
const pullsFromLastYear = await getCompletedPullsFromLastYear();
let cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - 90);
let pullStats: PrStats[] = [];
console.log("Extracting stats from pulls - this may take a while");
for (let i = 0; i < pullsFromLastYear.length; i++) {
let pull = pullsFromLastYear[i];
if (new Date(pull.created_at) > cutoffDate && pull.merged_at) {
pullStats.push(await extractPrStats(pull, pullsFromLastYear));
}
if (i % 10 === 0) {
process.stdout.write(".");
}
}
console.log("\nDone extracting stats, formatting results");
let statBuckets: { [key: number]: PrStats[] } = {};
let bucketEnd = new Date();
bucketEnd.setUTCHours(23, 59, 59, 999);
pullStats.forEach((pullStat) => {
let bucketStart = getMetricBucketStartDate(pullStat, bucketEnd);
if (bucketStart in statBuckets) {
statBuckets[bucketStart].push(pullStat);
} else {
statBuckets[bucketStart] = [pullStat];
}
});
await reportMetrics(statBuckets);
}
gatherMetrics();
export {};