index : website | |
Archlinux32 website - obsolete | gitolite user |
summaryrefslogtreecommitdiff |
-rw-r--r-- | mirrors.php | 268 |
diff --git a/mirrors.php b/mirrors.php new file mode 100644 index 0000000..7758846 --- /dev/null +++ b/mirrors.php @@ -0,0 +1,268 @@ +<?php +require_once "init.php"; + +require_once BASE . "/lib/mysql.php"; +require_once BASE . "/lib/style.php"; +require_once BASE . "/lib/format.php"; + +$options = explode('?', $_SERVER['REQUEST_URI'], 2); + +$uri_parts = explode('/', $options[0]); +if (count($options) == 2) + $options = $options[1]; +else + $options = ''; +if ($uri_parts[count($uri_parts)-1] == '') + array_pop($uri_parts); + +if ($uri_parts[0] != '' || $uri_parts[1] != 'mirrors') + throw_http_error(422, 'Unprocessable Entity'); + +array_splice( + $uri_parts, + 0, 2 +); + +if (count($uri_parts) == 0) { // index + + $cutoff = 3600; + + $sorts = array( + "server" => array( + "title" => "server", + "label" => "Server", + "mysql" => "`url`" + ), + "country" => array( + "title" => "country", + "label" => "Country", + "mysql" => "`l_ms`.`country_code`" + ), + "isos" => array( + "title" => "wether isos are available", + "label" => "ISOs", + "mysql" => "`l_ms`.`isos`" + ), + "protocols" => array( + "title" => "available protocols", + "label" => "Protocols", + "mysql" => "`protocols`" + ) + ); + + $query = + "SELECT " . + "GROUP_CONCAT(`l_ms`.`protocol`) AS `protocols`," . + "SUBSTRING(`l_ms`.`url`,LENGTH(`l_ms`.`protocol`)+4) AS `url`," . + "`l_ms`.`country`," . + "`l_ms`.`country_code`," . + "`l_ms`.`isos`," . + "`l_ms`.`ipv4`," . + "`l_ms`.`ipv6`" . + " FROM (" . + "SELECT " . + "`mirror_statuses`.`url`," . + "MAX(`mirror_statuses`.`start`) AS `start`" . + " FROM `mirror_statuses`" . + " WHERE `mirror_statuses`.`start` > UNIX_TIMESTAMP(NOW())-" . $cutoff . + " GROUP BY `mirror_statuses`.`url`" . + ") AS `ls`" . + " JOIN `mirror_statuses` AS `l_ms`" . + " ON `ls`.`url`=`l_ms`.`url`" . + " AND `ls`.`start`=`l_ms`.`start`" . + " GROUP BY `url`" . + " ORDER BY "; + + if (array_key_exists("sort", $_GET)) { + if (isset($sorts[$_GET["sort"]]["mysql"])) + $query .= $sorts[$_GET["sort"]]["mysql"] . ","; + elseif (isset($sorts[substr($_GET["sort"],1)]["mysql"])) + $query .= $sorts[substr($_GET["sort"],1)]["mysql"] . " DESC,"; + } + + $query .= "`url`"; + + $result = mysql_run_query( + $query + ); + + $last_check = 0; + $max_count = 0; + + while($row = $result->fetch_assoc()) + $rows[] = $row; + + print_header("Mirror Overview"); + +?> + <div id="dev-mirrorlist" class="box"> + <h2>Mirror Overview</h2> + <table class="results"> + <thead> + <tr> +<?php + foreach ($sorts as $get => $sort) { + print " <th>\n"; + print " <a href=\"?"; + print substr(str_replace( + "&sort=".$_GET["sort"]."&", + "&", + "&".$_SERVER["QUERY_STRING"]."&" + ),1)."sort="; + if ($_GET["sort"] == $get) + print "-"; + print $get."\" title=\"Sort package by ".$sort["title"]."\">".$sort["label"]."</a>\n"; + print " </th>\n"; + } +?> + </tr> + </thead> + <tbody> +<?php + + $oddity = "odd"; + foreach ($rows as $row) { + print " <tr class=\"" . $oddity ."\">\n"; + print " <td>\n"; + print " " . $row["url"] . "\n"; + print " </td>\n"; + print " <td class=\"country\">\n"; + print " <span class=\"fam-flag fam-flag-" . $row["country_code"] . "\" title=\"" . $row["country"] . "\">\n"; + print " </span>\n"; + print " " . $row["country"] . "\n"; + print " </td>\n"; + print " <td>\n"; + if ($row["isos"]) + print " Yes\n"; + else + print " No\n"; + print " </td>\n"; + print " <td class=\"wrap\">\n"; + print " " . $row["protocols"] . "\n"; + print " </td>\n"; + print " </tr>\n"; + if ($oddity == "odd") + $oddity = "even"; + else + $oddity = "odd"; + } + +?> + </tbody> + </table> + </div> +<?php + + print_footer(); + die(); + +} // index + +if ($uri_parts[0] == 'status') { + + $cutoff = 86400; + + $result = mysql_run_query( + "SELECT " . + "`l_ms`.`protocol`," . + "`l_ms`.`url`," . + "`l_ms`.`country`," . + "`l_ms`.`country_code`," . + "`l_ms`.`last_sync`," . + "`l_ms`.`start`," . + "AVG(IF(`a_ms`.`active`,(`a_ms`.`start`-`a_ms`.`last_sync`)/3600,NULL)) AS `delay`," . + "AVG(IF(`a_ms`.`active`,(`a_ms`.`stop`-`a_ms`.`start`)/3600,NULL)) AS `duration_avg`," . + "STD(IF(`a_ms`.`active`,(`a_ms`.`stop`-`a_ms`.`start`)/3600,NULL)) AS `duration_stddev`," . + "`l_ms`.`isos`," . + "`l_ms`.`ipv4`," . + "`l_ms`.`ipv6`," . + "`l_ms`.`active`," . + "(`l_ms`.`active` AND (`l_ms`.`start` > UNIX_TIMESTAMP(NOW()) - 3600)) AS `recently_active`," . + "AVG(IF(`a_ms`.`active`,1,0)) AS `completion_pct`," . + "COUNT(1) AS `count`" . + " FROM (" . + "SELECT " . + "`mirror_statuses`.`url`," . + "MAX(`mirror_statuses`.`start`) AS `start`" . + " FROM `mirror_statuses`" . + " WHERE `mirror_statuses`.`start` > UNIX_TIMESTAMP(NOW())-" . $cutoff . + " GROUP BY `mirror_statuses`.`url`" . + ") AS `ls`" . + " JOIN `mirror_statuses` AS `l_ms`" . + " ON `ls`.`url`=`l_ms`.`url`" . + " AND `ls`.`start`=`l_ms`.`start`" . + " JOIN `mirror_statuses` AS `a_ms`" . + " ON `a_ms`.`url`=`l_ms`.`url`" . + " AND `a_ms`.`start` > UNIX_TIMESTAMP(NOW())-" . $cutoff . + " GROUP BY `l_ms`.`id`" + ); + + $last_check = 0; + $max_count = 0; + + while($row = $result->fetch_assoc()) { + foreach (array( + "start", + "delay", + "duration_avg", + "duration_stddev", + "completion_pct", + "count", + "isos", + "ipv4", + "ipv6", + "active", + "recently_active" + ) as $key) + $row[$key] = floatval($row[$key]); + $row["last_sync"] = gmdate("Y-m-d\TH:i:s\Z", $row["last_sync"]); + $row["score"] = + ($row["delay"] + $row["duration_avg"] + $row["duration_stddev"]) / $row["completion_pct"]; + $urls[] = $row; + $last_check = max ($row["start"], $last_check); + $max_count = max ($row["count"], $max_count); + } + + $content = array( + "cutoff" => $cutoff, + "check_frequency" => $cutoff/$max_count, + "num_checks" => $max_count, + "last_check" => gmdate("Y-m-d\TH:i:s.v\Z",$last_check), //"2018-06-15T07:25:06.741Z", + // "version" => 3, + "urls" => $urls + ); + + if (count($uri_parts) == 1) { // human readable + } // human readable + + if (!export_as_requested( + array( + "json" => $content, + "tsv" => $urls + ), + $uri_parts[1] + )) { + throw_http_error( + 406, + 'Not Acceptable', + implode( + "<br>\n", + array_merge( + array( + 'Unknown output format.', + 'Accepted:' + ), + array_map( + function($type){ + return '<a href="/mirrors/status/' . $type . '/">' . $type . '</a>'; + }, + array('json', 'tsv') + ) + ) + ) + ); + } + die(); +} // status + +throw_http_error(501, "Not Implemented"); |