30c89d17a5dc7ad2e1678253ff0ceb952d7a9cab
[WebKit.git] / Websites / perf.webkit.org / public / v3 / models / root-set.js
1 'use strict';
2
3 class RootSet extends DataModelObject {
4
5     constructor(id, object)
6     {
7         super(id);
8         this._repositories = [];
9         this._repositoryToCommitMap = {};
10         this._latestCommitTime = null;
11
12         if (!object)
13             return;
14
15         for (var row of object.roots) {
16             var repositoryId = row.repository;
17             row.repository = Repository.findById(repositoryId);
18
19             console.assert(!this._repositoryToCommitMap[repositoryId]);
20             this._repositoryToCommitMap[repositoryId] = CommitLog.ensureSingleton(row.id, row);
21             this._repositories.push(row.repository);
22         }
23     }
24
25     repositories() { return this._repositories; }
26     commitForRepository(repository) { return this._repositoryToCommitMap[repository.id()]; }
27
28     revisionForRepository(repository)
29     {
30         var commit = this._repositoryToCommitMap[repository.id()];
31         return commit ? commit.revision() : null;
32     }
33
34     latestCommitTime()
35     {
36         if (this._latestCommitTime == null) {
37             var maxTime = 0;
38             for (var repositoryId in this._repositoryToCommitMap)
39                 maxTime = Math.max(maxTime, +this._repositoryToCommitMap[repositoryId].time());
40             this._latestCommitTime = maxTime;
41         }
42         return this._latestCommitTime;
43     }
44
45     equals(other)
46     {
47         if (this._repositories.length != other._repositories.length)
48             return false;
49         for (var repositoryId in this._repositoryToCommitMap) {
50             if (this._repositoryToCommitMap[repositoryId] != other._repositoryToCommitMap[repositoryId])
51                 return false;
52         }
53         return true;
54     }
55
56     static containsMultipleCommitsForRepository(rootSets, repository)
57     {
58         console.assert(repository instanceof Repository);
59         if (rootSets.length < 2)
60             return false;
61         var firstCommit = rootSets[0].commitForRepository(repository);
62         for (var set of rootSets) {
63             var anotherCommit = set.commitForRepository(repository);
64             if (!firstCommit != !anotherCommit || (firstCommit && firstCommit.revision() != anotherCommit.revision()))
65                 return true;
66         }
67         return false;
68     }
69 }
70
71 class MeasurementRootSet extends RootSet {
72
73     constructor(id, revisionList)
74     {
75         super(id, null);
76         for (var values of revisionList) {
77             // [<commit-id>, <repository-id>, <revision>, <time>]
78             var commitId = values[0];
79             var repositoryId = values[1];
80             var revision = values[2];
81             var time = values[3];
82             var repository = Repository.findById(repositoryId);
83             if (!repository)
84                 continue;
85
86             this._repositoryToCommitMap[repositoryId] = CommitLog.ensureSingleton(commitId, {repository: repository, revision: revision, time: time});
87             this._repositories.push(repository);
88         }
89     }
90
91     static ensureSingleton(measurementId, revisionList)
92     {
93         var rootSetId = measurementId + '-rootset';
94         return RootSet.findById(rootSetId) || (new MeasurementRootSet(rootSetId, revisionList));
95     }
96 }
97
98 class CustomRootSet {
99
100     constructor()
101     {
102         this._revisionListByRepository = new Map;
103     }
104
105     setRevisionForRepository(repository, revision)
106     {
107         console.assert(repository instanceof Repository);
108         this._revisionListByRepository.set(repository, revision);
109     }
110
111     repositories() { return Array.from(this._revisionListByRepository.keys()); }
112     revisionForRepository(repository) { return this._revisionListByRepository.get(repository); }
113
114 }
115
116 if (typeof module != 'undefined') {
117     module.exports.RootSet = RootSet;
118     module.exports.MeasurementRootSet = MeasurementRootSet;
119     module.exports.CustomRootSet = CustomRootSet;
120 }