<?php
const DB_FILE = '2b2t_queue.db';
const ENDPOINT_MAGIC = '69042'; // nice
$db = new SQLite3(DB_FILE);
if (isset($_POST['update']) && $_POST['auth'] == ENDPOINT_MAGIC) {
// update mode
$ts = intval($_POST['ts']);
$qp = intval($_POST['qp']);
$ret = $db->query('SELECT seq, qp FROM `2b2q_log` ORDER BY id DESC LIMIT 1')->fetchArray();
$seq = $ret[0];
$last_qp = $ret[1];
if ($last_qp < $qp) {
$seq++;
}
$stmt = $db->prepare('INSERT INTO `2b2q_log` (seq, ts, qp) VALUES (:seq, :ts, :qp)');
$stmt->bindValue(':seq', $seq);
$stmt->bindValue(':ts', $ts);
$stmt->bindValue(':qp', $qp);
$stmt->execute();
header('HTTP/1.0 204 No Content');
} else {
// stats mode
$now = time();
$last_seq = $db->query('SELECT seq FROM `2b2q_log` ORDER BY id DESC LIMIT 1')->fetchArray()[0];
$ret = $db->query("SELECT MIN(id), MAX(id) FROM `2b2q_log` WHERE seq = $last_seq")->fetchArray();
$first_id = $ret[0];
$last_id = $ret[1];
if ($first_id == $last_id) {
die('Insufficient data');
}
$ret = $db->query("SELECT ts, qp FROM `2b2q_log` WHERE id = $first_id")->fetchArray();
$initial_ts = $ret[0];
$initial_qp = $ret[1];
$ret = $db->query("SELECT ts, qp FROM `2b2q_log` WHERE id = $last_id")->fetchArray();
$current_ts = $ret[0];
$current_qp = $ret[1];
// don't divide by zero dummy
if ($initial_ts == $current_ts || $initial_qp == $current_qp) {
die('Insufficient data');
}
// linear regression using only two points isn't that great, the head of the queue moves slower
$est_total_wait = $current_qp * ($current_ts - $initial_ts) / ($initial_qp - $current_qp);
$est_remaining_wait = $initial_ts + $est_total_wait - $now;
if ($est_remaining_wait < 0) {
$est_remaining_wait = 0;
}
?><head>
<meta charset="utf-8">
<meta http-equiv="Refresh" content="60">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>2b2t Queue Info</title>
</head>
<body>
<p>Last update: <?= date(DATE_RFC1123, $current_ts); ?></p>
<p>Queue position: <?= $current_qp ?></p>
<p>Estimated wait time: <?= round($est_remaining_wait / 60) ?> minutes</p>
</body>
<?php
} // end else
$db->close();
Comments