build-requests should use conform to JSON API format
[WebKit-https.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 = array();
11         $this->root_sets_by_id = array();
12     }
13
14     function fetch_for_task($task_id) {
15         $this->rows = $this->db->query_and_fetch_all('SELECT *
16             FROM build_requests LEFT OUTER JOIN builds ON request_build = build_id, analysis_test_groups
17             WHERE request_group = testgroup_id AND testgroup_task = $1
18             ORDER BY request_group, request_order', array($task_id));
19     }
20
21     function fetch_for_group($test_group_id) {
22         $this->rows = $this->db->query_and_fetch_all('SELECT *
23             FROM build_requests LEFT OUTER JOIN builds ON request_build = build_id
24             WHERE request_group = $1 ORDER BY request_order', array($test_group_id));
25     }
26
27     function fetch_incomplete_requests_for_triggerable($triggerable_id) {
28         $this->rows = $this->db->query_and_fetch_all('SELECT * FROM build_requests
29             WHERE request_triggerable = $1 AND request_status != \'completed\'
30             ORDER BY request_created_at, request_group, request_order', array($triggerable_id));
31     }
32
33     function fetch_request($request_id) {
34         $this->rows = $this->db->select_rows('build_requests', 'request', array('id' => $request_id));
35     }
36
37     function has_results() { return is_array($this->rows); }
38     function results() { return $this->results_internal(false); }
39     function results_with_resolved_ids() { return $this->results_internal(true); }
40
41     private function results_internal($resolve_ids) {
42         if (!$this->rows)
43             return array();
44
45         $id_to_platform_name = array();
46         if ($resolve_ids) {
47             foreach ($this->db->select_rows('platforms', 'platform', array()) as $platform)
48                 $id_to_platform_name[$platform['platform_id']] = $platform['platform_name'];
49         }
50         $test_path_resolver = new TestPathResolver($this->db);
51
52         $requests = array();
53         foreach ($this->rows as $row) {
54             $test_id = $row['request_test'];
55             $platform_id = $row['request_platform'];
56             $root_set_id = $row['request_root_set'];
57
58             $this->fetch_roots_for_set_if_needed($root_set_id, $resolve_ids);
59
60             array_push($requests, array(
61                 'id' => $row['request_id'],
62                 'triggerable' => $row['request_triggerable'],
63                 'test' => $resolve_ids ? $test_path_resolver->path_for_test($test_id) : $test_id,
64                 'platform' => $resolve_ids ? $id_to_platform_name[$platform_id] : $platform_id,
65                 'testGroup' => $row['request_group'],
66                 'order' => $row['request_order'],
67                 'rootSet' => $root_set_id,
68                 'status' => $row['request_status'],
69                 'url' => $row['request_url'],
70                 'build' => $row['request_build'],
71                 'createdAt' => $row['request_created_at'] ? strtotime($row['request_created_at']) * 1000 : NULL,
72             ));
73         }
74         return $requests;
75     }
76
77     function root_sets() {
78         return $this->root_sets;
79     }
80
81     function roots() {
82         return $this->roots;
83     }
84
85     private function fetch_roots_for_set_if_needed($root_set_id, $resolve_ids) {
86         if (array_key_exists($root_set_id, $this->root_sets_by_id))
87             return;
88
89         $root_rows = $this->db->query_and_fetch_all('SELECT *
90             FROM roots, commits LEFT OUTER JOIN repositories ON commit_repository = repository_id
91             WHERE root_commit = commit_id AND root_set = $1', array($root_set_id));
92
93         $root_ids = array();
94         foreach ($root_rows as $row) {
95             $repository = $row['repository_id'];
96             $revision = $row['commit_revision'];
97             $commit_time = $row['commit_time'];
98             $root_id = $root_set_id . '-' . $repository;
99             array_push($root_ids, $root_id);
100             $repository_id = $resolve_ids ? $row['repository_name'] : $row['repository_id'];
101             array_push($this->roots, array(
102                 'id' => $root_id,
103                 'repository' => $repository_id,
104                 'revision' => $revision,
105                 'time' => Database::to_js_time($commit_time)));
106         }
107
108         $this->root_sets_by_id[$root_set_id] = TRUE;
109
110         array_push($this->root_sets, array('id' => $root_set_id, 'roots' => $root_ids));
111     }
112 }
113
114 ?>