From d625efdedc9d578a73ce2b1f329d69de1bef70f5 Mon Sep 17 00:00:00 2001 From: Stephen Hoogendijk Date: Fri, 19 Jun 2015 11:23:31 +0200 Subject: [PATCH] Fixed query builder last method, added median method; fixed adding retention policies --- src/Leaseweb/InfluxDB/Client.php | 62 +++++++++++++++++++------ src/Leaseweb/InfluxDB/Database.php | 7 ++- src/Leaseweb/InfluxDB/Query/Builder.php | 12 +++-- 3 files changed, 61 insertions(+), 20 deletions(-) diff --git a/src/Leaseweb/InfluxDB/Client.php b/src/Leaseweb/InfluxDB/Client.php index 38125bcc22..e3eb5f4c58 100644 --- a/src/Leaseweb/InfluxDB/Client.php +++ b/src/Leaseweb/InfluxDB/Client.php @@ -69,6 +69,11 @@ class Client */ protected $httpClient; + /** + * @var array + */ + protected $options = array(); + /** * @param string $host * @param int $port @@ -87,7 +92,7 @@ class Client $username = '', $password = '', $ssl = false, - $verifySSL = false, + $verifySSL = true, $timeout = 0 // $useUdp = false, // $udpPort = 4444 @@ -103,6 +108,9 @@ class Client if ($ssl) { $this->scheme = 'https'; + $this->options += array( + 'verify' => $verifySSL + ); } // the the base URI @@ -150,9 +158,10 @@ class Client } $params = array_merge(array('q' => $query), $params); + $options = array_merge($this->options, array('query' => $params, 'http_errors' => false)); try { - $response = $this->httpClient->get('query', array('query' => $params, 'http_errors' => false)); + $response = $this->httpClient->get('query', $options); $raw = (string) $response->getBody(); @@ -170,15 +179,24 @@ class Client { $result = $this->query(null, 'SHOW DATABASES')->getPoints(); - $names = array(); - - foreach ($result as $item) { - $names[] = $item['name']; - } - - return $names; + return $this->pointsToArray($result); } + /** + * List all the users + * + * @return array + * @todo implement once issue #3048 is fixed + * + * @throws Exception + */ +// public function listUsers() +// { +// $result = $this->query(null, 'SHOW USERS')->getPoints(); +// +// return $this->pointsToArray($result); +// } + /** * Build the client from a dsn * @@ -186,12 +204,12 @@ class Client * * @param string $dsn * - * @todo finish this functionality + * @todo implement this functionality */ - public static function fromDSN($dsn) - { - $args = array(); - } +// public static function fromDSN($dsn) +// { +// $args = array(); +// } /** * @return mixed @@ -224,4 +242,20 @@ class Client { $this->timeout = $timeout; } + + /** + * @param array $points + * + * @return array + */ + protected function pointsToArray(array $points) + { + $names = array(); + + foreach ($points as $item) { + $names[] = $item['name']; + } + + return $names; + } } \ No newline at end of file diff --git a/src/Leaseweb/InfluxDB/Database.php b/src/Leaseweb/InfluxDB/Database.php index d4de7b973f..d18252361c 100644 --- a/src/Leaseweb/InfluxDB/Database.php +++ b/src/Leaseweb/InfluxDB/Database.php @@ -82,7 +82,7 @@ class Database $this->query(sprintf('CREATE DATABASE %s', $this->name)); if ($retentionPolicy) { - $this->query($this->getRetentionPolicyQuery('CREATE', $retentionPolicy)); + $this->createRetentionPolicy($retentionPolicy); } } catch (\Exception $e) { @@ -92,6 +92,11 @@ class Database } } + public function createRetentionPolicy(RetentionPolicy $retentionPolicy) + { + $this->query($this->getRetentionPolicyQuery('CREATE', $retentionPolicy)); + } + /** * Writes points into INfluxdb * @param Point[] $points diff --git a/src/Leaseweb/InfluxDB/Query/Builder.php b/src/Leaseweb/InfluxDB/Query/Builder.php index c29863fd6e..aa61ce9b98 100644 --- a/src/Leaseweb/InfluxDB/Query/Builder.php +++ b/src/Leaseweb/InfluxDB/Query/Builder.php @@ -88,11 +88,13 @@ class Builder } /** + * @param string $field + * * @return $this */ - public function selectAll() + public function count($field = 'type') { - $this->selection = '*'; + $this->selection = sprintf('count(%s)', $field); return $this; } @@ -102,9 +104,9 @@ class Builder * * @return $this */ - public function count($field = 'type') + public function median($field = 'type') { - $this->selection = sprintf('count(%s)', $field); + $this->selection = sprintf('median(%s)', $field); return $this; } @@ -152,7 +154,7 @@ class Builder */ public function last($field = 'type') { - $this->selection = sprintf('first(%s)', $field); + $this->selection = sprintf('last(%s)', $field); return $this; }