Пример #1
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()));
     }
 }
Пример #2
0
 /**
  *
  * @api /service/[:ids]/links/graph
  * @method GET
  * @since 3.0.0
  */
 public function graphServiceV1Action()
 {
     $router = Di::getDefault()->get('router');
     $params = $router->request()->params();
     if (false === isset($params['start']) || false === is_numeric($params['start']) || false === isset($params['end']) || false === is_numeric($params['end'])) {
         throw new BadRequestException('Missing parameter start or end', 'You must specify a start and an end timestamp');
     }
     $start = $params['start'];
     $end = $params['end'];
     $ids = explode(',', $params['ids']);
     $result = array();
     $nbPoints = 200;
     foreach ($ids as $id) {
         $data = array();
         $service = new Service($id, $start, $end);
         $serviceData = $service->getValues($nbPoints);
         /* Parse for replace NAN */
         for ($i = 0; $i < count($serviceData); $i++) {
             if (isset($serviceData[$i]['data'])) {
                 $times = array_keys($serviceData[$i]['data']);
                 $values = array_map(function ($element) {
                     if (strtoupper($element) == 'NAN') {
                         return null;
                     }
                     return $element;
                 }, array_values($serviceData[$i]['data']));
             }
             $serviceData[$i]['data'] = $values;
             $serviceData[$i]['label'] = $serviceData[$i]['legend'];
             unset($serviceData[$i]['legend']);
             $serviceData[$i]['type'] = $serviceData[$i]['graph_type'];
             unset($serviceData[$i]['graph_type']);
         }
         $result[] = array('service_id' => $id, 'data' => $serviceData, 'times' => $times, 'size' => $nbPoints);
     }
     $this->sendJsonApiResponse('graph', $result);
 }