-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Results can now be fetched in json format instead of just an image (r…
…esults/json.php
- Loading branch information
1 parent
8be8aba
commit 3937b94
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'])); |