Example #1
0
 public function testConvertArrayWithFactor()
 {
     $values = array(12200, 23440);
     $this->assertEquals(array(0.0122, 0.02344), HumanReadable::convertArrayWithFactor($values, 'b', 2));
     /* Test with bad unit and not convert decimal */
     $this->assertEquals($values, HumanReadable::convertArrayWithFactor($values, 'toto', 2));
     /* Test with bad unit with convert decimal */
     $this->assertEquals(array(12.2, 23.5), HumanReadable::convertArrayWithFactor(array(12.2334, 23.4531), 'toto', 1, 1));
     /* Test with some null value */
     $values = array(12200, 23440, null);
     $this->assertEquals(array(0.0122, 0.02344, null), HumanReadable::convertArrayWithFactor($values, 'b', 2));
     $values = array(12.2334, 23.4531, null);
     $this->assertEquals(array(12.2, 23.5, null), HumanReadable::convertArrayWithFactor($values, 'toto', 1, 1));
 }
Example #2
0
 /**
  * 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()));
     }
 }