-
Notifications
You must be signed in to change notification settings - Fork 1
/
cycleTimeTextLogger.js
170 lines (141 loc) · 7.35 KB
/
cycleTimeTextLogger.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
const fs = require('fs');
const moment = require('moment-business-time');
const cycleTimeFileUtils = require('./cycleTimeFileUtils');
function writeResults(prMetrics, team, onlyIncludeWorkingHours = false, filePrefix = '') {
const header = getHeader(team);
const definitions = printDefinitions();
const overallMetricResults = getOverallStatisticsResults(prMetrics);
const prMetricResults = [...prMetrics]
.sort((a, b) => b.cycleTime - a.cycleTime)
.map(prStat => `${printPullRequestStatistics(prStat)}\n\n`);
try {
fs.writeFileSync(
getResultFileName(team, onlyIncludeWorkingHours, filePrefix),
[header, definitions, overallMetricResults, prMetricResults].join(`\n`)
)
} catch (err) {
console.error(err)
}
}
module.exports.writeResults = writeResults;
function printDefinitions() {
return [`Definitions`,
`--------------------`,
`Time to open: Time from first commit to when PR is created. When a PR is rebased & forced pushed, this might be ? minutes`,
`Time to first interaction: Time from pr opening to the first collaborator interaction (comment/review)`,
`Time to merge: Time from created to pr close`,
`Cycle time: Time from first commit || pr created to close`,
`Conversation break duration: Duration of break between author/collaborator interactions`,
`Conversation breaks: Number of conversation breaks that happen in a PR - breaks are defined by a switch in speaker`
].join(`\n`);
}
function printPullRequestStatistics(prMetrics) {
const prMetricResults = [
`\nPR-${prMetrics.number}: ${prMetrics.title}`,
`--------------------`,
`Time to open: ${formatTimeStat(prMetrics.timeToOpen)}`,
`Time to first interaction: ${formatTimeStat(prMetrics.timeToFirstInteraction)}`,
`Time to merge: ${formatTimeStat(prMetrics.timeToMerge)}`,
`Cycle time: ${formatTimeStat(prMetrics.cycleTime)}`,
`Number of commits: ${prMetrics.numberOfCommits}`,
`Number of files: ${prMetrics.numberOfFiles}`,
`Lines changed: ${calculateLinesChanges(prMetrics)}`,
`Number of reviews: ${prMetrics.numberOfReviews}`,
`Conversation break duration: median: ${formatTimeStat(calculateMedian(prMetrics.conversationBreakDurations))}, average ${formatTimeStat(calculateAverage(prMetrics.conversationBreakDurations))}`,
`Conversation break durations: ${prMetrics.conversationBreakDurations.map(duration => formatTimeStat(duration))}`,
`Conversation breaks: ${prMetrics.conversationBreaks}`
].join(`\n`);
const timelineEvents = prMetrics.events.map(event => `${(formatTimestamp(event.time))}: ${event.message}`);
return [].concat(prMetricResults, ['\nTimeline:'], timelineEvents).join(`\n`);
}
function getOverallStatisticsResults(prMetrics) {
const allPRMetrics = prMetrics
.reduce((all, curr) => {
all.timeToOpen.push(curr.timeToOpen);
all.timeToFirstInteraction.push(curr.timeToFirstInteraction);
all.timeToMerge.push(curr.timeToMerge);
all.cycleTime.push(curr.cycleTime);
all.numberOfCommits.push(curr.numberOfCommits);
all.numberOfFiles.push(curr.numberOfFiles);
all.lineChanges.push(calculateLinesChanges(curr));
all.numberOfReviews.push(curr.numberOfReviews);
all.conversationBreakDurations.push(curr.conversationBreakDurations);
all.conversationBreaks.push(curr.conversationBreaks);
return all;
}, {
timeToOpen: [],
timeToFirstInteraction: [],
timeToMerge: [],
cycleTime: [],
numberOfCommits: [],
numberOfFiles: [],
lineChanges: [],
numberOfReviews: [],
conversationBreakDurations: [],
conversationBreaks: []
});
const sortedByMergedAt = [...prMetrics].sort((a, b) => momentSort(a.merged_at, b.merged_at))
const latestMergedPR = formatTimestamp(sortedByMergedAt[sortedByMergedAt.length - 1].merged_at);
const earliestMergedPR = formatTimestamp(sortedByMergedAt[0].merged_at);
const unreviewed = prMetrics.filter(pr => pr.conversationBreakDurations.length === 0).length;
return [
`\nOverall metrics for ${prMetrics.length} PRs spanning PRs merged on ${earliestMergedPR} to ${latestMergedPR}:`,
'--------------------',
`Time to open: ${formatTimeMetrics(allPRMetrics.timeToOpen)}`,
`Time to first interaction: ${formatTimeMetrics(allPRMetrics.timeToFirstInteraction)}`,
`Time to merge: ${formatTimeMetrics(allPRMetrics.timeToMerge)}`,
`Cycle time: ${formatTimeMetrics(allPRMetrics.cycleTime)}`,
`Number of commits: ${formatNumberMetrics(allPRMetrics.numberOfCommits)}`,
`Number of files: ${formatNumberMetrics(allPRMetrics.numberOfFiles)}`,
`Lines changed: ${formatNumberMetrics(allPRMetrics.lineChanges)}`,
`Number of reviews: ${formatNumberMetrics(allPRMetrics.numberOfReviews)}`,
`Conversation break duration: ${formatTimeMetrics(allPRMetrics.conversationBreakDurations.flat())}`,
`Conversation breaks: ${formatNumberMetrics(allPRMetrics.conversationBreaks)}`,
`Number of unreviewed PRs: ${unreviewed}/${prMetrics.length}`
].join(`\n`);
function formatTimeMetrics(metrics) {
return `median: ${formatTimeStat(calculateMedian(metrics))}, average: ${formatTimeStat(calculateAverage(metrics))}`;
}
function formatNumberMetrics(metrics) {
return `median: ${calculateMedian(metrics).toFixed(2)}, average: ${calculateAverage(metrics).toFixed(2)}`
}
}
function calculateLinesChanges(prMetrics) {
return prMetrics.totalAdditions + prMetrics.totalDeletions;
}
function formatTimeStat(timeInMinutes) {
if (timeInMinutes) {
if (timeInMinutes > 1440) {
return `${(timeInMinutes / 1440).toFixed(2)} days`
}
if (timeInMinutes > 60) {
return `${(timeInMinutes / 60).toFixed(2)} hours`
}
return `${timeInMinutes.toFixed(2)} minutes`
}
return `? minutes`
}
function formatTimestamp(timestamp) {
return moment(timestamp).format('ddd, MMM Do h:mma');
}
function calculateMedian(numbers) {
const sortedNums = [...numbers].filter(n => !!n).sort((a, b) => a - b);
const mid = Math.floor(sortedNums.length / 2)
return numbers.length % 2 !== 0 ? sortedNums[mid] : (sortedNums[mid - 1] + sortedNums[mid]) / 2;
}
function calculateAverage(numbers) {
const total = numbers.filter(n => !!n).reduce((sum, curr) => sum + curr, 0);
return total / numbers.length;
}
function momentSort(a, b) {
return moment(a).toDate().getTime() - moment(b).toDate().getTime();
}
function getResultFileName(team, onlyIncludeWorkingHours, filePrefix) {
return cycleTimeFileUtils.getResultFileName(filePrefix, `txt`, team, onlyIncludeWorkingHours);
}
function getHeader(team) {
if (team) {
return `------------- Git metrics for ${team} -------------\n`;
}
return `------------------- Git metrics -------------------\n`;
}