Create analysis task should use build time as fallback when commit time is not available.
[WebKit.git] / Websites / perf.webkit.org / public / privileged-api / create-analysis-task.php
1 <?php
2
3 require_once('../include/json-header.php');
4
5 function main() {
6     $db = connect();
7     $data = ensure_privileged_api_data_and_token_or_slave($db);
8
9     $author = remote_user_name($data);
10     $name = array_get($data, 'name');
11
12     $segmentation_name = array_get($data, 'segmentationStrategy');
13     $test_range_name = array_get($data, 'testRangeStrategy');
14
15     if (!$name)
16         exit_with_error('MissingName', array('name' => $name));
17
18     $range = validate_arguments($data, array('startRun' => 'int', 'endRun' => 'int'));
19
20     $start_run = ensure_row_by_id($db, 'test_runs', 'run', $range['startRun'], 'InvalidStartRun', $range);
21     $start_run_id = $start_run['run_id'];
22     $end_run = ensure_row_by_id($db, 'test_runs', 'run', $range['endRun'], 'InvalidEndRun', $range);
23     $end_run_id = $end_run['run_id'];
24
25     $config = ensure_config_from_runs($db, $start_run, $end_run);
26
27     $start_run_time = time_for_run($db, $start_run_id);
28     $end_run_time = time_for_run($db, $end_run_id);
29     if (!$start_run_time || !$end_run_time || $start_run_time == $end_run_time)
30         exit_with_error('InvalidTimeRange', array('startTime' => $start_run_time, 'endTime' => $end_run_time));
31
32     $db->begin_transaction();
33
34     $segmentation_id = NULL;
35     if ($segmentation_name) {
36         $segmentation_id = $db->select_or_insert_row('analysis_strategies', 'strategy', array('name' => $segmentation_name));
37         if (!$segmentation_id) {
38             $db->rollback_transaction();
39             exit_with_error('CannotFindOrInsertSegmentationStrategy', array('segmentationStrategy' => $segmentation_name));
40         }
41     }
42
43     $test_range_id = NULL;
44     if ($test_range_name) {
45         $test_range_id = $db->select_or_insert_row('analysis_strategies', 'strategy', array('name' => $test_range_name));
46         if (!$test_range_id) {
47             $db->rollback_transaction();
48             exit_with_error('CannotFindOrInsertTestRangeStrategy', array('testRangeStrategy' => $test_range_name));
49         }
50     }
51
52     $duplicate = $db->select_first_row('analysis_tasks', 'task', array('start_run' => $start_run_id, 'end_run' => $end_run_id));
53     if ($duplicate) {
54         $db->rollback_transaction();
55         exit_with_error('DuplicateAnalysisTask', array('duplicate' => $duplicate));
56     }
57
58     $task_id = $db->insert_row('analysis_tasks', 'task', array(
59         'name' => $name,
60         'author' => $author,
61         'platform' => $config['config_platform'],
62         'metric' => $config['config_metric'],
63         'start_run' => $start_run_id,
64         'start_run_time' => $start_run_time,
65         'end_run' => $end_run_id,
66         'end_run_time' => $end_run_time,
67         'segmentation' => $segmentation_id,
68         'test_range' => $test_range_id));
69     $db->commit_transaction();
70
71     exit_with_success(array('taskId' => $task_id));
72 }
73
74 function ensure_row_by_id($db, $table, $prefix, $id, $error_name, $error_params) {
75     $row = $db->select_first_row($table, $prefix, array('id' => $id));
76     if (!$row)
77         exit_with_error($error_name, array($error_params));
78     return $row;
79 }
80
81 function ensure_config_from_runs($db, $start_run, $end_run) {
82     $range = array('startRun' => $start_run, 'endRun' => $end_run);
83
84     if ($start_run['run_config'] != $end_run['run_config'])
85         exit_with_error('RunConfigMismatch', $range);
86
87     return ensure_row_by_id($db, 'test_configurations', 'config', $start_run['run_config'], 'ConfigNotFound', $range);
88 }
89
90 function time_for_run($db, $run_id) {
91     $result = $db->query_and_fetch_all('SELECT max(commit_time) as time, max(build_time) as build_time
92         FROM test_runs JOIN builds ON run_build = build_id
93             JOIN build_commits ON commit_build = build_id
94             JOIN commits ON build_commit = commit_id
95         WHERE run_id = $1', array($run_id));
96
97     $first_result = array_get($result, 0, array());
98     return $first_result['time'] ? $first_result['time'] : $first_result['build_time'];
99 }
100
101 main();
102
103 ?>