summaryrefslogtreecommitdiffstats
path: root/autobuild/results.php
blob: c9bb05dde1ff4e8651ea1435af45ce0bb97f392c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<html>
<head>
    <link rel="author" href="http://www.mageia.org/">
    <link rel="icon" type="image/png" href="../themes/mageia/favicon.png">
    <link rel="stylesheet" href="../themes/mageia/style.css">
<?php

function parse_package($rpm) {
	if (preg_match("/(.*)-([^-]*-[^-]*mga)[0-9]+.src.rpm/", $rpm, $matches)) {
	    return Array('package' => $matches[1], 'version' => $matches[2]);
	} else {
	    return false;
	}
}

$db = new SQLite3('autobuild.db');
$runs = Array();
$handle = opendir('cauldron/x86_64/core/');
while (false !== ($entry = readdir($handle))) {
	if (preg_match("/^....-..-..$/", $entry, $matches)) {
		array_push($runs, $matches[0]);
	}
}
closedir($handle);
$handle = opendir('cauldron/aarch64/core/');
while (false !== ($entry = readdir($handle))) {
	if (preg_match("/^....-..-..$/", $entry, $matches)) {
		array_push($runs, $matches[0]);
	}
}
closedir($handle);
sort($runs);

$latestlinks = glob("cauldron/*/core/latest");
$latest = 0;
foreach ($latestlinks as $link) {
	# This is ugly but readlink() returns false even if is_link() returns
	# true and readlink() as user apache in php cli works.
	$date = trim(`readlink $link`);
	if ($date > $latest) {
		$latest = $date;
	}
}

$run = $_GET['run'];
if (!$run || $run=="latest") {
	$run = $latest;
}

foreach ($runs as $r) {
	if ($r==$run) {
		break;
	}
	$prev = $r;
}
$runepoc = strtotime($run);

$packages = Array();
if ($handle = opendir('/distrib/bootstrap/distrib/cauldron/SRPMS/core/release/')) {
	while (false !== ($entry = readdir($handle))) {
		if ($parsed = parse_package($entry)) {
			$packages[$parsed['package']] = $entry;
		}
	}
	closedir($handle);
}

$prev_failure = Array();
if ($prev) {
	$status_name = array_shift(glob("cauldron/*/core/$prev/status.core.log"));
	$status_file = fopen($status_name, "r");
	if ($status_file !== false) {
		// The file should always be there but better not get an infinite loop flooding the logs when it disappears
		while (!feof($status_file)) {
			$line = fgets($status_file);
			if (preg_match("/^(.*): (.*)$/", $line, $matches)) {
				$rpm = parse_package($matches[1]);
				$status = $matches[2];
				if ($status != "ok" && $status != "unknown" && $status != "not_on_this_arch") {
					$prev_failure[$rpm['package']] = 1;
				}
			}
		}
		fclose($status_file);
	}
}

$success = Array();
$failure = Array();
$fixed = Array();
$removed = Array();
$broken = Array();

$base_dir = array_shift(glob("cauldron/*/core/$run"));

$status_name = "$base_dir/status.core.log";
if (!file_exists($status_name)) {
	echo "Invalid run";
	exit;
}

$start_time = 0;
if (file_exists("$base_dir/VERSION")) {
#Mageia 5 Devel-i586-Download 20140311 15:50
#Mageia 5 Devel-x86_64-Download 20140311 15:50
	$version = file_get_contents("$base_dir/VERSION");
	preg_match('/ (\d{8} \d{2}:\d{2})$/', $version, $matches);
	$start_time = strtotime($matches[1]);
}
$status_file = fopen($status_name, "r");
while (!feof($status_file)) {
	$line = fgets($status_file);
	if (preg_match("/^(.*): (.*)$/", $line, $matches)) {
		$rpm = $matches[1];
		$status = $matches[2];
		if ($start_time == 0 && $status != 'recreate_srpm_failure' && $status != 'not_on_this_arch') {
			$build_stat = stat("$base_dir/$rpm");
			if ($build_stat && $build_stat['mtime']) {
				$start_time = $build_stat['mtime'];
			}
		}
		if ($status == "ok") {
			array_push($success, $rpm);
		} elseif ($status != "unknown" && $status != "not_on_this_arch"){
			$failure[$rpm] = $status;
			$parsed = parse_package($rpm);
			$package = $parsed['package'];
			if(!array_key_exists($package, $prev_failure)) {
				$broken[$rpm] = 1;
			}
			if(!array_key_exists($package, $packages)) {
				$removed[$rpm] = 1;
			} else {
				# For recreate_srpm_failure, $rpm is the one that was in the repo
				# For other failures, mga release may be incorrect so use something less reliable
				if ($status == 'recreate_srpm_failure' && $rpm != $packages[$package]) {
					$fixed[$rpm] = 1;
				} else {
					$file = '/distrib/bootstrap/distrib/cauldron/SRPMS/core/release/' . $packages[$package];
					if (file_exists($file)) {
						$pkg_stat = stat($file);
						if ($pkg_stat['mtime'] > $start_time) {
							$fixed[$rpm] = 1;
						}
					}
				}
			}
		}
	}
}
fclose($status_file);

sort($success);
ksort($failure);

$nb_failed = count($failure);
$nb_success = count($success);
$nb_fixed = count($fixed);
$nb_removed = count($removed);
$nb_still_broken = $nb_failed - $nb_fixed - $nb_removed;
$nb_tried = $nb_failed + $nb_success;
$succes_percent = round($nb_success*1000/$nb_tried)/10;
$estimated_percent = round(($nb_success+$nb_fixed)*1000/($nb_tried-$nb_removed))/10;

$title = "Autobuild results for $run";
echo "<title>$title</title>\n";
?>
<style>
table.failureTable {
  background-color: #f7f7f7;
}
table.failureTable tr:nth-child(even) {
  background: rgba(50,160,200, 0.1);
}
table.failureTable thead th {
  font-weight: bold;
}
</style>
<meta charset="utf-8">
</head>
<body>
<?php

# See if the pattern of recent build failures indicate a flaky build
# This looks at the last 10 builds for at least 4 result change
function is_flaky($history) {
	  if (count($history) < 10)
		return false;

	  # Most recent 5 builds
	  $num_change = 0;
	  $current = $history[0];
	  for ($i = 1; $i < 10; $i++) {
		  if ($history[$i] != $current) {
			  $num_change++;
			  $current = $history[$i];
		  }
	  }

	  return $num_change >= 4;
}

function get_package_id($package) {
	global $db;
	return $db->querySingle("SELECT Id FROM Packages WHERE Name = '$package'");
}

function get_build_history($package_id) {
	global $db;
	if ($package_id)  {
		# TODO: optimize by retrieving Runs.Id once at the start
		$result = $db->query("SELECT ResultValues.Name FROM Runs, Results, ResultValues WHERE Runs.Id = Results.Run AND Results.Result = ResultValues.Id AND Results.Package = '$package_id' ORDER BY Start DESC LIMIT 10");
		if ($result) {
			$build_stats = Array();
			while ($entry = $result->fetchArray(SQLITE3_ASSOC)) {
			    array_push($build_stats, $entry['Name']);
			}
			return $build_stats;
		}
	}
	return false;
}

function get_package_attributes($package_id) {
	global $db, $runepoc;
	if ($package_id)  {
	         # TODO: optimize by retrieving Runs.Id once at the start
	         return $db->querySingle("SELECT Attr FROM Attributes, Runs WHERE Runs.Id = Attributes.Run AND Runs.Start = $runepoc AND Attributes.Package = '$package_id';");
	}
	return "";
}

function get_all_attributes() {
	global $db, $runepoc;
	$attributes = Array();
	$attr_types = Array();
        $result = $db->query("SELECT Attr FROM Attributes, Runs WHERE Runs.Id = Attributes.Run AND Runs.Start = $runepoc;");
	if ($result) {
		while ($entry = $result->fetchArray(SQLITE3_ASSOC)) {
			foreach (explode(" ", $entry['Attr']) as $attr) {
				if ($attr) {
					$attributes[$attr] = True;
					$attr_types[explode("_", $attr)[0]] = Array();
				}
			}
		}
		foreach (array_keys($attributes) as $attr) {
			$parts = explode("_", $attr);
			array_push($attr_types[$parts[0]], $parts[1]);
		}
		foreach ($attr_types as $attr_type => $attrs) {
			sort($attr_types[$attr_type]);
		}
	}
	ksort($attr_types);
	return $attr_types;
}

function get_package_maintainers() {
	global $failure;
	$pkgs = Array();
	$maintdb = file('/var/www/bs/data/maintdb.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
	foreach ($maintdb as $l) {
		list($pkg, $m) = explode(" ", $l);
		if ($m == "") {
			$m = "nobody";
		}
		$pkgs[$pkg] = $m;
	}
	$failedpkgs = Array();
	foreach ($failure as $rpm => $error) {
		$parsed = parse_package($rpm);
		$pkg = $parsed["package"];
		$failedpkgs[$pkg] = $pkgs[$pkg];
	}
	return $failedpkgs;
}

echo "<header id=\"mgnavt\"><h1>$title</h1><ul><li>$succes_percent% success</li></ul></header><article>\n";
echo "<form><select name='run' onChange='document.location.href=\"".$_SERVER["PHP_SELF"]."?run=\"+this.form.run.value'>";
foreach ($runs as $r) {
	$in_progress = ($r > $latest) ? ' (in progress)' : '';
	$selected = ($r == $run) ? ' selected' : '';
	echo "<option value='$r'$selected>$r$in_progress</option>";
}
echo "</select></form>";
if (array_key_exists('include_success', $_GET) && $_GET['include_success'] === 'true') {
	echo "<a href=\"" . $_SERVER["PHP_SELF"] . "?run=$run\">Only failures</a>";
} else {
	echo "<a href=\"" . $_SERVER["PHP_SELF"] . "?run=$run&amp;include_success=true\">Include successful packages</a>";
}
echo "</div>\n";
echo "<h2>Changes since this run</h2>\n";
echo "<table>\n";
echo "<tr><td>Fixed</td><td>$nb_fixed</td></tr>\n";
echo "<tr><td>Removed</td><td>$nb_removed</td></tr>\n";
echo "<tr><td>Unchanged</td><td>$nb_still_broken</td></tr>\n";
echo "</table>\n";
echo "<br />If no new package was broken, success rate next time should be $estimated_percent%.\n";
$all_attrs = get_all_attributes();
$pkg_maintainers = get_package_maintainers();
$maintainers_package_count = array_count_values($pkg_maintainers);
arsort($maintainers_package_count);
$filters = Array();
if ($all_attrs) {
	echo "<h2>Filters</h2>\n";
	echo "<i>If nothing is checked on a line, that category is not filtered, else only the values checked appear.<i>";
	echo "<form action=\"" . $_SERVER["PHP_SELF"] . "\" method=\"get\">\n";
	if ($_GET["run"]) {
		echo "<input type=\"hidden\" name=\"run\" value=\"" . $_GET["run"] . "\" />\n";
	}
	foreach ($all_attrs as $attr_type => $attrs) {
		echo "<b>$attr_type: </b>";
		$filters[$attr_type] = Array();
		foreach ($attrs as $attr) {
			$checked = "";
			if (isset($_GET["attr_${attr_type}_$attr"]) && $_GET["attr_${attr_type}_$attr"]) {
				$checked = "checked";
				array_push($filters[$attr_type], $attr);
			}
			echo "<input type=\"checkbox\" $checked name=\"attr_${attr_type}_$attr\" value=\"1\">$attr</input>\n";
		}
		echo "<br/>\n";
	}
	echo "<b>maintainer: </b>";
	$filters["maintainer"] = Array();
	echo "<select name=\"maintainer\" id=\"maintainer\">\n";
	echo "<option />\n";
	foreach ($maintainers_package_count as $m => $c) {
		if (isset($_GET["maintainer"]) && $m === $_GET["maintainer"]) {
			$selected = "selected";
			array_push($filters["maintainer"], $m) ;
		} else {
			$selected = "";
		}
		echo "<option $selected value=\"$m\">$m ($c)</option>\n";
	}
	echo "</select><br/>\n";
	$filters["history"] = Array();
	$checked = "";
	if (isset($_GET["history_new"]) && $_GET["history_new"]) {
		$checked = "checked";
		array_push($filters["history"], "new");
	}
	echo "<input type=\"checkbox\" $checked name=\"history_new\" value=\"1\">Only show newly broken packages</input><br/>\n";
	$filters["status"] = Array();
	$checked = "";
	if (isset($_GET["status_broken"]) && $_GET["status_broken"]) {
		$checked = "checked";
		array_push($filters["status"], "broken");
	}
	echo "<input type=\"checkbox\" $checked name=\"status_broken\" value=\"1\">Hide fixed packages</input><br/>\n";
	echo "<input type=\"submit\" value=\"Apply\" />\n";
	echo "</form>";
}
echo "<h2>Failed builds ($nb_failed/$nb_tried)</h2>\n";
echo "<table class=\"failureTable\" >\n";
echo "<tr><th></th><th>Maintainer</th><th>Status</th><th>Failure type</th><th>Build step</th><th></th><th>Languages</th><th>Detected errors</th></tr>\n";
foreach ($failure as $rpm => $error) {
	$parsed = parse_package($rpm);
	$packageid = get_package_id($parsed['package']);
	$attributes = get_package_attributes($packageid);
	if (isset($pkg_maintainers[$parsed['package']])) {
		$maintainer = $pkg_maintainers[$parsed['package']];
	} else {
		$maintainer = "nobody";
	}
	$attributes .= " maintainer_$maintainer";
	$history_link = '<a href="history.php?package='.$parsed['package'].'">History</a>';
	$status_html = "";
	if (array_key_exists($rpm, $fixed)) {
		$status_html .= " <img src='icons/state-fixed.png' title='Fixed!' /> Fixed";
		$attributes .= " status_fixed";
	} elseif (array_key_exists($rpm, $removed)) {
		$status_html .= " <img src='icons/state-removed.png' title='Removed' /> Removed";
		$attributes .= " status_removed";
	} else {
		$attributes .= " status_broken";
	}
	if (array_key_exists($rpm, $broken)) {
		$status_html .= " <img src='icons/state-new.png' title='New!' /> New";
		$attributes .= " history_new";
	}
	$error_html = $error;
	if (file_exists("icons/error-$error.png")) {
		$error_html = "<img src='icons/error-$error.png' title='$error'/> $error";
	}
	$history = get_build_history($packageid);
	if ($history && is_flaky($history)) {
		$history_link = $history_link . " <img src='icons/warning-flaky.png' title='Flaky build' />";
	}
	$langs = "";
	$errors = "";
	$stage = "";
	if ($attributes) {
		# Attributes are space separated words of the form "TYPE_SPECIFIER", e.g., "lang_php"
		# Types are currently build (use of a particular build system),
		# lang (use of a programming language) or err (a common build error).
		$to_filter = $filters;
		foreach (explode(" ", $attributes) as $attr) {
			if (empty($attr)) {
				continue;
			}
			$attrtype = explode("_", $attr)[0];
			$attrname = explode("_", $attr)[1];
			if ($to_filter[$attrtype]) {
				if (in_array($attrname, $filters[$attrtype])) {
					# This package matches one of the wanted attributes for this type,
					# no need to check further.
					$to_filter[$attrtype] = Array();
				}
			}
			if ($attrtype === "lang") {
				$lang = ucfirst($attrname);
				$icon_fn = "icons/" . str_replace("_", "-", $attr) . ".png";
				if (file_exists($icon_fn)) {
					$langs = $langs . " <img src='$icon_fn' title='Uses $lang' />";
				} else {
					$langs = $langs . " $lang";
				}
			} elseif ($attrtype === "err") {
				$errors = "$errors <a href='https://wiki.mageia.org/en/Autobuild_errors#$attrname'>$attrname</a>";
			} elseif ($attrtype === "stage") {
				if ($attrname != 'nobuild') {
					if ($attrname == 'preprep') {
						$stage = 'Before %prep';
					} else {
						$stage = "%$attrname";
					}
				}
			}
		}
		# If filters on any type of attribute is not satisfied, skip the package
		foreach ($to_filter as $attr_type => $attrs) {
			if ($attrs) {
				continue 2;
			}
		}
	}
	echo "<tr>";
	if (file_exists("$base_dir/$rpm/")) {
		echo "<td><a href='$base_dir/$rpm/'>$rpm</a></td>";
	} else {
		echo "<td>$rpm</td>";
	}
	echo "<td>$maintainer</td><td>$status_html</td><td>$error_html</td><td>$stage</td><td>$history_link</td><td>$langs</td><td>$errors</td></tr>\n";
}
$db->close();
?>
</table>
<?php
if (array_key_exists('include_success', $_GET) && $_GET['include_success'] === 'true') {
	echo "<h2>Successful builds ($nb_success/$nb_tried)</h2>\n<ul>\n";
	foreach ($success as $rpm) {
		$parsed = parse_package($rpm);
		$history_link = '<a href="history.php?package='.$parsed['package'].'">[h]</a>';
		if (file_exists("$base_dir/$rpm/")) {
			echo "<li><a href='$base_dir/$rpm/'>$rpm</a> $history_link</li>\n";
		} else {
			echo "<li>$rpm $history_link</li>\n";
		}
	}
	echo "</ul>\n";
}
?>
    <script src="//nav.mageia.org/js/"></script>
    <div class="clear"></div>
    <hr />
    <footer>
            Code for this page is in <a rel="nofollow" href="http://gitweb.mageia.org/web/pkgsubmit/tree/autobuild/">http://gitweb.mageia.org/web/pkgsubmit/tree/autobuild/</a>.
    </footer>
    </article>
</body>
</html>