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