Skip to content

Commit

Permalink
Results can now be fetched in json format instead of just an image (r…
Browse files Browse the repository at this point in the history
…esults/json.php
  • Loading branch information
adolfintel committed Apr 21, 2021
1 parent 8be8aba commit 3937b94
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions results/json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

require_once 'telemetry_db.php';

/**
* @param int|float $d
*
* @return string
*/
function format($d)
{
if ($d < 10) {
return number_format($d, 2, '.', '');
}
if ($d < 100) {
return number_format($d, 1, '.', '');
}

return number_format($d, 0, '.', '');
}

/**
* @param array $speedtest
*
* @return array
*/
function formatSpeedtestData($speedtest)
{
// format values for the image
$speedtest['dl'] = format($speedtest['dl']);
$speedtest['ul'] = format($speedtest['ul']);
$speedtest['ping'] = format($speedtest['ping']);
$speedtest['jitter'] = format($speedtest['jitter']);
$speedtest['timestamp'] = $speedtest['timestamp'];

$ispinfo = json_decode($speedtest['ispinfo'], true)['processedString'];
$dash = strpos($ispinfo, '-');
if ($dash !== false) {
$ispinfo = substr($ispinfo, $dash + 2);
$par = strrpos($ispinfo, '(');
if ($par !== false) {
$ispinfo = substr($ispinfo, 0, $par);
}
} else {
$ispinfo = '';
}

$speedtest['ispinfo'] = $ispinfo;

return $speedtest;
}

$speedtest = getSpeedtestUserById($_GET['id']);
if (!is_array($speedtest)) {
echo '{}';
}
$speedtest = formatSpeedtestData($speedtest);

echo json_encode(array('timestamp'=>$speedtest['timestamp'],'download'=>$speedtest['dl'],'upload'=>$speedtest['ul'],'ping'=>$speedtest['ping'],'jitter'=>$speedtest['jitter'],'ispinfo'=>$speedtest['ispinfo']));

0 comments on commit 3937b94

Please sign in to comment.