mirror of
https://github.com/stylersnico/librenms.git
synced 2026-08-03 08:01:38 +02:00
Few tweaks on CS
This commit is contained in:
+19
-34
@@ -1,17 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Stephen "TheCodeAssassin" Hoogendijk
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace InfluxDB;
|
namespace InfluxDB;
|
||||||
|
|
||||||
|
|
||||||
use InfluxDB\Client\Exception as ClientException;
|
use InfluxDB\Client\Exception as ClientException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Client
|
* Class Client
|
||||||
*
|
*
|
||||||
* @package InfluxDB
|
* @package InfluxDB
|
||||||
|
* @author Stephen "TheCodeAssassin" Hoogendijk
|
||||||
*/
|
*/
|
||||||
class Client
|
class Client
|
||||||
{
|
{
|
||||||
@@ -94,21 +91,17 @@ class Client
|
|||||||
$ssl = false,
|
$ssl = false,
|
||||||
$verifySSL = true,
|
$verifySSL = true,
|
||||||
$timeout = 0
|
$timeout = 0
|
||||||
)
|
) {
|
||||||
{
|
$this->host = (string) $host;
|
||||||
|
|
||||||
$this->host = $host;
|
|
||||||
$this->port = (int) $port;
|
$this->port = (int) $port;
|
||||||
$this->username = $username;
|
$this->username = (string) $username;
|
||||||
$this->password = $password;
|
$this->password = (string) $password;
|
||||||
$this->timeout = $timeout;
|
$this->timeout = (int) $timeout;
|
||||||
$this->verifySSL = (bool) $verifySSL;
|
$this->verifySSL = (bool) $verifySSL;
|
||||||
|
|
||||||
if ($ssl) {
|
if ($ssl) {
|
||||||
$this->scheme = 'https';
|
$this->scheme = 'https';
|
||||||
$this->options += array(
|
$this->options += array('verify' => $verifySSL);
|
||||||
'verify' => $verifySSL
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// the the base URI
|
// the the base URI
|
||||||
@@ -119,6 +112,7 @@ class Client
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* For testing
|
* For testing
|
||||||
|
*
|
||||||
* @param \Guzzle\Http\Client $client
|
* @param \Guzzle\Http\Client $client
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
@@ -133,7 +127,6 @@ class Client
|
|||||||
* Use the given database
|
* Use the given database
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
*
|
|
||||||
* @return Database
|
* @return Database
|
||||||
*/
|
*/
|
||||||
public function selectDB($name)
|
public function selectDB($name)
|
||||||
@@ -147,13 +140,11 @@ class Client
|
|||||||
* @param string $database
|
* @param string $database
|
||||||
* @param string $query
|
* @param string $query
|
||||||
* @param array $params
|
* @param array $params
|
||||||
*
|
|
||||||
* @return ResultSet
|
* @return ResultSet
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function query($database, $query, $params = array())
|
public function query($database, $query, $params = array())
|
||||||
{
|
{
|
||||||
|
|
||||||
if ($database) {
|
if ($database) {
|
||||||
$params['db'] = $database;
|
$params['db'] = $database;
|
||||||
}
|
}
|
||||||
@@ -168,7 +159,6 @@ class Client
|
|||||||
$raw = (string) $response->send()->getBody();
|
$raw = (string) $response->send()->getBody();
|
||||||
|
|
||||||
return new ResultSet($raw);
|
return new ResultSet($raw);
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
throw new Exception(sprintf('Query has failed, exception: %s', $e->getMessage()));
|
throw new Exception(sprintf('Query has failed, exception: %s', $e->getMessage()));
|
||||||
}
|
}
|
||||||
@@ -180,25 +170,24 @@ class Client
|
|||||||
* @param string $database
|
* @param string $database
|
||||||
* @param string $data
|
* @param string $data
|
||||||
* @param string $precision The timestamp precision
|
* @param string $precision The timestamp precision
|
||||||
*
|
|
||||||
* @return bool
|
* @return bool
|
||||||
|
* @throws Exception
|
||||||
*
|
*
|
||||||
* @internal Internal method, do not use directly
|
* @internal Internal method, do not use directly
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
public function write($database, $data, $precision)
|
public function write($database, $data, $precision)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
$result = $this->httpClient->post(
|
||||||
$result = $this->httpClient->post($this->getBaseURI()
|
$this->getBaseURI() .
|
||||||
. '/write?db=' . $database
|
'/write?db=' . $database .
|
||||||
. '&precision=' . $precision
|
'&precision=' . $precision,
|
||||||
, null, $data,
|
null,
|
||||||
|
$data,
|
||||||
array('timeout' => $this->getTimeout())
|
array('timeout' => $this->getTimeout())
|
||||||
)->send();
|
)->send();
|
||||||
|
|
||||||
return $result->getStatusCode() == 204;
|
return $result->getStatusCode() === 204;
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
throw new Exception(sprintf('Writing has failed, exception: %s', $e->getMessage()));
|
throw new Exception(sprintf('Writing has failed, exception: %s', $e->getMessage()));
|
||||||
}
|
}
|
||||||
@@ -218,7 +207,6 @@ class Client
|
|||||||
* List all the users
|
* List all the users
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function listUsers()
|
public function listUsers()
|
||||||
@@ -230,16 +218,12 @@ class Client
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Build the client from a dsn
|
* Build the client from a dsn
|
||||||
*
|
|
||||||
* Example: https+influxdb://username:pass@localhost:8086/databasename', timeout=5
|
* Example: https+influxdb://username:pass@localhost:8086/databasename', timeout=5
|
||||||
*
|
*
|
||||||
* @param string $dsn
|
* @param string $dsn
|
||||||
*
|
|
||||||
* @param int $timeout
|
* @param int $timeout
|
||||||
* @param bool $verifySSL
|
* @param bool $verifySSL
|
||||||
*
|
|
||||||
* @return Client|Database
|
* @return Client|Database
|
||||||
*
|
|
||||||
* @throws ClientException
|
* @throws ClientException
|
||||||
*/
|
*/
|
||||||
public static function fromDSN($dsn, $timeout = 0, $verifySSL = false)
|
public static function fromDSN($dsn, $timeout = 0, $verifySSL = false)
|
||||||
@@ -259,8 +243,8 @@ class Client
|
|||||||
throw new ClientException(sprintf('%s is not a valid scheme', $scheme));
|
throw new ClientException(sprintf('%s is not a valid scheme', $scheme));
|
||||||
}
|
}
|
||||||
|
|
||||||
$ssl = ($modifier && $modifier == 'https' ? true : false);
|
$ssl = $modifier === 'https' ? true : false;
|
||||||
$dbName = ($connParams['path'] ? substr($connParams['path'], 1) : null);
|
$dbName = $connParams['path'] ? substr($connParams['path'], 1) : null;
|
||||||
|
|
||||||
$client = new self(
|
$client = new self(
|
||||||
$connParams['host'],
|
$connParams['host'],
|
||||||
@@ -274,6 +258,7 @@ class Client
|
|||||||
|
|
||||||
return ($dbName ? $client->selectDB($dbName) : $client);
|
return ($dbName ? $client->selectDB($dbName) : $client);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -9,5 +9,4 @@ namespace InfluxDB\Client;
|
|||||||
*/
|
*/
|
||||||
class Exception extends \InfluxDB\Exception
|
class Exception extends \InfluxDB\Exception
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Stephen "TheCodeAssassin" Hoogendijk
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace InfluxDB;
|
namespace InfluxDB;
|
||||||
|
|
||||||
|
use InfluxDB\Database\Exception as DatabaseException;
|
||||||
use InfluxDB\Database\RetentionPolicy;
|
use InfluxDB\Database\RetentionPolicy;
|
||||||
use InfluxDB\Query\Builder as QueryBuilder;
|
use InfluxDB\Query\Builder as QueryBuilder;
|
||||||
use InfluxDB\Database\Exception as DatabaseException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Database
|
* Class Database
|
||||||
@@ -15,10 +12,10 @@ use InfluxDB\Database\Exception as DatabaseException;
|
|||||||
* @todo admin functionality
|
* @todo admin functionality
|
||||||
*
|
*
|
||||||
* @package InfluxDB
|
* @package InfluxDB
|
||||||
|
* @author Stephen "TheCodeAssassin" Hoogendijk
|
||||||
*/
|
*/
|
||||||
class Database
|
class Database
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name of the Database
|
* The name of the Database
|
||||||
*
|
*
|
||||||
@@ -31,7 +28,6 @@ class Database
|
|||||||
*/
|
*/
|
||||||
protected $client;
|
protected $client;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Precision constants
|
* Precision constants
|
||||||
*/
|
*/
|
||||||
@@ -47,23 +43,19 @@ class Database
|
|||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param Client $client
|
* @param Client $client
|
||||||
*
|
|
||||||
* @throws DatabaseException
|
|
||||||
*/
|
*/
|
||||||
public function __construct($name, Client $client)
|
public function __construct($name, Client $client)
|
||||||
{
|
{
|
||||||
$this->client = $client;
|
if (empty($name)) {
|
||||||
|
|
||||||
if (!$name) {
|
|
||||||
throw new \InvalidArgumentException('No database name provided');
|
throw new \InvalidArgumentException('No database name provided');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
|
$this->client = $client;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string db name
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
@@ -75,9 +67,7 @@ class Database
|
|||||||
*
|
*
|
||||||
* @param string $query
|
* @param string $query
|
||||||
* @param array $params
|
* @param array $params
|
||||||
*
|
|
||||||
* @return ResultSet
|
* @return ResultSet
|
||||||
*
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function query($query, $params = array())
|
public function query($query, $params = array())
|
||||||
@@ -89,9 +79,7 @@ class Database
|
|||||||
* Create this database
|
* Create this database
|
||||||
*
|
*
|
||||||
* @param RetentionPolicy $retentionPolicy
|
* @param RetentionPolicy $retentionPolicy
|
||||||
*
|
|
||||||
* @return ResultSet
|
* @return ResultSet
|
||||||
*
|
|
||||||
* @throws DatabaseException
|
* @throws DatabaseException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@@ -103,7 +91,6 @@ class Database
|
|||||||
if ($retentionPolicy) {
|
if ($retentionPolicy) {
|
||||||
$this->createRetentionPolicy($retentionPolicy);
|
$this->createRetentionPolicy($retentionPolicy);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
throw new DatabaseException(
|
throw new DatabaseException(
|
||||||
sprintf('Failed to created database %s, exception: %s', $this->name, $e->getMessage())
|
sprintf('Failed to created database %s, exception: %s', $this->name, $e->getMessage())
|
||||||
@@ -113,7 +100,6 @@ class Database
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param RetentionPolicy $retentionPolicy
|
* @param RetentionPolicy $retentionPolicy
|
||||||
*
|
|
||||||
* @return ResultSet
|
* @return ResultSet
|
||||||
*/
|
*/
|
||||||
public function createRetentionPolicy(RetentionPolicy $retentionPolicy)
|
public function createRetentionPolicy(RetentionPolicy $retentionPolicy)
|
||||||
@@ -126,7 +112,6 @@ class Database
|
|||||||
*
|
*
|
||||||
* @param Point[] $points Array of points
|
* @param Point[] $points Array of points
|
||||||
* @param string $precision The timestamp precision (defaults to nanoseconds)
|
* @param string $precision The timestamp precision (defaults to nanoseconds)
|
||||||
*
|
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@@ -135,7 +120,6 @@ class Database
|
|||||||
$payload = array();
|
$payload = array();
|
||||||
|
|
||||||
foreach ($points as $point) {
|
foreach ($points as $point) {
|
||||||
|
|
||||||
if (! $point instanceof Point) {
|
if (! $point instanceof Point) {
|
||||||
throw new \InvalidArgumentException('An array of Point[] should be passed');
|
throw new \InvalidArgumentException('An array of Point[] should be passed');
|
||||||
}
|
}
|
||||||
@@ -166,7 +150,6 @@ class Database
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function listRetentionPolicies()
|
public function listRetentionPolicies()
|
||||||
@@ -174,7 +157,6 @@ class Database
|
|||||||
return $this->query(sprintf('SHOW RETENTION POLICIES %s', $this->name))->getPoints();
|
return $this->query(sprintf('SHOW RETENTION POLICIES %s', $this->name))->getPoints();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drop this database
|
* Drop this database
|
||||||
*/
|
*/
|
||||||
@@ -202,9 +184,8 @@ class Database
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $method
|
* @param string $method
|
||||||
* @param RetentionPolicy $retentionPolicy
|
* @param RetentionPolicy $retentionPolicy
|
||||||
*
|
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getRetentionPolicyQuery($method, RetentionPolicy $retentionPolicy)
|
protected function getRetentionPolicyQuery($method, RetentionPolicy $retentionPolicy)
|
||||||
@@ -229,5 +210,4 @@ class Database
|
|||||||
|
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Stephen "TheCodeAssassin" Hoogendijk
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace InfluxDB\Database;
|
namespace InfluxDB\Database;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Stephen "TheCodeAssassin" Hoogendijk
|
||||||
|
*/
|
||||||
class Exception extends \InfluxDB\Exception
|
class Exception extends \InfluxDB\Exception
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Stephen "TheCodeAssassin" Hoogendijk
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace InfluxDB\Database;
|
namespace InfluxDB\Database;
|
||||||
|
|
||||||
@@ -9,6 +6,7 @@ namespace InfluxDB\Database;
|
|||||||
* Class RetentionPolicy
|
* Class RetentionPolicy
|
||||||
*
|
*
|
||||||
* @package InfluxDB\Database
|
* @package InfluxDB\Database
|
||||||
|
* @author Stephen "TheCodeAssassin" Hoogendijk
|
||||||
*/
|
*/
|
||||||
class RetentionPolicy
|
class RetentionPolicy
|
||||||
{
|
{
|
||||||
@@ -16,20 +14,22 @@ class RetentionPolicy
|
|||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $name;
|
public $name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $duration;
|
public $duration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int
|
* @var int
|
||||||
*/
|
*/
|
||||||
public $replication;
|
public $replication;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
public $default;
|
public $default;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $duration
|
* @param string $duration
|
||||||
@@ -43,7 +43,6 @@ class RetentionPolicy
|
|||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
$this->duration = $duration;
|
$this->duration = $duration;
|
||||||
$this->replication = $replication;
|
$this->replication = $replication;
|
||||||
|
|
||||||
$this->default = (bool) $default;
|
$this->default = (bool) $default;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Stephen "TheCodeAssassin" Hoogendijk
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace InfluxDB;
|
namespace InfluxDB;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Stephen "TheCodeAssassin" Hoogendijk
|
||||||
|
*/
|
||||||
class Exception extends \Exception
|
class Exception extends \Exception
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
+41
-19
@@ -11,35 +11,44 @@ use InfluxDB\Database\Exception as DatabaseException;
|
|||||||
*/
|
*/
|
||||||
class Point
|
class Point
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
private $measurement;
|
private $measurement;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $tags = array();
|
private $tags = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $fields = array();
|
private $fields = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $timestamp = null;
|
private $timestamp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The timestamp is optional.
|
* The timestamp is optional. If you do not specify a timestamp the server’s
|
||||||
* If you do not specify a timestamp the server’s local timestamp will be used
|
* local timestamp will be used
|
||||||
*
|
|
||||||
* @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
|
|
||||||
*
|
*
|
||||||
|
* @param string $measurement
|
||||||
|
* @param float $value
|
||||||
|
* @param array $tags
|
||||||
|
* @param array $additionalFields
|
||||||
|
* @param null $timestamp
|
||||||
* @throws DatabaseException
|
* @throws DatabaseException
|
||||||
*/
|
*/
|
||||||
public function __construct($measurement, $value, array $tags = array(), array $additionalFields = array(), $timestamp = null)
|
public function __construct(
|
||||||
{
|
$measurement,
|
||||||
|
$value,
|
||||||
|
array $tags = array(),
|
||||||
|
array $additionalFields = array(),
|
||||||
|
$timestamp = null
|
||||||
|
) {
|
||||||
if (empty($measurement)) {
|
if (empty($measurement)) {
|
||||||
throw new DatabaseException('Invalid measurement name provided');
|
throw new DatabaseException('Invalid measurement name provided');
|
||||||
}
|
}
|
||||||
@@ -81,26 +90,39 @@ class Point
|
|||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $arr
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
private function arrayToString(array $arr)
|
private function arrayToString(array $arr)
|
||||||
{
|
{
|
||||||
$strParts = array();
|
$strParts = array();
|
||||||
|
|
||||||
foreach ($arr as $key => $value) {
|
foreach ($arr as $key => $value) {
|
||||||
$strParts[] = "{$key}={$value}";
|
$strParts[] = sprintf('%s=%s', $key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return implode(",", $strParts);
|
return implode(',', $strParts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $timestamp
|
* @param int $timestamp
|
||||||
*
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function isValidTimeStamp($timestamp)
|
private function isValidTimeStamp($timestamp)
|
||||||
{
|
{
|
||||||
return ((int) $timestamp === $timestamp)
|
if ((int) $timestamp === $timestamp) {
|
||||||
&& ($timestamp <= PHP_INT_MAX)
|
return true;
|
||||||
&& ($timestamp >= ~PHP_INT_MAX);
|
}
|
||||||
|
|
||||||
|
if ($timestamp <= PHP_INT_MAX) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($timestamp >= ~PHP_INT_MAX) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Stephen "TheCodeAssassin" Hoogendijk <s.hoogendijk@tech.leaseweb.com>
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace InfluxDB\Query;
|
namespace InfluxDB\Query;
|
||||||
|
|
||||||
@@ -24,16 +21,43 @@ use InfluxDB\ResultSet;
|
|||||||
* @todo add merge
|
* @todo add merge
|
||||||
*
|
*
|
||||||
* @package InfluxDB\Query
|
* @package InfluxDB\Query
|
||||||
|
* @author Stephen "TheCodeAssassin" Hoogendijk <s.hoogendijk@tech.leaseweb.com>
|
||||||
*/
|
*/
|
||||||
class Builder
|
class Builder
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var Database
|
||||||
|
*/
|
||||||
|
protected $db;
|
||||||
|
|
||||||
protected $db = null;
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
protected $selection = '*';
|
protected $selection = '*';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
protected $where = array();
|
protected $where = array();
|
||||||
protected $startTime = null;
|
|
||||||
protected $endTime = null;
|
/**
|
||||||
protected $metric = null;
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $startTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $endTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $metric;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
protected $limitClause = '';
|
protected $limitClause = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -46,7 +70,6 @@ class Builder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $metric The metric to select (required)
|
* @param string $metric The metric to select (required)
|
||||||
*
|
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function from($metric)
|
public function from($metric)
|
||||||
@@ -64,7 +87,6 @@ class Builder
|
|||||||
* $series->select('sum(value)',
|
* $series->select('sum(value)',
|
||||||
*
|
*
|
||||||
* @param string $customSelect
|
* @param string $customSelect
|
||||||
*
|
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function select($customSelect)
|
public function select($customSelect)
|
||||||
@@ -83,7 +105,6 @@ class Builder
|
|||||||
*/
|
*/
|
||||||
public function where(array $conditions)
|
public function where(array $conditions)
|
||||||
{
|
{
|
||||||
|
|
||||||
foreach ($conditions as $condition) {
|
foreach ($conditions as $condition) {
|
||||||
$this->where[] = $condition;
|
$this->where[] = $condition;
|
||||||
}
|
}
|
||||||
@@ -93,7 +114,6 @@ class Builder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
*
|
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function count($field = 'type')
|
public function count($field = 'type')
|
||||||
@@ -105,7 +125,6 @@ class Builder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
*
|
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function median($field = 'type')
|
public function median($field = 'type')
|
||||||
@@ -117,7 +136,6 @@ class Builder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
*
|
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function mean($field = 'type')
|
public function mean($field = 'type')
|
||||||
@@ -129,7 +147,6 @@ class Builder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
*
|
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function sum($field = 'type')
|
public function sum($field = 'type')
|
||||||
@@ -141,7 +158,6 @@ class Builder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
*
|
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function first($field = 'type')
|
public function first($field = 'type')
|
||||||
@@ -153,7 +169,6 @@ class Builder
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
*
|
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function last($field = 'type')
|
public function last($field = 'type')
|
||||||
@@ -166,9 +181,8 @@ class Builder
|
|||||||
/**
|
/**
|
||||||
* Set's the time range to select data from
|
* Set's the time range to select data from
|
||||||
*
|
*
|
||||||
* @param int $from Unix timestamp from
|
* @param int $from
|
||||||
* @param int $to Unix timestamp to
|
* @param int $to
|
||||||
*
|
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setTimeRange($from, $to)
|
public function setTimeRange($from, $to)
|
||||||
@@ -207,7 +221,6 @@ class Builder
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
@@ -239,6 +252,7 @@ class Builder
|
|||||||
|
|
||||||
for ($i = 0; $i < count($this->where); $i++) {
|
for ($i = 0; $i < count($this->where); $i++) {
|
||||||
$selection = 'WHERE';
|
$selection = 'WHERE';
|
||||||
|
|
||||||
if ($i > 0) {
|
if ($i > 0) {
|
||||||
$selection = 'AND';
|
$selection = 'AND';
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Stephen "TheCodeAssassin" Hoogendijk
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace InfluxDB\Query;
|
namespace InfluxDB\Query;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Stephen "TheCodeAssassin" Hoogendijk
|
||||||
|
*/
|
||||||
class Exception extends \InfluxDB\Exception
|
class Exception extends \InfluxDB\Exception
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
+14
-33
@@ -1,7 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
|
||||||
* @author Stephen "TheCodeAssassin" Hoogendijk
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace InfluxDB;
|
namespace InfluxDB;
|
||||||
|
|
||||||
@@ -11,41 +8,35 @@ use InfluxDB\Client\Exception as ClientException;
|
|||||||
* Class ResultSet
|
* Class ResultSet
|
||||||
*
|
*
|
||||||
* @package InfluxDB
|
* @package InfluxDB
|
||||||
|
* @author Stephen "TheCodeAssassin" Hoogendijk
|
||||||
*/
|
*/
|
||||||
class ResultSet
|
class ResultSet
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $raw = '';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array|mixed
|
* @var array|mixed
|
||||||
*/
|
*/
|
||||||
protected $parsedResults = array();
|
protected $parsedResults = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $raw
|
* @param string $raw
|
||||||
*
|
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct($raw)
|
public function __construct($raw)
|
||||||
{
|
{
|
||||||
$this->raw = $raw;
|
$this->parsedResults = json_decode((string) $raw, true);
|
||||||
|
|
||||||
$this->parsedResults = json_decode($raw, true);
|
|
||||||
|
|
||||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||||
throw new \InvalidArgumentException("Invalid JSON");
|
throw new \InvalidArgumentException('Invalid JSON');
|
||||||
}
|
}
|
||||||
|
|
||||||
// There was an error in the query thrown by influxdb
|
// There was an error in the query thrown by influxdb
|
||||||
if (isset($this->parsedResults['error'])) {
|
if (isset($this->parsedResults['error'])) {
|
||||||
throw new ClientException($this->parsedResults['error']);
|
throw new ClientException($this->parsedResults['error']);
|
||||||
|
}
|
||||||
|
|
||||||
// Check if there are errors in the first serie
|
// Check if there are errors in the first serie
|
||||||
} elseif (isset($this->parsedResults['results'][0]['error'])) {
|
if (isset($this->parsedResults['results'][0]['error'])) {
|
||||||
throw new ClientException($this->parsedResults['results'][0]['error']);
|
throw new ClientException($this->parsedResults['results'][0]['error']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -53,7 +44,6 @@ class ResultSet
|
|||||||
/**
|
/**
|
||||||
* @param $metricName
|
* @param $metricName
|
||||||
* @param array $tags
|
* @param array $tags
|
||||||
*
|
|
||||||
* @return array $points
|
* @return array $points
|
||||||
*/
|
*/
|
||||||
public function getPoints($metricName = '', array $tags = array())
|
public function getPoints($metricName = '', array $tags = array())
|
||||||
@@ -62,7 +52,6 @@ class ResultSet
|
|||||||
$series = $this->getSeries();
|
$series = $this->getSeries();
|
||||||
|
|
||||||
foreach ($series as $serie) {
|
foreach ($series as $serie) {
|
||||||
|
|
||||||
if ((empty($metricName) && empty($tags)
|
if ((empty($metricName) && empty($tags)
|
||||||
|| $serie['name'] == $metricName
|
|| $serie['name'] == $metricName
|
||||||
|| (isset($serie['tags']) && array_intersect($tags, $serie['tags'])))
|
|| (isset($serie['tags']) && array_intersect($tags, $serie['tags'])))
|
||||||
@@ -82,25 +71,21 @@ class ResultSet
|
|||||||
* each containing the keys for a series
|
* each containing the keys for a series
|
||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*
|
|
||||||
* @return array $series
|
* @return array $series
|
||||||
*/
|
*/
|
||||||
public function getSeries()
|
public function getSeries()
|
||||||
{
|
{
|
||||||
$pickSeries = function ($object) {
|
return array_shift(
|
||||||
|
array_map(
|
||||||
|
function ($object) {
|
||||||
if (isset($object['error'])) {
|
if (isset($object['error'])) {
|
||||||
throw new ClientException($object['error']);
|
throw new ClientException($object['error']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (isset($object['series']) ? $object['series'] : array());
|
return isset($object['series']) ? $object['series'] : array();
|
||||||
};
|
},
|
||||||
|
$this->parsedResults['results']
|
||||||
// we use array_shift because of compatibility with php5.3
|
)
|
||||||
// Foreach object, pick series key
|
|
||||||
$map = array_map($pickSeries, $this->parsedResults['results']);
|
|
||||||
return array_shift(
|
|
||||||
$map
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,13 +98,9 @@ class ResultSet
|
|||||||
$points = array();
|
$points = array();
|
||||||
|
|
||||||
foreach ($serie['values'] as $point) {
|
foreach ($serie['values'] as $point) {
|
||||||
$points[] = array_combine(
|
$points[] = array_combine($serie['columns'], $point);
|
||||||
$serie['columns'],
|
|
||||||
$point
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $points;
|
return $points;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user