Reverted guzzle to v.5 to support php 5.4, increased verbosity and improved internal api; improved readme

This commit is contained in:
Stephen Hoogendijk
2015-06-25 11:48:30 +02:00
parent c01fc4d3d0
commit a3405a7c15
10 changed files with 313 additions and 160 deletions
+7 -4
View File
@@ -116,10 +116,11 @@ class Client
$this->setBaseURI(sprintf('%s://%s:%d', $this->scheme, $this->host, $this->port));
$this->httpClient = new httpClient(array(
'base_uri' => $this->getBaseURI(),
'base_url' => $this->getBaseURI(),
'timeout' => $this->getTimeout()
)
);
}
/**
@@ -156,11 +157,12 @@ class Client
$params += array('db' => $database);
}
$params = array_merge(array('q' => $query), $params);
$options = array_merge($this->options, array('query' => $params, 'http_errors' => false));
$params = '?'.http_build_query(array_merge(array('q' => $query), $params));
$options = array_merge($this->options, array('exceptions' => false));
try {
$response = $this->httpClient->get('query', $options);
$response = $this->httpClient->get('query'.$params, $options);
$raw = (string) $response->getBody();
@@ -180,6 +182,7 @@ class Client
public function write($database, $data)
{
try {
$this->httpClient->post(
$this->getBaseURI() . '/write?db=' . $database,
array('body' => $data)
+9 -4
View File
@@ -100,16 +100,21 @@ class Database
}
}
/**
* @param RetentionPolicy $retentionPolicy
*/
public function createRetentionPolicy(RetentionPolicy $retentionPolicy)
{
$this->query($this->getRetentionPolicyQuery('CREATE', $retentionPolicy));
}
/**
* Writes points into INfluxdb
* Writes points into InfluxDB
*
* @param Point []
* @return ResultSet
* @param array $points
*
* @return bool
* @throws Exception
*/
public function writePoints(array $points)
{
@@ -118,7 +123,7 @@ class Database
foreach ($points as $point) {
if (!$point instanceof Point) {
throw new \InvalidArgumentException('Array of Point should be passed');
throw new \InvalidArgumentException('An array of Point[] should be passed');
}
$payload[] = (string) $point;
+52 -17
View File
@@ -2,6 +2,8 @@
namespace InfluxDB;
use InfluxDB\Database\Exception;
/**
* Class Point
*
@@ -13,30 +15,45 @@ class Point
/**
* @var array
*/
private $tags;
private $tags = array();
/**
* @var array
*/
private $fields;
private $fields = array();
/**
* @var string
*/
private $timestamp;
private $timestamp = null;
/**
* The timestamp is optional.
* If you do not specify a timestamp the servers local timestamp will be used
*
* @param $measurement
* @param array $tags
* @param array $fields
* @param string $timestamp
* @param string $measurement Name of the measurement
* @param float $value Value of the measurement
* @param array $tags Array of tags
* @param array $additionalFields Array of optional fields
* @param int $timestamp Optional timestamp
*
* @throws Exception
*/
public function __construct($measurement, array $tags, array $fields, $timestamp = '')
public function __construct($measurement, $value, array $tags = array(), array $additionalFields = array(), $timestamp = null)
{
$this->measurement = $measurement;
if (empty($measurement)) {
throw new Exception('Invalid measurement name provided');
}
$this->measurement = (string) $measurement;
$this->tags = $tags;
$this->fields = $fields;
$this->fields = $additionalFields;
$this->fields += array('value' => (float) $value);
if ($timestamp && !$this->isValidTimeStamp($timestamp)) {
throw new Exception(sprintf('%s is not a valid timestamp', $timestamp));
}
$this->timestamp = $timestamp;
}
@@ -48,13 +65,20 @@ class Point
*/
public function __toString()
{
return sprintf(
'%s,%s %s %s',
$this->measurement,
$this->arrayToString($this->tags),
$this->arrayToString($this->fields),
$this->timestamp
);
$string = $this->measurement;
if (count($this->tags) > 0) {
$string .= ',' . $this->arrayToString($this->tags);
}
$string .= ' ' .$this->arrayToString($this->fields);
if ($this->timestamp) {
$string .= ' '.$this->timestamp;
}
return $string;
}
private function arrayToString(array $arr)
@@ -68,4 +92,15 @@ class Point
return implode(",", $strParts);
}
/**
* @param $timestamp
*
* @return bool
*/
private function isValidTimeStamp($timestamp)
{
return ((int) $timestamp === $timestamp)
&& ($timestamp <= PHP_INT_MAX)
&& ($timestamp >= ~PHP_INT_MAX);
}
}
+25 -8
View File
@@ -20,6 +20,9 @@ use InfluxDB\ResultSet;
*
* $series->select('*')->from('*')->getResult();
*
* @todo add inner join
* @todo add merge
*
* @package InfluxDB\Query
*/
class Builder
@@ -31,6 +34,7 @@ class Builder
protected $startTime = null;
protected $endTime = null;
protected $metric = null;
protected $limitClause = '';
/**
* @param Database $db
@@ -190,25 +194,34 @@ class Builder
}
/**
* Gets the result from the database (builds the query)
* Limit the ResultSet to n records
*
* @param bool $raw always return the ResultSeriesObjects, even when using an aggregation function
* @param int $count
*
* @return $this
*/
public function limit($count)
{
$this->limitClause = sprintf(' LIMIT %s', (int) $count);
return $this;
}
// public function orderBy
/**
* Gets the result from the database (builds the query)
*
* @return ResultSet
*/
public function getResultSet($raw = false)
public function getResultSet()
{
$query = sprintf("SELECT %s FROM %s", $this->selection, $this->metric);
$aggregateKey = null;
if (!$this->metric) {
throw new \InvalidArgumentException('No metric provided to from()');
}
if (preg_match("/([a-z]+)\(/i", $this->selection, $matches)) {
$aggregateKey = $matches[1];
}
for ($i=0; $i < count($this->where); $i++) {
$selection = 'WHERE';
if ($i > 0) {
@@ -220,6 +233,10 @@ class Builder
}
if ($this->limitClause) {
$query .= $this->limitClause;
}
return $this->db->query($query);
}
}
+1 -1
View File
@@ -92,7 +92,7 @@ class ResultSet
throw new ClientException($object['error']);
}
return $object['series'];
return (isset($object['series']) ? $object['series'] : array());
};
// Foreach object, pick series key