BuildRequest should have a method to fetch all in-progress and pending requests for...
[WebKit.git] / Websites / perf.webkit.org / public / include / build-requests-fetcher.php
1 <?php
2
3 require_once('test-path-resolver.php');
4
5 class BuildRequestsFetcher {
6     function __construct($db) {
7         $this->db = $db;
8         $this->rows = null;
9         $this->root_sets = array();
10         $this->roots_by_id = array();
11         $this->roots = array();
12         $this->root_sets_by_id = array();
13     }
14
15     function fetch_for_task($task_id) {
16         $this->rows = $this->db->query_and_fetch_all('SELECT *
17             FROM build_requests LEFT OUTER JOIN builds ON request_build = build_id, analysis_test_groups
18             WHERE request_group = testgroup_id AND testgroup_task = $1
19             ORDER BY request_group, request_order', array($task_id));
20     }
21
22     function fetch_for_group($test_group_id) {
23         $this->rows = $this->db->query_and_fetch_all('SELECT *
24             FROM build_requests LEFT OUTER JOIN builds ON request_build = build_id
25             WHERE request_group = $1 ORDER BY request_order', array($test_group_id));
26     }
27
28     function fetch_incomplete_requests_for_triggerable($triggerable_id) {
29         $this->rows = $this->db->query_and_fetch_all('SELECT * FROM build_requests
30             WHERE request_triggerable = $1 AND request_group
31                 IN (SELECT testgroup_id FROM analysis_test_groups WHERE EXISTS
32                     (SELECT 1 FROM build_requests WHERE testgroup_id = request_group AND request_status
33                         IN (\'pending\', \'scheduled\', \'running\')))
34             ORDER BY request_group, request_order', array($triggerable_id));
35     }
36
37     function fetch_request($request_id) {
38         $this->rows = $this->db->select_rows('build_requests', 'request', array('id' => $request_id));
39     }
40
41     function has_results() { return is_array($this->rows); }
42     function results() { return $this->results_internal(false); }
43     function results_with_resolved_ids() { return $this->results_internal(true); }
44
45     private function results_internal($resolve_ids) {
46         if (!$this->rows)
47             return array();
48
49         $id_to_platform_name = array();
50         if ($resolve_ids) {
51             foreach ($this->db->select_rows('platforms', 'platform', array()) as $platform)
52                 $id_to_platform_name[$platform['platform_id']] = $platform['platform_name'];
53         }
54         $test_path_resolver = new TestPathResolver($this->db);
55
56         $requests = array();
57         foreach ($this->rows as $row) {
58             $test_id = $row['request_test'];
59             $platform_id = $row['request_platform'];
60             $root_set_id = $row['request_root_set'];
61
62             $this->fetch_roots_for_set_if_needed($root_set_id, $resolve_ids);
63
64             array_push($requests, array(
65                 'id' => $row['request_id'],
66                 'triggerable' => $row['request_triggerable'],
67                 'test' => $resolve_ids ? $test_path_resolver->path_for_test($test_id) : $test_id,
68                 'platform' => $resolve_ids ? $id_to_platform_name[$platform_id] : $platform_id,
69                 'testGroup' => $row['request_group'],
70                 'order' => $row['request_order'],
71                 'rootSet' => $root_set_id,
72                 'status' => $row['request_status'],
73                 'url' => $row['request_url'],
74                 'build' => $row['request_build'],
75                 'createdAt' => $row['request_created_at'] ? strtotime($row['request_created_at']) * 1000 : NULL,
76             ));
77         }
78         return $requests;
79     }
80
81     function root_sets() {
82         return $this->root_sets;
83     }
84
85     function roots() {
86         return $this->roots;
87     }
88
89     private function fetch_roots_for_set_if_needed($root_set_id, $resolve_ids) {
90         if (array_key_exists($root_set_id, $this->root_sets_by_id))
91             return;
92
93         $root_rows = $this->db->query_and_fetch_all('SELECT *
94             FROM roots, commits LEFT OUTER JOIN repositories ON commit_repository = repository_id
95             WHERE root_commit = commit_id AND root_set = $1', array($root_set_id));
96
97         $root_ids = array();
98         foreach ($root_rows as $row) {
99             $repository_id = $resolve_ids ? $row['repository_name'] : $row['repository_id'];
100             $revision = $row['commit_revision'];
101             $commit_time = $row['commit_time'];
102             array_push($root_ids, $row['commit_id']);
103
104             $root_id = $row['commit_id'];
105             if (array_key_exists($root_id, $this->roots_by_id))
106                 continue;
107
108             array_push($this->roots, array(
109                 'id' => $root_id,
110                 'repository' => $repository_id,
111                 'revision' => $revision,
112                 'time' => Database::to_js_time($commit_time)));
113
114             $this->roots_by_id[$root_id] = TRUE;
115         }
116
117         $this->root_sets_by_id[$root_set_id] = TRUE;
118
119         array_push($this->root_sets, array('id' => $root_set_id, 'roots' => $root_ids));
120     }
121 }
122
123 ?>