public function testConvertArray() { $newUnit = ''; /* Test bytes */ $values = array(12200, 23440); $this->assertEquals(array(11.9140625, 22.890625), HumanReadable::convertArray($values, 'B', $newUnit)); $this->assertEquals('kB', $newUnit); /* Test with 2 decimal */ $this->assertEquals(array(11.91, 22.89), HumanReadable::convertArray($values, 'B', $newUnit, 2)); /* Test octets */ $this->assertEquals(array(11.9140625, 22.890625), HumanReadable::convertArray($values, 'o', $newUnit)); $this->assertEquals('ko', $newUnit); /* Test bits */ $this->assertEquals(array(12.2, 23.44), HumanReadable::convertArray($values, 'b', $newUnit)); $this->assertEquals('kb', $newUnit); /* Test bad unit */ $this->assertEquals($values, HumanReadable::convertArray($values, 'toto', $newUnit)); /* Test with Zero has value */ $this->assertEquals(null, HumanReadable::convertArray(array(0), 'b', $newUnit)); }
/** * * @param type $domain * @param type $service * @param type $metricList * @return string */ public static function normalizeMetricsForStorage($domain, $service, $metricList) { $normalizeMetricSet = array(); if (!isset($metricList['used'])) { return array(); } $metric = $metricList['used']; $newUnit = ""; $memoryValue = HumanReadable::convertArray(array($metric['current_value'], $metric['max']), $metric['unit_name'], $newUnit, 2); $normalizeMetricSet['current'] = $memoryValue[0]; $normalizeMetricSet['max'] = $memoryValue[1]; if (!empty($newUnit)) { $metric['unit_name'] = $newUnit; } $normalizeMetricSet['unit'] = $metric['unit_name']; $normalizeMetricSet['status'] = strtolower(StatusUtils::numToString($service['state'], StatusUtils::TYPE_SERVICE)); return $normalizeMetricSet; }
/** * * @param type $metricId * @param type $startTime * @param type $endTime * @return type */ public static function getMetricsValuesFromRrd($metricId, $startTime = null, $endTime = null, $unit = null) { $rrdHandler = new Rrd(); if (!is_null($startTime) && !is_null($endTime)) { $rrdHandler->setPeriod($startTime, $endTime); } $datas = array_map(function ($data) { $data = floatval($data); if (is_nan($data)) { return null; } return $data; }, array_values($rrdHandler->getValues($metricId))); $newUnit = ""; if (!is_null($unit)) { $datas = HumanReadable::convertArray($datas, $unit, $newUnit, 2); } return array('datas' => $datas, 'unit' => $newUnit); }