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