public function testGetFactor() { $value = array(1223123344); $this->assertEquals(3, HumanReadable::getFactor($value)); $value = array(10000000); $this->assertEquals(2, HumanReadable::getFactor($value)); $value = array(0); $this->assertEquals(false, HumanReadable::getFactor($value)); }
/** * * @param array $resultSet */ protected function formatDatas(&$resultSet) { $router = Di::getDefault()->get('router'); foreach ($resultSet as &$myHostSet) { $myHostSet['host_name'] = '<span class="icoListing">' . HostRepository::getIconImage($myHostSet['host_name']) . '</span>' . $myHostSet['host_name']; /* Templates */ $myHostSet['host_template'] = ""; $templates = HostRepository::getTemplateChain($myHostSet['host_id'], array(), 1); foreach ($templates as $template) { $myHostSet['host_template'] .= '<a href="' . $router->getPathFor("/centreon-configuration/hosttemplate/[i:id]", array('id' => $template['id'])) . '"><i class="icon-template ico-20"></i></a>'; } /* Display human readable the check/retry interval */ $myHostSet['host_check_interval'] = HumanReadable::convert($myHostSet['host_check_interval'], 's', $units, null, true); $myHostSet['host_retry_check_interval'] = HumanReadable::convert($myHostSet['host_retry_check_interval'], 's', $units, null, true); $sideMenuCustom = new SlideMenu($myHostSet['host_id']); $events = Di::getDefault()->get('events'); $events->emit('centreon-configuration.slide.menu.host.template', array($sideMenuCustom)); //$myHostSet['DT_RowData']['right_side_details'] = $router->getPathFor('/centreon-configuration/host/snapshot/').$myHostSet['host_id']; $myHostSet['DT_RowData']['right_side_menu_list'] = $sideMenuCustom->getMenu(); $myHostSet['DT_RowData']['right_side_default_menu'] = $sideMenuCustom->getDefaultMenu(); /* Get personal tags */ $myHostSet['tagname'] = ''; $aTagUsed = array(); $aTags = TagsRepository::getList('host', $myHostSet['host_id'], 0, 0); foreach ($aTags as $oTags) { if (!in_array($oTags['id'], $aTagUsed)) { $aTagUsed[] = $oTags['id']; $myHostSet['tagname'] .= TagsRepository::getTag('host', $myHostSet['host_id'], $oTags['id'], $oTags['text'], $oTags['user_id'], 1); } } $myHostSet['tagname'] .= TagsRepository::getAddTag('host', $myHostSet['host_id']); } }
/** * * @param array $resultSet */ protected function formatDatas(&$resultSet) { $router = Di::getDefault()->get('router'); foreach ($resultSet as &$myServiceSet) { $myServiceSet['service_description'] = '<span class="icoListing">' . ServiceRepository::getIconImage($myServiceSet['service_id']) . '</span>' . $myServiceSet['service_description']; // Set Tpl Chain $tplStr = null; if (isset($myServiceSet["service_template_model_stm_id"])) { $tplArr = ServicetemplateRepository::getMyServiceTemplateModels($myServiceSet["service_template_model_stm_id"]); $tplRoute = str_replace("//", "/", Di::getDefault()->get('router')->getPathFor('/centreon-configuration/servicetemplate/[i:id]', array('id' => $tplArr['id']))); if (isset($tplArr['description'])) { $tplStr .= '<span><a href="' . $tplRoute . '">' . $tplArr['description'] . '</a></span>'; } } $sideMenuCustom = new SlideMenu($myServiceSet['service_id']); $events = Di::getDefault()->get('events'); $events->emit('centreon-configuration.slide.menu.service.template', array($sideMenuCustom)); //$myHostSet['DT_RowData']['right_side_details'] = $router->getPathFor('/centreon-configuration/host/snapshot/').$myHostSet['host_id']; $myServiceSet['DT_RowData']['right_side_menu_list'] = $sideMenuCustom->getMenu(); $myServiceSet['DT_RowData']['right_side_default_menu'] = $sideMenuCustom->getDefaultMenu(); $myServiceSet['service_template_model_stm_id'] = $tplStr; /* Display human readable the check/retry interval */ $myServiceSet['service_normal_check_interval'] = HumanReadable::convert($myServiceSet['service_normal_check_interval'], 's', $units, null, true); $myServiceSet['service_retry_check_interval'] = HumanReadable::convert($myServiceSet['service_retry_check_interval'], 's', $units, null, true); /* Get personal tags */ $myServiceSet['tagname'] = ''; $aTagUsed = array(); $aTags = TagsRepository::getList('service', $myServiceSet['service_id'], 0, 0); foreach ($aTags as $oTags) { if (!in_array($oTags['id'], $aTagUsed)) { $aTagUsed[] = $oTags['id']; $myServiceSet['tagname'] .= TagsRepository::getTag('service', $myServiceSet['service_id'], $oTags['id'], $oTags['text'], $oTags['user_id'], 1); } } $myServiceSet['tagname'] .= TagsRepository::getAddTag('service', $myServiceSet['service_id']); } }
/** * * @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); }
/** * Get the data for graph * * @method POST * @route /graph */ public function graphDataAction() { try { $router = Di::getDefault()->get('router'); /* Get post information */ $end = $router->request()->param('end_time', time()); $start = $router->request()->param('start_time', $end - 3600 * 72); $serviceId = $router->request()->param('service_id'); if (is_null($serviceId)) { throw new \Exception("Can't get service id"); } $service = new ServiceGraphRepository($serviceId, $start, $end); $data = array(); $serviceData = $service->getValues(200); /* Get times and convert for javascript */ $data['times'] = array_keys($serviceData[0]['data']); $data['times'] = array_map(function ($time) { return $time * 1000; }, $data['times']); $data['metrics'] = array(); /* Check unit for human readable */ $units = array(); foreach ($serviceData as $metric) { if (false === isset($units[$metric['unit']])) { $units[$metric['unit']] = 0; } $values = array_map(function ($value) { if (strval($value) == "NAN") { return 0; } return $value; }, $metric['data']); $factor = HumanReadable::getFactor($values); if ($units[$metric['unit']] < $factor) { $units[$metric['unit']] = $factor; } } /* Convert data for c3js */ foreach ($serviceData as $metric) { $metric['data'] = array_values($metric['data']); $metric['data'] = array_map(function ($data) { if (strval($data) == "NAN") { return null; } return $data; }, $metric['data']); $metric['data'] = HumanReadable::convertArrayWithFactor($metric['data'], $metric['unit'], $units[$metric['unit']], 3); if (in_array($metric['unit'], array_keys(HumanReadable::$units))) { $metric['unit'] = HumanReadable::$units[$metric['unit']]['units'][$units[$metric['unit']]]; } $data['metrics'][] = $metric; } $router->response()->json($data); } catch (\Exception $e) { $router->response()->code(500)->json(array('success' => false, 'error' => $e->getMessage())); } }
/** * * @param int $interval * @return string */ public static function formatNotificationOptions($interval) { return HumanReadable::convert($interval, 's', $units, null, true); }