Ejemplo n.º 1
0
/**
 * Converts value to actual value.
 * Example:
 * 	6442450944 B convert to 6 GB
 *
 * @param array  $options
 * @param string $options['value']
 * @param string $options['units']
 * @param string $options['convert']
 * @param string $options['byteStep']
 * @param string $options['pow']
 * @param bool   $options['ignoreMillisec']
 * @param string $options['length']
 *
 * @return string
 */
function convert_units($options = array())
{
    $defOptions = array('value' => null, 'units' => null, 'convert' => ITEM_CONVERT_WITH_UNITS, 'byteStep' => false, 'pow' => false, 'ignoreMillisec' => false, 'length' => false);
    $options = zbx_array_merge($defOptions, $options);
    // special processing for unix timestamps
    if ($options['units'] == 'unixtime') {
        return zbx_date2str(DATE_TIME_FORMAT_SECONDS, $options['value']);
    }
    // special processing of uptime
    if ($options['units'] == 'uptime') {
        return convertUnitsUptime($options['value']);
    }
    // special processing for seconds
    if ($options['units'] == 's') {
        return convertUnitsS($options['value'], $options['ignoreMillisec']);
    }
    // any other unit
    // black list of units that should have no multiplier prefix (K, M, G etc) applied
    $blackList = array('%', 'ms', 'rpm', 'RPM');
    if (in_array($options['units'], $blackList) || zbx_empty($options['units']) && $options['convert'] == ITEM_CONVERT_WITH_UNITS) {
        if (abs($options['value']) >= ZBX_UNITS_ROUNDOFF_THRESHOLD) {
            $options['value'] = round($options['value'], ZBX_UNITS_ROUNDOFF_UPPER_LIMIT);
        }
        $options['value'] = sprintf('%.' . ZBX_UNITS_ROUNDOFF_LOWER_LIMIT . 'f', $options['value']);
        $options['value'] = preg_replace('/^([\\-0-9]+)(\\.)([0-9]*)[0]+$/U', '$1$2$3', $options['value']);
        $options['value'] = rtrim($options['value'], '.');
        return trim($options['value'] . ' ' . $options['units']);
    }
    // if one or more items is B or Bps, then Y-scale use base 8 and calculated in bytes
    if ($options['byteStep']) {
        $step = 1024;
    } else {
        switch ($options['units']) {
            case 'Bps':
            case 'B':
                $step = 1024;
                $options['convert'] = $options['convert'] ? $options['convert'] : ITEM_CONVERT_NO_UNITS;
                break;
            case 'b':
            case 'bps':
                $options['convert'] = $options['convert'] ? $options['convert'] : ITEM_CONVERT_NO_UNITS;
            default:
                $step = 1000;
        }
    }
    if ($options['value'] < 0) {
        $abs = bcmul($options['value'], '-1');
    } else {
        $abs = $options['value'];
    }
    if (bccomp($abs, 1) == -1) {
        $options['value'] = round($options['value'], ZBX_UNITS_ROUNDOFF_MIDDLE_LIMIT);
        $options['value'] = $options['length'] && $options['value'] != 0 ? sprintf('%.' . $options['length'] . 'f', $options['value']) : $options['value'];
        return trim($options['value'] . ' ' . $options['units']);
    }
    // init intervals
    static $digitUnits;
    if (is_null($digitUnits)) {
        $digitUnits = array();
    }
    if (!isset($digitUnits[$step])) {
        $digitUnits[$step] = array(array('pow' => 0, 'short' => '', 'long' => ''), array('pow' => 1, 'short' => _x('K', 'Kilo short'), 'long' => _('Kilo')), array('pow' => 2, 'short' => _x('M', 'Mega short'), 'long' => _('Mega')), array('pow' => 3, 'short' => _x('G', 'Giga short'), 'long' => _('Giga')), array('pow' => 4, 'short' => _x('T', 'Tera short'), 'long' => _('Tera')), array('pow' => 5, 'short' => _x('P', 'Peta short'), 'long' => _('Peta')), array('pow' => 6, 'short' => _x('E', 'Exa short'), 'long' => _('Exa')), array('pow' => 7, 'short' => _x('Z', 'Zetta short'), 'long' => _('Zetta')), array('pow' => 8, 'short' => _x('Y', 'Yotta short'), 'long' => _('Yotta')));
        foreach ($digitUnits[$step] as $dunit => $data) {
            // skip milli & micro for values without units
            $digitUnits[$step][$dunit]['value'] = bcpow($step, $data['pow'], 9);
        }
    }
    $valUnit = array('pow' => 0, 'short' => '', 'long' => '', 'value' => $options['value']);
    if ($options['pow'] === false || $options['value'] == 0) {
        foreach ($digitUnits[$step] as $dnum => $data) {
            if (bccomp($abs, $data['value']) > -1) {
                $valUnit = $data;
            } else {
                break;
            }
        }
    } else {
        foreach ($digitUnits[$step] as $data) {
            if ($options['pow'] == $data['pow']) {
                $valUnit = $data;
                break;
            }
        }
    }
    if (round($valUnit['value'], ZBX_UNITS_ROUNDOFF_MIDDLE_LIMIT) > 0) {
        $valUnit['value'] = bcdiv(sprintf('%.10f', $options['value']), sprintf('%.10f', $valUnit['value']), ZBX_PRECISION_10);
    } else {
        $valUnit['value'] = 0;
    }
    switch ($options['convert']) {
        case 0:
            $options['units'] = trim($options['units']);
        case 1:
            $desc = $valUnit['short'];
            break;
        case 2:
            $desc = $valUnit['long'];
            break;
    }
    $options['value'] = preg_replace('/^([\\-0-9]+)(\\.)([0-9]*)[0]+$/U', '$1$2$3', round($valUnit['value'], ZBX_UNITS_ROUNDOFF_UPPER_LIMIT));
    $options['value'] = rtrim($options['value'], '.');
    // fix negative zero
    if (bccomp($options['value'], 0) == 0) {
        $options['value'] = 0;
    }
    return trim(sprintf('%s %s%s', $options['length'] ? sprintf('%.' . $options['length'] . 'f', $options['value']) : $options['value'], $desc, $options['units']));
}
Ejemplo n.º 2
0
function convert_units($value, $units, $convert = ITEM_CONVERT_WITH_UNITS)
{
    // special processing for unix timestamps
    if ($units == 'unixtime') {
        return zbx_date2str(_('Y.m.d H:i:s'), $value);
    }
    // special processing of uptime
    if ($units == 'uptime') {
        return convertUnitsUptime($value);
    }
    // special processing for seconds
    if ($units == 's') {
        return convertUnitsS($value);
    }
    // any other unit
    // black list wich do not require units metrics..
    $blackList = array('%', 'ms', 'rpm', 'RPM');
    if (in_array($units, $blackList) || zbx_empty($units) && ($convert == ITEM_CONVERT_WITH_UNITS || $value < 1)) {
        if (abs($value) >= ZBX_UNITS_ROUNDOFF_THRESHOLD) {
            $value = round($value, ZBX_UNITS_ROUNDOFF_UPPER_LIMIT);
        }
        $value = sprintf('%.' . ZBX_UNITS_ROUNDOFF_LOWER_LIMIT . 'f', $value);
        $value = preg_replace('/^([\\-0-9]+)(\\.)([0-9]*)[0]+$/U', '$1$2$3', $value);
        $value = rtrim($value, '.');
        if (zbx_empty($units)) {
            return $value;
        } else {
            return $value . ' ' . $units;
        }
    }
    switch ($units) {
        case 'Bps':
        case 'B':
            $step = 1024;
            $convert = $convert ? $convert : ITEM_CONVERT_NO_UNITS;
            break;
        case 'b':
        case 'bps':
            $convert = $convert ? $convert : ITEM_CONVERT_NO_UNITS;
        default:
            $step = 1000;
    }
    // init intervals
    static $digitUnits;
    if (is_null($digitUnits)) {
        $digitUnits = array();
    }
    if (!isset($digitUnits[$step])) {
        $digitUnits[$step] = array(array('pow' => -2, 'short' => _x('µ', 'Micro short'), 'long' => _('Micro')), array('pow' => -1, 'short' => _x('m', 'Milli short'), 'long' => _('Milli')), array('pow' => 0, 'short' => '', 'long' => ''), array('pow' => 1, 'short' => _x('K', 'Kilo short'), 'long' => _('Kilo')), array('pow' => 2, 'short' => _x('M', 'Mega short'), 'long' => _('Mega')), array('pow' => 3, 'short' => _x('G', 'Giga short'), 'long' => _('Giga')), array('pow' => 4, 'short' => _x('T', 'Tera short'), 'long' => _('Tera')), array('pow' => 5, 'short' => _x('P', 'Peta short'), 'long' => _('Peta')), array('pow' => 6, 'short' => _x('E', 'Exa short'), 'long' => _('Exa')), array('pow' => 7, 'short' => _x('Z', 'Zetta short'), 'long' => _('Zetta')), array('pow' => 8, 'short' => _x('Y', 'Yotta short'), 'long' => _('Yotta')));
        foreach ($digitUnits[$step] as $dunit => $data) {
            // skip mili & micro for values without units
            $digitUnits[$step][$dunit]['value'] = bcpow($step, $data['pow'], 9);
        }
    }
    if ($value < 0) {
        $abs = bcmul($value, '-1');
    } else {
        $abs = $value;
    }
    $valUnit = array('pow' => 0, 'short' => '', 'long' => '', 'value' => $value);
    if ($abs > 999 || $abs < 0.001) {
        foreach ($digitUnits[$step] as $dnum => $data) {
            if (bccomp($abs, $data['value']) > -1) {
                $valUnit = $data;
            } else {
                break;
            }
        }
        if (round($valUnit['value'], 6) > 0) {
            $valUnit['value'] = bcdiv(sprintf('%.6f', $value), sprintf('%.6f', $valUnit['value']), 6);
        } else {
            $valUnit['value'] = 0;
        }
    }
    switch ($convert) {
        case 0:
            $units = trim($units);
        case 1:
            $desc = $valUnit['short'];
            break;
        case 2:
            $desc = $valUnit['long'];
            break;
    }
    $value = preg_replace('/^([\\-0-9]+)(\\.)([0-9]*)[0]+$/U', '$1$2$3', round($valUnit['value'], ZBX_UNITS_ROUNDOFF_UPPER_LIMIT));
    $value = rtrim($value, '.');
    return rtrim(sprintf('%s %s%s', $value, $desc, $units));
}