This repository has been archived by the owner on Apr 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
214 lines (189 loc) · 5.67 KB
/
index.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
const Promise = require('bluebird');
const octokit = require('@octokit/rest');
const math = require('mathjs')
const moment = require('moment');
const defaults = require('defaults');
const store = require('abstract-blob-store');
const momentDurationSetup = require('moment-duration-format');
const jsonParse = require('json-parse-stream');
momentDurationSetup(moment);
const getIssues = async (client, opts) => {
let response = await client.issues.getForOrg(opts)
let {data} = response
while (client.hasNextPage(response)) {
response = await client.getNextPage(response)
data = data.concat(response.data)
}
return data
}
class CycleTime {
constructor (options = {}) {
this.options = defaults(options, {
baseUrl: 'https://api.github.com',
cache: new store(),
org: null,
token: null
});
if (this.options.org === null) {
new Error('Missing Required Option: `org`');
}
if (!this.options.token === null) {
new Error('Missing Required Option: `token`');
}
this.Client = octokit({
baseUrl: this.options.baseUrl,
});
this.Client.authenticate({
type: 'token',
token: this.options.token
});
}
_cache(key, p) {
return new Promise((resolve, reject) => {
this.options.cache.exists(key, (err, exists) => {
if (err) reject(err);
if (exists) {
console.log('cache hit');
let data = [];
this.options.cache.createReadStream(key)
.on('data', (chunk) => {
data.push(chunk.toString());
})
.on('end', () => {
return resolve(JSON.parse(data.join('')));
});
} else {
console.log('cache miss');
return resolve(p.then((data) => {
this.options.cache.createWriteStream(key)
.write(JSON.stringify(data), 'utf8')
.end();
return data;
}));
}
});
});
}
_clear(key) {
this.options.cache.remove(key);
}
_format(data) {
let tickets = data.map((ticket) => {
return {
id: ticket.number,
org: this.options.org,
repos: ticket.repository.name,
opened: ticket.created_at,
reopened: null,
assigned: null,
closed: null,
reaction_time: null,
cycle_time: null,
lead_time: null
}
});
return tickets;
}
_process(ticket, timeline) {
timeline.forEach((tl) => {
switch(tl.event) {
case 'closed':
ticket.closed = tl.created_at;
break;
case 'reopened':
ticket.reopened = tl.created_at;
break;
case 'assigned':
ticket.assigned = tl.created_at;
break;
default:
break;
}
});
delete ticket._timeline;
return ticket;
}
_duration(start, end) {
start = new moment(start);
end = new moment(end);
if (start.isValid() && end.isValid()) {
return moment.duration(end.diff(start)).as('seconds');
} else {
return 0;
}
}
_times(ticket) {
ticket.reaction_time = this._duration(ticket.opened, ticket.assigned);
ticket.cycle_time = this._duration(ticket.assigned, ticket.closed);
ticket.lead_time = this._duration(ticket.opened, ticket.closed);
return ticket;
}
_aggregate(data, type) {
let agg = data.map(v => v[type]);
return [math.mean(agg.slice(0)), math.median(agg.slice(0))];
}
_calculate(tickets) {
return tickets.map((ticket) => {
return this._times(ticket)
});
}
_timeline(ticket) {
let opts = { owner: ticket.org, number: ticket.id, repo: ticket.repos }
let key = `github-cycle-time_timeline-${ticket.org}-${ticket.repos}-${ticket.id}.json`;
return this._cache(key, this.Client.issues.getEventsTimeline(opts)
.then((response) => {
return this._process(ticket, response.data)
}).catch(error => error)
);
}
_enhance(tickets) {
return tickets.map((ticket) => {
setTimeout(()=>{}, 100);
return this._timeline(ticket);
});
}
_fetch(since) {
let opts = { org: this.options.org, filter: 'all', state: 'all', direction: 'asc', per_page: 100, page: 1, since: since };
return this._cache('github-cycle-time_fetch.json', getIssues(this.Client, opts));
}
_humanize(duration_in_seconds) {
return moment
.duration(duration_in_seconds, 'seconds')
.format({
template: "w[W] d[D] h[H] m[M]",
precision: 1,
trim: true
});
}
tickets(since) {
return this._fetch(since)
.then(tickets => this._format(tickets))
.then(tickets => this._enhance(tickets))
.then(Promise.all)
.then(tickets => this._calculate(tickets));
}
metrics(since) {
return this.tickets(since)
.then((data) => {
let [rt_mean, rt_median] = this._aggregate(data, 'reaction_time');
let [ct_mean, ct_median] = this._aggregate(data, 'cycle_time');
let [lt_mean, lt_median] = this._aggregate(data, 'lead_time');
return {
org: this.options.org,
reaction_time_mean: rt_mean,
reaction_time_median: rt_median,
reaction_time_mean_human: this._humanize(rt_mean),
reaction_time_median_human: this._humanize(rt_median),
cycle_time_mean: ct_mean,
cycle_time_median: ct_median,
cycle_time_mean_human: this._humanize(ct_mean),
cycle_time_median_human: this._humanize(ct_median),
lead_time_mean: lt_mean,
lead_time_median: lt_median,
lead_time_mean_human: this._humanize(lt_mean),
lead_time_median_human: this._humanize(lt_median)
}
});
}
};
module.exports = CycleTime;