OltReportController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. namespace StatsBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\Workflow\Exception\ExceptionInterface;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Sonata\AdminBundle\Admin\Pool;
  9. use StatsBundle\Admin\OltAdmin;
  10. class OltReportController extends Controller
  11. {
  12. /**
  13. * @Route("admin/stats/olt/{oltServerId}/{oltDeviceId}/report", name="admin_stats_olt_report")
  14. */
  15. public function oltReport($oltServerId, $oltDeviceId) {
  16. $adminPool = $this->get('sonata.admin.pool');
  17. $filter = array('deviceId' => $oltDeviceId, 'deviceServer' => $oltServerId, 'deviceType' => 'FTTHBundle\Entity\OLT');
  18. $em = $this->get('doctrine')->getManager();
  19. $device = $em->getRepository('StatsBundle:Device')->findOneBy($filter);
  20. $data = $device->jsonExtraData();
  21. return $this->render('StatsBundle:Device:report.html.twig', array(
  22. 'base_template' => $adminPool->getTemplate('layout'),
  23. 'admin_pool' => $adminPool,
  24. 'admin' => $adminPool->getAdminByAdminCode("stats.admin.olt"),
  25. 'device' => $device,
  26. 'data' => $data
  27. ));
  28. }
  29. public function onuStateCountAction($oltServerId, $oltDeviceId) {
  30. $em = $this->get('doctrine')->getManager();
  31. $onuStateCount = array();
  32. $count = $em->getRepository('StatsBundle:Onu')->createQueryBuilder('o')
  33. ->select('o.status','COUNT(o)')
  34. ->where('o.deviceServer = :oltServerId')
  35. ->andWhere('o.oltDeviceId = :oltDeviceId')
  36. ->setParameter('oltServerId', $oltServerId)
  37. ->setParameter('oltDeviceId', $oltDeviceId)
  38. ->groupBy('o.status')
  39. ->getQuery()->getScalarResult();
  40. $percent_online = $online = $offline = 0;
  41. foreach($count as $c) {
  42. if($c['status']) {
  43. $status = "Online";
  44. $color = "#53D356";
  45. $online = $c[1];
  46. } else {
  47. $status = "Offline";
  48. $color = "#F82929";
  49. $offline = $c[1];
  50. }
  51. $onuStateCount[$status] = array('count' => $c[1], 'color' => $color);
  52. }
  53. if($offline != 0 || $online != 0) {
  54. $percent_online = ($online / ($offline + $online)) * 100;
  55. }
  56. return $this->render('StatsBundle:Device:Report/onuStateCount.html.twig', array(
  57. 'onuStateCount' => $onuStateCount,
  58. 'percent_online'=> $percent_online
  59. ));
  60. }
  61. public function oltInformationAction($oltServerId, $oltDeviceId) {
  62. $filter = array('deviceId' => $oltDeviceId, 'deviceServer' => $oltServerId, 'deviceType' => 'FTTHBundle\Entity\OLT');
  63. $em = $this->get('doctrine')->getManager();
  64. $device = $em->getRepository('StatsBundle:Device')->findOneBy($filter);
  65. $subName = "d_{$oltDeviceId}_s_{$oltServerId}";
  66. $redis = $this->get('redis');
  67. $key = "olt_scan_card_{$subName}";
  68. $dataCached = $redis->get($key);
  69. $cpu = $memory = array();
  70. if(isset($dataCached['cpu'])) {$cpu = $dataCached['cpu'];}
  71. if(isset($dataCached['memory'])) {$memory = $dataCached['memory'];}
  72. $data = $device->jsonExtraData();
  73. $load = "75";
  74. $ramMax = 1024;
  75. $ram = 500;
  76. return $this->render('StatsBundle:Device:Report/oltInformation.html.twig', array(
  77. 'data' => $data,
  78. 'load' => $load,
  79. 'ramMax' => $ramMax,
  80. 'ram' => $ram,
  81. 'device' => $device,
  82. 'cpu' => $cpu,
  83. 'memory' => $memory
  84. ));
  85. }
  86. public function oltCommandsAction($oltServerId, $oltDeviceId, $mark) {
  87. $mark = strtolower($mark);
  88. $subName = "d_{$oltDeviceId}_s_{$oltServerId}_cmd_{$mark}";
  89. $result = $targets = array();
  90. $endpoint = $this->get('endpoint.mysql');
  91. $targets["onu_stats"] = array("target" => "{$subName}_onu_stats");
  92. $targets["pon_stats"] = array("target" => "{$subName}_pon_stats");
  93. $targets["pon_octets"] = array("target" => "{$subName}_pon_octets");
  94. $targets["pon_scan"] = array("target" => "{$subName}_pon_scan");
  95. $targets["onu_scan"] = array("target" => "{$subName}_onu_scan");
  96. $errors = array();
  97. foreach($targets as $index => $t) {
  98. $search = array('targets' => array(0 => $t), 'maxDataPoints' => 10);
  99. try {
  100. $data = $endpoint->get(json_encode($search),'last', true);
  101. } catch(\Exception $e) {
  102. $errors[] = array("msg" => "Falla mysql_json_endpoint({$index}): {$e->getMessage()}");
  103. $data = array();
  104. }
  105. if(isset($data[0]) && isset($data[0]['datapoints'])) {
  106. $points = $data[0]['datapoints']; krsort($points);
  107. $result[$index] = $points;
  108. } else {
  109. $result[$index] = array();
  110. }
  111. }
  112. if($errors) {$this->logReport($oltDeviceId, $oltServerId, $errors);}
  113. return $this->render('StatsBundle:Device:Report/oltCommands.html.twig', array(
  114. 'times' => $result
  115. ));
  116. }
  117. public function oltBandwidthAction($oltServerId, $oltDeviceId) {
  118. $subName = "d_{$oltDeviceId}_s_{$oltServerId}";
  119. $result = $targets = array();
  120. $endpoint = $this->get('endpoint.mysql');
  121. $targets["in_bandwidth"] = array("target" => "{$subName}_inbandwidth_olt");
  122. $targets["out_bandwidth"] = array("target" => "{$subName}_outbandwidth_olt");
  123. $errors = array();
  124. foreach($targets as $index => $t) {
  125. $search = array('targets' => array(0 => $t), 'maxDataPoints' => 25);
  126. try {
  127. $data = $endpoint->get(json_encode($search),'last', true);
  128. } catch(\Exception $e) {
  129. $errors[] = array("msg" => "Falla mysql_json_endpoint({$index}): {$e->getMessage()}");
  130. $data = array();
  131. }
  132. if(isset($data[0]) && isset($data[0]['datapoints'])) {
  133. $points = $data[0]['datapoints']; krsort($points);
  134. $result[$index] = $points;
  135. } else {
  136. $result[$index] = array();
  137. }
  138. }
  139. if($errors) {$this->logReport($oltDeviceId, $oltServerId, $errors);}
  140. return $this->render('StatsBundle:Device:Report/oltBandwidth.html.twig', array(
  141. 'inBand' => $result['in_bandwidth'],
  142. 'outBand' => $result['out_bandwidth']
  143. ));
  144. }
  145. public function ponBandwidthAction($oltServerId, $oltDeviceId) {
  146. $subName = "d_{$oltDeviceId}_s_{$oltServerId}";
  147. $data = $totals = $result = array();
  148. $redis = $this->get('redis');
  149. $key_olt_scan = "olt_scan_pons_{$subName}";
  150. $key_olt_pon_bandwidth = "olt_bandwidth_pons_{$subName}";
  151. $dataCached = $redis->get($key_olt_scan);
  152. $bandwidthCached = $redis->get($key_olt_pon_bandwidth);
  153. $restPercent = $sum = $total = $count = $totalIn = $totalOut = 0;
  154. foreach($dataCached as $index => $pon) {
  155. if(isset($pon['nexo'])) continue;
  156. if(isset($bandwidthCached[$index])) {
  157. $in = number_format($bandwidthCached[$index]['inBand'] / 1048576, 2);
  158. $out = number_format($bandwidthCached[$index]['outBand'] / 1048576, 2);
  159. if($in == 0 && $out == 0) continue;
  160. $count++;
  161. $t = $in + $out;
  162. $totalIn += $in;
  163. $totalOut += $out;
  164. $total += $t;
  165. $totals[$index] = $t;
  166. $result[$index] = array('ponPort' => $pon['ponPort'], 'in' => $in, 'out' => $out, 'total' => $t);
  167. }
  168. }
  169. /* http://www.color-hex.com/color/ff7f24 */
  170. $colors = array("#cc651c","#e57220","#ff7f24","#ff8b39","#ff984f","#ffa565","#ffb27b","#ffbf91","#ffcba7","#ffd8bd","#ffe5d3","#fff2e9","#ffffff");
  171. if($total > 0) {
  172. arsort($totals);
  173. $tops = array_slice($totals, 0, 10, true);
  174. $i = 0;
  175. foreach($tops as $index => $t) {
  176. $data[$index] = $result[$index];
  177. $sum += $t;
  178. $percent = number_format(($t / $total)*100, 2);
  179. $data[$index]['percent'] = $percent;
  180. $data[$index]['color'] = $colors[$i];
  181. $i++;
  182. }
  183. $restPercent = number_format((($total - $sum) / $total)*100, 2);
  184. }
  185. /* print_r("<pre>");
  186. print_r($data);
  187. print_r($sum.PHP_EOL);
  188. print_r($total.PHP_EOL);
  189. print_r($totalIn.PHP_EOL);
  190. print_r($totalOut.PHP_EOL);
  191. die; */
  192. return $this->render('StatsBundle:Device:Report/ponBandwidth.html.twig', array(
  193. 'data' => $data,
  194. 'sum' => $sum,
  195. 'total' => $total,
  196. 'totalIn' => $totalIn,
  197. 'totalOut' => $totalOut,
  198. 'restPercent' => $restPercent
  199. ));
  200. }
  201. public function oltConsumptionAction($oltServerId, $oltDeviceId) {
  202. $subName = "d_{$oltDeviceId}_s_{$oltServerId}";
  203. $result = $targets = array();
  204. $endpoint = $this->get('endpoint.mysql');
  205. $targets["in_consumption"] = array("target" => "{$subName}_inconsumption_olt");
  206. $targets["out_consumption"] = array("target" => "{$subName}_outconsumption_olt");
  207. $errors = array();
  208. foreach($targets as $index => $t) {
  209. $search = array('targets' => array(0 => $t), 'maxDataPoints' => 50);
  210. try {
  211. $data = $endpoint->get(json_encode($search),'last', true);
  212. } catch(\Exception $e) {
  213. $errors[] = array("msg" => "Falla mysql_json_endpoint({$index}): {$e->getMessage()}");
  214. $data = array();
  215. }
  216. if(isset($data[0]) && isset($data[0]['datapoints'])) {
  217. $points = $data[0]['datapoints']; krsort($points);
  218. $result[$index] = $points;
  219. } else {
  220. $result[$index] = array();
  221. }
  222. }
  223. if($errors) {$this->logReport($oltDeviceId, $oltServerId, $errors);}
  224. return $this->render('StatsBundle:Device:Report/oltConsumption.html.twig', array(
  225. 'inCons' => $result['in_consumption'],
  226. 'outCons' => $result['out_consumption']
  227. ));
  228. }
  229. private function logReport($deviceId, $serverId, $errors) {
  230. $em = $this->get('doctrine')->getManager();
  231. $device = $em->getRepository('\StatsBundle\Entity\Device')->findOneBy(array('deviceServer' => $serverId, 'deviceType' => 'FTTHBundle\Entity\OLT', 'deviceId' => $deviceId));
  232. if($device) {
  233. $tenancyId = $device->getTenancyId();
  234. $reportManager = $this->get('stats.report.manager');
  235. foreach($errors as $error) {
  236. $reportManager->create("FTTHBundle\Entity\OLT", "MYSQL_ENDPOINT_ERROR", $deviceId, $serverId, $tenancyId, $error);
  237. }
  238. }
  239. }
  240. public function oltReportAction($oltServerId, $oltDeviceId) {
  241. $em = $this->get('doctrine')->getManager();
  242. $reports = $em->getRepository('StatsBundle:Report')->createQueryBuilder('r')
  243. ->select('r')
  244. ->where('r.deviceServer = :oltServerId')
  245. ->andWhere('r.deviceId = :oltDeviceId')
  246. ->andWhere('r.deviceType = :deviceType')
  247. ->andWhere('r.created >= :created')
  248. ->setParameter('oltServerId', $oltServerId)
  249. ->setParameter('oltDeviceId', $oltDeviceId)
  250. ->setParameter('deviceType', 'FTTHBundle\Entity\OLT')
  251. ->setParameter('created', date("Y-m-d 00:00:00",strtotime("-5 days")))
  252. ->orderBy('r.id', 'DESC')
  253. ->getQuery()->getResult();
  254. $results = array();
  255. foreach($reports as $report) {
  256. $row = array();
  257. $row['type'] = $report->getReportType();
  258. $row['time'] = $report->getCreated()->format('Y-m-d H:i:s');
  259. $aux = json_decode($report->getExtraData(),true);
  260. if(isset($aux['msg'])) {
  261. $row['msg'] = $aux['msg'];
  262. } else {
  263. continue;
  264. }
  265. $results[] = $row;
  266. }
  267. return $this->render('StatsBundle:Device:Report/reportList.html.twig', array(
  268. 'results' => $results
  269. ));
  270. }
  271. public function onuSignalAction($oltServerId, $oltDeviceId) {
  272. $em = $this->get('doctrine')->getManager();
  273. $onus = $em->getRepository('StatsBundle:Onu')->createQueryBuilder('o')
  274. ->select('o.txPower','o.rxPowerOlt', 'o.ponSerialNumber')
  275. ->where('o.deviceServer = :oltServerId')
  276. ->andWhere('o.oltDeviceId = :oltDeviceId')
  277. ->setParameter('oltServerId', $oltServerId)
  278. ->setParameter('oltDeviceId', $oltDeviceId)
  279. ->getQuery()->getResult();
  280. $colors = $signals = array();
  281. foreach($onus as $data) {
  282. if($data['txPower'] == null || $data['rxPowerOlt'] == null) continue;
  283. $color = $this->getRxColor($data['rxPowerOlt']);
  284. if(!isset($signals[$color])) $signals[$color] = array();
  285. $signals[$color][] = array('x' => $data['txPower'],'y' => $data['rxPowerOlt'], 'extraData' => $data['ponSerialNumber'], 'id' => "{$data['ponSerialNumber']}~{$oltDeviceId}~{$oltServerId}");
  286. //$colors[] = $this->getRxColor($data['rxPowerOlt']);
  287. }
  288. $_signals = array();
  289. foreach($signals as $color => $points) {
  290. $_signals[$color] = json_encode($points);
  291. }
  292. return $this->render('StatsBundle:Device:Report/onuSignal.html.twig', array(
  293. 'signals' => $_signals
  294. ));
  295. }
  296. private function getRxColor($value) {
  297. if($value < -30)
  298. return "#a00400"; //bordó
  299. elseif($value >= -30 and $value < -28)
  300. return "#ff4d3f"; //rojo
  301. elseif($value >= -28 and $value < -26)
  302. return "#ffa732"; //amarillo
  303. elseif($value >= -26 and $value < -20)
  304. return "#00a65a"; //verde
  305. elseif($value >= -20 and $value < -15)
  306. return "#ffa732"; //amarillo
  307. elseif($value >= -15 and $value < -13)
  308. return "#ff4d3f"; //rojo
  309. else
  310. return "#a00400"; //bordó
  311. }
  312. }