d36763b738ae12a40a02ca4d04e408873480a76b
[WebKit.git] / Websites / perf.webkit.org / public / include / manifest.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         $config_table = $this->db->fetch_table('test_configurations');
18         $platform_table = $this->db->fetch_table('platforms');
19         $repositories_table = $this->db->fetch_table('repositories');
20
21         $repositories_with_commit = $this->db->query_and_fetch_all(
22             'SELECT DISTINCT(commit_repository) FROM commits WHERE commit_reported IS TRUE');
23         if (!$repositories_with_commit)
24             $repositories_with_commit = array();
25
26         foreach ($repositories_with_commit as &$row)
27             $row = $row['commit_repository'];
28
29         $this->manifest = array(
30             'tests' => (object)$this->tests(),
31             'metrics' => (object)$this->metrics(),
32             'all' => (object)$this->platforms($config_table, $platform_table, false),
33             'dashboard' => (object)$this->platforms($config_table, $platform_table, true),
34             'repositories' => (object)$this->repositories($repositories_table, $repositories_with_commit),
35             'builders' => (object)$this->builders(),
36             'bugTrackers' => (object)$this->bug_trackers($repositories_table),
37             'dashboards' => (object)config('dashboards'),
38         );
39
40         $this->manifest['elapsedTime'] = (microtime(true) - $start_time) * 1000;
41
42         return TRUE;
43     }
44
45     function store() {
46         return generate_data_file('manifest.json', json_encode($this->manifest));
47     }
48
49     private function tests() {
50         $tests = array();
51         $tests_table = $this->db->fetch_table('tests');
52         if (!$tests_table)
53             return $tests;
54         foreach ($tests_table as $test_row) {
55             $tests[$test_row['test_id']] = array(
56                 'name' => $test_row['test_name'],
57                 'url' => $test_row['test_url'],
58                 'parentId' => $test_row['test_parent'],
59             );
60         }
61         return $tests;
62     }
63
64     private function metrics() {
65         $metrics = array();
66         $metrics_table = $this->db->query_and_fetch_all('SELECT * FROM test_metrics LEFT JOIN aggregators ON metric_aggregator = aggregator_id');
67         if (!$metrics_table)
68             return $metrics;
69         foreach ($metrics_table as $row) {
70             $metrics[$row['metric_id']] = array(
71                 'name' => $row['metric_name'],
72                 'test' => $row['metric_test'],
73                 'aggregator' => $row['aggregator_name']);
74         }
75         return $metrics;
76     }
77
78     private function platforms($config_table, $platform_table, $is_dashboard) {
79         $platform_metrics = array();
80         if ($config_table) {
81             foreach ($config_table as $config_row) {
82                 if ($is_dashboard && !$this->db->is_true($config_row['config_is_in_dashboard']))
83                     continue;
84
85                 $new_last_modified = array_get($config_row, 'config_runs_last_modified', 0);
86                 if ($new_last_modified)
87                     $new_last_modified = strtotime($config_row['config_runs_last_modified']) * 1000;
88
89                 $platform = &array_ensure_item_has_array($platform_metrics, $config_row['config_platform']);
90                 $metrics = &array_ensure_item_has_array($platform, 'metrics');
91                 $last_modified = &array_ensure_item_has_array($platform, 'last_modified');
92
93                 $metric_id = $config_row['config_metric'];
94                 $index = array_search($metric_id, $metrics);
95                 if ($index === FALSE) {
96                     array_push($metrics, $metric_id);
97                     array_push($last_modified, $new_last_modified);
98                 } else
99                     $last_modified[$index] = max($last_modified[$index], $new_last_modified);
100             }
101         }
102         $configurations = array();
103         
104         $platforms = array();
105         if ($platform_table) {
106             foreach ($platform_table as $platform_row) {
107                 if ($this->db->is_true($platform_row['platform_hidden']))
108                     continue;
109                 $id = $platform_row['platform_id'];
110                 if (array_key_exists($id, $platform_metrics)) {
111                     $platforms[$id] = array(
112                         'name' => $platform_row['platform_name'],
113                         'metrics' => $platform_metrics[$id]['metrics'],
114                         'lastModified' => $platform_metrics[$id]['last_modified']);
115                 }
116             }
117         }
118         return $platforms;
119     }
120
121     private function repositories($repositories_table, $repositories_with_commit) {
122         $repositories = array();
123         if (!$repositories_table)
124             return $repositories;
125         foreach ($repositories_table as $row) {
126             $repositories[$row['repository_id']] = array(
127                 'name' => $row['repository_name'],
128                 'url' => $row['repository_url'],
129                 'blameUrl' => $row['repository_blame_url'],
130                 'hasReportedCommits' => in_array($row['repository_id'], $repositories_with_commit));
131         }
132
133         return $repositories;
134     }
135
136     private function builders() {
137         $builders_table = $this->db->fetch_table('builders');
138         if (!$builders_table)
139             return array();
140         $builders = array();
141         foreach ($builders_table as $row)
142             $builders[$row['builder_id']] = array('name' => $row['builder_name'], 'buildUrl' => $row['builder_build_url']);
143
144         return $builders;
145     }
146
147     private function bug_trackers($repositories_table) {
148         $tracker_id_to_repositories = array();
149         $tracker_repositories_table = $this->db->fetch_table('tracker_repositories');
150         if ($tracker_repositories_table) {
151             foreach ($tracker_repositories_table as $row) {
152                 array_push(array_ensure_item_has_array($tracker_id_to_repositories, $row['tracrepo_tracker']),
153                     $row['tracrepo_repository']);
154             }
155         }
156
157         $bug_trackers = array();
158         $bug_trackers_table = $this->db->fetch_table('bug_trackers');
159         if ($bug_trackers_table) {
160             foreach ($bug_trackers_table as $row) {
161                 $bug_trackers[$row['tracker_id']] = array(
162                     'name' => $row['tracker_name'],
163                     'bugUrl' => $row['tracker_bug_url'],
164                     'newBugUrl' => $row['tracker_new_bug_url'],
165                     'repositories' => $tracker_id_to_repositories[$row['tracker_id']]);
166             }
167         }
168
169         return $bug_trackers;
170     }
171 }
172
173 ?>