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