From 2e21ab788e053f22b526c9f3289ac161303fb818 Mon Sep 17 00:00:00 2001 From: Pascal Terjan Date: Fri, 21 Jan 2011 11:41:03 +0000 Subject: Add queue current status and suggested time until next submit in the HTTP header --- index.php | 217 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 115 insertions(+), 102 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index d1e0b1d..547fd03 100644 --- a/index.php +++ b/index.php @@ -24,6 +24,84 @@ error_reporting(E_ALL); +/** + * @param array $pkg + * + * @return string +*/ +function pkg_gettype($pkg) { + if (array_key_exists("rejected", $pkg["status"])) + return "rejected"; + if (array_key_exists("youri", $pkg["status"])) { + if (array_key_exists("src", $pkg["status"])) + return "youri"; + else + return "uploaded"; + } + if (array_key_exists("failure", $pkg["status"])) + return "failure"; + if (array_key_exists("done", $pkg["status"])) + return "partial"; + if (array_key_exists("build", $pkg["status"])) + return "building"; + if (array_key_exists("todo", $pkg["status"])) + return "todo"; + return "unknown"; +} + +/** + * @param integer $num + * + * @return string +*/ +function plural($num) { + if ($num > 1) + return "s"; +} + +/** + * Return timestamp from package key + * @param string $key package submission key + * + * @return integer +*/ + +function key2timestamp($key) { + global $tz; + + $date = DateTime::createFromFormat("YmdHis", $key+0, $tz); + if ($date <= 0) + return null; + + return $date->getTimestamp(); +} + +function timediff($start, $end) { +/** + * Return human-readable time difference + * + * @param integer $start timestamp + * @param integer $end timestamp, defaults to now + * + * @return string +*/ + if (is_null($end)) { + $end = time(); + } + $diff = $end - $start; + if ($diff<60) + return $diff . " second" . plural($diff); + $diff = round($diff/60); + if ($diff<60) + return $diff . " minute" . plural($diff); + $diff = round($diff/60); + if ($diff<24) + return $diff . " hour" . plural($diff); + $diff = round($diff/24); + + return $diff . " day" . plural($diff); +} + $g_user = isset($_GET['user']) ? htmlentities(strip_tags($_GET['user'])) : null; $upload_dir = '/home/schedbot/uploads'; @@ -97,84 +175,52 @@ foreach ($matches as $val) { // sort by key in reverse order to have more recent pkgs first krsort($pkgs); -/** - * @param array $pkg - * - * @return string -*/ -function pkg_gettype($pkg) { - if (array_key_exists("rejected", $pkg["status"])) - return "rejected"; - if (array_key_exists("youri", $pkg["status"])) { - if (array_key_exists("src", $pkg["status"])) - return "youri"; - else - return "uploaded"; - } - if (array_key_exists("failure", $pkg["status"])) - return "failure"; - if (array_key_exists("done", $pkg["status"])) - return "partial"; - if (array_key_exists("build", $pkg["status"])) - return "building"; - if (array_key_exists("todo", $pkg["status"])) - return "todo"; - return "unknown"; -} - -/** - * @param integer $num - * - * @return string -*/ -function plural($num) { - if ($num > 1) - return "s"; -} +// count all packages statuses +$stats = array( + 'uploaded' => 0, + 'failure' => 0, + 'todo' => 0, + 'building' => 0, + 'partial' => 0, + 'built' => 0, +); +$total = count($pkgs); -/** - * Return timestamp from package key - * @param string $key package submission key - * - * @return integer -*/ +// count users' packages +$users = array(); -function key2timestamp($key) { - global $tz; +if ($total > 0) { + foreach ($pkgs as $key => $p) { + $pkgs[$key]['type'] = pkg_gettype($p); - $date = DateTime::createFromFormat("YmdHis", $key+0, $tz); - if ($date <= 0) - return null; + $stats[$pkgs[$key]['type']] += 1; - return $date->getTimestamp(); + if (!array_key_exists($p['user'], $users)) + $users[$p['user']] = 1; + else + $users[$p['user']] += 1; + } } -function timediff($start, $end) { -/** - * Return human-readable time difference - * - * @param integer $start timestamp - * @param integer $end timestamp, defaults to now - * - * @return string -*/ - if (is_null($end)) { - $end = time(); - } - $diff = $end - $start; - if ($diff<60) - return $diff . " second" . plural($diff); - $diff = round($diff/60); - if ($diff<60) - return $diff . " minute" . plural($diff); - $diff = round($diff/60); - if ($diff<24) - return $diff . " hour" . plural($diff); - $diff = round($diff/24); +// feedback labels +$badges = array( + 'uploaded' => 'Congrats %s! \o/', + 'failure' => 'Booooo! /o\\', + 'todo' => '', + 'building' => '', + 'partial' => '', + 'built' => '' +); - return $diff . " day" . plural($diff); +foreach ($stats as $k => $v) { + Header("X-BS-Queue-$k: $v"); } +$w = $stats['todo'] - 10; +if($w < 0) + $w = 0; +$w = $w * 60; +Header("X-BS-Throttle: $w"); ?> @@ -251,41 +297,8 @@ $tmpl = << T; -// count all packages statuses -$stats = array( - 'uploaded' => 0, - 'failure' => 0, - 'todo' => 0, - 'building' => 0, - 'partial' => 0, - 'built' => 0, -); -$total = count($pkgs); - -// count users' packages -$users = array(); - -// feedback labels -$badges = array( - 'uploaded' => 'Congrats %s! \o/', - 'failure' => 'Booooo! /o\\', - 'todo' => '', - 'building' => '', - 'partial' => '', - 'built' => '' -); - if ($total > 0) { foreach ($pkgs as $key => $p) { - $p['type'] = pkg_gettype($p); - - $stats[$p['type']] += 1; - - if (!array_key_exists($p['user'], $users)) - $users[$p['user']] = 1; - else - $users[$p['user']] += 1; - $s .= sprintf($tmpl, $p['type'], timediff(key2timestamp($key)) . ' ago', -- cgit v1.2.1