Added list methods, cleaned up API

This commit is contained in:
Stephen Hoogendijk
2015-06-18 16:17:57 +02:00
parent 72cd8b82bd
commit 43c89a161d
7 changed files with 199 additions and 56 deletions
+1
View File
@@ -3,3 +3,4 @@
vendor/
.idea
build/
test.php
+76 -22
View File
@@ -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;
}
}
+79 -15
View File
@@ -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;
}
}
@@ -0,0 +1,12 @@
<?php
/**
* @author Stephen "TheCodeAssassin" Hoogendijk
*/
namespace Leaseweb\InfluxDB\Database;
class Exception extends \Leaseweb\InfluxDB\Exception
{
}
@@ -5,25 +5,29 @@
namespace Leaseweb\InfluxDB\Database;
/**
* Class RetentionPolicy
*
* @package Leaseweb\InfluxDB\Database
*/
class RetentionPolicy
{
/**
* @var string
*/
private $name;
public $name;
/**
* @var string
*/
private $duration;
public $duration;
/**
* @var int
*/
private $replication;
public $replication;
/**
* @var bool
*/
private $default;
public $default;
/**
@@ -31,14 +35,15 @@ class RetentionPolicy
* @param string $duration
* @param int $replication
* @param bool $default
*
* @todo validate duration, replication
*/
public function __construct($name, $duration = '1d', $replication = 1, $default = false)
{
$this->name = $name;
$this->duration = $duration;
$this->replication = $replication;
$this->default = $default;
$this->default = (bool) $default;
}
}
+6 -11
View File
@@ -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);
}
}
+12
View File
@@ -0,0 +1,12 @@
<?php
/**
* @author Stephen "TheCodeAssassin" Hoogendijk
*/
namespace Leaseweb\InfluxDB\Query;
class Exception extends \Leaseweb\InfluxDB\Exception
{
}