From 43c89a161d6c49336ae7ef22bbb499c7224a8fe7 Mon Sep 17 00:00:00 2001 From: Stephen Hoogendijk Date: Thu, 18 Jun 2015 16:17:57 +0200 Subject: [PATCH] Added list methods, cleaned up API --- .gitignore | 1 + src/Leaseweb/InfluxDB/Client.php | 98 ++++++++++++++----- src/Leaseweb/InfluxDB/Database.php | 94 +++++++++++++++--- src/Leaseweb/InfluxDB/Database/Exception.php | 12 +++ .../InfluxDB/Database/RetentionPolicy.php | 21 ++-- src/Leaseweb/InfluxDB/Query/Builder.php | 17 ++-- src/Leaseweb/InfluxDB/Query/Exception.php | 12 +++ 7 files changed, 199 insertions(+), 56 deletions(-) create mode 100644 src/Leaseweb/InfluxDB/Database/Exception.php create mode 100644 src/Leaseweb/InfluxDB/Query/Exception.php diff --git a/.gitignore b/.gitignore index 133f00587..2826a22d7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ vendor/ .idea build/ +test.php \ No newline at end of file diff --git a/src/Leaseweb/InfluxDB/Client.php b/src/Leaseweb/InfluxDB/Client.php index 7854e10aa..38125bcc2 100644 --- a/src/Leaseweb/InfluxDB/Client.php +++ b/src/Leaseweb/InfluxDB/Client.php @@ -5,6 +5,8 @@ namespace Leaseweb\InfluxDB; + +use GuzzleHttp\Client as httpClient; /** * Class Client * @@ -32,11 +34,6 @@ class Client */ protected $password = ''; - /** - * @var string - */ - protected $database = ''; - /** * @var int */ @@ -62,18 +59,21 @@ class Client */ protected $udpPort = 4444; - /** * @var */ protected $baseURI; + /** + * @var \GuzzleHttp\Client + */ + protected $httpClient; + /** * @param string $host * @param int $port * @param string $username * @param string $password - * @param string $database * @param bool $ssl * @param bool $verifySSL * @param int $timeout @@ -86,7 +86,6 @@ class Client $port = 8086, $username = '', $password = '', - $database = '', $ssl = false, $verifySSL = false, $timeout = 0 @@ -99,7 +98,6 @@ class Client $this->port = (int) $port; $this->username = $username; $this->password = $password; - $this->database = $database; $this->timeout = $timeout; $this->verifySSL = (bool) $verifySSL; @@ -110,15 +108,11 @@ class Client // the the base URI $this->setBaseURI(sprintf('%s://%s:%d', $this->scheme, $this->host, $this->port)); - $return = null; - - // return a database instance if a database name has been given - if ($this->database) { - $return = $this->db($database); - } - - return $return; - + $this->httpClient = new httpClient(array( + 'base_uri' => $this->getBaseURI(), + 'timeout' => $this->getTimeout() + ) + ); } /** @@ -132,12 +126,59 @@ class Client { if (empty($name)) { - throw new \InvalidArgumentException(sprintf('No database provided')); + throw new \InvalidArgumentException(sprintf('No name provided')); } return new Database($name, $this); } + /** + * Query influxDB + * + * @param string $database + * @param string $query + * @param array $params + * + * @return ResultSet + * @throws Exception + */ + public function query($database = null, $query, $params = array()) + { + + if ($database) { + $params += array('db' => $database); + } + + $params = array_merge(array('q' => $query), $params); + + try { + $response = $this->httpClient->get('query', array('query' => $params, 'http_errors' => false)); + + $raw = (string) $response->getBody(); + + return new ResultSet($raw); + + } catch (\Exception $e) { + throw new Exception(sprintf('Query has failed, exception: %s', $e->getMessage())); + } + } + + /** + * List all the databases + */ + public function listDatabases() + { + $result = $this->query(null, 'SHOW DATABASES')->getPoints(); + + $names = array(); + + foreach ($result as $item) { + $names[] = $item['name']; + } + + return $names; + } + /** * Build the client from a dsn * @@ -150,9 +191,6 @@ class Client public static function fromDSN($dsn) { $args = array(); - - - } /** @@ -170,4 +208,20 @@ class Client { $this->baseURI = $baseURI; } + + /** + * @return int + */ + public function getTimeout() + { + return $this->timeout; + } + + /** + * @param int $timeout + */ + public function setTimeout($timeout) + { + $this->timeout = $timeout; + } } \ No newline at end of file diff --git a/src/Leaseweb/InfluxDB/Database.php b/src/Leaseweb/InfluxDB/Database.php index f62f3bdcc..8a594abd6 100644 --- a/src/Leaseweb/InfluxDB/Database.php +++ b/src/Leaseweb/InfluxDB/Database.php @@ -5,9 +5,9 @@ namespace Leaseweb\InfluxDB; -use GuzzleHttp\Client as httpClient; use Leaseweb\InfluxDB\Database\RetentionPolicy; use Leaseweb\InfluxDB\Query\Builder as QueryBuilder; +use Leaseweb\InfluxDB\Database\Exception as DatabaseException; /** * Class Database @@ -31,22 +31,24 @@ class Database */ protected $client; - /** - * @var httpClient - */ - protected $httpClient; - /** * Construct a database object * * @param string $name * @param Client $client + * + * @throws DatabaseException */ public function __construct($name, Client $client) { $this->client = $client; - $this->httpClient = new Client(array('base_uri' => $this->client->getBaseURI())); + if (!$name) { + throw new DatabaseException('No database name provided'); + } + + $this->name = $name; + } /** @@ -54,24 +56,58 @@ class Database * * @param string $query * @param array $params + * + * @return ResultSet + * + * @throws Exception */ public function query($query, $params = array()) { - $params = array_merge(array('q' => $query, $params)); - - $result = $this->httpClient->get('query', $params); - - die(var_dump($result)); + return $this->client->query($this->name, $query, $params); } /** * Create this database * * @param RetentionPolicy $retentionPolicy + * + * @return ResultSet + * + * @throws DatabaseException + * @throws Exception */ - public function create(RetentionPolicy $retentionPolicy) + public function create(RetentionPolicy $retentionPolicy = null) { - return $this->query(sprintf('CREATE DATABASE %s', $this->name)); + try { + $this->query(sprintf('CREATE DATABASE %s', $this->name)); + + if ($retentionPolicy) { + $this->query($this->getRetentionPolicyQuery('CREATE', $retentionPolicy)); + } + + } catch (\Exception $e) { + throw new DatabaseException( + sprintf('Failed to created database %s, exception: %s', $this->name, $e->getMessage()) + ); + } + } + + /** + * @param RetentionPolicy $retentionPolicy + */ + public function alterRetentionPolicy(RetentionPolicy $retentionPolicy) + { + $this->query($this->getRetentionPolicyQuery('ALTER', $retentionPolicy)); + } + + /** + * @return array + * + * @throws Exception + */ + public function listRetentionPolicies() + { + return $this->query(sprintf('SHOW RETENTION POLICIES %s', $this->name))->getPoints(); } /** @@ -79,7 +115,7 @@ class Database */ public function drop() { - + $this->query(sprintf('DROP DATABASE %s', $this->name)); } /** @@ -92,5 +128,33 @@ class Database return new QueryBuilder($this); } + /** + * @param $method + * @param RetentionPolicy $retentionPolicy + * + * @return string + */ + protected function getRetentionPolicyQuery($method, RetentionPolicy $retentionPolicy) + { + + if (!in_array($method, array('CREATE', 'ALTER'))) { + throw new \InvalidArgumentException(sprintf('%s is not a valid method')); + } + + $query = sprintf( + '%s RETENTION POLICY %s ON %s DURATION %s REPLICATION %s', + $method, + $retentionPolicy->name, + $this->name, + $retentionPolicy->duration, + $retentionPolicy->replication + ); + + if ($retentionPolicy->default) { + $query .= " DEFAULT"; + } + + return $query; + } } \ No newline at end of file diff --git a/src/Leaseweb/InfluxDB/Database/Exception.php b/src/Leaseweb/InfluxDB/Database/Exception.php new file mode 100644 index 000000000..858934f34 --- /dev/null +++ b/src/Leaseweb/InfluxDB/Database/Exception.php @@ -0,0 +1,12 @@ +name = $name; $this->duration = $duration; $this->replication = $replication; - $this->default = $default; + + $this->default = (bool) $default; } } \ No newline at end of file diff --git a/src/Leaseweb/InfluxDB/Query/Builder.php b/src/Leaseweb/InfluxDB/Query/Builder.php index 6117b3d8d..c29863fd6 100644 --- a/src/Leaseweb/InfluxDB/Query/Builder.php +++ b/src/Leaseweb/InfluxDB/Query/Builder.php @@ -6,9 +6,10 @@ namespace Leaseweb\InfluxDB\Query; use Leaseweb\InfluxDB\Database; +use Leaseweb\InfluxDB\ResultSet; /** - * Class QueryBuilder + * Class Builder * * Abstraction class for getting time series out of InfluxDB * @@ -19,7 +20,7 @@ use Leaseweb\InfluxDB\Database; * * $series->select('*')->from('*')->getResult(); * - * @package Leaseweb\InfluxDB + * @package Leaseweb\InfluxDB\Query */ class Builder { @@ -191,9 +192,9 @@ class Builder * * @param bool $raw always return the ResultSeriesObjects, even when using an aggregation function * - * @return array|null + * @return ResultSet */ - public function getResult($raw = false) + public function getResultSet($raw = false) { $query = sprintf("SELECT %s FROM %s", $this->selection, $this->metric); $aggregateKey = null; @@ -217,12 +218,6 @@ class Builder } - $queryResult = $this->db->query($query); - - if ($queryResult && $aggregateKey && !$raw) { - return (float) $queryResult[0]->$aggregateKey; - } - - return $queryResult; + return $this->db->query($query); } } \ No newline at end of file diff --git a/src/Leaseweb/InfluxDB/Query/Exception.php b/src/Leaseweb/InfluxDB/Query/Exception.php new file mode 100644 index 000000000..9c1e5dab9 --- /dev/null +++ b/src/Leaseweb/InfluxDB/Query/Exception.php @@ -0,0 +1,12 @@ +