Analysis task page is broken after r198479
[WebKit.git] / Websites / perf.webkit.org / public / v3 / models / commit-log.js
1 'use strict';
2
3 class CommitLog extends DataModelObject {
4     constructor(id, rawData)
5     {
6         console.assert(parseInt(id) == id);
7         super(id);
8         this._repository = rawData.repository;
9         console.assert(this._repository instanceof Repository);
10         this._rawData = rawData;
11         this._remoteId = rawData.id;
12         if (this._remoteId)
13             this.ensureNamedStaticMap('remoteId')[this._remoteId] = this;
14     }
15
16     updateSingleton(rawData)
17     {
18         super.updateSingleton(rawData);
19
20         console.assert(+this._rawData['time'] == +rawData['time']);
21         console.assert(this._rawData['revision'] == rawData['revision']);
22
23         if (rawData.authorName)
24             this._rawData.authorName = rawData.authorName;
25         if (rawData.message)
26             this._rawData.message = rawData.message;
27     }
28
29     repository() { return this._repository; }
30     time() { return new Date(this._rawData['time']); }
31     author() { return this._rawData['authorName']; }
32     revision() { return this._rawData['revision']; }
33     message() { return this._rawData['message']; }
34     url() { return this._repository.urlForRevision(this._rawData['revision']); }
35
36     label()
37     {
38         var revision = this.revision();
39         if (parseInt(revision) == revision) // e.g. r12345
40             return 'r' + revision;
41         else if (revision.length == 40) // e.g. git hash
42             return revision.substring(0, 8);
43         return revision;
44     }
45     title() { return this._repository.name() + ' at ' + this.label(); }
46
47     diff(previousCommit)
48     {
49         if (this == previousCommit)
50             previousCommit = null;
51
52         var repository = this._repository;
53         if (!previousCommit)
54             return {from: null, to: this.revision(), repository: repository, label: this.label(), url: this.url()};
55
56         var to = this.revision();
57         var from = previousCommit.revision();
58         var label = null;
59         if (parseInt(to) == to) { // e.g. r12345.
60             from = parseInt(from) + 1;
61             label = `r${from}-r${this.revision()}`;
62         } else if (to.length == 40) { // e.g. git hash
63             label = `${from}..${to}`;
64         } else
65             label = `${from} - ${to}`;
66
67         return {from: from, to: to, repository: repository, label: label, url: repository.urlForRevisionRange(from, to)};
68     }
69
70     static fetchBetweenRevisions(repository, from, to)
71     {
72         var params = [];
73         if (from && to) {
74             params.push(['from', from]);
75             params.push(['to', to]);
76         }
77
78         var url = '../api/commits/' + repository.id() + '/?' + params.map(function (keyValue) {
79             return encodeURIComponent(keyValue[0]) + '=' + encodeURIComponent(keyValue[1]);
80         }).join('&');
81
82
83         var cachedLogs = this._cachedCommitLogs(repository, from, to);
84         if (cachedLogs)
85             return new Promise(function (resolve) { resolve(cachedLogs); });
86
87         var self = this;
88         return RemoteAPI.getJSONWithStatus(url).then(function (data) {
89             var commits = data['commits'].map(function (rawData) {
90                 rawData.repository = repository;
91                 return CommitLog.ensureSingleton(rawData.id, rawData);
92             });
93             self._cacheCommitLogs(repository, from, to, commits);
94             return commits;
95         });
96     }
97
98     static _cachedCommitLogs(repository, from, to)
99     {
100         if (!this._caches)
101             return null;
102         var cache = this._caches[repository.id()];
103         if (!cache)
104             return null;
105         // FIXME: Make each commit know of its parent, then we can do a better caching. 
106         return cache[from + '|' + to];
107     }
108
109     static _cacheCommitLogs(repository, from, to, logs)
110     {
111         if (!this._caches)
112             this._caches = {};
113         if (!this._caches[repository.id()])
114             this._caches[repository.id()] = {};
115         this._caches[repository.id()][from + '|' + to] = logs;
116     }
117 }
118
119 if (typeof module != 'undefined')
120     module.exports.CommitLog = CommitLog;