Generate a list of builders/test suites from the buildbot json
[WebKit-https.git] / Tools / TestResultServer / static-dashboards / loader.js
1 // Copyright (C) 2012 Google Inc. All rights reserved.
2 // Copyright (C) 2012 Zan Dobersek <zandobersek@gmail.com>
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //         * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //         * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //         * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 var loader = loader || {};
31
32 (function() {
33
34 var TEST_RESULTS_SERVER = 'http://test-results.appspot.com/';
35 var CHROMIUM_EXPECTATIONS_URL = 'http://svn.webkit.org/repository/webkit/trunk/LayoutTests/platform/chromium/TestExpectations';
36
37 function pathToBuilderResultsFile(builderName) {
38     return TEST_RESULTS_SERVER + 'testfile?builder=' + builderName +
39            '&master=' + builderMaster(builderName).name +
40            '&testtype=' + g_crossDashboardState.testType + '&name=';
41 }
42
43 loader.request = function(url, success, error, opt_isBinaryData)
44 {
45     var xhr = new XMLHttpRequest();
46     xhr.open('GET', url, true);
47     if (opt_isBinaryData)
48         xhr.overrideMimeType('text/plain; charset=x-user-defined');
49     xhr.onreadystatechange = function(e) {
50         if (xhr.readyState == 4) {
51             if (xhr.status == 200)
52                 success(xhr);
53             else
54                 error(xhr);
55         }
56     }
57     xhr.send();
58 }
59
60 loader.Loader = function()
61 {
62     this._loadingSteps = [
63         this._loadBuildersList,
64         this._loadResultsFiles,
65         this._loadExpectationsFiles,
66     ];
67 }
68
69 loader.Loader.prototype = {
70     load: function()
71     {
72         this._loadNext();
73     },
74     _loadNext: function()
75     {
76         var loadingStep = this._loadingSteps.shift();
77         if (!loadingStep) {
78             resourceLoadingComplete();
79             return;
80         }
81         loadingStep.apply(this);
82     },
83     _loadBuildersList: function()
84     {
85         loadBuildersList(g_crossDashboardState.group, g_crossDashboardState.testType);
86         initBuilders();
87         this._loadNext();
88     },
89     _loadResultsFiles: function()
90     {
91         parseParameters();
92
93         for (var builderName in g_builders)
94             this._loadResultsFileForBuilder(builderName);
95     },
96     _loadResultsFileForBuilder: function(builderName)
97     {
98         var resultsFilename;
99         if (isTreeMap())
100             resultsFilename = 'times_ms.json';
101         else if (g_crossDashboardState.showAllRuns)
102             resultsFilename = 'results.json';
103         else
104             resultsFilename = 'results-small.json';
105
106         var resultsFileLocation = pathToBuilderResultsFile(builderName) + resultsFilename;
107         loader.request(resultsFileLocation,
108                 partial(function(loader, builderName, xhr) {
109                     loader._handleResultsFileLoaded(builderName, xhr.responseText);
110                 }, this, builderName),
111                 partial(function(loader, builderName, xhr) {
112                     loader._handleResultsFileLoadError(builderName);
113                 }, this, builderName));
114     },
115     _handleResultsFileLoaded: function(builderName, fileData)
116     {
117         if (isTreeMap())
118             this._processTimesJSONData(builderName, fileData);
119         else
120             this._processResultsJSONData(builderName, fileData);
121
122         // We need this work-around for webkit.org/b/50589.
123         if (!g_resultsByBuilder[builderName]) {
124             this._handleResultsFileLoadError(builderName);
125             return;
126         }
127
128         this._handleResourceLoad();
129     },
130     _processTimesJSONData: function(builderName, fileData)
131     {
132         // FIXME: We should probably include the builderName in the JSON
133         // rather than relying on only loading one JSON file per page.
134         g_resultsByBuilder[builderName] = JSON.parse(fileData);
135     },
136     _processResultsJSONData: function(builderName, fileData)
137     {
138         var builds = JSON.parse(fileData);
139
140         var json_version = builds['version'];
141         for (var builderName in builds) {
142             if (builderName == 'version')
143                 continue;
144
145             // If a test suite stops being run on a given builder, we don't want to show it.
146             // Assume any builder without a run in two weeks for a given test suite isn't
147             // running that suite anymore.
148             // FIXME: Grab which bots run which tests directly from the buildbot JSON instead.
149             var lastRunSeconds = builds[builderName].secondsSinceEpoch[0];
150             if ((Date.now() / 1000) - lastRunSeconds > ONE_WEEK_SECONDS)
151                 continue;
152
153             if ((Date.now() / 1000) - lastRunSeconds > ONE_DAY_SECONDS)
154                 g_staleBuilders.push(builderName);
155
156             if (json_version >= 4)
157                 builds[builderName][TESTS_KEY] = flattenTrie(builds[builderName][TESTS_KEY]);
158             g_resultsByBuilder[builderName] = builds[builderName];
159         }
160     },
161     _handleResultsFileLoadError: function(builderName)
162     {
163         var error = 'Failed to load results file for ' + builderName + '.';
164
165         if (isLayoutTestResults()) {
166             console.error(error);
167             g_buildersThatFailedToLoad.push(builderName);
168         } else {
169             // Avoid to show error/warning messages for non-layout tests. We may be
170             // checking the builders that are not running the tests.
171             console.info('info:' + error);
172         }
173
174         // Remove this builder from builders, so we don't try to use the
175         // data that isn't there.
176         delete g_builders[builderName];
177
178         // Change the default builder name if it has been deleted.
179         if (g_defaultBuilderName == builderName) {
180             g_defaultBuilderName = null;
181             for (var availableBuilderName in g_builders) {
182                 g_defaultBuilderName = availableBuilderName;
183                 g_defaultDashboardSpecificStateValues.builder = availableBuilderName;
184                 break;
185             }
186             if (!g_defaultBuilderName) {
187                 var error = 'No tests results found for ' + g_crossDashboardState.testType + '. Reload the page to try fetching it again.';
188                 console.error(error);
189                 addError(error);
190             }
191         }
192
193         // Proceed as if the resource had loaded.
194         this._handleResourceLoad();
195     },
196     _handleResourceLoad: function()
197     {
198         if (this._haveResultsFilesLoaded())
199             this._loadNext();
200     },
201     _haveResultsFilesLoaded: function()
202     {
203         for (var builder in g_builders) {
204             if (!g_resultsByBuilder[builder])
205                 return false;
206         }
207         return true;
208     },
209     _loadExpectationsFiles: function()
210     {
211         if (!isFlakinessDashboard() && !g_crossDashboardState.useTestData) {
212             this._loadNext();
213             return;
214         }
215
216         var expectationsFilesToRequest = {};
217         traversePlatformsTree(function(platform, platformName) {
218             if (platform.fallbackPlatforms)
219                 platform.fallbackPlatforms.forEach(function(fallbackPlatform) {
220                     var fallbackPlatformObject = platformObjectForName(fallbackPlatform);
221                     if (fallbackPlatformObject.expectationsDirectory && !(fallbackPlatform in expectationsFilesToRequest))
222                         expectationsFilesToRequest[fallbackPlatform] = EXPECTATIONS_URL_BASE_PATH + fallbackPlatformObject.expectationsDirectory + '/TestExpectations';
223                 });
224
225             if (platform.expectationsDirectory)
226                 expectationsFilesToRequest[platformName] = EXPECTATIONS_URL_BASE_PATH + platform.expectationsDirectory + '/TestExpectations';
227         });
228
229         for (platformWithExpectations in expectationsFilesToRequest)
230             loader.request(expectationsFilesToRequest[platformWithExpectations],
231                     partial(function(loader, platformName, xhr) {
232                         g_expectationsByPlatform[platformName] = getParsedExpectations(xhr.responseText);
233
234                         delete expectationsFilesToRequest[platformName];
235                         if (!Object.keys(expectationsFilesToRequest).length)
236                             loader._loadNext();
237                     }, this, platformWithExpectations),
238                     partial(function(platformName, xhr) {
239                         console.error('Could not load expectations file for ' + platformName);
240                     }, platformWithExpectations));
241     }
242 }
243
244 })();