From 017502425f843ce8184d61025dc181b9454448fc Mon Sep 17 00:00:00 2001 From: sam80180 Date: Sun, 2 Jun 2019 16:23:10 +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/InfluxDB/Client.php | 22 ++++++++++++------ src/InfluxDB/Database.php | 41 +++++++++++++++++++++++++++++----- src/InfluxDB/Query/Builder.php | 7 ++++-- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 14c8c51..568b009 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -4,6 +4,7 @@ use InfluxDB\Client\Admin; use InfluxDB\Client\Exception as ClientException; +use InfluxDB\Database; use InfluxDB\Driver\DriverInterface; use InfluxDB\Driver\Exception as DriverException; use InfluxDB\Driver\Guzzle; @@ -88,6 +89,7 @@ class Client */ protected $driver; + protected $precision = Database::PRECISION_NANOSECONDS; /** * Stores the last query that ran @@ -114,7 +116,8 @@ public function __construct( $ssl = false, $verifySSL = false, $timeout = 0, - $connectTimeout = 0 + $connectTimeout = 0, + $precision=Database::PRECISION_NANOSECONDS ) { $this->host = (string) $host; $this->port = (int) $port; @@ -123,7 +126,8 @@ public function __construct( $this->timeout = (float) $timeout; $this->connectTimeout = (float) $connectTimeout; $this->verifySSL = (bool) $verifySSL; - + if (!Database::validatePrecision($precision)) { throw new \Exception("Precisión inválida."); } // fin if + $this->precision = $precision; if ($ssl) { $this->scheme = 'https'; $this->options['verify'] = $verifySSL; @@ -171,9 +175,12 @@ public function query($database, $query, $parameters = []) if ($database) { $parameters['db'] = $database; } - + if (isset($parameters['epoch'])) { + if (!Database::validatePrecision($parameters['epoch'])) { throw new \Exception("Precisión inválida."); } // fin if + $parameters['epoch'] = Database::toValidQueryPrecision($parameters['epoch']); + } // fin if $parameters = [ - 'url' => 'query?' . http_build_query(array_merge(['q' => $query], $parameters)), + 'url' => Database::ENDPOINT_QUERY.'?' . http_build_query(array_merge(['q' => $query, 'epoch'=>Database::toValidQueryPrecision($this->precision)], $parameters)), 'database' => $database, 'method' => 'get' ]; @@ -193,7 +200,7 @@ public function query($database, $query, $parameters = []) return $driver->query(); } catch (\Exception $e) { - throw new Exception('Query has failed: ' . $e->getMessage(), $e->getCode(), $e); + throw new Exception('Query has failed', $e->getCode(), $e); } } @@ -267,7 +274,7 @@ public function listUsers() * @return Client|Database * @throws ClientException */ - public static function fromDSN($dsn, $timeout = 0, $verifySSL = false, $connectTimeout = 0) + public static function fromDSN($dsn, $timeout = 0, $verifySSL = false, $connectTimeout = 0, $precision=Database::PRECISION_NANOSECONDS) { $connParams = parse_url($dsn); @@ -300,7 +307,8 @@ public static function fromDSN($dsn, $timeout = 0, $verifySSL = false, $connectT $ssl, $verifySSL, $timeout, - $connectTimeout + $connectTimeout, + $precision ); // set the UDP driver when the DSN specifies UDP diff --git a/src/InfluxDB/Database.php b/src/InfluxDB/Database.php index e730aff..093503d 100644 --- a/src/InfluxDB/Database.php +++ b/src/InfluxDB/Database.php @@ -17,6 +17,9 @@ */ class Database { + const ENDPOINT_QUERY = "/query"; + const ENDPOINT_WRITE = "/write"; + /** * The name of the Database * @@ -32,12 +35,14 @@ class Database /** * Precision constants */ - const PRECISION_NANOSECONDS = '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"; /** * Construct a database object @@ -161,16 +166,18 @@ function (Point $point) { */ public function writePayload($payload, $precision = self::PRECISION_NANOSECONDS, $retentionPolicy = null) { + $precision = self::toValidQueryPrecision($precision, self::ENDPOINT_WRITE); try { + $query_params = ['db'=>$this->name, 'precision'=>$precision]; $parameters = [ - 'url' => sprintf('write?db=%s&precision=%s', $this->name, $precision), + 'url' => self::ENDPOINT_WRITE."?", 'database' => $this->name, 'method' => 'post' ]; if ($retentionPolicy !== null) { - $parameters['url'] .= sprintf('&rp=%s', $retentionPolicy); + $query_params['rp'] = $retentionPolicy; } - + $parameters['url'] .= http_build_query($query_params); return $this->client->write($parameters, $payload); } catch (Exception $e) { @@ -218,9 +225,9 @@ public function drop() * * @return QueryBuilder */ - public function getQueryBuilder() + public function getQueryBuilder(array $parameters=[]) { - return new QueryBuilder($this); + return new QueryBuilder($this, $parameters); } /** @@ -253,4 +260,26 @@ protected function getRetentionPolicyQuery($method, RetentionPolicy $retentionPo return $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=self::ENDPOINT_QUERY) { + switch ($type) { + case self::ENDPOINT_QUERY: + switch ($strPrecision) { + case self::PRECISION_RFC3339: return NULL; + case self::PRECISION_MICROSECONDS_U: return self::PRECISION_MICROSECONDS; + } // fin switch + break; + case self::ENDPOINT_WRITE: + 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() } diff --git a/src/InfluxDB/Query/Builder.php b/src/InfluxDB/Query/Builder.php index b91d8f0..66837d3 100644 --- a/src/InfluxDB/Query/Builder.php +++ b/src/InfluxDB/Query/Builder.php @@ -80,12 +80,15 @@ class Builder */ protected $orderBy; + protected $parameters; + /** * @param Database $db */ - public function __construct(Database $db) + public function __construct(Database $db, array $parameters=[]) { $this->db = $db; + $this->parameters = $parameters; } /** @@ -297,7 +300,7 @@ public function getQuery() */ public function getResultSet() { - return $this->db->query($this->parseQuery()); + return $this->db->query($this->parseQuery(), $this->parameters); } /**