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