Dashboard Cleanup: Add isLoadingComplete to the loader.Loader object.
[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     this._buildersThatFailedToLoad = [];
69     this._staleBuilders = [];
70     this._loadingComplete = false;
71 }
72
73 loader.Loader.prototype = {
74     load: function()
75     {
76         this._loadNext();
77     },
78     isLoadingComplete: function()
79     {
80         return this._loadingComplete;
81     },
82     _loadNext: function()
83     {
84         var loadingStep = this._loadingSteps.shift();
85         if (!loadingStep) {
86             this._loadingComplete = true;
87             // FIXME(jparent): Loader should not know about global
88             // functions, should use a callback or dispatch load
89             // event instead.
90             resourceLoadingComplete(this._getLoadingErrorMessages());
91             return;
92         }
93         loadingStep.apply(this);
94     },
95     _loadBuildersList: function()
96     {
97         loadBuildersList(g_crossDashboardState.group, g_crossDashboardState.testType);
98         this._loadNext();
99     },
100     _loadResultsFiles: function()
101     {
102         parseParameters();
103
104         for (var builderName in currentBuilders())
105             this._loadResultsFileForBuilder(builderName);
106     },
107     _loadResultsFileForBuilder: function(builderName)
108     {
109         var resultsFilename;
110         if (isTreeMap())
111             resultsFilename = 'times_ms.json';
112         else if (g_crossDashboardState.showAllRuns)
113             resultsFilename = 'results.json';
114         else
115             resultsFilename = 'results-small.json';
116
117         var resultsFileLocation = pathToBuilderResultsFile(builderName) + resultsFilename;
118         loader.request(resultsFileLocation,
119                 partial(function(loader, builderName, xhr) {
120                     loader._handleResultsFileLoaded(builderName, xhr.responseText);
121                 }, this, builderName),
122                 partial(function(loader, builderName, xhr) {
123                     loader._handleResultsFileLoadError(builderName);
124                 }, this, builderName));
125     },
126     _handleResultsFileLoaded: function(builderName, fileData)
127     {
128         if (isTreeMap())
129             this._processTimesJSONData(builderName, fileData);
130         else
131             this._processResultsJSONData(builderName, fileData);
132
133         // We need this work-around for webkit.org/b/50589.
134         if (!g_resultsByBuilder[builderName]) {
135             this._handleResultsFileLoadError(builderName);
136             return;
137         }
138
139         this._handleResourceLoad();
140     },
141     _processTimesJSONData: function(builderName, fileData)
142     {
143         // FIXME: We should probably include the builderName in the JSON
144         // rather than relying on only loading one JSON file per page.
145         g_resultsByBuilder[builderName] = JSON.parse(fileData);
146     },
147     _processResultsJSONData: function(builderName, fileData)
148     {
149         var builds = JSON.parse(fileData);
150
151         var json_version = builds['version'];
152         for (var builderName in builds) {
153             if (builderName == 'version')
154                 continue;
155
156             // If a test suite stops being run on a given builder, we don't want to show it.
157             // Assume any builder without a run in two weeks for a given test suite isn't
158             // running that suite anymore.
159             // FIXME: Grab which bots run which tests directly from the buildbot JSON instead.
160             var lastRunSeconds = builds[builderName].secondsSinceEpoch[0];
161             if ((Date.now() / 1000) - lastRunSeconds > ONE_WEEK_SECONDS)
162                 continue;
163
164             if ((Date.now() / 1000) - lastRunSeconds > ONE_DAY_SECONDS)
165                 this._staleBuilders.push(builderName);
166
167             if (json_version >= 4)
168                 builds[builderName][TESTS_KEY] = flattenTrie(builds[builderName][TESTS_KEY]);
169             g_resultsByBuilder[builderName] = builds[builderName];
170         }
171     },
172     _handleResultsFileLoadError: function(builderName)
173     {
174         console.error('Failed to load results file for ' + builderName + '.');
175
176         // FIXME: loader shouldn't depend on state defined in dashboard_base.js.
177         this._buildersThatFailedToLoad.push(builderName);
178
179         // Remove this builder from builders, so we don't try to use the
180         // data that isn't there.
181         delete currentBuilders()[builderName];
182
183         // Change the default builder name if it has been deleted.
184         if (g_defaultDashboardSpecificStateValues.builder == builderName) {
185             var defaultBuilderName = currentBuilderGroup().defaultBuilder();
186             g_defaultDashboardSpecificStateValues.builder = defaultBuilderName;
187             if (!defaultBuilderName) {
188                 var error = 'No tests results found for ' + g_crossDashboardState.testType + '. Reload the page to try fetching it again.';
189                 console.error(error);
190                 addError(error);
191             }
192        }
193
194         // Proceed as if the resource had loaded.
195         this._handleResourceLoad();
196     },
197     _handleResourceLoad: function()
198     {
199         if (this._haveResultsFilesLoaded())
200             this._loadNext();
201     },
202     _haveResultsFilesLoaded: function()
203     {
204         for (var builder in currentBuilders()) {
205             if (!g_resultsByBuilder[builder])
206                 return false;
207         }
208         return true;
209     },
210     _loadExpectationsFiles: function()
211     {
212         if (!isFlakinessDashboard() && !g_crossDashboardState.useTestData) {
213             this._loadNext();
214             return;
215         }
216
217         var expectationsFilesToRequest = {};
218         traversePlatformsTree(function(platform, platformName) {
219             if (platform.fallbackPlatforms)
220                 platform.fallbackPlatforms.forEach(function(fallbackPlatform) {
221                     var fallbackPlatformObject = platformObjectForName(fallbackPlatform);
222                     if (fallbackPlatformObject.expectationsDirectory && !(fallbackPlatform in expectationsFilesToRequest))
223                         expectationsFilesToRequest[fallbackPlatform] = EXPECTATIONS_URL_BASE_PATH + fallbackPlatformObject.expectationsDirectory + '/TestExpectations';
224                 });
225
226             if (platform.expectationsDirectory)
227                 expectationsFilesToRequest[platformName] = EXPECTATIONS_URL_BASE_PATH + platform.expectationsDirectory + '/TestExpectations';
228         });
229
230         for (platformWithExpectations in expectationsFilesToRequest)
231             loader.request(expectationsFilesToRequest[platformWithExpectations],
232                     partial(function(loader, platformName, xhr) {
233                         g_expectationsByPlatform[platformName] = getParsedExpectations(xhr.responseText);
234
235                         delete expectationsFilesToRequest[platformName];
236                         if (!Object.keys(expectationsFilesToRequest).length)
237                             loader._loadNext();
238                     }, this, platformWithExpectations),
239                     partial(function(platformName, xhr) {
240                         console.error('Could not load expectations file for ' + platformName);
241                     }, platformWithExpectations));
242     },
243     _getLoadingErrorMessages: function()
244     {
245         var errorMsgs = '';
246         if (this._buildersThatFailedToLoad.length)
247             errorMsgs += 'ERROR: Failed to get data from ' + this._buildersThatFailedToLoad.toString() + '.<br>';
248
249         if (this._staleBuilders.length)
250             errorMsgs +='ERROR: Data from ' + this._staleBuilders.toString() + ' is more than 1 day stale.<br>';
251
252         return errorMsgs;
253     }
254 }
255
256 })();