ce52cbad2c39aedd2f9f68b7501dbab733550eec
[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_by_id = array();
10     }
11
12     function fetch_for_task($task_id) {
13         $this->rows = $this->db->query_and_fetch_all('SELECT *
14             FROM build_requests LEFT OUTER JOIN builds ON request_build = build_id, analysis_test_groups
15             WHERE request_group = testgroup_id AND testgroup_task = $1
16             ORDER BY request_group, request_order', array($task_id));
17     }
18
19     function fetch_for_group($test_group_id) {
20         $this->rows = $this->db->query_and_fetch_all('SELECT *
21             FROM build_requests LEFT OUTER JOIN builds ON request_build = build_id
22             WHERE request_group = $1 ORDER BY request_order', array($test_group_id));
23     }
24
25     function fetch_incomplete_requests_for_triggerable($triggerable_id) {
26         $this->rows = $this->db->query_and_fetch_all('SELECT * FROM build_requests
27             WHERE request_triggerable = $1 AND request_status != \'completed\'
28             ORDER BY request_created_at, request_group, request_order', array($triggerable_id));
29     }
30
31     function has_results() { return is_array($this->rows); }
32     function results() { return $this->results_internal(false); }
33     function results_with_resolved_ids() { return $this->results_internal(true); }
34
35     private function results_internal($resolve_ids) {
36         if (!$this->rows)
37             return array();
38
39         $id_to_platform_name = array();
40         if ($resolve_ids) {
41             foreach ($this->db->select_rows('platforms', 'platform', array()) as $platform)
42                 $id_to_platform_name[$platform['platform_id']] = $platform['platform_name'];
43         }
44         $test_path_resolver = new TestPathResolver($this->db);
45
46         $requests = array();
47         foreach ($this->rows as $row) {
48             $test_id = $row['request_test'];
49             $platform_id = $row['request_platform'];
50             $root_set_id = $row['request_root_set'];
51
52             if (!array_key_exists($root_set_id, $this->root_sets_by_id))
53                 $this->root_sets_by_id[$root_set_id] = $this->fetch_roots_for_set($root_set_id);
54
55             array_push($requests, array(
56                 'id' => $row['request_id'],
57                 'triggerable' => $row['request_triggerable'],
58                 'test' => $resolve_ids ? $test_path_resolver->path_for_test($test_id) : $test_id,
59                 'platform' => $resolve_ids ? $id_to_platform_name[$platform_id] : $platform_id,
60                 'testGroup' => $row['request_group'],
61                 'order' => $row['request_order'],
62                 'rootSet' => $root_set_id,
63                 'status' => $row['request_status'],
64                 'url' => $row['request_url'],
65                 'build' => $row['request_build'],
66                 'createdAt' => $row['request_created_at'] ? strtotime($row['request_created_at']) * 1000 : NULL,
67             ));
68         }
69         return $requests;
70     }
71
72     function root_sets() {
73         return $this->root_sets_by_id;
74     }
75
76     private function fetch_roots_for_set($root_set_id) {
77         $root_rows = $this->db->query_and_fetch_all('SELECT *
78             FROM roots, commits LEFT OUTER JOIN repositories ON commit_repository = repository_id
79             WHERE root_commit = commit_id AND root_set = $1', array($root_set_id));
80
81         $roots = array();
82         foreach ($root_rows as $row)
83             $roots[$row['repository_name']] = $row['commit_revision'];
84
85         return $roots;
86     }
87 }
88
89 ?>