d8b8be458210429e54e2bc50463e6111dee7d4d9
[WebKit-https.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         $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             'dashboards' => (object)config('dashboards'),
44             'summary' => (object)config('summary'),
45         );
46
47         $this->manifest['elapsedTime'] = (microtime(true) - $start_time) * 1000;
48
49         return TRUE;
50     }
51
52     function manifest() { return $this->manifest; }
53
54     function store() {
55         return generate_data_file('manifest.json', json_encode($this->manifest));
56     }
57
58     private function tests() {
59         $tests = array();
60         $tests_table = $this->db->fetch_table('tests');
61         if (!$tests_table)
62             return $tests;
63         foreach ($tests_table as $test_row) {
64             $tests[$test_row['test_id']] = array(
65                 'name' => $test_row['test_name'],
66                 'url' => $test_row['test_url'],
67                 'parentId' => $test_row['test_parent'],
68             );
69         }
70         return $tests;
71     }
72
73     private function metrics() {
74         $metrics = array();
75         $metrics_table = $this->db->query_and_fetch_all('SELECT * FROM test_metrics LEFT JOIN aggregators ON metric_aggregator = aggregator_id');
76         if (!$metrics_table)
77             return $metrics;
78         foreach ($metrics_table as $row) {
79             $metrics[$row['metric_id']] = array(
80                 'name' => $row['metric_name'],
81                 'test' => $row['metric_test'],
82                 'aggregator' => $row['aggregator_name']);
83         }
84         return $metrics;
85     }
86
87     private function platforms($platform_table, $is_dashboard) {
88         $metrics = $this->db->query_and_fetch_all('SELECT config_metric AS metric_id, config_platform AS platform_id,
89             extract(epoch from max(config_runs_last_modified)) * 1000 AS last_modified, bool_or(config_is_in_dashboard) AS in_dashboard
90             FROM test_configurations GROUP BY config_metric, config_platform ORDER BY config_platform');
91
92         $platform_metrics = array();
93
94         if ($metrics) {
95             $current_platform_entry = null;
96             foreach ($metrics as $metric_row) {
97                 if ($is_dashboard && !Database::is_true($metric_row['in_dashboard']))
98                     continue;
99
100                 $platform_id = $metric_row['platform_id'];
101                 if (!$current_platform_entry || $current_platform_entry['id'] != $platform_id) {
102                     $current_platform_entry = &array_ensure_item_has_array($platform_metrics, $platform_id);
103                     $current_platform_entry['id'] = $platform_id;
104                     array_ensure_item_has_array($current_platform_entry, 'metrics');
105                     array_ensure_item_has_array($current_platform_entry, 'last_modified');
106                 }
107
108                 array_push($current_platform_entry['metrics'], $metric_row['metric_id']);
109                 array_push($current_platform_entry['last_modified'], intval($metric_row['last_modified']));
110             }
111         }
112         $configurations = array();
113
114         $platforms = array();
115         if ($platform_table) {
116             foreach ($platform_table as $platform_row) {
117                 if (Database::is_true($platform_row['platform_hidden']))
118                     continue;
119                 $id = $platform_row['platform_id'];
120                 if (array_key_exists($id, $platform_metrics)) {
121                     $platforms[$id] = array(
122                         'name' => $platform_row['platform_name'],
123                         'metrics' => $platform_metrics[$id]['metrics'],
124                         'lastModified' => $platform_metrics[$id]['last_modified']);
125                 }
126             }
127         }
128         return $platforms;
129     }
130
131     private function repositories($repositories_table, $repositories_with_commit) {
132         $repositories = array();
133         if (!$repositories_table)
134             return $repositories;
135         foreach ($repositories_table as $row) {
136             $repositories[$row['repository_id']] = array(
137                 'name' => $row['repository_name'],
138                 'url' => $row['repository_url'],
139                 'blameUrl' => $row['repository_blame_url'],
140                 'hasReportedCommits' => in_array($row['repository_id'], $repositories_with_commit));
141         }
142
143         return $repositories;
144     }
145
146     private function builders() {
147         $builders_table = $this->db->fetch_table('builders');
148         if (!$builders_table)
149             return array();
150         $builders = array();
151         foreach ($builders_table as $row)
152             $builders[$row['builder_id']] = array('name' => $row['builder_name'], 'buildUrl' => $row['builder_build_url']);
153
154         return $builders;
155     }
156
157     private function bug_trackers($repositories_table) {
158         $tracker_id_to_repositories = array();
159         $tracker_repositories_table = $this->db->fetch_table('tracker_repositories');
160         if ($tracker_repositories_table) {
161             foreach ($tracker_repositories_table as $row) {
162                 array_push(array_ensure_item_has_array($tracker_id_to_repositories, $row['tracrepo_tracker']),
163                     $row['tracrepo_repository']);
164             }
165         }
166
167         $bug_trackers = array();
168         $bug_trackers_table = $this->db->fetch_table('bug_trackers');
169         if ($bug_trackers_table) {
170             foreach ($bug_trackers_table as $row) {
171                 $bug_trackers[$row['tracker_id']] = array(
172                     'name' => $row['tracker_name'],
173                     'bugUrl' => $row['tracker_bug_url'],
174                     'newBugUrl' => $row['tracker_new_bug_url'],
175                     'repositories' => array_get($tracker_id_to_repositories, $row['tracker_id']));
176             }
177         }
178
179         return $bug_trackers;
180     }
181 }
182
183 ?>