Naming adjustments
This commit is contained in:
51
web/api/stats_rates.php
Normal file
51
web/api/stats_rates.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
require __DIR__ . '/../lib/app.php';
|
||||
auth_require();
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
|
||||
header('Pragma: no-cache');
|
||||
|
||||
$path = '/var/lib/torpanel/stats.json';
|
||||
if (!is_readable($path)) { echo json_encode(['ok'=>true,'data'=>[]]); exit; }
|
||||
|
||||
$raw = file_get_contents($path);
|
||||
$state = json_decode($raw, true);
|
||||
if (!is_array($state) || !is_array($state['data'] ?? null)) {
|
||||
echo json_encode(['ok'=>true,'data'=>[]]); exit;
|
||||
}
|
||||
|
||||
usort($state['data'], function($a,$b){
|
||||
return (int)($a['t'] ?? 0) <=> (int)($b['t'] ?? 0);
|
||||
});
|
||||
|
||||
$out = [];
|
||||
$prev = null;
|
||||
foreach ($state['data'] as $s) {
|
||||
$t = (int)($s['t'] ?? 0);
|
||||
if ($t <= 0) continue;
|
||||
|
||||
$rx_total = (int)($s['read'] ?? ($s['bytes_read'] ?? ($s['rx'] ?? 0)));
|
||||
$tx_total = (int)($s['written'] ?? ($s['bytes_written'] ?? ($s['tx'] ?? 0)));
|
||||
|
||||
$rx_mbps = 0.0; $tx_mbps = 0.0;
|
||||
if ($prev) {
|
||||
$dt = max(1, $t - $prev['t']);
|
||||
$drx = max(0, $rx_total - $prev['rx_total']);
|
||||
$dtx = max(0, $tx_total - $prev['tx_total']);
|
||||
$rx_mbps = ($drx * 8) / $dt / 1_000_000.0;
|
||||
$tx_mbps = ($dtx * 8) / $dt / 1_000_000.0;
|
||||
}
|
||||
|
||||
$out[] = [
|
||||
'ts' => $t * 1000,
|
||||
'rx_mbps' => round($rx_mbps, 3),
|
||||
'tx_mbps' => round($tx_mbps, 3),
|
||||
'rx_total' => $rx_total,
|
||||
'tx_total' => $tx_total,
|
||||
];
|
||||
$prev = ['t'=>$t, 'rx_total'=>$rx_total, 'tx_total'=>$tx_total];
|
||||
}
|
||||
|
||||
if (count($out) > 1440) $out = array_slice($out, -1440);
|
||||
echo json_encode(['ok'=>true,'data'=>$out]);
|
||||
Reference in New Issue
Block a user