Refactor to add event listeners and start periodic updates for all trac instances.
[WebKit-https.git] / Tools / BuildSlaveSupport / build.webkit.org-config / public_html / dashboard / Scripts / Main.js
1 /*
2  * Copyright (C) 2013, 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 var hasBubbles = typeof bubbleQueueServer != "undefined";
27 var BubblesCategory = "bubbles";
28
29 var categorizedQueuesByPlatformAndBuildType = {};
30 var platformsByFamily = {};
31
32 for (var i = 0; i < buildbots.length; ++i) {
33     var buildbot = buildbots[i];
34     for (var id in buildbot.queuesInfo) {
35         if (buildbot.queuesInfo[id].combinedQueues) {
36             var info = buildbot.queuesInfo[id];
37             var queue = {
38                 id: id,
39                 branches: info.branches,
40                 platform: info.platform.name,
41                 heading: info.heading,
42                 builder: info.builder,
43                 combinedQueues: Object.keys(info.combinedQueues).map(function(combinedQueueID) { return buildbot.queues[combinedQueueID]; }),
44             };
45         } else
46             var queue = buildbot.queues[id];
47
48         var platformName = queue.platform;
49         var platform = categorizedQueuesByPlatformAndBuildType[platformName];
50         if (!platform)
51             platform = categorizedQueuesByPlatformAndBuildType[platformName] = {};
52         if (!platform.builders)
53             platform.builders = [];
54
55         var categoryName;
56         if ("combinedQueues" in queue)
57             if (queue.builder)
58                 categoryName = "builderCombinedQueues";
59             else
60                 categoryName = "otherCombinedQueues"
61         else if (queue.builder)
62             categoryName = "builders";
63         else if (queue.tester)
64             categoryName = queue.testCategory;
65         else if (queue.performance)
66             categoryName = "performance";
67         else if (queue.leaks)
68             categoryName = "leaks";
69         else if (queue.staticAnalyzer)
70             categoryName = "staticAnalyzer";
71         else {
72             console.assert("Unknown queue type.");
73             continue;
74         }
75
76         category = platform[categoryName];
77         if (!category)
78             category = platform[categoryName] = [];
79
80         category.push(queue);
81     }
82 }
83
84 if (hasBubbles) {
85     for (var id in bubbleQueueServer.queues) {
86         var queue = bubbleQueueServer.queues[id];
87         var platform = categorizedQueuesByPlatformAndBuildType[queue.platform];
88         if (!platform)
89             platform = categorizedQueuesByPlatformAndBuildType[queue.platform] = {};
90         if (!platform.builders)
91             platform.builders = [];
92
93         var categoryName = BubblesCategory;
94
95         platformQueues = platform[categoryName];
96         if (!platformQueues)
97             platformQueues = platform[categoryName] = [];
98
99         platformQueues.push(queue);
100     }
101 }
102
103 var testNames = {};
104 testNames[Buildbot.TestCategory.WebKit2] = "WK2 Tests";
105 testNames[Buildbot.TestCategory.WebKit1] = "WK1 Tests";
106
107 function initPlatformsByFamily()
108 {
109     var platforms = Dashboard.sortedPlatforms;
110     for (var i in platforms) {
111         // Make sure the platform will be displayed on the page before considering its platform family.
112         if (!categorizedQueuesByPlatformAndBuildType[platforms[i].name])
113             continue;
114
115         var platformFamily = settings.parsePlatformFamily(platforms[i].name);
116         if (platformsByFamily[platformFamily])
117             platformsByFamily[platformFamily].push(platforms[i].name)
118         else
119             platformsByFamily[platformFamily] = [platforms[i].name]
120     }
121 }
122
123 function updateHiddenPlatforms()
124 {
125     var hiddenPlatformFamilies = settings.getObject("hiddenPlatformFamilies") || [];
126     var platformRows = document.querySelectorAll("tr.platform");
127     for (var i = 0; i < platformRows.length; ++i)
128         platformRows[i].classList.remove("hidden");
129
130     for (var i = 0; i < hiddenPlatformFamilies.length; ++i) {
131         var platformFamily = hiddenPlatformFamilies[i];
132         for (var j = 0; j < platformsByFamily[platformFamily].length; ++j) {
133             var name = platformsByFamily[platformFamily][j];
134             var platformRow = document.querySelector("tr.platform." + name);
135             if (platformRow)
136                 platformRow.classList.add("hidden");
137         }
138     }
139     settings.updateToggleButtons();
140 }
141
142 function applyAccessibilityColorSetting()
143 {
144     var useAccessibleColors = settings.getObject("accessibilityColorsEnabled");
145     var toggleAccessibilityColorButton = document.getElementById("accessibilityButton");
146     if (useAccessibleColors) {
147         toggleAccessibilityColorButton.textContent = "disable accessibility colors";
148         document.body.classList.toggle("accessibility-colors");
149     } else
150         toggleAccessibilityColorButton.textContent = "enable accessibility colors";
151 }
152
153 function toggleAccessibilityColors()
154 {
155     var isCurrentlyActivated = settings.getObject("accessibilityColorsEnabled");
156     if (isCurrentlyActivated === undefined)
157         isCurrentlyActivated = false;
158     
159     settings.setObject("accessibilityColorsEnabled", !isCurrentlyActivated);
160     document.body.classList.toggle("accessibility-colors");
161     var toggleAccessibilityColorButton = document.getElementById("accessibilityButton");
162     if (!isCurrentlyActivated)
163         toggleAccessibilityColorButton.textContent = "disable accessibility colors";
164     else
165         toggleAccessibilityColorButton.textContent = "enable accessibility colors";
166 }
167
168 function documentReady()
169 {
170     var table = document.createElement("table");
171     table.classList.add("queue-grid");
172
173     var row = document.createElement("tr");
174     row.classList.add("headers");
175
176     var header = document.createElement("th"); 
177     row.appendChild(header);
178
179     header = document.createElement("th");
180     header.textContent = "Builders";
181     row.appendChild(header);
182
183     for (var testerKey in Buildbot.TestCategory) {
184         var header = document.createElement("th");
185         header.textContent = testNames[Buildbot.TestCategory[testerKey]];
186         row.appendChild(header);
187     }
188
189     var header = document.createElement("th");
190     header.textContent = "Other";
191     row.appendChild(header);
192
193     table.appendChild(row);
194
195     var platforms = Dashboard.sortedPlatforms;
196
197     for (var i in platforms) {
198         var platform = platforms[i];
199         var platformQueues = categorizedQueuesByPlatformAndBuildType[platform.name];
200         if (!platformQueues)
201             continue;
202
203         var row = document.createElement("tr");
204         row.classList.add("platform");
205         row.classList.add(platform.name);
206
207         var cell = document.createElement("td");
208         cell.classList.add("logo");
209
210         var logoImage = document.createElement("img");
211         logoImage.classList.add("logo");
212         cell.appendChild(logoImage);
213
214         var ringImage = document.createElement("img");
215         ringImage.classList.add("ring");
216         ringImage.title = platform.readableName;
217         cell.appendChild(ringImage);
218
219         row.appendChild(cell);
220
221         cell = document.createElement("td");
222         var view = new BuildbotBuilderQueueView(platformQueues.builders);
223         cell.appendChild(view.element);
224         row.appendChild(cell);
225
226         if ("builderCombinedQueues" in platformQueues) {
227             for (var i = 0; i < platformQueues.builderCombinedQueues.length; ++i) {
228                 var view = new BuildbotCombinedQueueView(platformQueues.builderCombinedQueues[i]);
229                 cell.appendChild(view.element);
230             }
231         }
232
233         for (var testerKey in Buildbot.TestCategory) {
234             var cell = document.createElement("td");
235
236             var testerProperty = Buildbot.TestCategory[testerKey];
237             if (platformQueues[testerProperty]) {
238                 var view = new BuildbotTesterQueueView(platformQueues[testerProperty]);
239                 cell.appendChild(view.element);
240             }
241
242             row.appendChild(cell);
243         }
244
245         var cell = document.createElement("td");
246         if (platformQueues.performance) {
247             var view = new BuildbotPerformanceQueueView(platformQueues.performance);
248             cell.appendChild(view.element);
249         }
250
251         if (platformQueues.staticAnalyzer) {
252             var view = new BuildbotStaticAnalyzerQueueView(platformQueues.staticAnalyzer);
253             cell.appendChild(view.element);
254         }
255
256         if (platformQueues.leaks) {
257             var view = new BuildbotLeaksQueueView(platformQueues.leaks);
258             cell.appendChild(view.element);
259         }
260
261         if (platformQueues[BubblesCategory]) {
262             var view = new BubbleQueueView(platformQueues[BubblesCategory]);
263             cell.appendChild(view.element);
264         }
265
266         if ("otherCombinedQueues" in platformQueues) {
267             for (var i = 0; i < platformQueues.otherCombinedQueues.length; ++i) {
268                 var view = new BuildbotCombinedQueueView(platformQueues.otherCombinedQueues[i]);
269                 cell.appendChild(view.element);
270             }
271         }
272
273         row.appendChild(cell);
274
275         table.appendChild(row);
276     }
277
278     document.body.appendChild(table);
279
280     if (settings.available()) {
281         var settingsButton = document.createElement("div");
282         settingsButton.addEventListener("click", function () { settings.toggleSettingsDisplay(); });
283         settingsButton.classList.add("settings");
284         document.body.appendChild(settingsButton);
285
286         var settingsWrapper = document.createElement("div");
287         settingsWrapper.classList.add("unhide", "hidden", "settingsWrapper")
288
289         var platformFamilyToggleWrapper = document.createElement("div");
290         platformFamilyToggleWrapper.classList.add("unhide", "hidden", "familyToggleWrapper");
291
292         var unhideAllButton = document.createElement("div");
293         unhideAllButton.addEventListener("click", function () { settings.clearHiddenPlatformFamilies(); });
294         unhideAllButton.classList.add("unhide", "hidden", "platformFamilyToggleButton");
295         unhideAllButton.setAttribute("id", "all-platformFamilyToggleButton");
296         unhideAllButton.textContent = "all";
297         platformFamilyToggleWrapper.appendChild(unhideAllButton);
298
299         initPlatformsByFamily();
300         for (var platformFamily in platformsByFamily) {
301             var platformFamilyToggle = document.createElement("div");
302             platformFamilyToggle.addEventListener("click", function () {
303                 settings.toggleHiddenPlatformFamily(this.toString());
304             }.bind(platformFamily));
305             platformFamilyToggle.classList.add("unhide", "hidden", "platformFamilyToggleButton");
306             platformFamilyToggle.setAttribute("id", platformFamily + "-platformFamilyToggleButton");
307             platformFamilyToggle.textContent = platformFamily;
308             platformFamilyToggleWrapper.appendChild(platformFamilyToggle);
309         }
310         settingsWrapper.appendChild(platformFamilyToggleWrapper);
311
312         var toggleAccessibilityColorButton = document.createElement("div");
313         toggleAccessibilityColorButton.addEventListener("click", function() { toggleAccessibilityColors(); });
314         toggleAccessibilityColorButton.classList.add("unhide", "hidden", "accessibilityButton");
315         toggleAccessibilityColorButton.setAttribute("id", "accessibilityButton");
316         toggleAccessibilityColorButton.textContent = "enable accessibility colors";
317         settingsWrapper.appendChild(toggleAccessibilityColorButton);
318         document.body.appendChild(settingsWrapper);
319         applyAccessibilityColorSetting();
320
321         updateHiddenPlatforms();
322         settings.addSettingListener("hiddenPlatformFamilies", updateHiddenPlatforms);
323     }
324 }
325
326 var sortedRepositories = Dashboard.sortedRepositories;
327 for (var i = 0; i < sortedRepositories.length; ++i) {
328     var trac = sortedRepositories[i].trac;
329     if (typeof trac !== "undefined")
330         trac.startPeriodicUpdates();
331 }
332
333 document.addEventListener("DOMContentLoaded", documentReady);