mirrored from https://skia.googlesource.com/buildbot
/
cluster-lastn-page-sk.ts
464 lines (414 loc) · 13.3 KB
/
cluster-lastn-page-sk.ts
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
453
454
455
456
457
458
459
460
461
462
463
464
/**
* @module module/cluster-lastn-page-sk
* @description <h2><code>cluster-lastn-page-sk</code></h2>
*
* Allows trying out an alert by clustering over a range of commits.
*/
import dialogPolyfill from 'dialog-polyfill';
import { define } from 'elements-sk/define';
import { fromObject } from 'common-sk/modules/query';
import { html } from 'lit-html';
import { jsonOrThrow } from 'common-sk/modules/jsonOrThrow';
import { stateReflector } from 'common-sk/modules/stateReflector';
import { HintableObject } from 'common-sk/modules/hintable';
import { SpinnerSk } from 'elements-sk/spinner-sk/spinner-sk';
import { errorMessage } from '../errorMessage';
import { ElementSk } from '../../../infra-sk/modules/ElementSk';
import 'elements-sk/spinner-sk';
import 'elements-sk/styles/buttons';
import '../cluster-summary2-sk';
import '../commit-detail-sk';
import '../triage-status-sk';
import '../alert-config-sk';
import '../domain-picker-sk';
import {
ParamSet,
Alert,
FrameResponse,
RegressionAtCommit,
Direction,
ClusterSummary,
RegressionDetectionRequest,
AlertUpdateResponse,
progress,
} from '../json';
import { DomainPickerState } from '../domain-picker-sk/domain-picker-sk';
import { AlertConfigSk } from '../alert-config-sk/alert-config-sk';
import { TriageStatusSkStartTriageEventDetails } from '../triage-status-sk/triage-status-sk';
import { ClusterSummary2SkOpenKeysEventDetail } from '../cluster-summary2-sk/cluster-summary2-sk';
import { DomainPickerSk } from '../domain-picker-sk/domain-picker-sk';
import { messagesToErrorString, startRequest } from '../progress/progress';
export class ClusterLastNPageSk extends ElementSk {
// The range of commits over which we are clustering.
private domain: DomainPickerState = {
begin: 0,
end: Math.floor(Date.now() / 1000),
num_commits: 200,
request_type: 1,
};
// True if the Alert is being saved to the database.
private writingAlert: boolean = false;
// The paramsets for the alert config.
private paramset: ParamSet = {};
// The id of the currently running request.
private requestId: string = '';
// The text status of the currently running request.
private runningStatus = '';
// The fetch in connectedCallback will fill this is with the defaults.
private state: Alert | null = null;
// The regressions detected from the dryrun.
private regressions: (RegressionAtCommit | null)[] = [];
private alertDialog: HTMLDialogElement | null = null;
private triageDialog: HTMLDialogElement | null = null;
private alertConfig: AlertConfigSk | null = null;
private runSpinner: SpinnerSk | null = null;
// The state of the cluster-summary2-sk dialog.
private dialogState: Partial<TriageStatusSkStartTriageEventDetails> | null = {
full_summary: null,
triage: undefined,
};
/** Is true if the previous Run has returned an error. */
private hasError: boolean = false;
constructor() {
super(ClusterLastNPageSk.template);
if (window.sk.perf.demo) {
this.domain.end = Math.floor(new Date(2020, 4, 1).valueOf() / 1000);
}
}
private static template = (ele: ClusterLastNPageSk) => html`
<dialog id="alert-config-dialog">
<alert-config-sk
.config=${ele.state}
.paramset=${ele.paramset}
.key_order=${window.sk.perf.key_order}
></alert-config-sk>
<div class="buttons">
<button @click=${ele.alertClose}>Cancel</button>
<button @click=${ele.alertAccept}>Accept</button>
</div>
</dialog>
<div class="controls">
<p>
Use this page to test out an Alert configuration. Configure the Alert by
pressing the button below.
</p>
<button @click=${ele.alertEdit}>
${ClusterLastNPageSk.configTitle(ele)}
</button>
<p>
You can optionally change the range of commits over which the Alert is run:
</p>
<details>
<summary>
Range
</summary>
<domain-picker-sk
id="range"
.state=${ele.domain}
force_request_type="dense"
></domain-picker-sk>
</details>
<p>
Once configured, you can run the Alert and see the regressions it
detects.
</p>
<div class="running">
<button
class="action"
?disabled=${!ele.state!.query || !!ele.requestId}
@click=${ele.run}
>
Run
</button>
<spinner-sk id=run-spinner></spinner-sk>
<pre class="messages ${ClusterLastNPageSk.classIfError(ele.hasError)}">${ele.runningStatus}</pre>
</div>
<div class="saving">
<p>
Once satisfied with the Alert you can save it to be run periodically.
</p>
<button
@click=${ele.writeAlert}
class="action"
?disabled=${!ele.state!.query}
>
${ClusterLastNPageSk.writeAlertTitle(ele)}
</button>
<spinner-sk ?active=${ele.writingAlert}></spinner-sk>
</div>
</div>
<hr />
<dialog id="triage-cluster-dialog" @open-keys=${ele.openKeys}>
<cluster-summary2-sk
.full_summary=${ele.dialogState!.full_summary}
.triage=${ele.dialogState!.triage}
.alert=${ele.state}
notriage
></cluster-summary2-sk>
<div class="buttons">
<button @click=${ele.triageClose}>Close</button>
</div>
</dialog>
${ClusterLastNPageSk.table(ele)}
`;
/** The classname to add to an element if an error has occurred. */
private static classIfError(hasError: boolean): string {
return hasError ? 'error' : '';
}
private static stepUpAt(dir: Direction) {
return dir === 'UP' || dir === 'BOTH';
}
private static stepDownAt(dir: Direction) {
return dir === 'DOWN' || dir === 'BOTH';
}
private static notBoth(dir: Direction) {
return dir !== 'BOTH';
}
private static tableHeader = (ele: ClusterLastNPageSk) => {
const ret = [html` <th></th> `];
if (ClusterLastNPageSk.stepDownAt(ele.state!.direction)) {
ret.push(html` <th>Low</th> `);
}
if (ClusterLastNPageSk.stepUpAt(ele.state!.direction)) {
ret.push(html` <th>High</th> `);
}
if (ClusterLastNPageSk.notBoth(ele.state!.direction)) {
ret.push(html` <th></th> `);
}
return ret;
};
private static fullSummary(frame: FrameResponse, summary: ClusterSummary) {
return {
frame,
summary,
};
}
private static low = (ele: ClusterLastNPageSk, reg: RegressionAtCommit) => {
if (!ClusterLastNPageSk.stepDownAt(ele.state!.direction)) {
return html``;
}
if (reg.regression!.low) {
return html`
<td class="cluster">
<triage-status-sk
.alert=${ele.state}
cluster_type="low"
.full_summary=${ClusterLastNPageSk.fullSummary(
reg.regression!.frame!,
reg.regression!.low,
)}
.triage=${reg.regression!.low_status}
></triage-status-sk>
</td>
`;
}
return html` <td class="cluster"></td> `;
};
private static high = (ele: ClusterLastNPageSk, reg: RegressionAtCommit) => {
if (!ClusterLastNPageSk.stepUpAt(ele.state!.direction)) {
return html``;
}
if (reg.regression!.high) {
return html`
<td class="cluster">
<triage-status-sk
.alert=${ele.state}
cluster_type="high"
.full_summary=${ClusterLastNPageSk.fullSummary(
reg.regression!.frame!,
reg.regression!.high,
)}
.triage=${reg.regression!.high_status}
></triage-status-sk>
</td>
`;
}
return html` <td class="cluster"></td> `;
};
private static filler = (ele: ClusterLastNPageSk) => {
if (ClusterLastNPageSk.notBoth(ele.state!.direction)) {
return html` <td></td> `;
}
return html``;
};
private static tableRows = (ele: ClusterLastNPageSk) => ele.regressions.map(
(reg) => html`
<tr>
<td class="fixed">
<commit-detail-sk .cid=${reg!.cid}></commit-detail-sk>
</td>
${ClusterLastNPageSk.low(ele, reg!)}
${ClusterLastNPageSk.high(ele, reg!)}
${ClusterLastNPageSk.filler(ele)}
</tr>
`,
);
private static configTitle = (ele: ClusterLastNPageSk) => {
// Original style regression detection is indicated by the empty string for
// backwards compatibility, so calculate a display value in that case.
let detection: string = ele.state!.step;
if (ele.state!.step === '') {
detection = 'original';
}
return html`
Algo: ${detection}/${ele.state!.algo} - Radius: ${ele.state!.radius} - Sparse:
${ele.state!.sparse} - Threshold: ${ele.state!.interesting}
`;
};
private static writeAlertTitle = (ele: ClusterLastNPageSk) => {
if (ele.state?.id_as_string === '-1') {
return 'Create Alert';
}
return 'Update Alert';
};
private static table = (ele: ClusterLastNPageSk) => {
if (ele.requestId && !ele.regressions.length) {
return html` No regressions found yet. `;
}
return html`
<table @start-triage=${ele.triageStart}>
<tr>
<th>Commit</th>
<th colspan="2">Regressions</th>
</tr>
<tr> ${ClusterLastNPageSk.tableHeader(ele)} </tr>
${ClusterLastNPageSk.tableRows(ele)}
</table>
`;
};
connectedCallback(): void {
super.connectedCallback();
const init = fetch('/_/initpage/')
.then(jsonOrThrow)
.then((json: FrameResponse) => {
this.paramset = json.dataframe!.paramset;
});
const alertNew = fetch('/_/alert/new')
.then(jsonOrThrow)
.then((json: Alert) => {
this.state = json;
});
Promise.all([init, alertNew])
.then(() => {
this._render();
this.alertDialog = this.querySelector('#alert-config-dialog');
this.triageDialog = this.querySelector('#triage-cluster-dialog');
dialogPolyfill.registerDialog(this.alertDialog!);
dialogPolyfill.registerDialog(this.triageDialog!);
this.alertConfig = this.querySelector('alert-config-sk');
this.runSpinner = this.querySelector('#run-spinner');
this.stateHasChanged = stateReflector(
() => (this.state as unknown) as HintableObject,
(state) => {
this.state = (state as unknown) as Alert;
this._render();
},
);
})
.catch(errorMessage);
}
// Call this anytime something in private state is changed. Will be replaced
// with the real function once stateReflector has been setup.
// eslint-disable-next-line @typescript-eslint/no-empty-function
private stateHasChanged = () => {};
private alertEdit() {
this.alertDialog!.showModal();
}
private writeAlert() {
this.writingAlert = true;
this._render();
// Post the config.
fetch('/_/alert/update', {
method: 'POST',
body: JSON.stringify(this.state),
headers: {
'Content-Type': 'application/json',
},
})
.then(jsonOrThrow)
.then((json: AlertUpdateResponse) => {
this.state!.id_as_string = json.IDAsString;
this.writingAlert = false;
this._render();
})
.catch((msg) => {
this.writingAlert = false;
this._render();
errorMessage(msg);
});
}
private alertClose() {
this.alertDialog!.close();
}
private alertAccept() {
this.alertDialog!.close();
this.state = this.alertConfig!.config;
this.stateHasChanged();
this._render();
}
private triageStart(e: CustomEvent<TriageStatusSkStartTriageEventDetails>) {
this.dialogState = e.detail;
this._render();
this.triageDialog!.show();
}
private triageClose() {
this.triageDialog!.close();
}
private openKeys(e: CustomEvent<ClusterSummary2SkOpenKeysEventDetail>) {
const query = {
keys: e.detail.shortcut,
begin: e.detail.begin,
end: e.detail.end,
xbaroffset: e.detail.xbar.offset,
};
window.open(`/e/?${fromObject(query)}`, '_blank');
}
private catch(msg: string) {
this.hasError = true;
this.requestId = '';
this.runningStatus = msg;
this._render();
if (msg) {
errorMessage(msg);
}
}
private async run() {
this.hasError = false;
if (this.requestId) {
errorMessage('There is a pending query already running.');
return;
}
this.domain = this.querySelector<DomainPickerSk>('#range')!.state;
const body: RegressionDetectionRequest = {
domain: {
n: this.domain.num_commits,
offset: 0,
end: new Date(this.domain.end * 1000).toISOString(),
},
alert: this.state!,
step: 0,
total_queries: 1,
};
try {
this.requestId = 'running';
const finalProg = await startRequest('/_/dryrun/start', body, 300, this.runSpinner!, (prog: progress.SerializedProgress) => {
if (prog.results) {
this.regressions = prog.results;
}
this.runningStatus = prog.messages.map(((msg) => `${msg.key}: ${msg.value}`)).join('\n');
this._render();
});
if (finalProg.status !== 'Finished') {
throw (new Error(messagesToErrorString(finalProg.messages)));
}
this.regressions = finalProg.results;
this.runningStatus = '';
} catch (error) {
this.catch(error);
} finally {
this.requestId = '';
this._render();
}
}
}
define('cluster-lastn-page-sk', ClusterLastNPageSk);