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