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