Explorar o código

Merge branch 'master' of bitbucket.org:ikflowdat/stats

Luciano Andrade %!s(int64=8) %!d(string=hai) anos
pai
achega
1789b36739

+ 2 - 1
app/config/config.yml

@@ -38,7 +38,8 @@ framework:
     session:
         name: flowdat_stats_session
         # http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
-        handler_id:  session.handler.native_file
+#        handler_id: session.handler.pdo
+        handler_id: session.handler.native_file
         save_path:   "%kernel.root_dir%/../var/sessions/%kernel.environment%"
     fragments: ~
     http_method_override: true

+ 7 - 1
app/config/parameters.yml.dist

@@ -21,4 +21,10 @@ parameters:
     jms_serializer.camel_case_naming_strategy.class: JMS\Serializer\Naming\IdenticalPropertyNamingStrategy
     url_logout: 'http://200.50.168.111/base/app_dev.php/logout'
 
-    grafana_url: 'http://200.50.168.108:3000/dashboard-solo/db/'
+    grafana_url: 'http://200.50.168.108:3000/dashboard-solo/db/'
+
+    db_session_host: 127.0.0.1
+    db_session_port: null
+    db_session_name: fd_session
+    db_session_user: root
+    db_session_password: null

+ 6 - 0
app/config/parameters.yml.docker

@@ -31,3 +31,9 @@ parameters:
     rabbit_mq.vhost:    '/'
 
     grafana_url: 'http://grafana.fd3.flowdat.com/dashboard-solo/db/'
+
+    db_session_host: 127.0.0.1
+    db_session_port: null
+    db_session_name: fd_session
+    db_session_user: root
+    db_session_password: null

+ 6 - 6
composer.lock

@@ -1262,7 +1262,7 @@
             "source": {
                 "type": "git",
                 "url": "ssh://git@infra.flowdat.com:222/VendorSoftwareFlowdat3/BaseAdmin.git",
-                "reference": "bd26c21874c4daaf5ea1891c4be873bcf8af1090"
+                "reference": "6fc6cf57add753c378067f07ce58e894185533bf"
             },
             "type": "library",
             "autoload": {
@@ -1277,7 +1277,7 @@
                 "bootstrap",
                 "sonata"
             ],
-            "time": "2017-06-26 12:35:24"
+            "time": "2017-07-10 18:32:01"
         },
         {
             "name": "ik/device-bundle",
@@ -1285,7 +1285,7 @@
             "source": {
                 "type": "git",
                 "url": "ssh://git@infra.flowdat.com:222/VendorSoftwareFlowdat3/DeviceBundle.git",
-                "reference": "bf1029b73d5370885cc3790c5ed708b3263848ca"
+                "reference": "e8ca2d03e566e7a7daa02cb9355a5f4f051ce024"
             },
             "type": "library",
             "autoload": {
@@ -1300,7 +1300,7 @@
                 "bundle",
                 "validators"
             ],
-            "time": "2017-05-30 18:33:01"
+            "time": "2017-07-05 12:23:57"
         },
         {
             "name": "ik/extra-data-bundle",
@@ -1308,7 +1308,7 @@
             "source": {
                 "type": "git",
                 "url": "ssh://git@infra.flowdat.com:222/VendorSoftwareFlowdat3/ExtraDataBundle.git",
-                "reference": "a5a6158cd382490cafd6510334cc63fc50865a58"
+                "reference": "c88fcf124002df00876323fc45039b142bad1403"
             },
             "type": "library",
             "autoload": {
@@ -1323,7 +1323,7 @@
                 "bundle",
                 "extra-data"
             ],
-            "time": "2017-05-29 15:02:46"
+            "time": "2017-07-05 12:20:18"
         },
         {
             "name": "ik/oauthclient-bundle",

+ 10 - 3
src/StatsBundle/Admin/StatsDeviceAdmin.php

@@ -10,7 +10,7 @@ use Sonata\AdminBundle\Show\ShowMapper;
 use DeviceBundle\Utils\DeviceTypes;
 use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
 
-class StatsDeviceAdmin extends BaseAdmin
+class DeviceAdmin extends BaseAdmin
 {
 
     /**
@@ -19,9 +19,11 @@ class StatsDeviceAdmin extends BaseAdmin
     protected function configureDatagridFilters(DatagridMapper $datagridMapper)
     {
         $datagridMapper
+                ->add('deviceServer')
                 ->add('deviceId')
                 ->add('deviceType')
                 ->add('ip')
+                ->add('tenancyId')
         ;
     }
 
@@ -31,10 +33,13 @@ class StatsDeviceAdmin extends BaseAdmin
     protected function configureListFields(ListMapper $listMapper)
     {
         $listMapper
+                ->add('deviceServer')
+                ->add('shortType')
                 ->add('deviceId')
-                ->add('deviceType')
+                //->add('deviceType')
                 ->add('ip')
                 ->add('extraData')
+                ->add('tenancyId')
                 ->add('_action', null, array(
                     'actions' => array(
                         'show' => array(),
@@ -51,6 +56,7 @@ class StatsDeviceAdmin extends BaseAdmin
     protected function configureFormFields(FormMapper $formMapper)
     {
         $formMapper
+                ->add('deviceServer')
                 ->add('deviceId')
                 ->add('deviceType', ChoiceType::class, array(
                     'choices' => DeviceTypes::getChoices(),
@@ -68,6 +74,7 @@ class StatsDeviceAdmin extends BaseAdmin
     protected function configureShowFields(ShowMapper $showMapper)
     {
         $showMapper
+                ->add('deviceServer')
                 ->add('deviceId')
                 ->add('deviceType')
                 ->add('ip')
@@ -85,7 +92,7 @@ class StatsDeviceAdmin extends BaseAdmin
     {
         $actions = parent::configureActionButtons($action, $object);
         $actions['update_devices'] = array(
-            'template' => 'StatsBundle:StatsDevice:update_devices_button.html.twig',
+            'template' => 'StatsBundle:Device:update_devices_button.html.twig',
         );
 
         return $actions;

+ 2 - 2
src/StatsBundle/Command/StatsDevicesCommand.php

@@ -7,7 +7,7 @@ use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 use StatsBundle\Services\DeviceManager;
 
-class StatsDevicesCommand extends ContainerAwareCommand
+class DevicesCommand extends ContainerAwareCommand
 {
 
     protected function configure()
@@ -15,7 +15,7 @@ class StatsDevicesCommand extends ContainerAwareCommand
         $this
                 ->setName('stats:devices')
                 ->setDescription('Stats Devices command')
-                ->setHelp('This command allows you to get and update stats devices list from devices server url')
+                ->setHelp('This command allows you to get and update devices list from devices server url')
         ;
     }
 

+ 1 - 1
src/StatsBundle/Controller/StatsController.php

@@ -22,7 +22,7 @@ class StatsController extends Controller
         $statsDeviceManager = $this->get('stats.device.manager');
         $statsDeviceManager->getDevices();
 
-        return $this->redirect($this->generateUrl('admin_stats_statsdevice_list'));
+        return $this->redirect($this->generateUrl('admin_stats_device_list'));
     }
 
     /**

+ 98 - 9
src/StatsBundle/Entity/StatsDevice.php

@@ -11,9 +11,8 @@ use Symfony\Component\Validator\Constraints as Assert;
  * @ORM\Table
  * @ORM\Entity
  * 
- * @UniqueEntity(fields={"ip"}, message="errors.duplicate_key")
  */
-class StatsDevice
+class Device
 {
 
     use ExtraDataTrait;
@@ -21,7 +20,7 @@ class StatsDevice
     /**
      * @var int
      *
-     * @ORM\Column(name="id", type="integer")
+     * @ORM\Column(name="id", type="integer", nullable=false)
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      */
@@ -30,7 +29,7 @@ class StatsDevice
     /**
      * @var string
      *
-     * @ORM\Column(type="string", length=255, unique=true, nullable=true)
+     * @ORM\Column(type="string", length=255, nullable=true)
      * 
      * @Assert\Ip
      */
@@ -55,6 +54,24 @@ class StatsDevice
      */
     private $deviceId;
 
+    /**
+     * @ORM\ManyToOne(targetEntity="DeviceServer", inversedBy="devices", fetch="EXTRA_LAZY")
+     * 
+     */
+    protected $deviceServer;
+
+    /**
+     * @var int
+     *
+     * @ORM\Column(type="integer", nullable=false, options={"default":1})
+     */
+    protected $tenancyId = 1;
+
+    /**
+     * @ORM\Column(type="datetime")
+     */
+    protected $updated;
+    
 
     /**
      * @return string
@@ -65,17 +82,17 @@ class StatsDevice
     }
 
     /**
-     * @return int
+     * @return string
      */
     public function getId()
     {
-        return $this->id;
+        return "id";
     }
 
     /**
      * @param string $ip
      *
-     * @return StatsDevice
+     * @return Device
      */
     public function setIp($ip = null)
     {
@@ -95,7 +112,7 @@ class StatsDevice
     /**
      * @param string $deviceType
      *
-     * @return StatsDevice
+     * @return Device
      */
     public function setDeviceType($deviceType)
     {
@@ -115,7 +132,7 @@ class StatsDevice
     /**
      * @param int $deviceId
      *
-     * @return StatsDevice
+     * @return Device
      */
     public function setDeviceId($deviceId)
     {
@@ -132,4 +149,76 @@ class StatsDevice
         return $this->deviceId;
     }
 
+    /**
+     * @return DeviceServer
+     */
+    public function getDeviceServer()
+    {
+        return $this->deviceServer;
+    }
+
+    /**
+     * @param DeviceServer $deviceSerevr
+     * @return $this
+     */
+    public function setDeviceServer($deviceServer)
+    {
+        $this->deviceServer = $deviceServer;
+
+        return $this;
+    }
+
+    public function getShortType() 
+    {
+        return str_replace("FTTHBundle\\Entity\\","",$this->deviceType);
+    }
+
+    /**
+     * Set tenancyId
+     *
+     * @param int $tenancyId
+     *
+     * @return Device
+     */
+    public function setTenancyId($tenancyId)
+    {
+        $this->tenancyId = $tenancyId;
+
+        return $this;
+    }
+
+    /**
+     * Get tenancyId
+     *
+     * @return int
+     */
+    public function getTenancyId()
+    {
+        return $this->tenancyId;
+    }
+
+    /**
+     * Set updated
+     *
+     * @param Datetime $tenancyId
+     *
+     * @return Device
+     */
+    public function setUpdated($updated)
+    {
+        $this->updated = $updated;
+
+        return $this;
+    }
+
+    /**
+     * Get updated
+     *
+     * @return \DateTime
+     */
+    public function getUpdated()
+    {
+        return $this->updated;
+    }
+
 }

+ 5 - 5
src/StatsBundle/Resources/config/services.yml

@@ -8,14 +8,14 @@ services:
         calls:    
             - [setTranslationDomain, [StatsBundle]]
 
-    stats.admin.stats_device:
-        class: StatsBundle\Admin\StatsDeviceAdmin
-        arguments: [~, StatsBundle\Entity\StatsDevice, SonataAdminBundle:CRUD]
+    stats.admin.device:
+        class: StatsBundle\Admin\DeviceAdmin
+        arguments: [~, StatsBundle\Entity\Device, SonataAdminBundle:CRUD]
         tags:
-            - { name: sonata.admin, manager_type: orm, group: Stats, label: StatsDevice, label_catalogue: StatsBundle, label_translator_strategy: sonata.admin.label.strategy.underscore }
+            - { name: sonata.admin, manager_type: orm, group: Stats, label: Device, label_catalogue: StatsBundle, label_translator_strategy: sonata.admin.label.strategy.underscore }
         calls:    
             - [setTranslationDomain, [StatsBundle]]
-            - [setTemplate, ['show','StatsBundle:StatsDevice:show.html.twig']]
+            - [setTemplate, ['show','StatsBundle:Device:show.html.twig']]
 
     stats.device.manager:
         class: StatsBundle\Services\DeviceManager

+ 15 - 0
src/StatsBundle/Resources/translations/StatsBundle.es.yml

@@ -2,6 +2,8 @@ breadcrumb:
     link_device_server_list: Listado Servidor Dispositivos
     link_device_server_create: Crear Servidor Dispositivos
     link_stats_device_list: Listado Dispositivos
+    link_device_list: Listado de Dispositivos
+    link_device_create: Crear Dispositivo
 
 filter:
     label_name: Nombre
@@ -10,6 +12,9 @@ filter:
     label_ip: Ip
     label_device_type: Tipo Dispositivo
     label_device_id: Id Dispositivo
+    label_device_server: Servidor
+    label_short_type: Tipo
+    label_tenancy_id: Tenencia
 
 form:
     label_name: Nombre
@@ -18,6 +23,9 @@ form:
     label_ip: Ip
     label_device_type: Tipo Dispositivo
     label_device_id: Id Dispositivo
+    label_device_server: Servidor
+    label_short_type: Tipo
+    label_tenancy_id: Tenencia
 
 show:
     label_name: Nombre
@@ -26,6 +34,9 @@ show:
     label_ip: Ip
     label_device_type: Tipo Dispositivo
     label_device_id: Id Dispositivo
+    label_device_server: Servidor
+    label_short_type: Tipo
+    label_tenancy_id: Tenencia
 
 list:
     label_name: Nombre
@@ -35,6 +46,9 @@ list:
     label_ip: Ip
     label_device_type: Tipo Dispositivo
     label_device_id: Id Dispositivo
+    label_device_server: Servidor
+    label_short_type: Tipo
+    label_tenancy_id: Tenencia
     
 help:
     url: Url a consultar dispositivos
@@ -43,6 +57,7 @@ help:
 Stats: Estadísticas
 DeviceServer: Servidor Dispositivos
 StatsDevice: Dispositivo
+Device: Dispositivo
 
 link_action_update_devices: Actualizar dispositivos
 

src/StatsBundle/Resources/views/StatsDevice/show.html.twig → src/StatsBundle/Resources/views/Device/show.html.twig


src/StatsBundle/Resources/views/StatsDevice/update_devices_button.html.twig → src/StatsBundle/Resources/views/Device/update_devices_button.html.twig


+ 73 - 29
src/StatsBundle/Services/DeviceManager.php

@@ -4,7 +4,7 @@ namespace StatsBundle\Services;
 
 use Doctrine\ORM\EntityManagerInterface;
 use Doctrine\ORM\EntityRepository;
-use StatsBundle\Entity\StatsDevice;
+use StatsBundle\Entity\Device;
 use Symfony\Component\Validator\Validator\ValidatorInterface;
 use WebserviceBundle\Services\Webservice;
 
@@ -29,7 +29,7 @@ class DeviceManager
     /**
      * @var EntityRepository 
      */
-    private $statsDeviceRepository;
+    private $deviceRepository;
 
     /**
      * @var Webservice 
@@ -46,7 +46,7 @@ class DeviceManager
     {
         $this->em = $em;
         $this->deviceServerRepository = $em->getRepository('StatsBundle:DeviceServer');
-        $this->statsDeviceRepository = $em->getRepository('StatsBundle:StatsDevice');
+        $this->deviceRepository = $em->getRepository('StatsBundle:Device');
         $this->validator = $validator;
         $this->webservice = $webservice;
     }
@@ -58,23 +58,55 @@ class DeviceManager
     {
         $devices = array();
         $deviceServers = $this->deviceServerRepository->findAll();
+        $em = $this->em;
+        
+        $dateTime = new \DateTime("now");
+        $date = $dateTime->format("Y-m-d H:i:s");
+
         foreach ($deviceServers as $deviceServer) {
             $deviceTypes = $deviceServer->getDeviceTypes();
+            $deviceServerId = $deviceServer->getId();
+
+            $qb = $em->createQueryBuilder();
+            $qb->delete('StatsBundle:Device', 'd');
+            $qb->where('d.deviceServer = :server');
+            $qb->setParameter('server', $deviceServer);
+
+            $qb->getQuery()->getResult();
+            
             foreach ($deviceTypes as $deviceType) {
                 $filters = array(
                     'deviceType' => $deviceType,
                 );
-                $remoteDevices = $this->webservice->get($deviceServer->getUrl(), $filters);
-                foreach ($remoteDevices as $remoteDevice) {
-                    $deviceType = $remoteDevice['deviceType'];
-                    $deviceId = $remoteDevice['deviceId'];
-                    unset($remoteDevice['id']);
-                    unset($remoteDevice['deviceType']);
-                    unset($remoteDevice['deviceId']);
-                    $device = $this->create($deviceType, $deviceId, $remoteDevice);
-                    if ($device) {
-                        $devices[] = $device;
+
+                $limit = 2000;
+                $offset = 0;
+
+                $remoteDevices = $this->webservice->get($deviceServer->getUrl(), $filters, array(), $limit, $offset);
+                while(!empty($remoteDevices)) {
+
+                    $devices_values = array();
+
+                    foreach ($remoteDevices as $remoteDevice) {
+                        $deviceType = str_replace("\\","\\\\",$remoteDevice['deviceType']);
+                        $deviceId = $remoteDevice['deviceId'];
+                        $tenancyId = $remoteDevice['tenancyId'];
+                        $deviceIp = $remoteDevice['ip'];
+                        $deviceExtraData = $remoteDevice['extraData'];
+                        $devicesIds[] = $deviceId;
+                        $row = "(NULL, {$deviceServerId}, '{$deviceIp}','{$deviceType}', {$deviceId}, '{$deviceExtraData}', {$tenancyId}, '{$date}')";
+                        
+                        $devices_values[] = $row;
+                    
                     }
+
+                    $conn = $em->getConnection();
+                    $sql = "INSERT LOW_PRIORITY IGNORE INTO `device` (`id`, `device_server_id`, `ip`, `device_type`, `device_id`, `extra_data`, `tenancy_id`, `updated`) VALUES ".  implode(",", $devices_values).";";
+                    $conn->query($sql);
+                    $conn->close();
+                    
+                    $offset += $limit;
+                    $remoteDevices = $this->webservice->get($deviceServer->getUrl(), $filters, array(), $limit, $offset);
                 }
             }
         }
@@ -82,35 +114,47 @@ class DeviceManager
         return $devices;
     }
 
+
+    /*
+     * SE DEJA DE UTILIZAR, SE REALIZA UNA ACTUALIZACIÓN POR SQL PARA AGILIZAR, ELIMINACIÓN PREVIA
+     *
+     */
     /**
      * @param string $deviceType
      * @param int $deviceId
      * @param array $data
      * 
-     * @return StatsDevice
+     * @return Device
      */
-    public function create($deviceType, $deviceId, $data = array())
+    public function create($deviceType, $deviceId, $deviceServer, $tenancyId, $data = array(), $dateTime)
     {
         $device = null;
-        $statsDevice = $this->statsDeviceRepository->findOneBy(array(
+        $_device = $this->deviceRepository->findOneBy(array(
             'deviceType' => $deviceType,
             'deviceId' => $deviceId,
+            'deviceServer' => $deviceServer,
+            'tenancyId' => $tenancyId,
         ));
-        if (is_null($statsDevice)) {
-            $device = new StatsDevice();
-            $device->setDeviceType($deviceType);
-            $device->setDeviceId($deviceId);
-            foreach ($data as $column => $value) {
-                $method = 'set'.ucfirst($column);
-                if (method_exists($device, $method)) {
-                    $device->$method($value);
-                }
+        if (is_null($_device)) {
+            $device = new Device();
+        } else {
+            $device = $_device;
+        }
+        $device->setDeviceType($deviceType);
+        $device->setDeviceId($deviceId);
+        $device->setDeviceServer($deviceServer);
+        $device->setTenancyId($tenancyId);
+        $device->setUpdated($dateTime);
+        foreach ($data as $column => $value) {
+            $method = 'set'.ucfirst($column);
+            if (method_exists($device, $method)) {
+                $device->$method($value);
             }
+        }
 
-            if ($this->validator->validate($device)->count() == 0) {
-                $this->em->persist($device);
-                $this->em->flush($device);
-            }
+        if ($this->validator->validate($device)->count() == 0) {
+            $this->em->persist($device);
+            $this->em->flush($device);
         }
 
         return $device;