Yet another set of build fixes.
[WebKit-https.git] / Websites / test-results / public / include / test-results.php
1 <?php
2
3 require_once('db.php');
4
5 function float_to_time($time_in_float) {
6     $time = new DateTime();
7     $time->setTimestamp(floatval($time_in_float));
8     return $time;
9 }
10
11 function add_builder($db, $master, $builder_name) {
12     if (!in_array($master, config('masters')))
13         return NULL;
14
15     return $db->select_or_insert_row('builders', NULL, array('master' => $master, 'name' => $builder_name));
16 }
17
18 function add_build($db, $builder_id, $build_number) {
19     return $db->select_or_insert_row('builds', NULL, array('builder' => $builder_id, 'number' => $build_number));
20 }
21
22 function add_slave($db, $name) {
23     return $db->select_or_insert_row('slaves', NULL, array('name' => $name));
24 }
25
26 function fetch_and_parse_test_results_json($url, $jsonp = FALSE) {
27     $json_contents = file_get_contents($url);
28     if (!$json_contents)
29         return NULL;
30
31     if ($jsonp)
32         $json_contents = preg_replace('/^\w+\(|\);$/', '', $json_contents);
33
34     return json_decode($json_contents, true);
35 }
36
37 function store_test_results($db, $test_results, $build_id, $start_time, $end_time, $slave_id) {
38     $db->begin_transaction();
39
40     try {
41         recursively_add_test_results($db, $build_id, $test_results['tests'], '');
42
43         $db->query_and_get_affected_rows(
44             'UPDATE builds SET (start_time, end_time, slave) = (least($1, start_time), greatest($2, end_time), $3) WHERE id = $4',
45             array($start_time->format('Y-m-d H:i:s.u'), $end_time->format('Y-m-d H:i:s.u'), $slave_id, $build_id));
46         $db->commit_transaction();
47     } catch (Exception $e) {
48         $db->rollback_transaction();
49         return FALSE;
50     }
51
52     return TRUE;
53 }
54
55 function recursively_add_test_results($db, $build_id, $tests, $full_name) {
56     if (!array_key_exists('expected', $tests) and !array_key_exists('actual', $tests)) {
57         $prefix = $full_name ? $full_name . '/' : '';
58         foreach ($tests as $name => $subtests) {
59             require_format('test_name', $name, '/^[A-Za-z0-9 +_\-\.]+$/');
60             recursively_add_test_results($db, $build_id, $subtests, $prefix . $name);
61         }
62         return;
63     }
64
65     require_format('expected_result', $tests['expected'], '/^[A-Za-z \+]+$/');
66     require_format('actual_result', $tests['actual'], '/^[A-Za-z \+]+$/');
67     require_format('test_time', $tests['time'], '/^\d*$/');
68     $modifiers = array_get($tests, 'modifiers');
69     if ($modifiers)
70         require_format('test_modifiers', $modifiers, '/^[A-Za-z0-9 \.\/]+$/');
71     else
72         $modifiers = NULL;
73     $category = 'LayoutTest'; // FIXME: Support other test categories.
74
75     $test_id = $db->select_or_insert_row('tests', NULL,
76         array('name' => $full_name),
77         array('name' => $full_name, 'reftest_type' => json_encode(array_get($tests, 'reftest_type')), 'category' => $category));
78
79     $db->insert_row('results', NULL, array('test' => $test_id, 'build' => $build_id,
80         'expected' => $tests['expected'], 'actual' => $tests['actual'],
81         'time' => $tests['time'], 'modifiers' => $tests['modifiers']));
82 }
83
84 date_default_timezone_set('UTC');
85 function parse_revisions_array($postgres_array) {
86     // e.g. {"(WebKit,131456,\"2012-10-16 14:53:00\")","(Safari,162004,)"}
87     $outer_array = json_decode('[' . trim($postgres_array, '{}') . ']');
88     $revisions = array();
89     foreach ($outer_array as $item) {
90         $name_and_revision = explode(',', trim($item, '()'));
91         $time = strtotime(trim($name_and_revision[2], '"')) * 1000;
92         $revisions[trim($name_and_revision[0], '"')] = array(trim($name_and_revision[1], '"'), $time);
93     }
94     return $revisions;
95 }
96
97 function format_result($result) {
98     return array('buildTime' => strtotime($result['start_time']) * 1000,
99         'revisions' => parse_revisions_array($result['revisions']),
100         'slave' => $result['slave'],
101         'buildNumber' => $result['number'],
102         'actual' => $result['actual'],
103         'expected' => $result['expected'],
104         'time' => $result['time'],
105         'modifiers' => $result['modifiers']);
106 }
107
108 abstract class ResultsJSONWriter {
109     private $fp;
110     private $emitted_results;
111
112     public function __construct($fp) {
113         $this->fp = $fp;
114         $this->emitted_results = FALSE;
115     }
116
117     public function start($builder_id) {
118         fwrite($this->fp, "{\"status\": \"OK\", \"builders\": {\"$builder_id\":{");
119     }
120
121     public function end($total_time) {
122         fwrite($this->fp, "}}, \"totalGenerationTime\": $total_time}");
123     }
124
125     public function add_results_for_test_if_matches($current_test, $current_results) {
126         if (!count($current_results) || $this->pass_for_failure_type($current_results))
127             return;
128         // FIXME: Why do we need to check the count?
129
130         $prefix = $this->emitted_results ? ",\n" : "";
131         fwrite($this->fp, "$prefix\"$current_test\":");
132         fwrite($this->fp, json_encode($current_results, true));
133
134         $this->emitted_results = TRUE;
135     }
136
137     abstract protected function pass_for_failure_type(&$results);
138 }
139
140 class FailingResultsJSONWriter extends ResultsJSONWriter {
141     public function __construct($fp) { parent::__construct($fp); }
142     protected function pass_for_failure_type(&$results) {
143         return $results[0]['actual'] == 'PASS';
144     }
145 }
146
147 class FlakyResultsJSONWriter extends ResultsJSONWriter {
148     public function __construct($fp) { parent::__construct($fp); }
149     protected function pass_for_failure_type(&$results) {
150         $last_index = count($results) - 1;
151         for ($i = 1; $i < $last_index; $i++) {
152             $previous_actual = $results[$i - 1]['actual'];
153             $next_actual = $results[$i + 1]['actual'];
154             if ($previous_actual == $next_actual && $results[$i]['actual'] != $previous_actual) {
155                 $results[$i]['oneOffChange'] = TRUE;
156                 return FALSE;
157             }
158         }
159         return TRUE;
160     }
161 }
162
163 class WrongExpectationsResultsJSONWriter extends ResultsJSONWriter {
164     public function __construct($fp) { parent::__construct($fp); }
165     protected function pass_for_failure_type(&$results) {
166         $latest_expected_result = $results[0]['expected'];
167         $latest_actual_result = $results[0]['actual'];
168
169         if ($latest_expected_result == $latest_actual_result)
170             return TRUE;
171
172         $tokens = explode(' ', $latest_expected_result);
173         return array_search($latest_actual_result, $tokens) !== FALSE
174             || (($latest_actual_result == 'TEXT' || $latest_actual_result == 'TEXT+IMAGE') && array_search('FAIL', $tokens) !== FALSE);
175     }
176 }
177
178 class ResultsJSONGenerator {
179     private $db;
180     private $builder_id;
181
182     const MAXIMUM_NUMBER_OF_DAYS = 30;
183
184     public function __construct($db, $builder_id)
185     {
186         $this->db = $db;
187         $this->builder_id = $builder_id;
188     }
189
190     public function generate()
191     {
192         $start_time = microtime(true);
193
194         if (!$this->builder_id)
195             return FALSE;
196
197         $number_of_days = self::MAXIMUM_NUMBER_OF_DAYS;
198         $all_results = $this->db->query(
199         "SELECT results.*, builds.* FROM results
200             JOIN (SELECT builds.*, array_agg((build_revisions.repository, build_revisions.value, build_revisions.time)) AS revisions
201                     FROM builds, build_revisions
202                     WHERE build_revisions.build = builds.id AND builds.builder = $1 AND builds.start_time > now() - interval '$number_of_days days'
203                     GROUP BY builds.id
204                     ORDER BY max(build_revisions.time) DESC) as builds ON results.build = builds.id
205             ORDER BY results.test", array($this->builder_id));
206         if (!$all_results)
207             return FALSE;
208
209         $failing_json_fp = $this->open_json_for_failure_type('failing');
210         try {
211             $flaky_json_fp = $this->open_json_for_failure_type('flaky');
212             try {
213                 $wrongexpectations_json_fp = $this->open_json_for_failure_type('wrongexpectations');
214                 try {
215                     return $this->write_jsons($all_results, array(
216                         new FailingResultsJSONWriter($failing_json_fp),
217                         new FlakyResultsJSONWriter($flaky_json_fp),
218                         new WrongExpectationsResultsJSONWriter($wrongexpectations_json_fp)), $start_time);
219                 } catch (Exception $exception) {
220                     fclose($wrongexpectations_json_fp);
221                     throw $exception;
222                 }
223             } catch (Exception $exception) {
224                 fclose($flaky_json_fp);
225                 throw $exception;
226             }
227         } catch (Exception $exception) {
228             fclose($failing_json_fp);
229             throw $exception;
230         }
231         return FALSE;
232     }
233
234     private function open_json_for_failure_type($failure_type) {
235         $failing_json_path = configPath('dataDirectory', $this->builder_id . "-$failure_type.json");
236         if (!$failing_json_path)
237             exit_with_error('FailedToDetermineResultsJSONPath', array('builderId' => $this->builder_id, 'failureType' => $failure_type));
238         $fp = fopen($failing_json_path, 'w');
239         if (!$fp)
240             exit_with_error('FailedToOpenResultsJSON', array('builderId' => $this->builder_id, 'failureType' => $failure_type));
241         return $fp;
242     }
243
244     private function write_jsons($all_results, $writers, $start_time) {
245         foreach ($writers as $writer)
246             $writer->start($this->builder_id);
247         $current_test = NULL;
248         $current_results = array();
249         while ($result = $this->db->fetch_next_row($all_results)) {
250             if ($result['test'] != $current_test) {
251                 if ($current_test) {
252                     foreach ($writers as $writer)
253                         $writer->add_results_for_test_if_matches($current_test, $current_results);
254                 }
255                 $current_results = array();
256                 $current_test = $result['test'];
257             }
258             array_push($current_results, format_result($result));
259         }
260
261         $total_time = microtime(true) - $start_time;
262         foreach ($writers as $writer)
263             $writer->end($total_time);
264
265         return TRUE;
266     }
267 }
268
269 ?>