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