Fixed query builder last method, added median method; fixed adding retention policies

This commit is contained in:
Stephen Hoogendijk
2015-06-19 11:23:31 +02:00
parent aa25eae865
commit d625efdedc
3 changed files with 61 additions and 20 deletions
+48 -14
View File
@@ -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;
}
}
+6 -1
View File
@@ -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
+7 -5
View File
@@ -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;
}