545
545
* 6442450944 B convert to 6 GB
547
* @param array $options
548
* @param string $options['value']
549
* @param string $options['units']
550
* @param string $options['convert']
551
* @param string $options['byteStep']
552
* @param string $options['pow']
553
* @param bool $options['ignoreMillisec']
554
* @param string $options['length']
547
* @param string $value
548
* @param string $units
549
* @param string $convert
550
* @param string $byteStep
552
* @param bool $ignoreMillisec
558
function convert_units($options = array()) {
562
'convert' => ITEM_CONVERT_WITH_UNITS,
565
'ignoreMillisec' => false,
569
$options = zbx_array_merge($defOptions, $options);
556
function convert_units($value, $units, $convert = ITEM_CONVERT_WITH_UNITS, $byteStep = false, $pow = false, $ignoreMillisec = false) {
571
557
// special processing for unix timestamps
572
if ($options['units'] == 'unixtime') {
573
return zbx_date2str(_('Y.m.d H:i:s'), $options['value']);
558
if ($units == 'unixtime') {
559
return zbx_date2str(_('Y.m.d H:i:s'), $value);
576
562
// special processing of uptime
577
if ($options['units'] == 'uptime') {
578
return convertUnitsUptime($options['value']);
563
if ($units == 'uptime') {
564
return convertUnitsUptime($value);
581
567
// special processing for seconds
582
if ($options['units'] == 's') {
583
return convertUnitsS($options['value'], $options['ignoreMillisec']);
569
return convertUnitsS($value, $ignoreMillisec);
586
572
// any other unit
587
573
// black list wich do not require units metrics..
588
574
$blackList = array('%', 'ms', 'rpm', 'RPM');
590
if (in_array($options['units'], $blackList) || (zbx_empty($options['units'])
591
&& ($options['convert'] == ITEM_CONVERT_WITH_UNITS))) {
592
if (abs($options['value']) >= ZBX_UNITS_ROUNDOFF_THRESHOLD) {
593
$options['value'] = round($options['value'], ZBX_UNITS_ROUNDOFF_UPPER_LIMIT);
576
if (in_array($units, $blackList) || (zbx_empty($units) && ($convert == ITEM_CONVERT_WITH_UNITS))) {
577
if (abs($value) >= ZBX_UNITS_ROUNDOFF_THRESHOLD) {
578
$value = round($value, ZBX_UNITS_ROUNDOFF_UPPER_LIMIT);
595
$options['value'] = sprintf('%.'.ZBX_UNITS_ROUNDOFF_LOWER_LIMIT.'f', $options['value']);
596
$options['value'] = preg_replace('/^([\-0-9]+)(\.)([0-9]*)[0]+$/U', '$1$2$3', $options['value']);
597
$options['value'] = rtrim($options['value'], '.');
580
$value = sprintf('%.'.ZBX_UNITS_ROUNDOFF_LOWER_LIMIT.'f', $value);
581
$value = preg_replace('/^([\-0-9]+)(\.)([0-9]*)[0]+$/U', '$1$2$3', $value);
582
$value = rtrim($value, '.');
599
return trim($options['value'].' '.$options['units']);
584
if (zbx_empty($units)) {
588
return $value.' '.$units;
602
592
// if one or more items is B or Bps, then Y-scale use base 8 and calculated in bytes
603
if ($options['byteStep']) {
607
switch ($options['units']) {
611
$options['convert'] = $options['convert'] ? $options['convert'] : ITEM_CONVERT_NO_UNITS;
601
$convert = $convert ? $convert : ITEM_CONVERT_NO_UNITS;
615
$options['convert'] = $options['convert'] ? $options['convert'] : ITEM_CONVERT_NO_UNITS;
605
$convert = $convert ? $convert : ITEM_CONVERT_NO_UNITS;
621
if ($options['value'] < 0) {
622
$abs = bcmul($options['value'], '-1');
625
$abs = $options['value'];
628
if (bccomp($abs, 1) == -1) {
629
$options['value'] = round($options['value'], ZBX_UNITS_ROUNDOFF_MIDDLE_LIMIT);
630
$options['value'] = ($options['length'] && $options['value'] != 0)
631
? sprintf('%.'.$options['length'].'f',$options['value']) : $options['value'];
633
return trim($options['value'].' '.$options['units']);
636
611
// init intervals
637
612
static $digitUnits;
638
613
if (is_null($digitUnits)) {
642
617
if (!isset($digitUnits[$step])) {
643
618
$digitUnits[$step] = array(
619
array('pow' => -2, 'short' => _x('µ', 'Micro short'), 'long' => _('Micro')),
620
array('pow' => -1, 'short' => _x('m', 'Milli short'), 'long' => _('Milli')),
644
621
array('pow' => 0, 'short' => '', 'long' => ''),
645
622
array('pow' => 1, 'short' => _x('K', 'Kilo short'), 'long' => _('Kilo')),
646
623
array('pow' => 2, 'short' => _x('M', 'Mega short'), 'long' => _('Mega')),
675
658
foreach ($digitUnits[$step] as $data) {
676
if ($options['pow'] == $data['pow']) {
659
if ($pow == $data['pow']) {
677
660
$valUnit = $data;
683
if (round($valUnit['value'], ZBX_UNITS_ROUNDOFF_MIDDLE_LIMIT) > 0) {
684
$valUnit['value'] = bcdiv(sprintf('%.10f',$options['value']), sprintf('%.10f', $valUnit['value'])
666
if (round($valUnit['value'], ZBX_UNITS_ROUNDOFF_LOWER_LIMIT) > 0) {
667
if ($valUnit['pow'] >= 0) {
668
$valUnit['value'] = bcdiv(sprintf('%.6f',$value), sprintf('%.6f', $valUnit['value']),
669
ZBX_UNITS_ROUNDOFF_LOWER_LIMIT);
672
$valUnit['value'] = bcdiv(sprintf('%.10f',$value), sprintf('%.10f', $valUnit['value']), ZBX_PRECISION_10);
688
676
$valUnit['value'] = 0;
691
switch ($options['convert']) {
692
case 0: $options['units'] = trim($options['units']);
680
case 0: $units = trim($units);
693
681
case 1: $desc = $valUnit['short']; break;
694
682
case 2: $desc = $valUnit['long']; break;
697
$options['value'] = preg_replace('/^([\-0-9]+)(\.)([0-9]*)[0]+$/U','$1$2$3', round($valUnit['value'],
698
ZBX_UNITS_ROUNDOFF_UPPER_LIMIT));
700
$options['value'] = rtrim($options['value'], '.');
685
$value = preg_replace('/^([\-0-9]+)(\.)([0-9]*)[0]+$/U','$1$2$3', round($valUnit['value'], ZBX_UNITS_ROUNDOFF_UPPER_LIMIT));
686
$value = rtrim($value, '.');
702
688
// fix negative zero
703
if (bccomp($options['value'], 0) == 0) {
704
$options['value'] = 0;
689
if (bccomp($value, 0) == 0) {
707
return trim(sprintf('%s %s%s', $options['length']
708
? sprintf('%.'.$options['length'].'f',$options['value'])
709
: $options['value'], $desc, $options['units']));
693
return rtrim(sprintf('%s %s%s', $value, $desc, $units));