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