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