2021-05-18 05:48:50 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */
|
|
|
|
|
|
|
|
|
|
namespace Icinga\Module\Icingadb\Util;
|
|
|
|
|
|
|
|
|
|
class PerfDataFormat
|
|
|
|
|
{
|
|
|
|
|
protected static $instance;
|
|
|
|
|
|
|
|
|
|
protected static $generalBase = 1000;
|
|
|
|
|
|
|
|
|
|
protected static $bitPrefix = ['b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb'];
|
|
|
|
|
|
|
|
|
|
protected static $bytePrefix = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
|
|
|
|
|
|
|
|
protected static $wattHourPrefix = ['Wh', 'kWh', 'MWh', 'GWh', 'TWh', 'PWh', 'EWh', 'ZWh', 'YWh'];
|
|
|
|
|
|
2021-07-16 09:17:56 -04:00
|
|
|
protected static $wattPrefix = [-1 => 'mW', 'W', 'kW', 'MW', 'GW'];
|
|
|
|
|
|
|
|
|
|
protected static $amperePrefix = [-3 => 'nA', -2 => 'µA', -1 => 'mA', 'A', 'kA', 'MA', 'GA'];
|
|
|
|
|
|
|
|
|
|
protected static $ampSecondPrefix = [-2 => 'µAs', -1 => 'mAs', 'As', 'kAs', 'MAs', 'GAs'];
|
|
|
|
|
|
|
|
|
|
protected static $voltPrefix = [-2 => 'µV', -1 => 'mV', 'V', 'kV', 'MV', 'GV'];
|
|
|
|
|
|
|
|
|
|
protected static $ohmPrefix = ['Ω'];
|
|
|
|
|
|
|
|
|
|
protected static $gramPrefix = [
|
|
|
|
|
-5 => 'fg',
|
|
|
|
|
-4 => 'pg',
|
|
|
|
|
-3 => 'ng',
|
|
|
|
|
-2 => 'µg',
|
|
|
|
|
-1 => 'mg',
|
|
|
|
|
'g',
|
|
|
|
|
'kg',
|
|
|
|
|
't',
|
|
|
|
|
'ktǂ',
|
|
|
|
|
'Mt',
|
|
|
|
|
'Gt'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected static $literPrefix = [
|
|
|
|
|
-5 => 'fl',
|
|
|
|
|
-4 => 'pl',
|
|
|
|
|
-3 => 'nl',
|
|
|
|
|
-2 => 'µl',
|
|
|
|
|
-1 => 'ml',
|
|
|
|
|
'l',
|
|
|
|
|
'kl',
|
|
|
|
|
'Ml',
|
|
|
|
|
'Gl',
|
|
|
|
|
'Tl',
|
|
|
|
|
'Pl'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected static $secondPrefix = [-3 => 'ns', -2 => 'µs', -1 => 'ms', 's'];
|
2021-05-18 05:48:50 -04:00
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function getInstance(): self
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
|
|
|
|
if (self::$instance === null) {
|
|
|
|
|
self::$instance = new PerfDataFormat();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function bits($value): string
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
|
|
|
|
return self::formatForUnits($value, self::$bitPrefix, self::$generalBase);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function bytes($value): string
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
|
|
|
|
return self::formatForUnits($value, self::$bytePrefix, self::$generalBase);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function wattHours($value): string
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
|
|
|
|
return self::formatForUnits($value, self::$wattHourPrefix, self::$generalBase);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function watts($value): string
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
|
|
|
|
return self::formatForUnits($value, self::$wattPrefix, self::$generalBase);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function amperes($value): string
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
|
|
|
|
return self::formatForUnits($value, self::$amperePrefix, self::$generalBase);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function ampereSeconds($value): string
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
|
|
|
|
return self::formatForUnits($value, self::$ampSecondPrefix, self::$generalBase);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function volts($value): string
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
|
|
|
|
return self::formatForUnits($value, self::$voltPrefix, self::$generalBase);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function ohms($value): string
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
|
|
|
|
return self::formatForUnits($value, self::$ohmPrefix, self::$generalBase);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function grams($value): string
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
|
|
|
|
return self::formatForUnits($value, self::$gramPrefix, self::$generalBase);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function liters($value): string
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
2021-07-16 09:17:56 -04:00
|
|
|
return self::formatForUnits($value, self::$literPrefix, self::$generalBase);
|
2021-05-18 05:48:50 -04:00
|
|
|
}
|
|
|
|
|
|
2021-09-22 04:21:15 -04:00
|
|
|
public static function seconds($value): string
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
2022-08-05 03:56:49 -04:00
|
|
|
$value = (float) $value;
|
2021-05-18 05:48:50 -04:00
|
|
|
$absValue = abs($value);
|
|
|
|
|
|
|
|
|
|
if ($absValue < 60) {
|
|
|
|
|
return self::formatForUnits($value, self::$secondPrefix, self::$generalBase);
|
|
|
|
|
} elseif ($absValue < 3600) {
|
|
|
|
|
return sprintf('%0.2f m', $value / 60);
|
|
|
|
|
} elseif ($absValue < 86400) {
|
|
|
|
|
return sprintf('%0.2f h', $value / 3600);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sprintf('%0.2f d', $value / 86400);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 08:54:15 -04:00
|
|
|
protected static function formatForUnits(float $value, array &$units, int $base): string
|
2021-05-18 05:48:50 -04:00
|
|
|
{
|
|
|
|
|
$sign = '';
|
|
|
|
|
if ($value < 0) {
|
|
|
|
|
$value = abs($value);
|
|
|
|
|
$sign = '-';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($value == 0) {
|
|
|
|
|
$pow = $result = 0;
|
|
|
|
|
} else {
|
|
|
|
|
$pow = floor(log($value, $base));
|
2021-07-16 09:18:22 -04:00
|
|
|
|
|
|
|
|
// Identify nearest unit if unknown
|
|
|
|
|
while (! isset($units[$pow])) {
|
|
|
|
|
if ($pow < 0) {
|
|
|
|
|
$pow++;
|
|
|
|
|
} else {
|
|
|
|
|
$pow--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 05:48:50 -04:00
|
|
|
$result = $value / pow($base, $pow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1034.23 looks better than 1.03, but 2.03 is fine:
|
|
|
|
|
if ($pow > 0 && $result < 2) {
|
|
|
|
|
$result = $value / pow($base, --$pow);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
|
'%s%0.2f %s',
|
|
|
|
|
$sign,
|
|
|
|
|
$result,
|
2021-07-16 09:18:22 -04:00
|
|
|
$units[$pow]
|
2021-05-18 05:48:50 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|