StatsController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. namespace StatsBundle\Controller;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  4. use StatsBundle\Services\DeviceManager;
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Response;
  9. class StatsController extends Controller
  10. {
  11. /**
  12. * @Route("/admin/stats/statsdevice/update", name="stats_update_devices")
  13. *
  14. * @return string
  15. */
  16. public function updateDevicesAction()
  17. {
  18. /* @var $statsDeviceManager DeviceManager */
  19. $statsDeviceManager = $this->get('stats.device.manager');
  20. $statsDeviceManager->getDevices();
  21. return $this->redirect($this->generateUrl('admin_stats_device_list'));
  22. }
  23. /**
  24. * @Route("/admin/stats/olt/stats/graph", name="stats_graph")
  25. *
  26. * @return string
  27. */
  28. public function getStatsGraphAction(Request $request)
  29. {
  30. $grafana_url = $this->getParameter('grafana_url');
  31. // Puertos pon
  32. $pons = array();
  33. for ($x = 1; $x <= 8; $x++) {
  34. for ($y = 1; $y <= 8; $y++) {
  35. $index = "{$x}.{$y}";
  36. $pons[$index] = "PON PORT $index";
  37. }
  38. }
  39. for ($x = 11; $x <= 18; $x++) {
  40. for ($y = 1; $y <= 8; $y++) {
  41. $index = "{$x}.{$y}";
  42. $pons[$index] = "PON PORT {$index}";
  43. }
  44. }
  45. $panelIds = array(6, 4, 5, 1, 2, 3,);
  46. $pon = '1.1';
  47. if ($request->isMethod('POST')) {
  48. $pon = $request->request->get('pon', $pon);
  49. }
  50. $urls = array('olt-total-rate?orgId=1&panelId=1',);
  51. foreach ($panelIds as $panelId) {
  52. $urls[] = "olt-pon-ports?orgId=1&panelId={$panelId}&var-ponport={$pon}";
  53. }
  54. if ($request->isMethod('GET')) {
  55. return $this->render('StatsBundle:Stats:graph.html.twig', array(
  56. 'grafana_url' => $grafana_url,
  57. 'urls' => $urls,
  58. 'pons' => $pons,
  59. ));
  60. }
  61. return new JsonResponse(array(
  62. 'urls' => $urls,
  63. ));
  64. }
  65. /**
  66. * @Route("/admin/stats/onu/stats/graph", name="onu_stats_graph")
  67. *
  68. * @return string
  69. */
  70. public function getONUStatsGraphAction(Request $request)
  71. {
  72. $grafana_url = $this->getParameter('grafana_url');
  73. $onus = $this->get('webservice')->getData('onu', array(
  74. 'id' => $request->get('id')
  75. ));
  76. $urls = array();
  77. $serialNumber = 'fhtt000753b0';
  78. if (isset($onus[0]) && isset($onus[0]['ponSerialNumber'])) {
  79. $serialNumber = $onus[0]['ponSerialNumber'];
  80. }
  81. $panelIds = array(1, 2, 3, 4, 5);
  82. foreach ($panelIds as $panelId) {
  83. $urls[] = "onu?orgId=1&var-serialnumber={$serialNumber}&panelId={$panelId}";
  84. }
  85. if ($request->isMethod('GET')) {
  86. return $this->render('StatsBundle:Stats:onu_graph.html.twig', array(
  87. 'grafana_url' => $grafana_url,
  88. 'urls' => $urls,
  89. 'serial_number' => $serialNumber,
  90. ));
  91. }
  92. return new JsonResponse(array(
  93. 'urls' => $urls,
  94. ));
  95. }
  96. /**
  97. * @Route("/admin/stats/onu/stats/map", name="onu_stats_map")
  98. *
  99. * @return string
  100. */
  101. public function showMapAction(Request $request)
  102. {
  103. $map = array();
  104. $map['zoom'] = 12;
  105. $map['lng'] = -60.065884467624294;
  106. $map['lat'] = -33.902661208376706;
  107. $em = $this->get('doctrine')->getManager();
  108. $adminPool = $this->get('sonata.admin.pool');
  109. $api = $this->get('geoserver.api');
  110. $tenancyId = $this->get("base_tenancy.tenancy_service")->getTenancyIdCurrent();
  111. $deviceServerId = $this->getParameter('device_server_base');
  112. $gsHost = $this->getParameter('geoserver_host');
  113. $gsPort = $this->getParameter('geoserver_port');
  114. $workspace = "deviceServer_{$deviceServerId}";
  115. $layer = "onu_stats_tenancy_{$tenancyId}";
  116. $data = $api->getLayerData($workspace, $layer);
  117. $setLatLng = $setZoom = false;
  118. if(!empty($data)) {
  119. if(isset($data['featureType']['latLonBoundingBox'])) {
  120. $setLatLng = $setZoom = true;
  121. $srs = $data['featureType']['latLonBoundingBox'];
  122. } elseif(isset($data['featureType']['nativeBoundingBox'])) {
  123. $setLatLng = $setZoom = true;
  124. $srs = $data['featureType']['nativeBoundingBox'];
  125. }
  126. } else {
  127. $translator = $this->get('translator');
  128. $flashbag = $this->get('session')->getFlashBag();
  129. $flashbag->add("error", $translator->trans("msg_no_exist_map",array(),"StatsBundle"));
  130. return $this->redirect($this->generateUrl('admin_stats_onu_list'));
  131. }
  132. if($setLatLng) {
  133. $lng = $srs['minx'] + ($srs['maxx'] - $srs['minx']) / 2;
  134. $lat = $srs['miny'] + ($srs['maxy'] - $srs['miny']) / 2;
  135. }
  136. if($setZoom) {
  137. $lngDiff = $srs['maxx'] - $srs['minx'];
  138. $latDiff = $srs['maxy'] - $srs['miny'];
  139. $maxDiff = ($lngDiff > $latDiff) ? $lngDiff : $latDiff;
  140. if ($maxDiff < 360 / pow(2, 20)) {
  141. $zoom = 21;
  142. } else {
  143. $zoom = (-1*( (log($maxDiff)/log(2)) - (log(360)/log(2)))) + 1;
  144. if ($zoom < 1) $zoom = 1;
  145. }
  146. }
  147. $map = array('lat' => $lat, 'lng' => $lng, 'zoom' => $zoom);
  148. return $this->render('StatsBundle:Stats:onu_map.html.twig', array(
  149. 'base_template' => $adminPool->getTemplate('layout'),
  150. 'admin_pool' => $adminPool,
  151. 'admin' => $adminPool->getAdminByClass("StatsBundle\Entity\Onu"),
  152. 'map' => $map,
  153. 'ws' => $workspace,
  154. 'layer' => $layer,
  155. 'gsHost'=> $gsHost,
  156. 'gsPort'=> $gsPort
  157. ));
  158. }
  159. /**
  160. * @Route("/admin/stats/onu/stats/api_feature_json", name="api_feature_json")
  161. *
  162. * @return string
  163. */
  164. public function apiFeatureJsonAction(Request $request)
  165. {
  166. $params = $request->query->all();
  167. $data = explode(":",$params['LAYERS']);
  168. $workspace = $data[0];
  169. $api = $this->get('geoserver.api');
  170. $return = $api->getFeature($workspace, $params);
  171. $data = json_decode($return,true);
  172. if(isset($data['features']) && isset($data['features'][0])) {
  173. $return = json_encode($data['features'][0]['properties']);
  174. } else {
  175. $return = json_encode(array());
  176. }
  177. $response = new Response(
  178. $return,
  179. Response::HTTP_OK,
  180. array('content-type' => 'application/json')
  181. );
  182. return $response;
  183. }
  184. /**
  185. * @Route("/admin/stats/deviceserver/getroutes", name="deviceserver_get_routes")
  186. *
  187. * @return string
  188. */
  189. public function deviceServerGetRoutesAction()
  190. {
  191. $webservice = $this->get("webservice");
  192. $test = $webservice->makeGetRequest("http://200.50.168.118/base/app_dev.php/api/routes");
  193. $response = new Response(
  194. $test,
  195. Response::HTTP_OK,
  196. array('content-type' => 'application/json')
  197. );
  198. return $response;
  199. print_r("<pre>");
  200. var_dump($test);
  201. //$resource = $request->query->get('resource');
  202. //$url = $this->getParameter('url_ftth');
  203. //print_r($url);
  204. die;
  205. }
  206. /**
  207. * @Route("/admin/stats/onu/grafana", name="grafana_onu")
  208. *
  209. * @return string
  210. */
  211. public function grafanaOnuAction(Request $request)
  212. {
  213. $grafana_url = $this->getParameter('grafana_url');
  214. $ponSerialNumber = strtolower($request->get('ponSerialNumber'));
  215. $panelIds = array(6, 1, 2, 3, 4, 5);
  216. foreach ($panelIds as $panelId) {
  217. $urls[] = "onu?orgId=1&var-serialnumber={$ponSerialNumber}&panelId={$panelId}&theme=light";
  218. }
  219. if ($request->isMethod('GET')) {
  220. return $this->render('StatsBundle:Stats:onu_graph.html.twig', array(
  221. 'grafana_url' => $grafana_url,
  222. 'urls' => $urls,
  223. 'serial_number' => $ponSerialNumber,
  224. ));
  225. }
  226. return new JsonResponse(array(
  227. 'urls' => $urls,
  228. ));
  229. }
  230. /**
  231. * @Route("/admin/stats/ponport/grafana", name="grafana_ponport")
  232. *
  233. * @return string
  234. */
  235. public function grafanaPonPortAction(Request $request)
  236. {
  237. $grafana_url = $this->getParameter('grafana_url');
  238. $em = $this->get("doctrine.orm.entity_manager");
  239. $ponPortId = $request->get('id');
  240. $ponPort = $em->getRepository("StatsBundle:PonPort")->findOneById($ponPortId);
  241. $oltDeviceId = $ponPort->getOltDeviceId();
  242. $deviceServerId = $ponPort->getDeviceServer()->getId();
  243. $panelIds = array(1, 2, 3, 4);
  244. $data = explode("/", $ponPort->getPonPort());
  245. if(count($data) == 3) {
  246. $board = $data[0];
  247. $card = $data[1];
  248. $slot = $data[2];
  249. foreach ($panelIds as $panelId) {
  250. $urls[] = "olt-pon-ports-huawei?orgId=1&var-olt={$oltDeviceId}&var-server={$deviceServerId}&var-board={$board}&var-card={$card}&var-slot={$slot}&panelId={$panelId}&theme=light";
  251. }
  252. } else {
  253. $card = $data[0];
  254. $slot = $data[1];
  255. foreach ($panelIds as $panelId) {
  256. $urls[] = "olt-pon-ports?orgId=1&var-olt={$oltDeviceId}&var-server={$deviceServerId}&var-card={$card}&var-slot={$slot}&panelId={$panelId}&theme=light";
  257. }
  258. }
  259. if ($request->isMethod('GET')) {
  260. return $this->render('StatsBundle:Stats:ponport_graph.html.twig', array(
  261. 'grafana_url' => $grafana_url,
  262. 'urls' => $urls
  263. ));
  264. }
  265. return new JsonResponse(array(
  266. 'urls' => $urls,
  267. ));
  268. }
  269. /**
  270. * @Route("/admin/stats/ponport/stats/map", name="ponport_stats_map")
  271. *
  272. * @return string
  273. */
  274. public function showPonPortMapAction(Request $request)
  275. {
  276. $map = array();
  277. $map['zoom'] = 12;
  278. $map['lng'] = -60.065884467624294;
  279. $map['lat'] = -33.902661208376706;
  280. $ponPortId = $request->get('id');
  281. $em = $this->get('doctrine')->getManager();
  282. $ponPort = $em->getRepository("StatsBundle:PonPort")->findOneById($ponPortId);
  283. $oltDeviceId = $ponPort->getOltDeviceId();
  284. $deviceServerId = $ponPort->getDeviceServer()->getId();
  285. $tenancyId = $ponPort->getTenancyId();
  286. $data = explode("/", $ponPort->getPonPort());
  287. $card = $data[0];
  288. $slot = $data[1];
  289. $adminPool = $this->get('sonata.admin.pool');
  290. $api = $this->get('geoserver.api');
  291. $gsHost = $this->getParameter('geoserver_host');
  292. $gsPort = $this->getParameter('geoserver_port');
  293. $workspace = "deviceServer_{$deviceServerId}";
  294. $layer = "ponport_stats_t_{$tenancyId}_o_{$oltDeviceId}_s_{$deviceServerId}_c_{$card}_s_{$slot}";
  295. $data = $api->getLayerData($workspace, $layer);
  296. $setLatLng = $setZoom = false;
  297. if(!empty($data)) {
  298. if(isset($data['featureType']['latLonBoundingBox'])) {
  299. $setLatLng = $setZoom = true;
  300. $srs = $data['featureType']['latLonBoundingBox'];
  301. } elseif(isset($data['featureType']['nativeBoundingBox'])) {
  302. $setLatLng = $setZoom = true;
  303. $srs = $data['featureType']['nativeBoundingBox'];
  304. }
  305. } else {
  306. $translator = $this->get('translator');
  307. $flashbag = $this->get('session')->getFlashBag();
  308. $flashbag->add("error", $translator->trans("msg_no_exist_map",array(),"StatsBundle"));
  309. return $this->redirect($this->generateUrl('admin_stats_ponport_list'));
  310. }
  311. if($setLatLng) {
  312. $lng = $srs['minx'] + ($srs['maxx'] - $srs['minx']) / 2;
  313. $lat = $srs['miny'] + ($srs['maxy'] - $srs['miny']) / 2;
  314. }
  315. if($setZoom) {
  316. $lngDiff = $srs['maxx'] - $srs['minx'];
  317. $latDiff = $srs['maxy'] - $srs['miny'];
  318. $maxDiff = ($lngDiff > $latDiff) ? $lngDiff : $latDiff;
  319. if ($maxDiff < 360 / pow(2, 20)) {
  320. $zoom = 21;
  321. } else {
  322. $zoom = (-1*( (log($maxDiff)/log(2)) - (log(360)/log(2)))) + 1;
  323. if ($zoom < 1) $zoom = 1;
  324. }
  325. }
  326. $map = array('lat' => $lat, 'lng' => $lng, 'zoom' => $zoom);
  327. return $this->render('StatsBundle:Stats:ponport_map.html.twig', array(
  328. 'base_template' => $adminPool->getTemplate('layout'),
  329. 'admin_pool' => $adminPool,
  330. 'admin' => $adminPool->getAdminByClass("StatsBundle\Entity\PonPort"),
  331. 'map' => $map,
  332. 'layer' => $layer,
  333. 'ponPort' => $ponPort,
  334. 'ws' => $workspace,
  335. 'gsHost'=> $gsHost,
  336. 'gsPort'=> $gsPort
  337. ));
  338. }
  339. /**
  340. * @Route("/admin/stats/ponport/grafana", name="grafana_ponport")
  341. *
  342. * @return string
  343. */
  344. public function grafanaInterfaceAction(Request $request)
  345. {
  346. $grafana_url = $this->getParameter('grafana_url');
  347. $em = $this->get("doctrine.orm.entity_manager");
  348. $interfaceId = $request->get('id');
  349. $interface = $em->getRepository("StatsBundle:CmtsInterface")->findOneById($interfaceId);
  350. $cmtsDeviceId = $interface->getCmtsDeviceId();
  351. $deviceServerId = $interface->getDeviceServer()->getId();
  352. $index = $interface->getIndex();
  353. $panelIds = array(1, 2, 3);
  354. foreach ($panelIds as $panelId) {
  355. $urls[] = "cmts-interface?orgId=1&var-cmts={$cmtsDeviceId}&var-server={$deviceServerId}&var-interface={$index}&panelId={$panelId}&theme=light";
  356. }
  357. if ($request->isMethod('GET')) {
  358. return $this->render('StatsBundle:Stats:ponport_graph.html.twig', array(
  359. 'grafana_url' => $grafana_url,
  360. 'urls' => $urls
  361. ));
  362. }
  363. return new JsonResponse(array(
  364. 'urls' => $urls,
  365. ));
  366. }
  367. }