f579d108e5a5816b52355063a84a2008990d5e8d
[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     bugUrl: DS.attr('string'),
46     newBugUrl: DS.attr('string'),
47     repositories: DS.hasMany('repository'),
48 });
49
50 App.Platform = App.NameLabelModel.extend({
51     _metricSet: null,
52     _testSet: null,
53     metrics: DS.hasMany('metric'),
54     containsMetric: function (metric)
55     {
56         if (!this._metricSet)
57             this._metricSet = new Ember.Set(this.get('metrics'));
58         return this._metricSet.contains(metric);
59     },
60     containsTest: function (test)
61     {
62         if (!this._testSet) {
63             var set = new Ember.Set();
64             this.get('metrics').forEach(function (metric) {
65                 for (var test = metric.get('test'); test; test = test.get('parent'))
66                     set.push(test);
67             });
68             this._testSet = set;
69         }
70         return this._testSet.contains(test);
71     },
72 });
73
74 App.Repository = App.NameLabelModel.extend({
75     url: DS.attr('string'),
76     blameUrl: DS.attr('string'),
77     hasReportedCommits: DS.attr('boolean'),
78     urlForRevision: function (currentRevision) {
79         return (this.get('url') || '').replace(/\$1/g, currentRevision);
80     },
81     urlForRevisionRange: function (from, to) {
82         return (this.get('blameUrl') || '').replace(/\$1/g, from).replace(/\$2/g, to);
83     },
84 });
85
86 App.MetricSerializer = App.PlatformSerializer = DS.RESTSerializer.extend({
87     normalizePayload: function (payload)
88     {
89         var results = {
90             platforms: this._normalizeIdMap(payload['all']),
91             builders: this._normalizeIdMap(payload['builders']),
92             tests: this._normalizeIdMap(payload['tests']).map(function (test) {
93                 test['parent'] = test['parentId'];
94                 return test;
95             }),
96             metrics: this._normalizeIdMap(payload['metrics']),
97             repositories: this._normalizeIdMap(payload['repositories']),
98             bugTrackers: this._normalizeIdMap(payload['bugTrackers']),
99         };
100
101         for (var testId in payload['tests']) {
102             var test = payload['tests'][testId];
103             var parent = payload['tests'][test['parent']];
104             if (!parent)
105                 continue;
106             if (!parent['childTests'])
107                 parent['childTests'] = [];
108             parent['childTests'].push(testId);
109         }
110
111         for (var metricId in payload['metrics']) {
112             var metric = payload['metrics'][metricId];
113             var test = payload['tests'][metric['test']];
114             if (!test['metrics'])
115                 test['metrics'] = [];
116             test['metrics'].push(metricId);
117         }
118
119         return results;
120     },
121     _normalizeIdMap: function (idMap)
122     {
123         var results = [];
124         for (var id in idMap) {
125             var definition = idMap[id];
126             definition['id'] = id;
127             results.push(definition);
128         }
129         return results;
130     }
131 });
132
133 App.PlatformAdapter = DS.RESTAdapter.extend({
134     buildURL: function (type, id)
135     {
136         return '../data/manifest.json';
137     },
138 });
139
140 App.MetricAdapter = DS.RESTAdapter.extend({
141     buildURL: function (type, id)
142     {
143         return '../data/manifest.json';
144     },
145 });
146
147 App.Manifest = Ember.Controller.extend({
148     platforms: null,
149     topLevelTests: null,
150     repositories: [],
151     repositoriesWithReportedCommits: [],
152     bugTrackers: [],
153     _platformById: {},
154     _metricById: {},
155     _builderById: {},
156     _repositoryById: {},
157     _fetchPromise: null,
158     fetch: function ()
159     {
160         if (this._fetchPromise)
161             return this._fetchPromise;
162         // FIXME: We shouldn't use DS.Store at all.
163         var store = App.__container__.lookup('store:main');
164         var promise = store.findAll('platform');
165         this._fetchPromise = promise.then(this._fetchedManifest.bind(this, store));
166         return this._fetchPromise;
167     },
168     isFetched: function () { return !!this.get('platforms'); }.property('platforms'),
169     platform: function (id) { return this._platformById[id]; },
170     metric: function (id) { return this._metricById[id]; },
171     builder: function (id) { return this._builderById[id]; },
172     repository: function (id) { return this._repositoryById[id]; },
173     _fetchedManifest: function (store)
174     {
175         var startTime = Date.now();
176         var self = this;
177
178         var topLevelTests = [];
179         store.all('test').forEach(function (test) {
180             var parent = test.get('parent');
181             if (!parent)
182                 topLevelTests.push(test);
183         });
184         this.set('topLevelTests', topLevelTests);
185
186         store.all('metric').forEach(function (metric) {
187             self._metricById[metric.get('id')] = metric;
188         });
189         var platforms = store.all('platform');
190         platforms.forEach(function (platform) {
191             self._platformById[platform.get('id')] = platform;
192         });
193         this.set('platforms', platforms);
194
195         store.all('builder').forEach(function (builder) {
196             self._builderById[builder.get('id')] = builder;
197         });
198
199         var repositories = store.all('repository');
200         repositories.forEach(function (repository) {
201             self._repositoryById[repository.get('id')] = repository;
202         });
203         this.set('repositories', repositories.sortBy('id'));
204         this.set('repositoriesWithReportedCommits',
205             repositories.filter(function (repository) { return repository.get('hasReportedCommits'); }));
206
207         this.set('bugTrackers', store.all('bugTracker').sortBy('name'));
208     }
209 }).create();