6283a5808e708f5be5091db7c0749a7a81bf1cb2
[WebKit-https.git] / Websites / perf.webkit.org / public / v2 / manifest.js
1 App.NameLabelModel = DS.Model.extend({
2     name: DS.attr('string'),
3     label: function ()
4     {
5         return this.get('name');
6     }.property('name'),
7 });
8
9 App.Test = App.NameLabelModel.extend({
10     url: DS.attr('string'),
11     parent: DS.belongsTo('test', {inverse: 'childTests'}),
12     childTests: DS.hasMany('test', {inverse: 'parent'}),
13     metrics: DS.hasMany('metrics'),
14 });
15
16 App.Metric = App.NameLabelModel.extend({
17     test: DS.belongsTo('test'),
18     aggregator: DS.attr('string'),
19     label: function ()
20     {
21         return this.get('name') + ' : ' + this.get('aggregator');
22     }.property('name', 'aggregator'),
23     path: function ()
24     {
25         var path = [];
26         var parent = this.get('test');
27         while (parent) {
28             path.push(parent.get('name'));
29             parent = parent.get('parent');
30         }
31         return path.reverse();
32     }.property('name', 'test'),
33 });
34
35 App.Builder = App.NameLabelModel.extend({
36     buildUrl: DS.attr('string'),
37     urlFromBuildNumber: function (buildNumber)
38     {
39         var template = this.get('buildUrl');
40         return template ? template.replace(/\$buildNumber/g, buildNumber) : null;
41     }
42 });
43
44 App.BugTracker = App.NameLabelModel.extend({
45     buildUrl: DS.attr('string'),
46 });
47
48 App.Platform = App.NameLabelModel.extend({
49     _metricSet: null,
50     _testSet: null,
51     metrics: DS.hasMany('metric'),
52     containsMetric: function (metric)
53     {
54         if (!this._metricSet)
55             this._metricSet = new Ember.Set(this.get('metrics'));
56         return this._metricSet.contains(metric);
57     },
58     containsTest: function (test)
59     {
60         if (!this._testSet) {
61             var set = new Ember.Set();
62             this.get('metrics').forEach(function (metric) {
63                 for (var test = metric.get('test'); test; test = test.get('parent'))
64                     set.push(test);
65             });
66             this._testSet = set;
67         }
68         return this._testSet.contains(test);
69     },
70 });
71
72 App.Repository = App.NameLabelModel.extend({
73     url: DS.attr('string'),
74     blameUrl: DS.attr('string'),
75     urlForRevision: function (currentRevision) {
76         return (this.get('url') || '').replace(/\$1/g, currentRevision);
77     },
78     urlForRevisionRange: function (from, to) {
79         return (this.get('blameUrl') || '').replace(/\$1/g, from).replace(/\$2/g, to);
80     },
81 });
82
83 App.MetricSerializer = App.PlatformSerializer = DS.RESTSerializer.extend({
84     normalizePayload: function (payload)
85     {
86         var results = {
87             platforms: this._normalizeIdMap(payload['all']),
88             builders: this._normalizeIdMap(payload['builders']),
89             tests: this._normalizeIdMap(payload['tests']).map(function (test) {
90                 test['parent'] = test['parentId'];
91                 return test;
92             }),
93             metrics: this._normalizeIdMap(payload['metrics']),
94             repositories: this._normalizeIdMap(payload['repositories']),
95         };
96
97         for (var testId in payload['tests']) {
98             var test = payload['tests'][testId];
99             var parent = payload['tests'][test['parent']];
100             if (!parent)
101                 continue;
102             if (!parent['childTests'])
103                 parent['childTests'] = [];
104             parent['childTests'].push(testId);
105         }
106
107         for (var metricId in payload['metrics']) {
108             var metric = payload['metrics'][metricId];
109             var test = payload['tests'][metric['test']];
110             if (!test['metrics'])
111                 test['metrics'] = [];
112             test['metrics'].push(metricId);
113         }
114
115         return results;
116     },
117     _normalizeIdMap: function (idMap)
118     {
119         var results = [];
120         for (var id in idMap) {
121             var definition = idMap[id];
122             definition['id'] = id;
123             results.push(definition);
124         }
125         return results;
126     }
127 });
128
129 App.PlatformAdapter = DS.RESTAdapter.extend({
130     buildURL: function (type, id)
131     {
132         return '../data/manifest.json';
133     },
134 });
135
136 App.MetricAdapter = DS.RESTAdapter.extend({
137     buildURL: function (type, id)
138     {
139         return '../data/manifest.json';
140     },
141 });
142
143 App.Manifest = Ember.Controller.extend({
144     platforms: null,
145     topLevelTests: null,
146     _platformById: {},
147     _metricById: {},
148     _builderById: {},
149     _repositoryById: {},
150     _fetchPromise: null,
151     fetch: function ()
152     {
153         if (this._fetchPromise)
154             return this._fetchPromise;
155         // FIXME: We shouldn't use DS.Store at all.
156         var store = App.__container__.lookup('store:main');
157         var promise = store.findAll('platform');
158         this._fetchPromise = promise.then(this._fetchedManifest.bind(this, store));
159         return this._fetchPromise;
160     },
161     isFetched: function () { return !!this.get('platforms'); }.property('platforms'),
162     platform: function (id) { return this._platformById[id]; },
163     metric: function (id) { return this._metricById[id]; },
164     builder: function (id) { return this._builderById[id]; },
165     repository: function (id) { return this._repositoryById[id]; },
166     _fetchedManifest: function (store)
167     {
168         var startTime = Date.now();
169         var self = this;
170
171         var topLevelTests = [];
172         store.all('test').forEach(function (test) {
173             var parent = test.get('parent');
174             if (!parent)
175                 topLevelTests.push(test);
176         });
177         this.set('topLevelTests', topLevelTests);
178
179         store.all('metric').forEach(function (metric) {
180             self._metricById[metric.get('id')] = metric;
181         });
182         var platforms = store.all('platform');
183         platforms.forEach(function (platform) {
184             self._platformById[platform.get('id')] = platform;
185         });
186         this.set('platforms', platforms);
187
188         store.all('builder').forEach(function (builder) {
189             self._builderById[builder.get('id')] = builder;
190         });
191
192         store.all('repository').forEach(function (repository) {
193             self._repositoryById[repository.get('id')] = repository;
194         });
195     }
196 }).create();