浏览代码

Se agrega servicio que faltócommitear en trabajos anteriores.

Maximiliano Schvindt 6 年之前
父节点
当前提交
c37b89924f
共有 3 个文件被更改,包括 118 次插入0 次删除
  1. 42 0
      Command/EndpointMongoCommand.php
  2. 3 0
      Resources/config/services.yml
  3. 73 0
      Services/EndpointMongo.php

+ 42 - 0
Command/EndpointMongoCommand.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace StatsDBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class EndpointMongoCommand extends ContainerAwareCommand
+{
+
+    protected function configure()
+    {
+        $this
+                ->setName('endpoint:mongo')
+                ->setDescription('Test jsonendpoint mongo - Obtiene los últimos 10 registros de la métrica que se pasa')
+                ->addArgument('metric', InputArgument::REQUIRED, 'Metric name')
+        ;
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        //$json = '{"range":{"from":"2017-12-01T14:09:25.307Z","to":"2017-12-07T23:09:25.308Z"},"intervalMs":0,"targets":[{"target":"d_1_s_1_cmd_fiberhome_olt_scan"}],"format":"json","maxDataPoints":10}';
+        $endpoint = $this->getContainer()->get('endpoint.mongo');
+        $metric = $input->getArgument('metric');
+        $data = array('targets' => array(0 => array("target" => $metric)), 'maxDataPoints' => 10);
+        $json = json_encode($data);
+        $result = $endpoint->get($json,'last');
+        print_r($result);
+
+        
+        print_r(PHP_EOL);
+        print_r(date("Y-m-d H:i:s"));
+        print_r(PHP_EOL);
+    }
+
+}

+ 3 - 0
Resources/config/services.yml

@@ -5,3 +5,6 @@ services:
     endpoint.mysql:
         class: StatsDBundle\Services\EndpointMysql
         arguments: ["@service_container"]
+    endpoint.mongo:
+        class: StatsDBundle\Services\EndpointMongo
+        arguments: ["@service_container"]

+ 73 - 0
Services/EndpointMongo.php

@@ -0,0 +1,73 @@
+<?php
+
+namespace StatsDBundle\Services;
+
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use \Exception;
+
+/**
+ * Read statistics to the statsD
+ */
+class EndpointMongo
+{
+    /**
+     * @var ContainerInterface
+     */
+    protected $serviceContainer;
+    protected $location;
+    protected $query;
+
+    /**
+     * @param ContainerInterface $serviceContainer
+     */
+    public function __construct(ContainerInterface $serviceContainer)
+    {
+        $this->serviceContainer = $serviceContainer;
+
+        if($this->serviceContainer->getParameter("endpoint.mongodb.connection")) {
+            $this->location = $this->serviceContainer->getParameter("endpoint.mongodb.connection");
+        } else {
+            $this->location = null;
+        } 
+    }
+
+    /**
+     * @param string $json
+     * @param string $query
+     * query = [last|query|search]
+     */
+    
+    public function get($json, $method = "query", $exception = false) 
+    {
+        if(is_null($this->location)) return array();
+        
+        $ch = curl_init();
+        
+        curl_setopt($ch, CURLOPT_URL, "{$this->location}/{$method}");
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
+        curl_setopt($ch, CURLOPT_POST, 1);
+
+        $headers = array();
+        $headers[] = "Content-Type: application/json";
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+
+        $result = curl_exec($ch);
+        if (curl_errno($ch)) {
+            if($exception) {
+                curl_close($ch);
+                throw new Exception(curl_error($ch));
+            } else {
+                echo 'Error:' . curl_error($ch);
+            }
+        }
+        curl_close($ch);
+
+        $data = json_decode($result,true);
+        if(is_array($data)) 
+            return $data;
+
+        return array();
+    }
+
+}