forked from web-platform-tests/wpt.fyi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-runs-query.js
321 lines (301 loc) · 9.59 KB
/
test-runs-query.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
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
/**
* Copyright 2018 The WPT Dashboard Project. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
import { Channels, DefaultProducts, DefaultProductSpecs, ProductInfo } from './product-info.js';
import { QueryBuilder } from './results-navigation.js';
import { pluralize } from './pluralize.js';
const testRunsQueryComputer =
'computeTestRunQueryParams(shas, aligned, master, labels, productSpecs, to, from, maxCount, offset)';
const TestRunsQuery = (superClass, opt_queryCompute) => class extends QueryBuilder(
ProductInfo(superClass),
opt_queryCompute || testRunsQueryComputer) {
static get properties() {
return {
/* Parsed product objects, computed from the spec strings */
products: {
type: Array,
notify: true,
value: [],
},
productSpecs: {
type: Array,
notify: true,
value: [],
},
isDefaultProducts: {
type: Boolean,
computed: 'computeIsDefaultProducts(productSpecs, productSpecs.*)',
value: true,
},
labels: {
type: Array,
value: [],
},
maxCount: Number,
offset: Number,
shas: Array,
aligned: Boolean,
master: Boolean,
from: Date,
to: Date,
isLatest: {
type: Boolean,
computed: 'computeIsLatest(shas)'
},
resultsRangeMessage: {
type: String,
computed: 'computeResultsRangeMessage(shas, productSpecs, to, from, maxCount, labels, master)',
},
};
}
ready() {
super.ready();
this._createMethodObserver('productsUpdated(products, products.*)');
// Convert any initial product specs to products, if provided.
if (this.productSpecs && this.productSpecs.length
&& !(this.products && this.products.length)) {
this.products = this.productSpecs.map(p => this.parseProductSpec(p));
}
// Force-trigger a channel label expansion.
this.updateQueryParams(this.queryParams);
}
// sha is a convenience method for getting (the) single sha.
// Using multiple shas is rare.
get sha() {
return this.shas && this.shas.length && this.shas[0] || 'latest';
}
// eslint-disable-next-line no-unused-vars
productsUpdated(products, itemChange) {
this.productSpecs = (products || []).map(p => this.getSpec(p));
}
/**
* Convert the UI property values into their equivalent URI query params.
*/
computeTestRunQueryParams(shas, aligned, master, labels, productSpecs, to, from, maxCount, offset) {
const params = {};
if (!this.computeIsLatest(shas)) {
params.sha = shas;
}
// Convert bool master to label 'master'
labels = labels && Array.from(labels) || [];
if (master && !labels.includes('master')) {
labels.push('master');
}
if (labels.length) {
params.label = labels;
}
maxCount = maxCount || this.defaultMaxCount;
if (maxCount) {
params['max-count'] = maxCount;
}
if (offset) {
params['offset'] = offset;
}
if (from) {
params.from = from.toISOString();
}
if (to) {
params.to = to.toISOString();
}
// Collapse a globally shared channel into a single label.
if (this.products.length) {
let allChannelsSame = true;
const channel = (this.products[0].labels || []).find(l => Channels.has(l));
for (const p of this.products) {
if (!(p.labels || []).find(l => l === channel)) {
allChannelsSame = false;
}
}
let productSpecs;
if (allChannelsSame) {
productSpecs = this.products.map(p => {
const nonChannel = (p.labels || []).filter(l => !Channels.has(l));
return this.getSpec(Object.assign({}, p, {labels: nonChannel}));
});
if (!labels.includes(channel)) {
params.label = labels.concat(channel);
}
} else {
productSpecs = this.products.map(p => this.getSpec(p));
}
if (!this.computeIsDefaultProducts(productSpecs)) {
params.product = productSpecs;
}
}
// Requesting a specific SHA makes aligned redundant.
if (aligned && this.computeIsLatest(shas)) {
params.aligned = true;
}
return params;
}
computeProducts(productSpecs) {
return productSpecs && productSpecs.map(s => this.parseProductSpec(s));
}
computeIsDefaultProducts(productSpecs) {
return !productSpecs || !productSpecs.length
|| DefaultProductSpecs.join(',') === (productSpecs || []).join(',');
}
get emptyQuery() {
return {
products: DefaultProducts.map(p => Object.assign({}, p)),
labels: [],
maxCount: undefined,
shas: [],
aligned: undefined,
};
}
clearQuery() {
this.setProperties(this.emptyQuery);
}
/**
* Update this component's UI properties to match the given query params.
*/
updateQueryParams(params) {
if (!params) {
this.clearQuery();
return;
}
const batchUpdate = this.emptyQuery;
if (!this.computeIsLatest(params.sha)) {
batchUpdate.shas = params.sha;
}
if ('product' in params) {
batchUpdate.products = params.product.map(p => this.parseProductSpec(p));
}
// Expand any global channel labels into the separate products
let sharedChannel;
if ('label' in params) {
sharedChannel = params.label.find(l => Channels.has(l));
batchUpdate.labels = params.label.filter(l => !Channels.has(l));
}
if (sharedChannel) {
for (const i in batchUpdate.products) {
const labels = batchUpdate.products[i].labels.filter(l => !Channels.has(l) || l === sharedChannel);
if (!batchUpdate.products[i].labels.includes(sharedChannel)) {
labels.push(sharedChannel);
}
batchUpdate.products[i].labels = labels;
}
}
if ('max-count' in params) {
batchUpdate.maxCount = params['max-count'];
}
if ('offset' in params) {
batchUpdate.offset = params['offset'];
}
if ('from' in params) {
batchUpdate.from = new Date(params['from']);
}
if ('to' in params) {
batchUpdate.to = new Date(params['to']);
}
if ('aligned' in params) {
batchUpdate.aligned = params.aligned;
}
batchUpdate.master = batchUpdate.labels && batchUpdate.labels.includes('master');
if (batchUpdate.master) {
batchUpdate.labels = batchUpdate.labels.filter(l => l !== 'master');
}
this.setProperties(batchUpdate);
}
computeResultsRangeMessage(shas, productSpecs, from, to, maxCount, labels, master) {
const latest = this.computeIsLatest(shas) ? 'the latest ' : '';
const branch = master ? 'master ' : '';
let msg = `Showing ${latest}${branch}test runs`;
if (shas && shas.length) {
// Shorten to 7 chars.
shas = shas.map(s => !this.computeIsLatest(s) && s.length > 7 ? s.substr(0, 7) : s);
msg += ` from ${pluralize('revision', shas.length)} ${shas.join(', ')}`;
} else if (from) {
const max = to || Date.now();
var diff = Math.floor((max - from) / 86400000);
const shortDate = date => date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
});
msg += to
? ` from ${shortDate(from)} to ${shortDate(to)}`
: ` from the last ${diff} days`;
} else if (maxCount) {
msg = `Showing ${maxCount} ${branch}test runs per product`;
} else if (this.computeIsDefaultProducts(productSpecs)) {
msg += ' for the default product set';
} else {
msg += ` for ${productSpecs.join(', ')}`;
}
if (labels && labels.length) {
const joined = labels.map(l => `'${l}'`).join(', ');
msg = `${msg} with ${pluralize('label', labels.length)} ${joined}`;
}
return msg;
}
};
const TestRunsUIQuery = (superClass, opt_queryCompute) => class extends TestRunsQuery(
superClass,
opt_queryCompute || testRunsUIQueryComputer) {
static get properties() {
return {
search: {
type: String,
value: '',
notify: true,
},
diff: {
type: Boolean,
value: false,
notify: true,
},
diffFilter: {
type: String,
value: 'ADC', // Added, Deleted, Changed
},
pr: {
type: Number,
notify: true,
},
// Specific runs, listed by their IDs.
runIds: Array,
};
}
computeTestRunUIQueryParams(shas, aligned, master, labels, productSpecs, to, from, maxCount, offset, diff, search, pr, runIds) {
const params = this.computeTestRunQueryParams(shas, aligned, master, labels, productSpecs, to, from, maxCount, offset);
if (diff || this.diff) {
params.diff = true;
if (this.diffFilter) {
params.filter = this.diffFilter;
}
}
if (search) {
params.q = search;
}
if (pr) {
params.pr = pr;
}
if (runIds) {
params['run_id'] = Array.from(this.runIds);
}
return params;
}
updateQueryParams(params) {
super.updateQueryParams(params);
let batchUpdate = {};
batchUpdate.pr = params.pr;
batchUpdate.search = params.q;
batchUpdate.diff = params.diff;
if (batchUpdate.diff) {
batchUpdate.diffFilter = params.filter;
}
if ('run_id' in params) {
batchUpdate.runIds = Array.from(params['run_id']);
}
this.setProperties(batchUpdate);
}
};
// TODO(lukebjerring): Support to & from in the builder.
const testRunsUIQueryComputer =
'computeTestRunUIQueryParams(shas, aligned, master, labels, productSpecs, to, from, maxCount, offset, diff, search, pr, runIds)';
TestRunsQuery.Computer = testRunsQueryComputer;
TestRunsUIQuery.Computer = testRunsUIQueryComputer;
export { TestRunsQuery, TestRunsUIQuery };