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