New perf dashboard should provide UI to create a new analysis task
[WebKit-https.git] / Websites / perf.webkit.org / public / api / test-groups.php
1 <?php
2
3 require('../include/json-header.php');
4
5 function main($path) {
6     $db = new Database;
7     if (!$db->connect())
8         exit_with_error('DatabaseConnectionFailure');
9
10     if (count($path) > 1)
11         exit_with_error('InvalidRequest');
12
13     if (count($path) > 0 && $path[0]) {
14         $group_id = intval($path[0]);
15         $group = $db->select_first_row('analysis_test_groups', 'testgroup', array('id' => $group_id));
16         if (!$group)
17             exit_with_error('GroupNotFound', array('id' => $group_id));
18         $test_groups = array($group);
19         $build_requests = fetch_build_requests_for_group($db, $group_id);
20     } else {
21         $task_id = array_get($_GET, 'task');
22         if (!$task_id)
23             exit_with_error('TaskIdNotSpecified');
24
25         $test_groups = fetch_test_groups_for_task($db, $task_id);
26         if (!is_array($test_groups))
27             exit_with_error('FailedToFetchTestGroups');
28         $build_requests = fetch_build_requests_for_task($db, $task_id);
29     }
30     if (!is_array($build_requests))
31         exit_with_error('FailedToFetchBuildRequests');
32
33     $test_groups = array_map("format_test_group", $test_groups);
34     $group_by_id = array();
35     foreach ($test_groups as &$group)
36         $group_by_id[$group['id']] = &$group;
37
38     $build_requests = array_map("format_build_request", $build_requests);
39     foreach ($build_requests as $request)
40         array_push($group_by_id[$request['testGroup']]['buildRequests'], $request['id']);
41
42     exit_with_success(array('testGroups' => $test_groups, 'buildRequests' => $build_requests));
43 }
44
45 function fetch_test_groups_for_task($db, $task_id) {
46     return $db->select_rows('analysis_test_groups', 'testgroup', array('task' => $task_id));
47 }
48
49 function fetch_build_requests_for_task($db, $task_id) {
50     return $db->query_and_fetch_all('SELECT * FROM build_requests, builds
51         WHERE request_build = build_id
52             AND request_group IN (SELECT testgroup_id FROM analysis_test_groups WHERE testgroup_task = $1)
53         ORDER BY request_group, request_order', array($task_id));
54 }
55
56 function fetch_build_requests_for_group($db, $test_group_id) {
57     return $db->query_and_fetch_all('SELECT * FROM build_requests, builds
58         WHERE request_build = build_id AND request_group = $1 ORDER BY request_order', array($test_group_id));
59 }
60
61 date_default_timezone_set('UTC');
62 function format_test_group($group_row) {
63     return array(
64         'id' => $group_row['testgroup_id'],
65         'task' => $group_row['testgroup_task'],
66         'name' => $group_row['testgroup_name'],
67         'author' => $group_row['testgroup_author'],
68         'createdAt' => strtotime($group_row['testgroup_created_at']) * 1000,
69         'buildRequests' => array(),
70     );
71 }
72
73 function format_build_request($request_row) {
74     return array(
75         'id' => $request_row['request_id'],
76         'testGroup' => $request_row['request_group'],
77         'order' => $request_row['request_order'],
78         'rootSet' => $request_row['request_root_set'],
79         'build' => $request_row['request_build'],
80         'builder' => $request_row['build_builder'],
81         'buildNumber' => $request_row['build_number'],
82         'buildTime' => $request_row['build_time'] ? strtotime($request_row['build_time']) * 1000 : NULL,
83     );
84 }
85
86 main(array_key_exists('PATH_INFO', $_SERVER) ? explode('/', trim($_SERVER['PATH_INFO'], '/')) : array());
87
88 ?>