Make it possible to hide some repository groups
[WebKit-https.git] / Websites / perf.webkit.org / public / include / manifest-generator.php
1 <?php
2
3 class ManifestGenerator {
4     private $db;
5     private $manifest;
6
7     // FIXME: Compute this value from config.json
8     const MANIFEST_PATH = '../data/manifest.json';
9
10     function __construct($db) {
11         $this->db = $db;
12     }
13
14     function generate() {
15         $start_time = microtime(true);
16
17         $platform_table = $this->db->fetch_table('platforms');
18         $repositories_table = $this->db->fetch_table('repositories');
19
20         $repositories_with_commit = $this->db->query_and_fetch_all(
21             'SELECT DISTINCT(commit_repository) FROM commits WHERE commit_reported IS TRUE');
22         if (!$repositories_with_commit)
23             $repositories_with_commit = array();
24
25         foreach ($repositories_with_commit as &$row)
26             $row = $row['commit_repository'];
27
28         $tests = (object)$this->tests();
29         $metrics = (object)$this->metrics();
30         $platforms = (object)$this->platforms($platform_table, false);
31         $dashboard = (object)$this->platforms($platform_table, true);
32         $repositories = (object)$this->repositories($repositories_table, $repositories_with_commit);
33
34         $this->manifest = array(
35             'siteTitle' => config('siteTitle', 'Performance Dashboard'),
36             'tests' => &$tests,
37             'metrics' => &$metrics,
38             'all' => &$platforms,
39             'dashboard' => &$dashboard,
40             'repositories' => &$repositories,
41             'builders' => (object)$this->builders(),
42             'bugTrackers' => (object)$this->bug_trackers($repositories_table),
43             'triggerables'=> (object)$this->triggerables(),
44             'dashboards' => (object)config('dashboards'),
45             'summaryPages' => config('summaryPages'),
46             'fileUploadSizeLimit' => config('uploadFileLimitInMB', 0) * 1024 * 1024,
47             'testAgeToleranceInHours' => config('testAgeToleranceInHours'),
48         );
49
50         $this->manifest['elapsedTime'] = (microtime(true) - $start_time) * 1000;
51
52         return TRUE;
53     }
54
55     function manifest() { return $this->manifest; }
56
57     function store() {
58         return generate_data_file('manifest.json', json_encode($this->manifest));
59     }
60
61     private function tests() {
62         $tests = array();
63         $tests_table = $this->db->fetch_table('tests');
64         if (!$tests_table)
65             return $tests;
66         foreach ($tests_table as $test_row) {
67             $tests[$test_row['test_id']] = array(
68                 'name' => $test_row['test_name'],
69                 'url' => $test_row['test_url'],
70                 'parentId' => $test_row['test_parent'],
71             );
72         }
73         return $tests;
74     }
75
76     private function metrics() {
77         $metrics = array();
78         $metrics_table = $this->db->query_and_fetch_all('SELECT * FROM test_metrics LEFT JOIN aggregators ON metric_aggregator = aggregator_id');
79         if (!$metrics_table)
80             return $metrics;
81         foreach ($metrics_table as $row) {
82             $metrics[$row['metric_id']] = array(
83                 'name' => $row['metric_name'],
84                 'test' => $row['metric_test'],
85                 'aggregator' => $row['aggregator_name']);
86         }
87         return $metrics;
88     }
89
90     private function platforms($platform_table, $is_dashboard) {
91         $metrics = $this->db->query_and_fetch_all('SELECT config_metric AS metric_id, config_platform AS platform_id,
92             extract(epoch from max(config_runs_last_modified) at time zone \'utc\') * 1000 AS last_modified, bool_or(config_is_in_dashboard) AS in_dashboard
93             FROM test_configurations GROUP BY config_metric, config_platform ORDER BY config_platform');
94
95         $platform_metrics = array();
96
97         if ($metrics) {
98             $current_platform_entry = null;
99             foreach ($metrics as $metric_row) {
100                 if ($is_dashboard && !Database::is_true($metric_row['in_dashboard']))
101                     continue;
102
103                 $platform_id = $metric_row['platform_id'];
104                 if (!$current_platform_entry || $current_platform_entry['id'] != $platform_id) {
105                     $current_platform_entry = &array_ensure_item_has_array($platform_metrics, $platform_id);
106                     $current_platform_entry['id'] = $platform_id;
107                     array_ensure_item_has_array($current_platform_entry, 'metrics');
108                     array_ensure_item_has_array($current_platform_entry, 'last_modified');
109                 }
110
111                 array_push($current_platform_entry['metrics'], $metric_row['metric_id']);
112                 array_push($current_platform_entry['last_modified'], intval($metric_row['last_modified']));
113             }
114         }
115         $configurations = array();
116
117         $platforms = array();
118         if ($platform_table) {
119             foreach ($platform_table as $platform_row) {
120                 if (Database::is_true($platform_row['platform_hidden']))
121                     continue;
122                 $id = $platform_row['platform_id'];
123                 if (array_key_exists($id, $platform_metrics)) {
124                     $platforms[$id] = array(
125                         'name' => $platform_row['platform_name'],
126                         'metrics' => $platform_metrics[$id]['metrics'],
127                         'lastModified' => $platform_metrics[$id]['last_modified']);
128                 }
129             }
130         }
131         return $platforms;
132     }
133
134     private function repositories($repositories_table, $repositories_with_commit) {
135         $repositories = array();
136         if (!$repositories_table)
137             return $repositories;
138         foreach ($repositories_table as $row) {
139             $repositories[$row['repository_id']] = array(
140                 'name' => $row['repository_name'],
141                 'url' => $row['repository_url'],
142                 'blameUrl' => $row['repository_blame_url'],
143                 'owner'=> $row['repository_owner'],
144                 'hasReportedCommits' => in_array($row['repository_id'], $repositories_with_commit));
145         }
146
147         return $repositories;
148     }
149
150     private function builders() {
151         $builders_table = $this->db->fetch_table('builders');
152         if (!$builders_table)
153             return array();
154         $builders = array();
155         foreach ($builders_table as $row)
156             $builders[$row['builder_id']] = array('name' => $row['builder_name'], 'buildUrl' => $row['builder_build_url']);
157
158         return $builders;
159     }
160
161     private function bug_trackers($repositories_table) {
162         $tracker_id_to_repositories = array();
163         $tracker_repositories_table = $this->db->fetch_table('tracker_repositories');
164         if ($tracker_repositories_table) {
165             foreach ($tracker_repositories_table as $row) {
166                 array_push(array_ensure_item_has_array($tracker_id_to_repositories, $row['tracrepo_tracker']),
167                     $row['tracrepo_repository']);
168             }
169         }
170
171         $bug_trackers = array();
172         $bug_trackers_table = $this->db->fetch_table('bug_trackers');
173         if ($bug_trackers_table) {
174             foreach ($bug_trackers_table as $row) {
175                 $bug_trackers[$row['tracker_id']] = array(
176                     'name' => $row['tracker_name'],
177                     'bugUrl' => $row['tracker_bug_url'],
178                     'newBugUrl' => $row['tracker_new_bug_url'],
179                     'repositories' => array_get($tracker_id_to_repositories, $row['tracker_id']));
180             }
181         }
182
183         return $bug_trackers;
184     }
185
186     private function triggerables()
187     {
188         return ManifestGenerator::fetch_triggerables($this->db, array());
189     }
190
191     static function fetch_triggerables($db, $query)
192     {
193         $triggerables = $db->select_rows('build_triggerables', 'triggerable', $query);
194         if (!$triggerables)
195             return array();
196
197         $id_to_triggerable = array();
198         $triggerable_id_to_repository_set = array();
199         foreach ($triggerables as &$row) {
200             $id = $row['triggerable_id'];
201             $id_to_triggerable[$id] = array(
202                 'name' => $row['triggerable_name'],
203                 'isDisabled' => Database::is_true($row['triggerable_disabled']),
204                 'acceptedRepositories' => array(),
205                 'repositoryGroups' => array(),
206                 'configurations' => array());
207             $triggerable_id_to_repository_set[$id] = array();
208         }
209
210         $repository_groups = $db->fetch_table('triggerable_repository_groups', 'repositorygroup_name');
211         $group_repositories = $db->fetch_table('triggerable_repositories');
212         if ($repository_groups && $group_repositories) {
213             $repository_set_by_group = array();
214             foreach ($group_repositories as &$repository_row) {
215                 $group_id = $repository_row['trigrepo_group'];
216                 array_ensure_item_has_array($repository_set_by_group, $group_id);
217                 array_push($repository_set_by_group[$group_id], array(
218                     'repository' => $repository_row['trigrepo_repository'],
219                     'acceptsPatch' => Database::is_true($repository_row['trigrepo_accepts_patch'])));
220             }
221             foreach ($repository_groups as &$group_row) {
222                 $triggerable_id = $group_row['repositorygroup_triggerable'];
223                 if (!array_key_exists($triggerable_id, $id_to_triggerable))
224                     continue;
225                 $triggerable = &$id_to_triggerable[$triggerable_id];
226                 $group_id = $group_row['repositorygroup_id'];
227                 $repository_list = array_get($repository_set_by_group, $group_id, array());
228                 array_push($triggerable['repositoryGroups'], array(
229                     'id' => $group_row['repositorygroup_id'],
230                     'name' => $group_row['repositorygroup_name'],
231                     'description' => $group_row['repositorygroup_description'],
232                     'hidden' => Database::is_true($group_row['repositorygroup_hidden']),
233                     'acceptsCustomRoots' => Database::is_true($group_row['repositorygroup_accepts_roots']),
234                     'repositories' => $repository_list));
235                 // V2 UI compatibility.
236                 foreach ($repository_list as $repository_data) {
237                     $repository_id = $repository_data['repository'];
238                     $set = &$triggerable_id_to_repository_set[$triggerable_id];
239                     if (array_key_exists($repository_id, $set))
240                         continue;
241                     $set[$repository_id] = true;
242                     array_push($triggerable['acceptedRepositories'], $repository_id);
243                 }
244
245             }
246         }
247
248         $configuration_map = $db->fetch_table('triggerable_configurations');
249         if ($configuration_map) {
250             foreach ($configuration_map as &$row) {
251                 $triggerable_id = $row['trigconfig_triggerable'];
252                 if (!array_key_exists($triggerable_id, $id_to_triggerable))
253                     continue;
254                 $triggerable = &$id_to_triggerable[$triggerable_id];
255                 array_push($triggerable['configurations'], array($row['trigconfig_test'], $row['trigconfig_platform']));
256             }
257         }
258
259         return $id_to_triggerable;
260     }
261 }
262
263 ?>