OnuService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace Flowdat\Export\App\Service;
  3. use Flowdat\Export\App\Enum\NotificationEnum;
  4. use Flowdat\Export\App\Factory\ExportFactory;
  5. use Flowdat\Export\App\Factory\S3Factory;
  6. class OnuService
  7. {
  8. private $requestService;
  9. private $notificationService;
  10. /**
  11. * OnuService constructor.
  12. */
  13. public function __construct()
  14. {
  15. $this->requestService = new RequestService();
  16. $this->notificationService = new NotificationService();
  17. }
  18. public function exportOnus($data, $headers, $notificationId){
  19. try{
  20. $tenancyId = $data['tenancyId'];
  21. $format = $data['format'];
  22. $endpointBase = $data['endpointBase'];
  23. $endpointOnus = $data['endpointOnus'];
  24. $onus = $this->getOnus($endpointOnus, $headers, $tenancyId);
  25. $clientsIds = array_map(function ($onu){
  26. return $onu['clientId'];
  27. }, $onus);
  28. $clientsIds = array_unique($clientsIds);
  29. $clients = $this->getClients($clientsIds, $endpointBase, $headers);
  30. foreach($onus as $key => $onu){
  31. $onus[$key]['created'] = $onus[$key]['created']['date'];
  32. $onus[$key]['updated'] = is_array($onus[$key]['updated']) ? $onus[$key]['updated']['date'] : null;
  33. if(isset($clients[$onu['clientId']])){
  34. $client = $clients[$onu['clientId']];
  35. $onus[$key]['externalId'] = $client['externalId'];
  36. $onus[$key]['clientName'] = $client['name'];
  37. }
  38. $onus[$key] = array_reverse($onus[$key]);
  39. }
  40. $header = [
  41. 'Nombre Cliente',
  42. 'External ID Cliente',
  43. 'Client ID',
  44. 'Pon Serial Number',
  45. 'Serial Number',
  46. 'Estado',
  47. 'NAP',
  48. 'OLT',
  49. 'Código de Activación',
  50. 'VoIP',
  51. 'CATV',
  52. 'Position',
  53. 'LINK',
  54. 'SLOT',
  55. 'Validación Radius',
  56. 'MAC',
  57. 'IP',
  58. 'Creada',
  59. 'Actualizada'
  60. ];
  61. $fileName = $data['filename'];
  62. $filePath = ExportFactory::create($format, $fileName, $onus, $header);
  63. $filePathS3 = '';
  64. if(is_file($filePath)){
  65. $filePathS3 = S3Factory::send($filePath, $fileName);
  66. }
  67. unlink($filePath);
  68. $this->updateNotification($endpointBase, $headers, $notificationId, $filePathS3);
  69. return true;
  70. } catch (\Exception $ex){
  71. echo $ex->getMessage();
  72. $this->notificationService->update($endpointBase, $notificationId, [
  73. 'message' => $ex->getMessage(),
  74. 'status' => NotificationEnum::ERROR
  75. ], $headers);
  76. }
  77. return true;
  78. }
  79. /**
  80. * @param $endpoint
  81. * @param $tenancyId
  82. * @param $headers
  83. * @return bool|mixed
  84. * @throws \Exception
  85. */
  86. public function getOnus($endpoint, $headers, $tenancyId){
  87. try {
  88. $page = 1;
  89. $limit = 1000;
  90. $onus = [];
  91. $this->makeRequestAndInsertOnu($endpoint, $headers, $onus, $page, $limit, $tenancyId);
  92. return $onus;
  93. } catch (\Exception $ex) {
  94. throw new \Exception($ex->getMessage());
  95. }
  96. }
  97. public function makeRequestAndInsertOnu($endpoint, $headers, &$onus, $currentPage, $limit, $tenancyId){
  98. try{
  99. $params = [
  100. 'filters' => [
  101. 'currentPage' => $currentPage,
  102. 'limit' => $limit,
  103. 'tenancyId' => $tenancyId
  104. ]
  105. ];
  106. $requestOnu = $this->requestService->get($this->requestService->buildUrl($endpoint, $params), $headers);
  107. if($requestOnu['statusCode'] != 200){
  108. echo "Isn't possible to get a some ONUS \n";
  109. return true;
  110. }
  111. $response = json_decode($requestOnu['response'], true);
  112. $qtdPage = $response['pages'];
  113. echo "Getting ONUs {$currentPage}/{$qtdPage}".PHP_EOL;
  114. foreach ($response['data'] as $onu){
  115. array_push($onus, $onu);
  116. }
  117. if($response['hasNext']){
  118. $currentPage++;
  119. return $this->makeRequestAndInsertOnu($endpoint, $headers, $onus, $currentPage, $limit, $tenancyId);
  120. }
  121. return true;
  122. }catch (\Exception $ex){
  123. throw new \Exception($ex->getMessage());
  124. }
  125. }
  126. public function getClients($allClientsIds, $endpointBase, $headers){
  127. try{
  128. $clientsIdsChunk = array_chunk($allClientsIds, 1000);
  129. $clients = [];
  130. $qtdClientsPage = count($clientsIdsChunk);
  131. $i = 1;
  132. foreach ($clientsIdsChunk as $clientIds){
  133. $params = [
  134. 'filters' => [
  135. 'qb-criteria' => true,
  136. 'qb-ids' => implode(",", $clientIds)
  137. ]
  138. ];
  139. $url = $this->requestService->buildUrl($endpointBase.'/api/clients', $params);
  140. $requestClients = $this->requestService->get($url, $headers);
  141. if($requestClients['statusCode'] != 200){
  142. echo "Isn't possible get the clients \n";
  143. continue;
  144. }
  145. echo "Getting Clients $i/$qtdClientsPage".PHP_EOL;
  146. $i++;
  147. $response = json_decode($requestClients['response'], true);
  148. foreach ($response as $client){
  149. $clients[$client['id']] = ['id' => $client['id'], 'name' => $client['name'], 'externalId' => $client['externalId']];
  150. }
  151. }
  152. return $clients;
  153. }catch (\Exception $ex){
  154. throw new \Exception($ex->getMessage());
  155. }
  156. }
  157. public function updateNotification($endpointBase, $headers, $notificationId, $filePathS3){
  158. echo "Updating notification with ID ".$notificationId.PHP_EOL;
  159. try {
  160. $this->notificationService->update($endpointBase, $notificationId, [
  161. 'message' => 'Finalizó la exportación de ONUs',
  162. 'hasFile' => true,
  163. 'file' => $filePathS3,
  164. 'status' => NotificationEnum::FINISHED
  165. ], $headers);
  166. echo "Finished update".PHP_EOL;
  167. } catch (\Exception $e) {
  168. $this->notificationService->update($endpointBase, $notificationId, [
  169. 'message' => $e->getMessage(),
  170. 'status' => NotificationEnum::ERROR
  171. ], $headers);
  172. }
  173. }
  174. }