Hide the UI to trigger an A/B testing when there are no triggerables
[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         );
47
48         $this->manifest['elapsedTime'] = (microtime(true) - $start_time) * 1000;
49
50         return TRUE;
51     }
52
53     function manifest() { return $this->manifest; }
54
55     function store() {
56         return generate_data_file('manifest.json', json_encode($this->manifest));
57     }
58
59     private function tests() {
60         $tests = array();
61         $tests_table = $this->db->fetch_table('tests');
62         if (!$tests_table)
63             return $tests;
64         foreach ($tests_table as $test_row) {
65             $tests[$test_row['test_id']] = array(
66                 'name' => $test_row['test_name'],
67                 'url' => $test_row['test_url'],
68                 'parentId' => $test_row['test_parent'],
69             );
70         }
71         return $tests;
72     }
73
74     private function metrics() {
75         $metrics = array();
76         $metrics_table = $this->db->query_and_fetch_all('SELECT * FROM test_metrics LEFT JOIN aggregators ON metric_aggregator = aggregator_id');
77         if (!$metrics_table)
78             return $metrics;
79         foreach ($metrics_table as $row) {
80             $metrics[$row['metric_id']] = array(
81                 'name' => $row['metric_name'],
82                 'test' => $row['metric_test'],
83                 'aggregator' => $row['aggregator_name']);
84         }
85         return $metrics;
86     }
87
88     private function platforms($platform_table, $is_dashboard) {
89         $metrics = $this->db->query_and_fetch_all('SELECT config_metric AS metric_id, config_platform AS platform_id,
90             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
91             FROM test_configurations GROUP BY config_metric, config_platform ORDER BY config_platform');
92
93         $platform_metrics = array();
94
95         if ($metrics) {
96             $current_platform_entry = null;
97             foreach ($metrics as $metric_row) {
98                 if ($is_dashboard && !Database::is_true($metric_row['in_dashboard']))
99                     continue;
100
101                 $platform_id = $metric_row['platform_id'];
102                 if (!$current_platform_entry || $current_platform_entry['id'] != $platform_id) {
103                     $current_platform_entry = &array_ensure_item_has_array($platform_metrics, $platform_id);
104                     $current_platform_entry['id'] = $platform_id;
105                     array_ensure_item_has_array($current_platform_entry, 'metrics');
106                     array_ensure_item_has_array($current_platform_entry, 'last_modified');
107                 }
108
109                 array_push($current_platform_entry['metrics'], $metric_row['metric_id']);
110                 array_push($current_platform_entry['last_modified'], intval($metric_row['last_modified']));
111             }
112         }
113         $configurations = array();
114
115         $platforms = array();
116         if ($platform_table) {
117             foreach ($platform_table as $platform_row) {
118                 if (Database::is_true($platform_row['platform_hidden']))
119                     continue;
120                 $id = $platform_row['platform_id'];
121                 if (array_key_exists($id, $platform_metrics)) {
122                     $platforms[$id] = array(
123                         'name' => $platform_row['platform_name'],
124                         'metrics' => $platform_metrics[$id]['metrics'],
125                         'lastModified' => $platform_metrics[$id]['last_modified']);
126                 }
127             }
128         }
129         return $platforms;
130     }
131
132     private function repositories($repositories_table, $repositories_with_commit) {
133         $repositories = array();
134         if (!$repositories_table)
135             return $repositories;
136         foreach ($repositories_table as $row) {
137             $repositories[$row['repository_id']] = array(
138                 'name' => $row['repository_name'],
139                 'url' => $row['repository_url'],
140                 'blameUrl' => $row['repository_blame_url'],
141                 'hasReportedCommits' => in_array($row['repository_id'], $repositories_with_commit));
142         }
143
144         return $repositories;
145     }
146
147     private function builders() {
148         $builders_table = $this->db->fetch_table('builders');
149         if (!$builders_table)
150             return array();
151         $builders = array();
152         foreach ($builders_table as $row)
153             $builders[$row['builder_id']] = array('name' => $row['builder_name'], 'buildUrl' => $row['builder_build_url']);
154
155         return $builders;
156     }
157
158     private function bug_trackers($repositories_table) {
159         $tracker_id_to_repositories = array();
160         $tracker_repositories_table = $this->db->fetch_table('tracker_repositories');
161         if ($tracker_repositories_table) {
162             foreach ($tracker_repositories_table as $row) {
163                 array_push(array_ensure_item_has_array($tracker_id_to_repositories, $row['tracrepo_tracker']),
164                     $row['tracrepo_repository']);
165             }
166         }
167
168         $bug_trackers = array();
169         $bug_trackers_table = $this->db->fetch_table('bug_trackers');
170         if ($bug_trackers_table) {
171             foreach ($bug_trackers_table as $row) {
172                 $bug_trackers[$row['tracker_id']] = array(
173                     'name' => $row['tracker_name'],
174                     'bugUrl' => $row['tracker_bug_url'],
175                     'newBugUrl' => $row['tracker_new_bug_url'],
176                     'repositories' => array_get($tracker_id_to_repositories, $row['tracker_id']));
177             }
178         }
179
180         return $bug_trackers;
181     }
182
183     private function triggerables() {
184
185         $triggerables = $this->db->fetch_table('build_triggerables');
186         if (!$triggerables)
187             return array();
188
189         $id_to_triggerable = array();
190         foreach ($triggerables as $row) {
191             $id = $row['triggerable_id'];
192             $id_to_triggerable[$id] = array(
193                 'name' => $row['triggerable_name'],
194                 'acceptedRepositories' => array(),
195                 'configurations' => array());
196         }
197
198         $repository_map = $this->db->fetch_table('triggerable_repositories');
199         if ($repository_map) {
200             foreach ($repository_map as $row) {
201                 $triggerable = &$id_to_triggerable[$row['trigrepo_triggerable']];
202                 array_push($triggerable['acceptedRepositories'], $row['trigrepo_repository']);
203             }
204         }
205
206         $configuration_map = $this->db->fetch_table('triggerable_configurations');
207         if ($configuration_map) {
208             foreach ($configuration_map as $row) {
209                 $triggerable = &$id_to_triggerable[$row['trigconfig_triggerable']];
210                 array_push($triggerable['configurations'], array($row['trigconfig_test'], $row['trigconfig_platform']));
211             }
212         }
213
214         return $id_to_triggerable;
215     }
216 }
217
218 ?>