mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-11-30 12:14:36 -05:00
Merge pull request #5809 from AJ0070/navbar-no-stats
fix: show n/a for load and cpu before zmstats has reported
This commit is contained in:
2 files changed
+59
-2
No files matched your search
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
$failures = 0;
|
||||
$passes = 0;
|
||||
|
||||
function check($name, $got, $want) {
|
||||
global $failures, $passes;
|
||||
if ($got === $want) {
|
||||
$passes++;
|
||||
echo " ok $name\n";
|
||||
} else {
|
||||
$failures++;
|
||||
echo " FAIL $name\n";
|
||||
echo ' got: '.var_export($got, true)."\n";
|
||||
echo ' want: '.var_export($want, true)."\n";
|
||||
}
|
||||
}
|
||||
|
||||
class FakeServer {
|
||||
public $CpuLoad = -1;
|
||||
public $CpuUsagePercent = -1;
|
||||
public function ReadStats() {}
|
||||
}
|
||||
|
||||
function canView($p) { return true; }
|
||||
function translate($s) { return $s; }
|
||||
|
||||
$path = __DIR__.'/../../web/skins/classic/includes/functions.php';
|
||||
$src = file_get_contents($path);
|
||||
foreach (array('getCpuUsageHTML', 'getSysLoadHTML') as $fn) {
|
||||
if (!preg_match('/^function '.$fn.'\(.*?\n\}/ms', $src, $m)) {
|
||||
echo "Could not find $fn() in $path\n";
|
||||
exit(1);
|
||||
}
|
||||
eval($m[0]);
|
||||
}
|
||||
|
||||
$thisServer = new FakeServer();
|
||||
$load = function() { return trim(preg_replace('/^.*?(Load:.*)$/s', '$1', strip_tags(getSysLoadHTML()))); };
|
||||
$cpu = function() { return trim(preg_replace('/^.*?(Cpu:.*)$/s', '$1', strip_tags(getCpuUsageHTML()))); };
|
||||
|
||||
check('load before any stats', $load(), 'Load: n/a');
|
||||
check('cpu before any stats', $cpu(), 'Cpu: n/a');
|
||||
|
||||
$thisServer->CpuLoad = 0.5;
|
||||
$thisServer->CpuUsagePercent = 12.34;
|
||||
check('load with stats', $load(), 'Load: 0.50');
|
||||
check('cpu with stats', $cpu(), 'Cpu: 12.3%');
|
||||
|
||||
$thisServer->CpuLoad = 0;
|
||||
$thisServer->CpuUsagePercent = 0;
|
||||
check('an idle load of zero is a real value', $load(), 'Load: 0.00');
|
||||
check('an idle cpu of zero is a real value', $cpu(), 'Cpu: 0.0%');
|
||||
|
||||
echo "\n$passes passed, $failures failed\n";
|
||||
exit($failures ? 1 : 0);
|
||||
@@ -858,7 +858,8 @@ function getCpuUsageHTML() {
|
||||
$thisServer->ReadStats();
|
||||
|
||||
$result .= '<li id="getCpuUsageHTML" class="CpuUsage nav-item mx-2">'.PHP_EOL;
|
||||
$result .= ' '.translate('Cpu').': '.number_format($thisServer->CpuUsagePercent, 1, '.', '').'%'.PHP_EOL;
|
||||
# -1 is what Server leaves in place when zmstats has not written any stats yet.
|
||||
$result .= ' '.translate('Cpu').': '.($thisServer->CpuUsagePercent < 0 ? 'n/a' : number_format($thisServer->CpuUsagePercent, 1, '.', '').'%').PHP_EOL;
|
||||
$result .= '</li>'.PHP_EOL;
|
||||
}
|
||||
return $result;
|
||||
@@ -874,7 +875,7 @@ function getSysLoadHTML() {
|
||||
|
||||
$result .= '<li id="getSysLoadHTML" class="Load nav-item mx-2">';
|
||||
$result .= '<i class="material-icons md-18">trending_up</i>';
|
||||
$result .= ' '.translate('Load').': '.number_format($thisServer->CpuLoad, 2, '.', '');
|
||||
$result .= ' '.translate('Load').': '.($thisServer->CpuLoad < 0 ? 'n/a' : number_format($thisServer->CpuLoad, 2, '.', ''));
|
||||
$result .= '</li>'.PHP_EOL;
|
||||
}
|
||||
return $result;
|
||||
|
||||
Reference in new issue
Block a user