Extend create-analysis-test API to be able to create with confirming test group.
[WebKit-https.git] / Websites / perf.webkit.org / public / api / analysis-tasks.php
index a31b067..160cff4 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
-require('../include/json-header.php');
+require_once('../include/json-header.php');
+require_once('../include/commit-log-fetcher.php');
 
 function main($path) {
     $db = new Database;
@@ -10,8 +11,16 @@ function main($path) {
     if (count($path) > 1)
         exit_with_error('InvalidRequest');
 
-    if (count($path) > 0 && $path[0]) {
-        $task_id = intval($path[0]);
+    $build_request_id = array_get($_GET, 'buildRequest');
+    $task_id = count($path) > 0 && $path[0] ? $path[0] : array_get($_GET, 'id');
+
+    if ($build_request_id) {
+        $tasks = $db->query_and_fetch_all('SELECT analysis_tasks.* FROM build_requests, analysis_test_groups, analysis_tasks
+            WHERE request_id = $1 AND request_group = testgroup_id AND testgroup_task = task_id', array(intval($build_request_id)));
+        if (!$tasks)
+            exit_with_error('TaskNotFound', array('buildRequest' => $build_request_id));
+    } else if ($task_id) {
+        $task_id = intval($task_id);
         $task = $db->select_first_row('analysis_tasks', 'task', array('id' => $task_id));
         if (!$task)
             exit_with_error('TaskNotFound', array('id' => $task_id));
@@ -34,12 +43,10 @@ function main($path) {
     }
 
     $tasks = array_map("format_task", $tasks);
-    $bugs = fetch_and_push_bugs_to_tasks($db, $tasks);
-
-    exit_with_success(array('analysisTasks' => $tasks, 'bugs' => $bugs));
+    exit_with_success(fetch_associated_data_for_tasks($db, $tasks));
 }
 
-function fetch_and_push_bugs_to_tasks($db, &$tasks) {
+function fetch_associated_data_for_tasks($db, &$tasks) {
     $task_ids = array();
     $task_by_id = array();
     foreach ($tasks as &$task) {
@@ -57,10 +64,15 @@ function fetch_and_push_bugs_to_tasks($db, &$tasks) {
         array_push($associated_task['bugs'], $bug['id']);
     }
 
+    $commit_log_fetcher = new CommitLogFetcher($db);
+    $commits = $commit_log_fetcher->fetch_for_tasks($task_ids, $task_by_id);
+    if (!is_array($commits))
+        exit_with_error('FailedToFetchCommits');
+
     $task_build_counts = $db->query_and_fetch_all('SELECT
         testgroup_task AS "task",
         count(testgroup_id) as "total",
-        sum(case when request_status = \'failed\' or request_status = \'completed\' then 1 else 0 end) as "finished"
+        sum(case when request_status = \'failed\' or request_status = \'completed\' or request_status = \'canceled\' then 1 else 0 end) as "finished"
         FROM analysis_test_groups, build_requests
         WHERE request_group = testgroup_id AND testgroup_task = ANY($1) GROUP BY testgroup_task',
         array('{' . implode(', ', $task_ids) . '}'));
@@ -73,7 +85,7 @@ function fetch_and_push_bugs_to_tasks($db, &$tasks) {
         $task['finishedBuildRequestCount'] = $build_count['finished'];
     }
 
-    return $bugs;
+    return array('analysisTasks' => $tasks, 'bugs' => $bugs, 'commits' => $commits);
 }
 
 function format_task($task_row) {
@@ -81,12 +93,20 @@ function format_task($task_row) {
         'id' => $task_row['task_id'],
         'name' => $task_row['task_name'],
         'author' => $task_row['task_author'],
+        'segmentationStrategy' => $task_row['task_segmentation'],
+        'testRangeStragegy' => $task_row['task_test_range'],
         'createdAt' => strtotime($task_row['task_created_at']) * 1000,
         'platform' => $task_row['task_platform'],
         'metric' => $task_row['task_metric'],
         'startRun' => $task_row['task_start_run'],
+        'startRunTime' => Database::to_js_time($task_row['task_start_run_time']),
         'endRun' => $task_row['task_end_run'],
+        'endRunTime' => Database::to_js_time($task_row['task_end_run_time']),
+        'result' => $task_row['task_result'],
+        'needed' => $task_row['task_needed'] ? Database::is_true($task_row['task_needed']) : null,
         'bugs' => array(),
+        'causes' => array(),
+        'fixes' => array(),
     );
 }