-
Notifications
You must be signed in to change notification settings - Fork 353
/
Pushes.jsx
452 lines (400 loc) · 15.8 KB
/
Pushes.jsx
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
import React from 'react';
import PropTypes from 'prop-types';
import pick from 'lodash/pick';
import keyBy from 'lodash/keyBy';
import isEqual from 'lodash/isEqual';
import max from 'lodash/max';
import { thDefaultRepo, thEvents, thMaxPushFetchSize } from '../../helpers/constants';
import { parseQueryParams } from '../../helpers/url';
import {
getAllUrlParams,
getQueryString,
getUrlParam,
setLocation,
setUrlParam,
} from '../../helpers/location';
import { isUnclassifiedFailure } from '../../helpers/job';
import PushModel from '../../models/push';
import JobModel from '../../models/job';
import { reloadOnChangeParameters } from '../../helpers/filter';
import { withNotifications } from '../../shared/context/Notifications';
const PushesContext = React.createContext({});
const defaultPushCount = 10;
// Keys that, if present on the url, must be passed into the push
// polling endpoint
const pushPollingKeys = ['tochange', 'enddate', 'revision', 'author'];
const pushFetchKeys = [...pushPollingKeys, 'fromchange', 'startdate'];
const pushPollInterval = 60000;
export class PushesClass extends React.Component {
constructor(props) {
super(props);
this.skipNextPageReload = false;
this.state = {
pushList: [],
jobMap: {},
revisionTips: [],
jobsLoaded: false,
loadingPushes: true,
oldestPushTimestamp: null,
latestJobTimestamp: null,
cachedReloadTriggerParams: this.getNewReloadTriggerParams(),
allUnclassifiedFailureCount: 0,
filteredUnclassifiedFailureCount: 0,
};
this.value = {
...this.state,
updateJobMap: this.updateJobMap,
getAllShownJobs: this.getAllShownJobs,
fetchPushes: this.fetchPushes,
recalculateUnclassifiedCounts: this.recalculateUnclassifiedCounts,
setRevisionTips: this.setRevisionTips,
addPushes: this.addPushes,
getPush: this.getPush,
updateUrlFromchange: this.updateUrlFromchange,
getNextPushes: this.getNextPushes,
handleUrlChanges: this.handleUrlChanges,
getGeckoDecisionJob: this.getGeckoDecisionJob,
getGeckoDecisionTaskId: this.getGeckoDecisionTaskId,
};
}
componentDidMount() {
this.setValue = this.setValue.bind(this);
this.updateJobMap = this.updateJobMap.bind(this);
this.getAllShownJobs = this.getAllShownJobs.bind(this);
this.fetchPushes = this.fetchPushes.bind(this);
this.recalculateUnclassifiedCounts = this.recalculateUnclassifiedCounts.bind(this);
this.setRevisionTips = this.setRevisionTips.bind(this);
this.addPushes = this.addPushes.bind(this);
this.getPush = this.getPush.bind(this);
this.updateUrlFromchange = this.updateUrlFromchange.bind(this);
this.getNextPushes = this.getNextPushes.bind(this);
this.handleUrlChanges = this.handleUrlChanges.bind(this);
this.getGeckoDecisionJob = this.getGeckoDecisionJob.bind(this);
this.getGeckoDecisionTaskId = this.getGeckoDecisionTaskId.bind(this);
this.getLastModifiedJobTime = this.getLastModifiedJobTime.bind(this);
this.poll = this.poll.bind(this);
// TODO: this.value needs to now get the bound versions of the functions.
// But when we move the function binding to the constructors, we won't
// have to re-do this in componentDidMount.
this.value = {
...this.state,
updateJobMap: this.updateJobMap,
getAllShownJobs: this.getAllShownJobs,
fetchPushes: this.fetchPushes,
recalculateUnclassifiedCounts: this.recalculateUnclassifiedCounts,
setRevisionTips: this.setRevisionTips,
addPushes: this.addPushes,
getPush: this.getPush,
updateUrlFromchange: this.updateUrlFromchange,
getNextPushes: this.getNextPushes,
handleUrlChanges: this.handleUrlChanges,
getGeckoDecisionJob: this.getGeckoDecisionJob,
getGeckoDecisionTaskId: this.getGeckoDecisionTaskId,
};
window.addEventListener('hashchange', this.handleUrlChanges, false);
// get our first set of resultsets
const fromchange = getUrlParam('fromchange');
// If we have a ``fromchange`` url param. We don't want to limit ourselves
// to the default of 10 pushes on the first load.
this.fetchPushes(fromchange ? thMaxPushFetchSize : defaultPushCount);
this.poll();
}
componentWillUnmount() {
if (this.pushIntervalId) {
clearInterval(this.pushIntervalId);
this.pushIntervalId = null;
}
}
setValue(newState, callback) {
this.value = { ...this.value, ...newState };
this.setState(newState, callback);
}
getNewReloadTriggerParams() {
const params = parseQueryParams(getQueryString());
return reloadOnChangeParameters.reduce(
(acc, prop) => (params[prop] ? { ...acc, [prop]: params[prop] } : acc), {});
}
getAllShownJobs(pushId) {
const { jobMap } = this.state;
const jobList = Object.values(jobMap);
return pushId ?
jobList.filter(job => job.push_id === pushId && job.visible) :
jobList.filter(job => job.visible);
}
setRevisionTips() {
const { pushList } = this.state;
this.setValue({ revisionTips: pushList.map(push => ({
revision: push.revision,
author: push.author,
title: push.revisions[0].comments.split('\n')[0],
})) });
}
getPush(pushId) {
const { pushList } = this.state;
return pushList.find(push => pushId === push.id);
}
getNextPushes(count) {
const params = getAllUrlParams();
this.setValue({ loadingPushes: true });
if (params.has('revision')) {
// We are viewing a single revision, but the user has asked for more.
// So we must replace the ``revision`` param with ``tochange``, which
// will make it just the top of the range. We will also then get a new
// ``fromchange`` param after the fetch.
this.skipNextPageReload = true;
const revision = params.get('revision');
params.delete('revision');
params.set('tochange', revision);
setLocation(params);
} else if (params.has('startdate')) {
// We are fetching more pushes, so we don't want to limit ourselves by
// ``startdate``. And after the fetch, ``startdate`` will be invalid,
// and will be replaced on the location bar by ``fromchange``.
this.skipNextPageReload = true;
setUrlParam('startdate', null);
}
this.fetchPushes(count).then(this.updateUrlFromchange);
}
getGeckoDecisionJob(pushId) {
const { jobMap } = this.state;
return Object.values(jobMap).find(job => (
job.push_id === pushId &&
job.platform === 'gecko-decision' &&
job.state === 'completed' &&
job.job_type_symbol === 'D'));
}
getGeckoDecisionTaskId(pushId, repoName) {
const decisionTask = this.getGeckoDecisionJob(pushId);
if (decisionTask) {
return JobModel.get(repoName, decisionTask.id).then(
(job) => {
// this failure case is unlikely, but I guess you
// never know
if (!job.taskcluster_metadata) {
return Promise.reject('Decision task missing taskcluster metadata');
}
return job.taskcluster_metadata.task_id;
});
}
// no decision task, we fail
return Promise.reject('No decision task');
}
getLastModifiedJobTime() {
const { jobMap } = this.state;
const latest = max(Object.values(jobMap).map(job => new Date(`${job.last_modified}Z`))) || new Date();
latest.setSeconds(latest.getSeconds() - 3);
return latest;
}
poll() {
this.pushIntervalId = setInterval(() => {
const { notify } = this.props;
const { pushList } = this.state;
// these params will be passed in each time we poll to remain
// within the constraints of the URL params
const locationSearch = parseQueryParams(getQueryString());
const pushPollingParams = pushPollingKeys.reduce(
(acc, prop) => (locationSearch[prop] ? { ...acc, [prop]: locationSearch[prop] } : acc), {});
if (pushList.length === 1 && locationSearch.revision) {
// If we are on a single revision, no need to poll for more pushes, but
// we need to keep polling for jobs.
this.fetchNewJobs();
} else {
if (pushList.length) {
// We have a range of pushes, but not bound to a single push,
// so get only pushes newer than our latest.
pushPollingParams.fromchange = pushList[pushList.length - 1].revision;
}
// We will either have a ``revision`` param, but no push for it yet,
// or a ``fromchange`` param because we have at least 1 push already.
PushModel.getList(pushPollingParams)
.then(async (resp) => {
if (resp.ok) {
const data = await resp.json();
this.addPushes(data);
this.fetchNewJobs();
} else {
notify('Error fetching new push data', 'danger', { sticky: true });
}
});
}
}, pushPollInterval);
}
// reload the page if certain params were changed in the URL. For
// others, such as filtering, just re-filter without reload.
// the param ``skipNextPageReload`` will cause a single run through
// this code to skip the page reloading even on a param that would
// otherwise trigger a page reload. This is useful for a param that
// is being changed by code in a specific situation as opposed to when
// the user manually edits the URL location bar.
handleUrlChanges() {
const { cachedReloadTriggerParams } = this.state;
const newReloadTriggerParams = this.getNewReloadTriggerParams();
// if we are just setting the repo to the default because none was
// set initially, then don't reload the page.
const defaulting = newReloadTriggerParams.repo === thDefaultRepo &&
!cachedReloadTriggerParams.repo;
if (!defaulting && cachedReloadTriggerParams &&
!isEqual(newReloadTriggerParams, cachedReloadTriggerParams) &&
!this.skipNextPageReload) {
window.location.reload();
} else {
this.setState({ cachedReloadTriggerParams: newReloadTriggerParams });
}
this.skipNextPageReload = false;
this.recalculateUnclassifiedCounts();
}
/**
* Get the next batch of pushes based on our current offset.
* @param count How many to fetch
*/
fetchPushes(count) {
const { notify } = this.props;
const { oldestPushTimestamp } = this.state;
// const isAppend = (repoData.pushes.length > 0);
// Only pass supported query string params to this endpoint.
const options = {
...pick(parseQueryParams(getQueryString()), pushFetchKeys),
count,
};
if (oldestPushTimestamp) {
// If we have an oldestTimestamp, then this isn't our first fetch,
// we're fetching more pushes. We don't want to limit this fetch
// by the current ``fromchange`` or ``tochange`` value. Deleting
// these params here do not affect the params on the location bar.
delete options.fromchange;
delete options.tochange;
options.push_timestamp__lte = oldestPushTimestamp;
}
return PushModel.getList(options).then(async (resp) => {
if (resp.ok) {
const data = await resp.json();
this.addPushes(data.results.length ? data : { results: [] });
} else {
notify('Error retrieving push data!', 'danger', { sticky: true });
}
}).then(() => this.setValue({ loadingPushes: false }));
}
addPushes(data) {
const { pushList } = this.state;
if (data.results.length > 0) {
const pushIds = pushList.map(push => push.id);
const newPushList = [...pushList, ...data.results.filter(push => !pushIds.includes(push.id))];
newPushList.sort((a, b) => b.push_timestamp - a.push_timestamp);
const oldestPushTimestamp = newPushList[newPushList.length - 1].push_timestamp;
this.recalculateUnclassifiedCounts();
this.setValue(
{ pushList: newPushList, oldestPushTimestamp },
() => this.setRevisionTips(),
);
}
}
async fetchNewJobs() {
const { pushList } = this.state;
if (!pushList.length) {
// If we have no pushes, then no need to poll for jobs.
return;
}
const pushIds = pushList.map(push => push.id);
const lastModified = this.getLastModifiedJobTime();
const jobList = await PushModel.getJobs(pushIds, { lastModified });
// break the jobs up per push
const jobs = jobList.reduce((acc, job) => {
const pushJobs = acc[job.push_id] ? [...acc[job.push_id], job] : [job];
return { ...acc, [job.push_id]: pushJobs };
}, {});
// If a job is selected, and one of the jobs we just fetched is the
// updated version of that selected job, then send that with the event.
const selectedJobId = getUrlParam('selectedJob');
const updatedSelectedJob = selectedJobId ?
jobList.find(job => job.id === parseInt(selectedJobId, 10)) : null;
window.dispatchEvent(new CustomEvent(
thEvents.applyNewJobs,
{ detail: { jobs, updatedSelectedJob } },
));
}
updateUrlFromchange() {
// since we fetched more pushes, we need to persist the push state in the URL.
const { pushList } = this.state;
const updatedLastRevision = pushList[pushList.length - 1].revision;
if (getUrlParam('fromchange') !== updatedLastRevision) {
this.skipNextPageReload = true;
setUrlParam('fromchange', updatedLastRevision);
}
}
/**
* Loops through the map of unclassified failures and checks if it is
* within the enabled tiers and if the job should be shown. This essentially
* gives us the difference in unclassified failures and, of those jobs, the
* ones that have been filtered out
*/
recalculateUnclassifiedCounts() {
const { jobMap } = this.state;
const { filterModel } = this.props;
const tiers = filterModel.urlParams.tier || [];
let allUnclassifiedFailureCount = 0;
let filteredUnclassifiedFailureCount = 0;
Object.values(jobMap).forEach((job) => {
if (isUnclassifiedFailure(job)) {
if (tiers.includes(String(job.tier))) {
if (filterModel.showJob(job)) {
filteredUnclassifiedFailureCount++;
}
allUnclassifiedFailureCount++;
}
}
});
this.setValue({ allUnclassifiedFailureCount, filteredUnclassifiedFailureCount });
}
updateJobMap(jobList) {
const { jobMap, pushList } = this.state;
if (jobList.length) {
// lodash ``keyBy`` is significantly faster than doing a ``reduce``
this.setValue({
jobMap: { ...jobMap, ...keyBy(jobList, 'id') },
jobsLoaded: pushList.every(push => push.jobsLoaded),
pushList: [...pushList],
});
}
}
render() {
return (
<PushesContext.Provider value={this.value}>
{this.props.children}
</PushesContext.Provider>
);
}
}
PushesClass.propTypes = {
children: PropTypes.object.isRequired,
filterModel: PropTypes.object.isRequired,
notify: PropTypes.func.isRequired,
};
export const Pushes = withNotifications(PushesClass);
export function withPushes(Component) {
return function PushesComponent(props) {
return (
<PushesContext.Consumer>
{context => (
<Component
{...props}
pushList={context.pushList}
revisionTips={context.revisionTips}
jobMap={context.jobMap}
jobsLoaded={context.jobsLoaded}
loadingPushes={context.loadingPushes}
allUnclassifiedFailureCount={context.allUnclassifiedFailureCount}
filteredUnclassifiedFailureCount={context.filteredUnclassifiedFailureCount}
updateJobMap={context.updateJobMap}
getAllShownJobs={context.getAllShownJobs}
fetchPushes={context.fetchPushes}
getPush={context.getPush}
recalculateUnclassifiedCounts={context.recalculateUnclassifiedCounts}
getNextPushes={context.getNextPushes}
getGeckoDecisionJob={context.getGeckoDecisionJob}
getGeckoDecisionTaskId={context.getGeckoDecisionTaskId}
/>
)}
</PushesContext.Consumer>
);
};
}