[perf dashboard] Make test fressness head and indicator perfect centered
[WebKit.git] / Websites / perf.webkit.org / public / v3 / pages / test-freshness-page.js
1
2 class TestFreshnessPage extends PageWithHeading {
3     constructor(summaryPageConfiguration, testAgeToleranceInHours)
4     {
5         super('test-freshness', null);
6         this._testAgeTolerance = (testAgeToleranceInHours || 24) * 3600 * 1000;
7         this._timeDuration = this._testAgeTolerance * 2;
8         this._excludedConfigurations = {};
9         this._lastDataPointByConfiguration = null;
10         this._indicatorByConfiguration = null;
11         this._renderTableLazily = new LazilyEvaluatedFunction(this._renderTable.bind(this));
12         this._currentlyHighlightedIndicator = null;
13         this._hoveringTooltip = false;
14         this._builderByIndicator = null;
15         this._renderTooltipLazily = new LazilyEvaluatedFunction(this._renderTooltip.bind(this));
16
17         this._loadConfig(summaryPageConfiguration);
18     }
19
20     didConstructShadowTree()
21     {
22         const tooltipTable = this.content('tooltip-table');
23         tooltipTable.addEventListener('mouseenter', () => {
24             this._hoveringTooltip = true;
25             this.enqueueToRender();
26         });
27         tooltipTable.addEventListener('mouseleave', () => {
28             this._hoveringTooltip = false;
29             this.enqueueToRender();
30         });
31     }
32
33     name() { return 'Test-Freshness'; }
34
35     _loadConfig(summaryPageConfiguration)
36     {
37         const platformIdSet = new Set;
38         const metricIdSet = new Set;
39
40         for (const config of summaryPageConfiguration) {
41             for (const platformGroup of config.platformGroups) {
42                 for (const platformId of platformGroup.platforms)
43                     platformIdSet.add(platformId);
44             }
45
46             for (const metricGroup of config.metricGroups) {
47                 for (const subgroup of metricGroup.subgroups) {
48                     for (const metricId of subgroup.metrics)
49                         metricIdSet.add(metricId);
50                 }
51             }
52
53             const excludedConfigs = config.excludedConfigurations;
54             for (const platform in excludedConfigs) {
55                 if (platform in this._excludedConfigurations)
56                     this._excludedConfigurations[platform] = this._excludedConfigurations[platform].concat(excludedConfigs[platform]);
57                 else
58                     this._excludedConfigurations[platform] = excludedConfigs[platform];
59             }
60         }
61         this._platforms = [...platformIdSet].map((platformId) => Platform.findById(platformId));
62         this._metrics = [...metricIdSet].map((metricId) => Metric.findById(metricId));
63     }
64
65     open(state)
66     {
67         this._fetchTestResults();
68         super.open(state);
69     }
70
71     _fetchTestResults()
72     {
73         this._measurementSetFetchTime = Date.now();
74         this._lastDataPointByConfiguration = new Map;
75         this._builderByIndicator = new Map;
76
77         const startTime = this._measurementSetFetchTime - this._timeDuration;
78
79         for (const platform of this._platforms) {
80             const lastDataPointByMetric = new Map;
81             this._lastDataPointByConfiguration.set(platform, lastDataPointByMetric);
82
83             for (const metric of this._metrics) {
84                 if (!this._isValidPlatformMetricCombination(platform, metric, this._excludedConfigurations))
85                     continue;
86
87                 const measurementSet = MeasurementSet.findSet(platform.id(), metric.id(), platform.lastModified(metric));
88                 measurementSet.fetchBetween(startTime, this._measurementSetFetchTime).then(() => {
89                     const currentTimeSeries = measurementSet.fetchedTimeSeries('current', false, false);
90
91                     let timeForLatestBuild = startTime;
92                     let lastBuild = null;
93                     let builder = null;
94                     let commitSetOfLastPoint = null;
95                     const lastPoint = currentTimeSeries.lastPoint();
96                     if (lastPoint) {
97                         timeForLatestBuild = lastPoint.build().buildTime().getTime();
98                         const view = currentTimeSeries.viewBetweenPoints(currentTimeSeries.firstPoint(), lastPoint);
99                         for (const point of view) {
100                             const build = point.build();
101                             if (!build)
102                                 continue;
103                             if (build.buildTime().getTime() >= timeForLatestBuild) {
104                                 timeForLatestBuild = build.buildTime().getTime();
105                                 lastBuild = build;
106                                 builder = build.builder();
107                             }
108                         }
109                         commitSetOfLastPoint = lastPoint.commitSet();
110                     }
111
112                     lastDataPointByMetric.set(metric, {time: timeForLatestBuild, hasCurrentDataPoint: !!lastPoint,
113                         lastBuild, builder, commitSetOfLastPoint});
114                     this.enqueueToRender();
115                 });
116             }
117         }
118     }
119
120     render()
121     {
122         super.render();
123
124         this._renderTableLazily.evaluate(this._platforms, this._metrics);
125
126         let buildSummaryForCurrentlyHighlightedIndicator = null;
127         let buildForCurrentlyHighlightedIndicator = null;
128         let commitSetForCurrentHighlightedIndicator = null;
129         const builderForCurrentlyHighlightedIndicator = this._currentlyHighlightedIndicator ? this._builderByIndicator.get(this._currentlyHighlightedIndicator) : null;
130         for (const [platform, lastDataPointByMetric] of this._lastDataPointByConfiguration.entries()) {
131             for (const [metric, lastDataPoint] of lastDataPointByMetric.entries()) {
132                 const timeDuration = this._measurementSetFetchTime - lastDataPoint.time;
133                 const timeDurationSummaryPrefix = lastDataPoint.hasCurrentDataPoint ? '' : 'More than ';
134                 const timeDurationSummary = BuildRequest.formatTimeInterval(timeDuration);
135                 const summary = `${timeDurationSummaryPrefix}${timeDurationSummary} since latest data point.`;
136                 const url = this._router.url('charts', ChartsPage.createStateForDashboardItem(platform.id(), metric.id(),
137                     this._measurementSetFetchTime - this._timeDuration));
138
139                 const indicator = this._indicatorByConfiguration.get(platform).get(metric);
140                 if (this._currentlyHighlightedIndicator && this._currentlyHighlightedIndicator === indicator) {
141                     buildSummaryForCurrentlyHighlightedIndicator = summary;
142                     buildForCurrentlyHighlightedIndicator = lastDataPoint.lastBuild;
143                     commitSetForCurrentHighlightedIndicator = lastDataPoint.commitSetOfLastPoint;
144                 }
145                 this._builderByIndicator.set(indicator, lastDataPoint.builder);
146                 indicator.update(timeDuration, this._testAgeTolerance, url, builderForCurrentlyHighlightedIndicator && builderForCurrentlyHighlightedIndicator === lastDataPoint.builder);
147             }
148         }
149         this._renderTooltipLazily.evaluate(this._currentlyHighlightedIndicator, this._hoveringTooltip, buildSummaryForCurrentlyHighlightedIndicator, buildForCurrentlyHighlightedIndicator, commitSetForCurrentHighlightedIndicator);
150     }
151
152     _renderTooltip(indicator, hoveringTooltip, buildSummary, build, commitSet)
153     {
154         if (!indicator || !buildSummary) {
155             this.content('tooltip-table').style.display = hoveringTooltip ? null : 'none';
156             return;
157         }
158         const element = ComponentBase.createElement;
159         const link = ComponentBase.createLink;
160
161         const rect = indicator.element().getBoundingClientRect();
162         const tooltipTable = this.content('tooltip-table');
163         tooltipTable.style.display = null;
164
165         const tooltipContainerComputedStyle = getComputedStyle(tooltipTable);
166         const containerMarginTop = parseFloat(tooltipContainerComputedStyle.paddingTop);
167         const containerMarginLeft = parseFloat(tooltipContainerComputedStyle.marginLeft);
168
169         tooltipTable.style.position = 'absolute';
170         tooltipTable.style.top = rect.top - (tooltipTable.offsetHeight + containerMarginTop)  + 'px';
171         tooltipTable.style.left = rect.left + rect.width / 2 - tooltipTable.offsetWidth / 2 + containerMarginLeft + 'px';
172
173         let tableContent = [element('tr', element('td', {colspan: 2}, buildSummary))];
174
175         if (commitSet) {
176             if (commitSet.repositories().length)
177                 tableContent.push(element('tr', element('th', {colspan: 2}, 'Latest build information')));
178
179             tableContent.push(Repository.sortByNamePreferringOnesWithURL(commitSet.repositories()).map((repository) => {
180                 const commit = commitSet.commitForRepository(repository);
181                 return element('tr', [
182                     element('td', repository.name()),
183                     element('td', commit.url() ? link(commit.label(), commit.label(), commit.url(), true) : commit.label())
184                 ]);
185             }));
186         }
187
188         if (build) {
189             const url = build.url();
190             const buildNumber = build.buildNumber();
191             tableContent.push(element('tr', [
192                 element('td', 'Build'),
193                 element('td', {colspan: 2}, [
194                     url ? link(buildNumber, build.label(), url, true) : buildNumber
195                 ]),
196             ]));
197         }
198
199         this.renderReplace(tooltipTable,  tableContent);
200     }
201
202     _renderTable(platforms, metrics)
203     {
204         const element = ComponentBase.createElement;
205         const tableBodyElement = [];
206         const tableHeadElements = [element('th',  {class: 'table-corner row-head'}, 'Platform \\ Test')];
207
208         for (const metric of metrics)
209             tableHeadElements.push(element('th', {class: 'diagonal-head'}, element('div', metric.test().fullName())));
210
211         this._indicatorByConfiguration = new Map;
212         for (const platform of platforms) {
213             const indicatorByMetric = new Map;
214             this._indicatorByConfiguration.set(platform, indicatorByMetric);
215             tableBodyElement.push(element('tr',
216                 [element('th', {class: 'row-head'}, platform.label()), ...metrics.map((metric) => this._constructTableCell(platform, metric, indicatorByMetric))]));
217         }
218
219         this.renderReplace(this.content('test-health'), [element('thead', tableHeadElements), element('tbody', tableBodyElement)]);
220     }
221
222     _isValidPlatformMetricCombination(platform, metric)
223     {
224         return !(this._excludedConfigurations && this._excludedConfigurations[platform.id()]
225             && this._excludedConfigurations[platform.id()].some((metricId) => metricId == metric.id()))
226             && platform.hasMetric(metric);
227     }
228
229     _constructTableCell(platform, metric, indicatorByMetric)
230     {
231         const element = ComponentBase.createElement;
232
233         if (!this._isValidPlatformMetricCombination(platform, metric))
234             return element('td', {class: 'blank-cell'}, element('div'));
235
236         const indicator = new FreshnessIndicator;
237         indicator.listenToAction('select', (originator) => {
238             this._currentlyHighlightedIndicator = originator;
239             this.enqueueToRender();
240         });
241         indicator.listenToAction('unselect', () => {
242             this._currentlyHighlightedIndicator = null;
243             this.enqueueToRender();
244         });
245         indicatorByMetric.set(metric, indicator);
246         return element('td', {class: 'status-cell'}, indicator);
247     }
248
249     static htmlTemplate()
250     {
251         return `<section class="page-with-heading"><table id="tooltip-table"></table><table id="test-health"></table></section>`;
252     }
253
254     static cssTemplate()
255     {
256         return `
257             .page-with-heading {
258                 display: flex;
259                 justify-content: center;
260             }
261             #test-health {
262                 font-size: 1rem;
263             }
264             #test-health thead {
265                 display: block;
266                 align: right;
267             }
268             #test-health th.table-corner {
269                 text-align: right;
270                 vertical-align: bottom;
271             }
272             #test-health .row-head {
273                 min-width: 15.5rem;
274             }
275             #test-health th {
276                 text-align: left;
277                 border-bottom: 0.1rem solid #ccc;
278                 font-weight: normal;
279             }
280             #test-health th.diagonal-head {
281                 white-space: nowrap;
282                 height: 16rem;
283                 width: 2.2rem;
284                 border-bottom: 0rem;
285                 padding: 0;
286             }
287             #test-health th.diagonal-head > div {
288                 transform: translate(1.1rem, 7.5rem) rotate(315deg);
289                 transform-origin: center left;
290                 width: 2.2rem;
291                 border: 0rem;
292             }
293             #test-health tbody {
294                 display: block;
295                 overflow: auto;
296                 height: calc(100vh - 24rem);
297             }
298             #test-health td.status-cell {
299                 margin: 0;
300                 padding: 0;
301                 max-width: 2.2rem;
302                 max-height: 2.2rem;
303                 min-width: 2.2rem;
304                 min-height: 2.2rem;
305             }
306             #test-health td.blank-cell {
307                 margin: 0;
308                 padding: 0;
309                 max-width: 2.2rem;
310                 max-height: 2.2rem;
311                 min-width: 2.2rem;
312                 min-height: 2.2rem;
313             }
314             #test-health td.blank-cell > div  {
315                 background-color: #F9F9F9;
316                 height: 1.6rem;
317                 width: 1.6rem;
318                 margin: auto;
319                 padding: 0;
320                 position: relative;
321                 overflow: hidden;
322             }
323             #test-health td.blank-cell > div::before {
324                 content: "";
325                 position: absolute;
326                 top: -1px;
327                 left: -1px;
328                 display: block;
329                 width: 0px;
330                 height: 0px;
331                 border-right: calc(1.6rem + 1px) solid #ddd;
332                 border-top: calc(1.6rem + 1px) solid transparent;
333             }
334             #test-health td.blank-cell > div::after {
335                 content: "";
336                 display: block;
337                 position: absolute;
338                 top: 1px;
339                 left: 1px;
340                 width: 0px;
341                 height: 0px;
342                 border-right: calc(1.6rem - 1px) solid #F9F9F9;
343                 border-top: calc(1.6rem - 1px) solid transparent;
344             }
345             #tooltip-table {
346                 width: 22rem;
347                 background-color: #34495E;
348                 opacity: 0.9;
349                 margin: 0.3rem;
350                 padding: 0.3rem;
351                 border-radius: 0.4rem;
352                 z-index: 1;
353                 text-align: center;
354                 display: inline-table;
355                 color: white;
356             }
357             #tooltip-table td {
358                 overflow: hidden;
359                 max-width: 22rem;
360                 text-overflow: ellipsis;
361             }
362             #tooltip-table::after {
363                 content: " ";
364                 position: absolute;
365                 top: 100%;
366                 left: 50%;
367                 margin-left: -1rem;
368                 border-width: 0.2rem;
369                 border-style: solid;
370                 border-color: #34495E transparent transparent transparent;
371             }
372             #tooltip-table a {
373                 color: #B03A2E;
374                 font-weight: bold;
375             }
376         `;
377     }
378
379     routeName() { return 'test-freshness'; }
380 }