From 698422918f3808c0e6e4dbd63c1a299a4517bf58 Mon Sep 17 00:00:00 2001 From: sam80180 Date: Sun, 2 Jun 2019 16:34:35 +0800 Subject: [PATCH] =?UTF-8?q?se=20puede=20especificar=20la=20precisi=C3=B3n?= =?UTF-8?q?=20cuando=20hacer=20/query=20y=20/write.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Adapter/Http/Options.php | 21 ++++++++++++++++++++- src/Adapter/Http/Reader.php | 20 +++++++++++++------- src/Adapter/Http/Writer.php | 24 +++++++++++++++--------- src/Client.php | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 17 deletions(-) diff --git a/src/Adapter/Http/Options.php b/src/Adapter/Http/Options.php index 89fe77b..a938da7 100644 --- a/src/Adapter/Http/Options.php +++ b/src/Adapter/Http/Options.php @@ -1,6 +1,8 @@ setPassword("root"); $this->setProtocol("http"); $this->setPrefix(""); - + $this->setEpoch(InfluxClient::PRECISION_RFC3339); + $this->setPrecision(InfluxClient::PRECISION_NANOSECONDS); $this->setRetentionPolicy("default"); $this->setTags([]); } @@ -124,4 +129,18 @@ public function setDatabase($database) $this->database = $database; return $this; } + + public function getEpoch() { return $this->epoch; } // fin getEpoch() + + public function setEpoch($value) { + if (!InfluxClient::validatePrecision($value)) { throw new \Exception("Precisión inválida."); } // fin if + $this->epoch = $value; + } // fin setEpoch() + + public function getPrecision() { return $this->precision; } // fin getPrecision() + + public function setPrecision($value) { + if (!InfluxClient::validatePrecision($value)) { throw new \Exception("Precisión inválida."); } // fin if + $this->precision = $value; + } // fin setPrecision() } diff --git a/src/Adapter/Http/Reader.php b/src/Adapter/Http/Reader.php index fd19a64..d2ca443 100644 --- a/src/Adapter/Http/Reader.php +++ b/src/Adapter/Http/Reader.php @@ -2,6 +2,7 @@ namespace InfluxDB\Adapter\Http; use InfluxDB\Adapter\Http\Options; +use InfluxDB\Client as InfluxClient; use GuzzleHttp\Client; use InfluxDB\Adapter\QueryableInterface; @@ -10,6 +11,8 @@ class Reader implements QueryableInterface private $httpClient; private $options; + const ENDPOINT = "query"; + public function __construct(Client $httpClient, Options $options) { $this->httpClient = $httpClient; @@ -23,11 +26,13 @@ public function getOptions() public function query($query) { + $objOpts = $this->getOptions(); $options = [ - "auth" => [$this->getOptions()->getUsername(), $this->getOptions()->getPassword()], + "auth" => [$objOpts->getUsername(), $objOpts->getPassword()], 'query' => [ "q" => $query, - "db" => $this->getOptions()->getDatabase(), + "db" => $objOpts->getDatabase(), + "epoch"=>InfluxClient::toValidQueryPrecision($objOpts->getEpoch(), self::ENDPOINT) ] ]; @@ -42,17 +47,18 @@ private function get(array $httpMessage) protected function getHttpQueryEndpoint() { - return $this->getHttpEndpoint("query"); + return $this->getHttpEndpoint(self::ENDPOINT); } private function getHttpEndpoint($operation) { + $objOpts = $this->getOptions(); $url = sprintf( "%s://%s:%d%s/%s", - $this->getOptions()->getProtocol(), - $this->getOptions()->getHost(), - $this->getOptions()->getPort(), - $this->getOptions()->getPrefix(), + $objOpts->getProtocol(), + $objOpts->getHost(), + $objOpts->getPort(), + $objOpts->getPrefix(), $operation ); diff --git a/src/Adapter/Http/Writer.php b/src/Adapter/Http/Writer.php index 8863c47..bd2545a 100644 --- a/src/Adapter/Http/Writer.php +++ b/src/Adapter/Http/Writer.php @@ -5,6 +5,7 @@ use InfluxDB\Adapter\WriterTrait; use InfluxDB\Adapter\Http\Options; use InfluxDB\Adapter\WritableInterface; +use InfluxDB\Client as InfluxClient; class Writer implements WritableInterface { @@ -13,6 +14,8 @@ class Writer implements WritableInterface private $httpClient; private $options; + const ENDPOINT = "write"; + public function __construct(Client $httpClient, Options $options) { $this->httpClient = $httpClient; @@ -26,13 +29,15 @@ public function getOptions() public function send(array $message) { + $objOptions = $this->getOptions(); $httpMessage = [ - "auth" => [$this->getOptions()->getUsername(), $this->getOptions()->getPassword()], + "auth" => [$objOptions->getUsername(), $objOptions->getPassword()], 'query' => [ - "db" => $this->getOptions()->getDatabase(), - "retentionPolicy" => $this->getOptions()->getRetentionPolicy(), + "db" => $objOptions->getDatabase(), + "retentionPolicy" => $objOptions->getRetentionPolicy(), + "precision"=>InfluxClient::toValidQueryPrecision($objOpts->getPrecision(), self::ENDPOINT) ], - "body" => $this->messageToLineProtocol($message, $this->getOptions()->getTags()) + "body" => $this->messageToLineProtocol($message, $objOptions->getTags()) ]; $endpoint = $this->getHttpSeriesEndpoint(); @@ -41,17 +46,18 @@ public function send(array $message) protected function getHttpSeriesEndpoint() { - return $this->getHttpEndpoint("write"); + return $this->getHttpEndpoint(self::ENDPOINT); } private function getHttpEndpoint($operation) { + $objOptions = $this->getOptions(); $url = sprintf( "%s://%s:%d%s/%s", - $this->getOptions()->getProtocol(), - $this->getOptions()->getHost(), - $this->getOptions()->getPort(), - $this->getOptions()->getPrefix(), + $objOptions->getProtocol(), + $objOptions->getHost(), + $objOptions->getPort(), + $objOptions->getPrefix(), $operation ); diff --git a/src/Client.php b/src/Client.php index 6f0d9a0..bb12b2f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,6 +2,8 @@ namespace InfluxDB; +use InfluxDB\Adapter\Http\Reader as HttpReader; +use InfluxDB\Adapter\Http\Writer as HttpWriter; use InfluxDB\Adapter\WritableInterface as Writer; use InfluxDB\Adapter\QueryableInterface as Reader; @@ -13,6 +15,16 @@ class Client private $reader; private $writer; + // las constantes de precisión + const PRECISION_NANOSECONDS = "ns"; + const PRECISION_MICROSECONDS_U = "µ"; + const PRECISION_MICROSECONDS = "u"; + const PRECISION_MILLISECONDS = "ms"; + const PRECISION_SECONDS = "s"; + const PRECISION_MINUTES = "m"; + const PRECISION_HOURS = "h"; + const PRECISION_RFC3339 = "rfc3339"; + public function __construct(Reader $reader, Writer $writer) { $this->reader = $reader; @@ -45,4 +57,26 @@ public function query($query) { return $this->getReader()->query($query); } + + public static function validatePrecision($strPrecision) { + return (in_array($strPrecision, [self::PRECISION_HOURS, self::PRECISION_MINUTES, self::PRECISION_SECONDS, self::PRECISION_MILLISECONDS, self::PRECISION_MICROSECONDS, self::PRECISION_MICROSECONDS_U, self::PRECISION_NANOSECONDS, self::PRECISION_RFC3339], TRUE) ? TRUE : FALSE); + } // fin validatePrecision() + + public static function toValidQueryPrecision($strPrecision, $type=HttpReader::ENDPOINT) { + switch ($type) { + case HttpReader::ENDPOINT: + switch ($strPrecision) { + case self::PRECISION_RFC3339: return NULL; + case self::PRECISION_MICROSECONDS_U: return self::PRECISION_MICROSECONDS; + } // fin switch + break; + case HttpWriter::ENDPOINT: + switch ($strPrecision) { + case self::PRECISION_RFC3339: return self::PRECISION_NANOSECONDS; + case self::PRECISION_MICROSECONDS_U: return self::PRECISION_MICROSECONDS; + } // fin switch + break; + } // fin switch + return $strPrecision; + } // fin toValidQueryPrecision() } \ No newline at end of file