1 // We don't use DS.Model for these object types because we can't afford to process millions of them.
6 _maxNetworkLatency: 3 * 60 * 1000 /* 3 minutes */,
9 PrivilegedAPI.sendRequest = function (url, parameters)
11 return this._generateTokenInServerIfNeeded().then(function (token) {
12 return PrivilegedAPI._post(url, $.extend({token: token}, parameters));
16 PrivilegedAPI._generateTokenInServerIfNeeded = function ()
19 return new Ember.RSVP.Promise(function (resolve, reject) {
20 if (self._token && self._expiration > Date.now() + self._maxNetworkLatency)
23 PrivilegedAPI._post('generate-csrf-token')
24 .then(function (result, reject) {
25 self._token = result['token'];
26 self._expiration = new Date(result['expiration']);
32 PrivilegedAPI._post = function (url, parameters)
34 return new Ember.RSVP.Promise(function (resolve, reject) {
36 url: '../privileged-api/' + url,
38 contentType: 'application/json',
39 data: parameters ? JSON.stringify(parameters) : '{}',
41 }).done(function (data) {
42 if (data.status != 'OK')
46 }).fail(function (xhr, status, error) {
47 reject(xhr.status + (error ? ', ' + error : '') + '\n\nWith response:\n' + xhr.responseText);
53 _cachedCommitsByRepository: {}
56 CommitLogs.fetchForTimeRange = function (repository, from, to, keyword)
60 params.push(['from', from]);
61 params.push(['to', to]);
64 params.push(['keyword', keyword]);
66 // FIXME: We should be able to use the cache if all commits in the range have been cached.
67 var useCache = from && to && !keyword;
69 var url = '../api/commits/' + repository + '/?' + params.map(function (keyValue) {
70 return encodeURIComponent(keyValue[0]) + '=' + encodeURIComponent(keyValue[1]);
74 var cachedCommitsForRange = CommitLogs._cachedCommitsBetween(repository, from, to);
75 if (cachedCommitsForRange)
76 return new Ember.RSVP.Promise(function (resolve) { resolve(cachedCommitsForRange); });
79 return new Ember.RSVP.Promise(function (resolve, reject) {
80 $.getJSON(url, function (data) {
81 if (data.status != 'OK') {
86 var fetchedCommits = data.commits;
87 fetchedCommits.forEach(function (commit) { commit.time = new Date(commit.time.replace(' ', 'T')); });
90 CommitLogs._cacheConsecutiveCommits(repository, from, to, fetchedCommits);
92 resolve(fetchedCommits);
93 }).fail(function (xhr, status, error) {
94 reject(xhr.status + (error ? ', ' + error : ''));
99 CommitLogs._cachedCommitsBetween = function (repository, from, to)
101 var cachedCommits = this._cachedCommitsByRepository[repository];
105 var startCommit = cachedCommits.commitsByRevision[from];
106 var endCommit = cachedCommits.commitsByRevision[to];
107 if (!startCommit || !endCommit)
110 return cachedCommits.commitsByTime.slice(startCommit.cacheIndex, endCommit.cacheIndex + 1);
113 CommitLogs._cacheConsecutiveCommits = function (repository, from, to, consecutiveCommits)
115 var cachedCommits = this._cachedCommitsByRepository[repository];
116 if (!cachedCommits) {
117 cachedCommits = {commitsByRevision: {}, commitsByTime: []};
118 this._cachedCommitsByRepository[repository] = cachedCommits;
121 consecutiveCommits.forEach(function (commit) {
122 if (cachedCommits.commitsByRevision[commit.revision])
124 cachedCommits.commitsByRevision[commit.revision] = commit;
125 cachedCommits.commitsByTime.push(commit);
128 cachedCommits.commitsByTime.sort(function (a, b) { return a.time - b.time; });
129 cachedCommits.commitsByTime.forEach(function (commit, index) { commit.cacheIndex = index; });
133 function Measurement(rawData)
138 var revisions = this._raw['revisions'];
139 // FIXME: Fix this in the server side.
140 if (Array.isArray(revisions))
142 this._raw['revisions'] = revisions;
144 for (var repositoryId in revisions) {
145 var commitTimeOrUndefined = revisions[repositoryId][1]; // e.g. ["162190", 1389945046000]
146 if (latestTime < commitTimeOrUndefined)
147 latestTime = commitTimeOrUndefined;
149 this._latestCommitTime = latestTime !== -1 ? new Date(latestTime) : null;
150 this._buildTime = new Date(this._raw['buildTime']);
151 this._confidenceInterval = undefined;
152 this._formattedRevisions = undefined;
155 Measurement.prototype.revisionForRepository = function (repositoryId)
157 var revisions = this._raw['revisions'];
158 var rawData = revisions[repositoryId];
159 return rawData ? rawData[0] : null;
162 Measurement.prototype.commitTimeForRepository = function (repositoryId)
164 var revisions = this._raw['revisions'];
165 var rawData = revisions[repositoryId];
166 return rawData ? new Date(rawData[1]) : null;
169 Measurement.prototype.formattedRevisions = function (previousMeasurement)
171 var revisions = this._raw['revisions'];
172 var previousRevisions = previousMeasurement ? previousMeasurement._raw['revisions'] : null;
173 var formattedRevisions = {};
174 for (var repositoryId in revisions) {
175 var currentRevision = revisions[repositoryId][0];
176 var previousRevision = previousRevisions && previousRevisions[repositoryId] ? previousRevisions[repositoryId][0] : null;
177 var formatttedRevision = Measurement.formatRevisionRange(currentRevision, previousRevision);
178 formattedRevisions[repositoryId] = formatttedRevision;
181 return formattedRevisions;
184 Measurement.formatRevisionRange = function (currentRevision, previousRevision)
186 var revisionChanged = false;
187 if (previousRevision == currentRevision)
188 previousRevision = null;
190 revisionChanged = true;
192 var revisionPrefix = '';
193 var revisionDelimiter = '-';
195 if (parseInt(currentRevision) == currentRevision) { // e.g. r12345.
196 currentRevision = parseInt(currentRevision);
197 revisionPrefix = 'r';
198 if (previousRevision)
199 previousRevision = (parseInt(previousRevision) + 1);
200 } else if (currentRevision.indexOf(' ') >= 0) // e.g. 10.9 13C64.
201 revisionDelimiter = ' - ';
202 else if (currentRevision.length == 40) { // e.g. git hash
203 var formattedCurrentHash = currentRevision.substring(0, 8);
204 if (previousRevision)
205 label = previousRevision.substring(0, 8) + '..' + formattedCurrentHash;
207 label = formattedCurrentHash;
211 if (previousRevision)
212 label = revisionPrefix + previousRevision + revisionDelimiter + revisionPrefix + currentRevision;
214 label = revisionPrefix + currentRevision;
219 previousRevision: previousRevision,
220 currentRevision: currentRevision,
221 revisionChanged: revisionChanged
225 Measurement.prototype.id = function ()
227 return this._raw['id'];
230 Measurement.prototype.mean = function()
232 return this._raw['mean'];
235 Measurement.prototype.confidenceInterval = function()
237 if (this._confidenceInterval === undefined) {
238 var delta = Statistics.confidenceIntervalDelta(0.95, this._raw["iterationCount"], this._raw["sum"], this._raw["squareSum"]);
239 var mean = this.mean();
240 this._confidenceInterval = isNaN(delta) ? null : [mean - delta, mean + delta];
242 return this._confidenceInterval;
245 Measurement.prototype.latestCommitTime = function()
247 return this._latestCommitTime || this._buildTime;
250 Measurement.prototype.buildId = function()
252 return this._raw['build'];
255 Measurement.prototype.buildNumber = function ()
257 return this._raw['buildNumber'];
260 Measurement.prototype.builderId = function ()
262 return this._raw['builder'];
265 Measurement.prototype.buildTime = function()
267 return this._buildTime;
270 Measurement.prototype.formattedBuildTime = function ()
272 return Measurement._formatDate(this.buildTime());
275 Measurement._formatDate = function (date)
277 return date.toISOString().replace('T', ' ').replace(/\.\d+Z$/, '');
280 Measurement.prototype.bugs = function ()
282 return this._raw['bugs'];
285 Measurement.prototype.hasBugs = function ()
287 var bugs = this.bugs();
288 return bugs && Object.keys(bugs).length;
291 function RunsData(rawData)
293 this._measurements = rawData.map(function (run) { return new Measurement(run); });
296 RunsData.prototype.count = function ()
298 return this._measurements.length;
301 RunsData.prototype.timeSeriesByCommitTime = function ()
303 return new TimeSeries(this._measurements.map(function (measurement) {
304 var confidenceInterval = measurement.confidenceInterval();
306 measurement: measurement,
307 time: measurement.latestCommitTime(),
308 value: measurement.mean(),
309 interval: measurement.confidenceInterval(),
314 RunsData.prototype.timeSeriesByBuildTime = function ()
316 return new TimeSeries(this._measurements.map(function (measurement) {
318 measurement: measurement,
319 time: measurement.buildTime(),
320 value: measurement.mean(),
321 interval: measurement.confidenceInterval(),
326 // FIXME: We need to devise a way to fetch runs in multiple chunks so that
327 // we don't have to fetch the entire time series to just show the last 3 days.
328 RunsData.fetchRuns = function (platformId, metricId, testGroupId, useCache)
330 var url = useCache ? '../data/' : '../api/runs/';
332 url += platformId + '-' + metricId + '.json';
334 url += '?testGroup=' + testGroupId;
336 return new Ember.RSVP.Promise(function (resolve, reject) {
337 $.getJSON(url, function (response) {
338 if (response.status != 'OK') {
339 reject(response.status);
342 delete response.status;
344 var data = response.configurations;
345 for (var config in data)
346 data[config] = new RunsData(data[config]);
348 if (response.lastModified)
349 response.lastModified = new Date(response.lastModified);
352 }).fail(function (xhr, status, error) {
353 if (xhr.status == 404 && useCache)
356 reject(xhr.status + (error ? ', ' + error : ''));
361 function TimeSeries(series)
363 this._series = series.sort(function (a, b) { return a.time - b.time; });
367 this._series.forEach(function (point, index) {
369 point.seriesIndex = index;
370 if (min === undefined || min > point.value)
372 if (max === undefined || max < point.value)
379 TimeSeries.prototype.findPointByBuild = function (buildId)
381 return this._series.find(function (point) { return point.measurement.buildId() == buildId; })
384 TimeSeries.prototype.findPointByRevisions = function (revisions)
386 return this._series.find(function (point, index) {
387 for (var repositoryId in revisions) {
388 if (point.measurement.revisionForRepository(repositoryId) != revisions[repositoryId])
395 TimeSeries.prototype.findPointByMeasurementId = function (measurementId)
397 return this._series.find(function (point) { return point.measurement.id() == measurementId; });
400 TimeSeries.prototype.findPointAfterTime = function (time)
402 return this._series.find(function (point) { return point.time >= time; });
405 TimeSeries.prototype.seriesBetweenPoints = function (startPoint, endPoint)
407 if (!startPoint.seriesIndex || !endPoint.seriesIndex)
409 return this._series.slice(startPoint.seriesIndex, endPoint.seriesIndex + 1);
412 TimeSeries.prototype.minMaxForTimeRange = function (startTime, endTime, ignoreOutlier)
414 var data = this._series;
416 if (startTime !== undefined) {
417 for (i = 0; i < data.length; i++) {
419 if (point.time >= startTime)
425 var min = Number.MAX_VALUE;
426 var max = Number.MIN_VALUE;
427 for (; i < data.length; i++) {
429 if (point.isOutlier && ignoreOutlier)
431 var currentMin = point.interval ? point.interval[0] : point.value;
432 var currentMax = point.interval ? point.interval[1] : point.value;
434 if (currentMin < min)
436 if (currentMax > max)
439 if (point.time >= endTime)
445 TimeSeries.prototype.series = function () { return this._series; }
447 TimeSeries.prototype.lastPoint = function ()
449 if (!this._series || !this._series.length)
451 return this._series[this._series.length - 1];
454 TimeSeries.prototype.previousPoint = function (point)
456 if (!point.seriesIndex)
458 return this._series[point.seriesIndex - 1];
461 TimeSeries.prototype.nextPoint = function (point)
463 if (!point.seriesIndex)
465 return this._series[point.seriesIndex + 1];