352f72391ec3c647b93d9585f8f804bc615c69a1
[WebKit-https.git] / Websites / perf.webkit.org / public / api / analysis-tasks.php
1 <?php
2
3 require('../include/json-header.php');
4 require('../include/commit-log-fetcher.php');
5
6 function main($path) {
7     $db = new Database;
8     if (!$db->connect())
9         exit_with_error('DatabaseConnectionFailure');
10
11     if (count($path) > 1)
12         exit_with_error('InvalidRequest');
13
14     $build_request_id = array_get($_GET, 'buildRequest');
15     $task_id = count($path) > 0 && $path[0] ? $path[0] : array_get($_GET, 'id');
16
17     if ($build_request_id) {
18         $tasks = $db->query_and_fetch_all('SELECT analysis_tasks.* FROM build_requests, analysis_test_groups, analysis_tasks
19             WHERE request_id = $1 AND request_group = testgroup_id AND testgroup_task = task_id', array(intval($build_request_id)));
20         if (!$tasks)
21             exit_with_error('TaskNotFound', array('buildRequest' => $build_request_id));
22     } else if ($task_id) {
23         $task_id = intval($task_id);
24         $task = $db->select_first_row('analysis_tasks', 'task', array('id' => $task_id));
25         if (!$task)
26             exit_with_error('TaskNotFound', array('id' => $task_id));
27         $tasks = array($task);
28     } else {
29         $metric_id = array_get($_GET, 'metric');
30         $platform_id = array_get($_GET, 'platform');
31         if (!!$metric_id != !!$platform_id)
32             exit_with_error('InvalidArguments', array('metricId' => $metric_id, 'platformId' => $platform_id));
33
34         if ($metric_id)
35             $tasks = $db->select_rows('analysis_tasks', 'task', array('platform' => $platform_id, 'metric' => $metric_id));
36         else {
37             // FIXME: Limit the number of tasks we fetch.
38             $tasks = array_reverse($db->fetch_table('analysis_tasks', 'task_created_at'));
39         }
40
41         if (!is_array($tasks))
42             exit_with_error('FailedToFetchTasks');
43     }
44
45     $tasks = array_map("format_task", $tasks);
46     exit_with_success(fetch_associated_data_for_tasks($db, $tasks));
47 }
48
49 function fetch_associated_data_for_tasks($db, &$tasks) {
50     $task_ids = array();
51     $task_by_id = array();
52     foreach ($tasks as &$task) {
53         array_push($task_ids, $task['id']);
54         $task_by_id[$task['id']] = &$task;
55     }
56
57     $bugs = $db->query_and_fetch_all('SELECT bug_id AS "id", bug_task AS "task", bug_tracker AS "bugTracker", bug_number AS "number"
58         FROM bugs WHERE bug_task = ANY ($1)', array('{' . implode(', ', $task_ids) . '}'));
59     if (!is_array($bugs))
60         exit_with_error('FailedToFetchBugs');
61
62     foreach ($bugs as $bug) {
63         $associated_task = &$task_by_id[$bug['task']];
64         array_push($associated_task['bugs'], $bug['id']);
65     }
66
67     $commit_log_fetcher = new CommitLogFetcher($db);
68     $commits = $commit_log_fetcher->fetch_for_tasks($task_ids, $task_by_id);
69     if (!is_array($commits))
70         exit_with_error('FailedToFetchCommits');
71
72     $task_build_counts = $db->query_and_fetch_all('SELECT
73         testgroup_task AS "task",
74         count(testgroup_id) as "total",
75         sum(case when request_status = \'failed\' or request_status = \'completed\' or request_status = \'canceled\' then 1 else 0 end) as "finished"
76         FROM analysis_test_groups, build_requests
77         WHERE request_group = testgroup_id AND testgroup_task = ANY($1) GROUP BY testgroup_task',
78         array('{' . implode(', ', $task_ids) . '}'));
79     if (!is_array($task_build_counts))
80         exit_with_error('FailedToFetchTestGroups');
81
82     foreach ($task_build_counts as $build_count) {
83         $task = &$task_by_id[$build_count['task']];
84         $task['buildRequestCount'] = $build_count['total'];
85         $task['finishedBuildRequestCount'] = $build_count['finished'];
86     }
87
88     return array('analysisTasks' => $tasks, 'bugs' => $bugs, 'commits' => $commits);
89 }
90
91 function format_task($task_row) {
92     return array(
93         'id' => $task_row['task_id'],
94         'name' => $task_row['task_name'],
95         'author' => $task_row['task_author'],
96         'segmentationStrategy' => $task_row['task_segmentation'],
97         'testRangeStragegy' => $task_row['task_test_range'],
98         'createdAt' => strtotime($task_row['task_created_at']) * 1000,
99         'platform' => $task_row['task_platform'],
100         'metric' => $task_row['task_metric'],
101         'startRun' => $task_row['task_start_run'],
102         'startRunTime' => Database::to_js_time($task_row['task_start_run_time']),
103         'endRun' => $task_row['task_end_run'],
104         'endRunTime' => Database::to_js_time($task_row['task_end_run_time']),
105         'result' => $task_row['task_result'],
106         'needed' => $task_row['task_needed'] ? Database::is_true($task_row['task_needed']) : null,
107         'bugs' => array(),
108         'causes' => array(),
109         'fixes' => array(),
110     );
111 }
112
113 main(array_key_exists('PATH_INFO', $_SERVER) ? explode('/', trim($_SERVER['PATH_INFO'], '/')) : array());
114
115 ?>