Unreviewed. Commit the forgotten change.
[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             'dashboards' => config('dashboards'),
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_id']] = array(
104                 'name' => $row['repository_name'],
105                 'url' => $row['repository_url'],
106                 'blameUrl' => $row['repository_blame_url'],
107                 'hasReportedCommits' => in_array($row['repository_id'], $repositories_with_commit));
108         }
109
110         return $repositories;
111     }
112
113     private function builders() {
114         $builders_table = $this->db->fetch_table('builders');
115         if (!$builders_table)
116             return array();
117         $builders = array();
118         foreach ($builders_table as $row)
119             $builders[$row['builder_id']] = array('name' => $row['builder_name'], 'buildUrl' => $row['builder_build_url']);
120
121         return $builders;
122     }
123
124     private function bug_trackers($repositories_table) {
125         $tracker_id_to_repositories = array();
126         $tracker_repositories_table = $this->db->fetch_table('tracker_repositories');
127         if ($tracker_repositories_table) {
128             foreach ($tracker_repositories_table as $row) {
129                 array_push(array_ensure_item_has_array($tracker_id_to_repositories, $row['tracrepo_tracker']),
130                     $row['tracrepo_repository']);
131             }
132         }
133
134         $bug_trackers = array();
135         $bug_trackers_table = $this->db->fetch_table('bug_trackers');
136         if ($bug_trackers_table) {
137             foreach ($bug_trackers_table as $row) {
138                 $bug_trackers[$row['tracker_id']] = array(
139                     'name' => $row['tracker_name'],
140                     'bugUrl' => $row['tracker_bug_url'],
141                     'newBugUrl' => $row['tracker_new_bug_url'],
142                     'repositories' => $tracker_id_to_repositories[$row['tracker_id']]);
143             }
144         }
145
146         return $bug_trackers;
147     }
148 }
149
150 ?>