af79fff04e7c1027d0c18c2bc9aa0c90268d7e38
[WebKit-https.git] / Websites / perf.webkit.org / public / v3 / models / commit-set.js
1 'use strict';
2
3 class CommitSet extends DataModelObject {
4
5     constructor(id, object)
6     {
7         super(id);
8         this._repositories = [];
9         this._repositoryToCommitMap = {};
10         this._latestCommitTime = null;
11         this._customRoots = [];
12
13         if (!object)
14             return;
15
16         for (let row of object.commits) {
17             const repositoryId = row.repository.id();
18             console.assert(!this._repositoryToCommitMap[repositoryId]);
19             this._repositoryToCommitMap[repositoryId] = CommitLog.ensureSingleton(row.id, row);
20             this._repositories.push(row.repository);
21         }
22         for (let fileId of object.customRoots) {
23             const uploadedFile = UploadedFile.findById(fileId);
24             this._customRoots.push(uploadedFile);
25         }
26     }
27
28     repositories() { return this._repositories; }
29     customRoots() { return this._customRoots; }
30     commitForRepository(repository) { return this._repositoryToCommitMap[repository.id()]; }
31
32     revisionForRepository(repository)
33     {
34         var commit = this._repositoryToCommitMap[repository.id()];
35         return commit ? commit.revision() : null;
36     }
37
38     // FIXME: This should return a Date object.
39     latestCommitTime()
40     {
41         if (this._latestCommitTime == null) {
42             var maxTime = 0;
43             for (var repositoryId in this._repositoryToCommitMap)
44                 maxTime = Math.max(maxTime, +this._repositoryToCommitMap[repositoryId].time());
45             this._latestCommitTime = maxTime;
46         }
47         return this._latestCommitTime;
48     }
49
50     equals(other)
51     {
52         if (this._repositories.length != other._repositories.length)
53             return false;
54         for (var repositoryId in this._repositoryToCommitMap) {
55             if (this._repositoryToCommitMap[repositoryId] != other._repositoryToCommitMap[repositoryId])
56                 return false;
57         }
58         return CommitSet.areCustomRootsEqual(this._customRoots, other._customRoots);
59     }
60
61     static areCustomRootsEqual(customRoots1, customRoots2)
62     {
63         if (customRoots1.length != customRoots2.length)
64             return false;
65         const set2 = new Set(customRoots2);
66         for (let file of customRoots1) {
67             if (!set2.has(file))
68                 return false;
69         }
70         return true;
71     }
72
73     static containsMultipleCommitsForRepository(commitSets, repository)
74     {
75         console.assert(repository instanceof Repository);
76         if (commitSets.length < 2)
77             return false;
78         const firstCommit = commitSets[0].commitForRepository(repository);
79         for (let set of commitSets) {
80             const anotherCommit = set.commitForRepository(repository);
81             if (!firstCommit != !anotherCommit || (firstCommit && firstCommit.revision() != anotherCommit.revision()))
82                 return true;
83         }
84         return false;
85     }
86 }
87
88 class MeasurementCommitSet extends CommitSet {
89
90     constructor(id, revisionList)
91     {
92         super(id, null);
93         for (var values of revisionList) {
94             // [<commit-id>, <repository-id>, <revision>, <time>]
95             var commitId = values[0];
96             var repositoryId = values[1];
97             var revision = values[2];
98             var time = values[3];
99             var repository = Repository.findById(repositoryId);
100             if (!repository)
101                 continue;
102
103             this._repositoryToCommitMap[repositoryId] = CommitLog.ensureSingleton(commitId, {repository: repository, revision: revision, time: time});
104             this._repositories.push(repository);
105         }
106     }
107
108     // Use CommitSet's static maps because MeasurementCommitSet and CommitSet are logically of the same type.
109     // FIXME: Idaelly, DataModel should take care of this but traversing prototype chain is expensive.
110     namedStaticMap(name) { return CommitSet.namedStaticMap(name); }
111     ensureNamedStaticMap(name) { return CommitSet.ensureNamedStaticMap(name); }
112     static namedStaticMap(name) { return CommitSet.namedStaticMap(name); }
113     static ensureNamedStaticMap(name) { return CommitSet.ensureNamedStaticMap(name); }
114
115     static ensureSingleton(measurementId, revisionList)
116     {
117         const commitSetId = measurementId + '-commitset';
118         return CommitSet.findById(commitSetId) || (new MeasurementCommitSet(commitSetId, revisionList));
119     }
120 }
121
122 class CustomCommitSet {
123
124     constructor()
125     {
126         this._revisionListByRepository = new Map;
127         this._customRoots = [];
128     }
129
130     setRevisionForRepository(repository, revision)
131     {
132         console.assert(repository instanceof Repository);
133         this._revisionListByRepository.set(repository, revision);
134     }
135
136     equals(other)
137     {
138         console.assert(other instanceof CustomCommitSet);
139         if (this._revisionListByRepository.size != other._revisionListByRepository.size)
140             return false;
141         for (let repository of this._revisionListByRepository.keys()) {
142             const thisRevision = this._revisionListByRepository.get(repository);
143             const otherRevision = other._revisionListByRepository.get(repository);
144             if (thisRevision != otherRevision)
145                 return false;
146         }
147         return CommitSet.areCustomRootsEqual(this._customRoots, other._customRoots);
148     }
149
150     repositories() { return Array.from(this._revisionListByRepository.keys()); }
151     revisionForRepository(repository) { return this._revisionListByRepository.get(repository); }
152     customRoots() { return this._customRoots; }
153
154     addCustomRoot(uploadedFile)
155     {
156         console.assert(uploadedFile instanceof UploadedFile);
157         this._customRoots.push(uploadedFile);
158     }
159 }
160
161 if (typeof module != 'undefined') {
162     module.exports.CommitSet = CommitSet;
163     module.exports.MeasurementCommitSet = MeasurementCommitSet;
164     module.exports.CustomCommitSet = CustomCommitSet;
165 }