forked from useshortcut/project-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.js
More file actions
315 lines (254 loc) · 8.69 KB
/
fetch.js
File metadata and controls
315 lines (254 loc) · 8.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
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
var fs = require('fs');
var _ = require('lodash');
var moment = require('moment');
var request = require('request');
var MILLISECONDS_IN_A_DAY = 1000 * 60 * 60 * 24;
var DATE_FORMAT = 'YYYY-MM-DD';
var MILESTONE_FILE = 'data/milestones.js';
var TOKEN = process.env.CLUBHOUSE_API_TOKEN;
function fetchMilestones(callback) {
request({
url: 'https://api.clubhouse.io/api/v3/milestones?token=' + TOKEN,
json: true
}, callback);
}
function fetchMilestoneEpics(milestoneID, callback) {
request({
url: 'https://api.clubhouse.io/api/v3/milestones/' + milestoneID + '/epics?token=' + TOKEN,
json: true
}, callback);
}
function fetchCompletedStoriesForEpic(epicName, callback) {
request({
url: 'https://api.clubhouse.io/api/v3/search/stories?token=' + TOKEN,
method: 'GET',
json: true,
body: { query: 'epic:"' + epicName + '"' }
}, callback);
}
function fetchCompletedStoriesForMilestone(milestoneID, callback) {
request({
url: 'https://api.clubhouse.io/api/v3/search/stories?token=' + TOKEN,
method: 'POST',
json: true,
body: { archived: false, milestone_ids: [milestoneID], workflow_state_types: ['done'] }
}, callback);
}
function createDateRange(fromDate, toDate) {
var stack = [];
var fromMoment = moment(fromDate);
var toMoment = moment(toDate);
while (fromMoment.isBefore(toMoment) || fromMoment.isSame(toMoment, 'days')) {
stack.push(fromMoment.format(DATE_FORMAT));
fromMoment = fromMoment.add(1, 'days');
}
return stack;
}
function storiesToCompletedTimestamps(stories) {
return _.map(stories, function (story) {
return new Date(story.created_at).getTime();
});
}
function calculateDateRangeForStories(stories) {
var timestamps = storiesToCompletedTimestamps(stories);
var fromDate = _.min(timestamps);
var toDate = _.max(timestamps);
return createDateRange(fromDate, toDate);
}
function calculateStoryRatioData(stories, dateRange) {
var data = 'Data.StoryTypeRatios = [\n';
var totals = {
feature: 0,
bug: 0,
chore: 0,
total: 0
};
_.each(dateRange, function (day) {
_.each(stories, function (story) {
// Measure by points:
// if (story.estimate) {
// totals[story.story_type] += story.estimate;
// }
if (story.completed && story.completed_at.split('T')[0] === day) {
// Measure by story count:
totals[story.story_type] += 1;
totals.total += 1;
}
});
data += ' [new Date("' + day + '"), ' + (totals.feature / totals.total) + ', ' + (totals.bug / totals.total) + ', ' + (totals.chore / totals.total) + '],\n';
});
data += '];\n';
return data;
}
function calculateStoryTypeData(stories, dateRange) {
var data = 'Data.StoryTypeData = [\n';
var totals = {
feature: 0,
bug: 0,
chore: 0
};
_.each(dateRange, function (day) {
_.each(stories, function (story) {
// Measure by points:
// if (story.estimate) {
// totals[story.story_type] += story.estimate;
// }
if (story.completed && story.completed_at.split('T')[0] === day) {
// Measure by story count:
totals[story.story_type] += 1;
}
});
data += ' [new Date("' + day + '"), ' + totals.feature + ', ' + totals.bug + ', ' + totals.chore + '],\n';
});
data += '];\n';
return data;
}
function calculateMonthlyVelocityChartData(stories, dateRange) {
var data = 'Data.MonthlyVelocityChart = [\n';
var velocity = 0;
_.each(dateRange, function (day) {
_.each(stories, function (story) {
// Measure by points:
// if (story.estimate) {
// velocity += story.estimate;
// }
if (story.completed && story.completed_at.split('T')[0] === day) {
// Measure by story count:
velocity += 1;
}
});
if (day.split('-')[2] === '01') {
data += ' [new Date("' + day + '"), ' + velocity + '],\n';
velocity = 0;
}
});
data += '];\n';
return data;
}
function calculateCycleTimeChartData(stories, dateRange) {
var data = 'Data.CycleTimeChart = [\n';
var cycleTimes = [];
_.each(dateRange, function (day) {
_.each(stories, function (story) {
if (story.completed) {
if (story.completed_at.split('T')[0] === day) {
var cycleTime = (new Date(story.completed_at).getTime() - new Date(story.started_at).getTime()) / MILLISECONDS_IN_A_DAY;
cycleTimes.push(cycleTime);
}
}
});
if (day.split('-')[2] === '01') {
data += ' [new Date("' + day + '"), ' + _.max(cycleTimes) + ', ' + _.mean(cycleTimes) + ', ' + _.min(cycleTimes) + '],\n';
cycleTimes = [];
}
});
data += '];\n';
return data;
}
function calculateEstimateChartData(stories) {
var estimates = { None: 0 };
_.each(stories, function (story) {
var estimate = _.isNumber(story.estimate) ? story.estimate : 'None';
if (estimates[estimate]) {
estimates[estimate]++;
} else {
estimates[estimate] = 1;
}
});
var data = 'Data.EstimateChart = ' + JSON.stringify(estimates) + ';\n';
return data;
}
function compileChartData(stories, milestone) {
console.log('Compiling story data...');
stories = _.sortBy(stories, function (story) {
return new Date(story.completed_at).getTime();
});
var dateRange = calculateDateRangeForStories(stories);
// console.log(dateRange)
var data = 'var Data = {}; Data.MilestoneName = "' + milestone.name + '"; Data.LastFetched="' + moment().format('MMMM D, YYYY') + '"; ';
console.log("Number of stories: " + stories.length)
data += calculateStoryTypeData(stories, dateRange);
data += calculateStoryRatioData(stories, dateRange);
data += calculateMonthlyVelocityChartData(stories, dateRange);
data += calculateCycleTimeChartData(stories, dateRange);
data += calculateEstimateChartData(stories);
fs.writeFileSync('data/milestone-' + milestone.id + '.js', data);
}
function saveMilestonesToFile(milestones) {
var data = 'var ClubhouseMilestones = [];';
_.each(_.filter(milestones, { completed: false }), function (milestone) {
data += 'ClubhouseMilestones.push({ id: ' + milestone.id + ', name: "' + milestone.name + '" });';
});
_.each(_.filter(milestones, { completed: true }), function (milestone) {
data += 'ClubhouseMilestones.push({ id: ' + milestone.id + ', name: "' + milestone.name + ' (completed)" });';
});
fs.writeFileSync(MILESTONE_FILE, data);
}
function fetchAndCompileChartForMilestone(milestone, callback) {
callback = _.isFunction(callback) ? callback : _.noop;
console.log('Fetching completed stories for milestone "' + milestone.name + '"...');
fetchMilestoneEpics(milestone.id, function(err, res, epics){
var epic = epics.shift();
// TODO: Gotta implement a recursion just like line 255 and maybe paginate through stories
if (epic) {
console.log(epic.name)
fetchCompletedStoriesForEpic(epic.name, function (err, res, stories) {
compileChartData(stories.data, milestone);
callback();
});
}
});
}
function fetchAndCompileChartsForAllMilestones(milestones) {
var milestone = milestones.shift();
if (milestone) {
fetchAndCompileChartForMilestone(milestone, function () {
fetchAndCompileChartsForAllMilestones(milestones);
});
}
}
function findMatchingMilestones(milestones, query) {
if (query === 'all') {
return _.filter(milestones, { completed: false });
}
return _.filter(milestones, function (milestone) {
return parseInt(query, 10) === milestone.id || milestone.name.toLowerCase().indexOf(query) === 0;
});
}
function compileMilestoneData() {
var query = process.argv[2];
console.log('Fetching milestones...');
fetchMilestones(function (err, res, milestones) {
if (err || !milestones || milestones.length === 0) {
console.log('No milestones found!');
return false;
}
milestones = _.sortBy(milestones, 'name');
saveMilestonesToFile(milestones);
var foundMilestones = findMatchingMilestones(milestones, query);
if (!query || foundMilestones.length === 0) {
if (foundMilestones.length === 0) {
console.log('Matching milestone not found!');
}
console.log('You have access to the following milestones:\n');
milestones.forEach(function (milestone) {
console.log(' - ' + milestone.name);
});
return false;
}
fetchAndCompileChartsForAllMilestones(foundMilestones);
});
}
function displayNoTokenMessage() {
console.log('Missing CLUBHOUSE_API_TOKEN environment variable.');
console.log('If you don\'t already have one, go to Clubhouse > Settings > Your Account > API Tokens to create one.');
console.log('Then run this command:');
console.log('CLUBHOUSE_API_TOKEN="MYTOKEN"');
}
function init() {
if (!TOKEN) {
return displayNoTokenMessage();
}
compileMilestoneData();
}
init();