浏览代码

stats:device command

Guillermo Espinoza 8 年之前
父节点
当前提交
b2c2493305

+ 1 - 0
app/AppKernel.php

@@ -31,6 +31,7 @@ class AppKernel extends Kernel
             new WebserviceBundle\WebserviceBundle(),
             new Base\AdminBundle\BaseAdminBundle(),
             new Base\OAuthClientBundle\BaseOAuthClientBundle(),
+            new StatsBundle\StatsBundle(),
         ];
 
         if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {

+ 2 - 0
app/config/config.yml

@@ -10,6 +10,8 @@ imports:
     - { resource: "@BaseOAuthClientBundle/Resources/config/hwioauthbundle/services.yml" }
     - { resource: "@BaseOAuthClientBundle/Resources/config/hwioauthbundle/config.yml" }
     - { resource: "@WebserviceBundle/Resources/config/services.yml" }
+    
+    - { resource: "@StatsBundle/Resources/config/services.yml" }
 
 # Put parameters here that don't need to change on each machine where the app is deployed
 # http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration

+ 5 - 0
app/config/routing.yml

@@ -1,3 +1,8 @@
+stats:
+    resource: "@StatsBundle/Controller/"
+    type:     annotation
+    prefix:   /
+
 login:
     path: /admin/login
     defaults:

+ 3 - 3
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
         "This file is @generated automatically"
     ],
-    "content-hash": "61897c12e75f6f5ef74172609bbd6edf",
+    "content-hash": "eb9095e465928315b30b603a09a932e4",
     "packages": [
         {
             "name": "behat/transliterator",
@@ -1308,7 +1308,7 @@
             "source": {
                 "type": "git",
                 "url": "ssh://git@200.50.168.30:222/VendorSoftwareFlowdat3/Webservice.git",
-                "reference": "c1bb4186bc6d3559c21ce54ed407283f69df7ed5"
+                "reference": "4c0ea5fbf016f9ae1c43cef17d458dcc9eb51237"
             },
             "require": {
                 "ext-curl": "*",
@@ -1321,7 +1321,7 @@
                 }
             },
             "description": "The Flowdat3 Webservice Rest",
-            "time": "2017-04-19 18:03:31"
+            "time": "2017-05-17 15:32:36"
         },
         {
             "name": "incenteev/composer-parameter-handler",

+ 88 - 0
src/StatsBundle/Admin/DeviceServerAdmin.php

@@ -0,0 +1,88 @@
+<?php
+
+namespace StatsBundle\Admin;
+
+use Base\AdminBundle\Admin\BaseAdmin;
+use Sonata\AdminBundle\Datagrid\DatagridMapper;
+use Sonata\AdminBundle\Datagrid\ListMapper;
+use Sonata\AdminBundle\Form\FormMapper;
+use Sonata\AdminBundle\Show\ShowMapper;
+use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
+use Symfony\Component\Form\Extension\Core\Type\TextType;
+use StatsBundle\Utils\DeviceTypes;
+
+class DeviceServerAdmin extends BaseAdmin
+{
+
+    /**
+     * @param DatagridMapper $datagridMapper
+     */
+    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
+    {
+        $datagridMapper
+                ->add('name')
+                ->add('url')
+                ->add('deviceTypes')
+        ;
+    }
+
+    /**
+     * @param ListMapper $listMapper
+     */
+    protected function configureListFields(ListMapper $listMapper)
+    {
+        $listMapper
+                ->add('name')
+                ->add('url')
+                ->add('deviceTypes', 'html', array(
+                    'template' => 'BaseAdminBundle:Utils:badge_field.html.twig',
+                ))
+                ->add('_action', null, array(
+                    'actions' => array(
+                        'show' => array(),
+                        'edit' => array(),
+                        'delete' => array(),
+                    )
+                ))
+        ;
+    }
+
+    /**
+     * @param FormMapper $formMapper
+     */
+    protected function configureFormFields(FormMapper $formMapper)
+    {
+        $formMapper
+                ->add('name')
+                ->add('url', TextType::class, array(
+                    'help' => $this->trans('help.url'),
+                ))
+                ->add('deviceTypes', ChoiceType::class, array(
+                    'choices' => DeviceTypes::getChoices(),
+                    'multiple' => true,
+                    'expanded' => true,
+                    'required' => true,
+                    'choice_translation_domain' => false,
+                    'help' => $this->trans('help.deviceTypes'),
+                ))
+        ;
+    }
+
+    /**
+     * @param ShowMapper $showMapper
+     */
+    protected function configureShowFields(ShowMapper $showMapper)
+    {
+        $showMapper
+                ->add('name')
+                ->add('url')
+                ->add('deviceTypes', 'string', array(
+                    'template' => 'BaseAdminBundle:Utils:badge_field.html.twig',
+                ))
+        ;
+        $this->parameters = array(
+            'template_type' => 'show',
+        );
+    }
+
+}

+ 75 - 0
src/StatsBundle/Admin/StatsDeviceAdmin.php

@@ -0,0 +1,75 @@
+<?php
+
+namespace StatsBundle\Admin;
+
+use Base\AdminBundle\Admin\BaseAdmin;
+use Sonata\AdminBundle\Datagrid\DatagridMapper;
+use Sonata\AdminBundle\Datagrid\ListMapper;
+use Sonata\AdminBundle\Form\FormMapper;
+use Sonata\AdminBundle\Show\ShowMapper;
+use StatsBundle\Utils\DeviceTypes;
+use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
+
+class StatsDeviceAdmin extends BaseAdmin
+{
+
+    /**
+     * @param DatagridMapper $datagridMapper
+     */
+    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
+    {
+        $datagridMapper
+                ->add('ip')
+                ->add('deviceType')
+                ->add('deviceId')
+        ;
+    }
+
+    /**
+     * @param ListMapper $listMapper
+     */
+    protected function configureListFields(ListMapper $listMapper)
+    {
+        $listMapper
+                ->add('ip')
+                ->add('deviceType')
+                ->add('deviceId')
+                ->add('_action', null, array(
+                    'actions' => array(
+                        'show' => array(),
+                        'edit' => array(),
+                        'delete' => array(),
+                    )
+                ))
+        ;
+    }
+
+    /**
+     * @param FormMapper $formMapper
+     */
+    protected function configureFormFields(FormMapper $formMapper)
+    {
+        $formMapper
+                ->add('ip')
+                ->add('deviceType', ChoiceType::class, array(
+                    'choices' => DeviceTypes::getChoices(),
+                    'choice_translation_domain' => false,
+                    'help' => $this->trans('help.deviceTypes'),
+                ))
+                ->add('deviceId')
+        ;
+    }
+
+    /**
+     * @param ShowMapper $showMapper
+     */
+    protected function configureShowFields(ShowMapper $showMapper)
+    {
+        $showMapper
+                ->add('ip')
+                ->add('deviceType')
+                ->add('deviceId')
+        ;
+    }
+
+}

+ 40 - 0
src/StatsBundle/Command/StatsDevicesCommand.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace StatsBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use StatsBundle\Services\DeviceManager;
+
+class StatsDevicesCommand extends ContainerAwareCommand
+{
+
+    protected function configure()
+    {
+        $this
+                ->setName('stats:devices')
+                ->setDescription('Stats Devices command')
+                ->setHelp('This command allows you to get and update stats devices list from devices server url')
+        ;
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        /* @var $statsDeviceManager DeviceManager */
+        $statsDeviceManager = $this->getContainer()->get('stats.device.manager');
+        $devices = $statsDeviceManager->getDevices();
+        if (count($devices)) {
+            foreach ($devices as $device) {
+                $output->writeln(sprintf('<info>DeviceType:</info> %s <info>DeviceIp:</info> %s', $device->getDeviceType(), $device->getIp()));
+            }
+        } else {
+            $output->writeln('No hay nuevos StatsDevice');
+        }
+    }
+
+}

+ 11 - 0
src/StatsBundle/Controller/StatsController.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace StatsBundle\Controller;
+
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
+
+class StatsController extends Controller
+{
+    
+}

+ 131 - 0
src/StatsBundle/Entity/DeviceServer.php

@@ -0,0 +1,131 @@
+<?php
+
+namespace StatsBundle\Entity;
+
+use Doctrine\ORM\Mapping as ORM;
+use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
+use Symfony\Component\Validator\Constraints as Assert;
+
+/**
+ * @ORM\Table
+ * @ORM\Entity
+ * 
+ * @UniqueEntity(fields={"name"}, message="errors.duplicate_key")
+ */
+class DeviceServer
+{
+
+    /**
+     * @var int
+     *
+     * @ORM\Column(name="id", type="integer")
+     * @ORM\Id
+     * @ORM\GeneratedValue(strategy="AUTO")
+     */
+    private $id;
+
+    /**
+     * @var string
+     *
+     * @ORM\Column(type="string", length=255, unique=true)
+     * 
+     * @Assert\NotNull
+     */
+    private $name;
+
+    /**
+     * @var string
+     *
+     * @ORM\Column(type="string", length=255)
+     * 
+     * @Assert\NotNull
+     */
+    private $url;
+
+    /**
+     * @var array
+     *
+     * @ORM\Column(type="array")
+     * 
+     * @Assert\NotNull
+     */
+    private $deviceTypes;
+
+    
+    /**
+     * @return string
+     */
+    public function __toString()
+    {
+        return strval($this->name);
+    }
+
+    /**
+     * @return int
+     */
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    /**
+     * @param string $name
+     *
+     * @return StatsServer
+     */
+    public function setName($name)
+    {
+        $this->name = $name;
+
+        return $this;
+    }
+
+    /**
+     * @return string
+     */
+    public function getName()
+    {
+        return $this->name;
+    }
+
+    /**
+     * @param string $url
+     *
+     * @return StatsServer
+     */
+    public function setUrl($url)
+    {
+        $this->url = $url;
+
+        return $this;
+    }
+
+    /**
+     * @return string
+     */
+    public function getUrl()
+    {
+        return $this->url;
+    }
+
+    /**
+     * @return string
+     */
+    public function getDeviceTypes()
+    {
+        return $this->deviceTypes;
+    }
+
+    /**
+     * @param string $deviceTypes
+     * 
+     * @return $this
+     */
+    public function setDeviceTypes($deviceTypes)
+    {
+        $this->deviceTypes = $deviceTypes;
+
+        return $this;
+    }
+
+}

+ 133 - 0
src/StatsBundle/Entity/StatsDevice.php

@@ -0,0 +1,133 @@
+<?php
+
+namespace StatsBundle\Entity;
+
+use Doctrine\ORM\Mapping as ORM;
+use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
+use Symfony\Component\Validator\Constraints as Assert;
+
+/**
+ * @ORM\Table
+ * @ORM\Entity
+ * 
+ * @UniqueEntity(fields={"ip"}, message="errors.duplicate_key")
+ */
+class StatsDevice
+{
+
+    /**
+     * @var int
+     *
+     * @ORM\Column(name="id", type="integer")
+     * @ORM\Id
+     * @ORM\GeneratedValue(strategy="AUTO")
+     */
+    private $id;
+
+    /**
+     * @var string
+     *
+     * @ORM\Column(type="string", length=255, unique=true)
+     * 
+     * @Assert\NotNull
+     * @Assert\Ip
+     */
+    private $ip;
+
+    /**
+     * @var string
+     *
+     * @ORM\Column(type="string", length=255)
+     * 
+     * @Assert\NotNull
+     */
+    private $deviceType;
+
+    /**
+     * @var int
+     *
+     * @ORM\Column(type="integer")
+     * 
+     * @Assert\NotNull
+     */
+    private $deviceId;
+
+
+    /**
+     * @return string
+     */
+    public function __toString()
+    {
+        return sprintf('%s %s', $this->deviceType, $this->ip);
+    }
+
+    
+    /**
+     * @return int
+     */
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    /**
+     * @param string $ip
+     *
+     * @return StatsDevice
+     */
+    public function setIp($ip)
+    {
+        $this->ip = $ip;
+
+        return $this;
+    }
+
+    /**
+     * @return string
+     */
+    public function getIp()
+    {
+        return $this->ip;
+    }
+
+    /**
+     * @param string $deviceType
+     *
+     * @return StatsDevice
+     */
+    public function setDeviceType($deviceType)
+    {
+        $this->deviceType = $deviceType;
+
+        return $this;
+    }
+
+    /**
+     * @return string
+     */
+    public function getDeviceType()
+    {
+        return $this->deviceType;
+    }
+
+    /**
+     * @param int $deviceId
+     *
+     * @return StatsDevice
+     */
+    public function setDeviceId($deviceId)
+    {
+        $this->deviceId = $deviceId;
+
+        return $this;
+    }
+
+    /**
+     * @return int
+     */
+    public function getDeviceId()
+    {
+        return $this->deviceId;
+    }
+
+}

+ 21 - 0
src/StatsBundle/Resources/config/services.yml

@@ -0,0 +1,21 @@
+services:
+    
+    stats.admin.device_server:
+        class: StatsBundle\Admin\DeviceServerAdmin
+        arguments: [~, StatsBundle\Entity\DeviceServer, SonataAdminBundle:CRUD]
+        tags:
+            - { name: sonata.admin, manager_type: orm, group: Stats, label: DeviceServer, label_catalogue: StatsBundle, label_translator_strategy: sonata.admin.label.strategy.underscore }
+        calls:    
+            - [setTranslationDomain, [StatsBundle]]
+
+    stats.admin.stats_device:
+        class: StatsBundle\Admin\StatsDeviceAdmin
+        arguments: [~, StatsBundle\Entity\StatsDevice, SonataAdminBundle:CRUD]
+        tags:
+            - { name: sonata.admin, manager_type: orm, group: Stats, label: StatsDevice, label_catalogue: StatsBundle, label_translator_strategy: sonata.admin.label.strategy.underscore }
+        calls:    
+            - [setTranslationDomain, [StatsBundle]]
+
+    stats.device.manager:
+        class: StatsBundle\Services\DeviceManager
+        arguments: ['@doctrine.orm.entity_manager','@validator','@webservice']

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

@@ -0,0 +1,45 @@
+breadcrumb:
+    link_device_server_list: Listado Servidor Dispositivos
+    link_device_server_create: Crear Servidor Dispositivos
+    link_stats_device_list: Listado Dispositivos
+
+filter:
+    label_name: Nombre
+    label_url: Url
+    label_device_types: Tipo dispositivo
+    label_ip: Ip
+    label_device_type: Tipo Dispositivo
+    label_device_id: Id Dispositivo
+
+form:
+    label_name: Nombre
+    label_url: Url
+    label_device_types: Tipo dispositivo
+    label_ip: Ip
+    label_device_type: Tipo Dispositivo
+    label_device_id: Id Dispositivo
+
+show:
+    label_name: Nombre
+    label_url: Url
+    label_device_types: Tipo dispositivo
+    label_ip: Ip
+    label_device_type: Tipo Dispositivo
+    label_device_id: Id Dispositivo
+
+list:
+    label_name: Nombre
+    label_url: Url
+    label_device_types: Tipo dispositivo
+    label__action: Acciones
+    label_ip: Ip
+    label_device_type: Tipo Dispositivo
+    label_device_id: Id Dispositivo
+    
+help:
+    url: Url a consultar dispositivos
+    deviceTypes: Tipos de dispositivos a consultar estadísticas
+
+Stats: Estadísticas
+DeviceServer: Servidor Dispositivos
+StatsDevice: Dispositivo

+ 2 - 0
src/StatsBundle/Resources/translations/validators.es.yml

@@ -0,0 +1,2 @@
+errors:
+    duplicate_key: Clave duplicada

+ 105 - 0
src/StatsBundle/Services/DeviceManager.php

@@ -0,0 +1,105 @@
+<?php
+
+namespace StatsBundle\Services;
+
+use Doctrine\ORM\EntityManagerInterface;
+use Doctrine\ORM\EntityRepository;
+use StatsBundle\Entity\StatsDevice;
+use Symfony\Component\Validator\Validator\ValidatorInterface;
+use WebserviceBundle\Services\Webservice;
+
+class DeviceManager
+{
+
+    /**
+     * @var EntityManagerInterface
+     */
+    private $em;
+
+    /**
+     * @var ValidatorInterface
+     */
+    private $validator;
+
+    /**
+     * @var EntityRepository 
+     */
+    private $deviceServerRepository;
+
+    /**
+     * @var EntityRepository 
+     */
+    private $statsDeviceRepository;
+
+    /**
+     * @var Webservice 
+     */
+    private $webservice;
+
+
+    /**
+     * @param EntityManagerInterface $em
+     * @param ValidatorInterface $validator
+     * @param Webservice $webservice
+     */
+    public function __construct(EntityManagerInterface $em, ValidatorInterface $validator, Webservice $webservice)
+    {
+        $this->em = $em;
+        $this->deviceServerRepository = $em->getRepository('StatsBundle:DeviceServer');
+        $this->statsDeviceRepository = $em->getRepository('StatsBundle:StatsDevice');
+        $this->validator = $validator;
+        $this->webservice = $webservice;
+    }
+
+    /**
+     * @return array
+     */
+    public function getDevices()
+    {
+        $devices = array();
+        $deviceServers = $this->deviceServerRepository->findAll();
+        foreach ($deviceServers as $deviceServer) {
+            $deviceTypes = $deviceServer->getDeviceTypes();
+            foreach ($deviceTypes as $deviceType) {
+                $filters = array(
+                    'deviceType' => $deviceType,
+                );
+                $remoteDevices = $this->webservice->get($deviceServer->getUrl(), $filters);
+                foreach ($remoteDevices as $remoteDevice) {
+                    $device = $this->create($remoteDevice['deviceType'], $remoteDevice['deviceId'], $remoteDevice['ip']);
+                    if ($device) {
+                        $devices[] = $device;
+                    }
+                }
+            }
+        }
+
+        return $devices;
+    }
+
+    /**
+     * @param string $deviceType
+     * @param int $deviceId
+     * @param string $ip
+     * 
+     * @return StatsDevice
+     */
+    public function create($deviceType, $deviceId, $ip)
+    {
+        $device = null;
+        if (is_null($this->statsDeviceRepository->findOneByIp($ip))) {
+            $device = new StatsDevice();
+            $device->setDeviceType($deviceType);
+            $device->setDeviceId($deviceId);
+            $device->setIp($ip);
+
+            if ($this->validator->validate($device)->count() == 0) {
+                $this->em->persist($device);
+                $this->em->flush($device);
+            }
+        }
+
+        return $device;
+    }
+
+}

+ 9 - 0
src/StatsBundle/StatsBundle.php

@@ -0,0 +1,9 @@
+<?php
+
+namespace StatsBundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+class StatsBundle extends Bundle
+{
+}

+ 34 - 0
src/StatsBundle/Utils/ChoiceTrait.php

@@ -0,0 +1,34 @@
+<?php
+
+namespace StatsBundle\Utils;
+
+trait ChoiceTrait
+{
+
+    /**
+     * @return array
+     */
+    static function getConstants()
+    {
+        $oClass = new \ReflectionClass(__CLASS__);
+
+        return $oClass->getConstants();
+    }
+
+    /**
+     * Retorna un array con todas las constantes de la clase 
+     * para utilizar en campos de tipo choice en forms
+     * 
+     * @return array
+     */
+    public static function getChoices()
+    {
+        $choices = array();
+        foreach (self::getConstants() as $constant) {
+            $choices[$constant] = $constant;
+        }
+
+        return $choices;
+    }
+
+}

+ 13 - 0
src/StatsBundle/Utils/DeviceTypes.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace StatsBundle\Utils;
+
+class DeviceTypes
+{
+
+    use ChoiceTrait;
+
+    const FTTH_OLT = 'FTTHBundle\Entity\OLT';
+
+
+}