Intermediate commit; added querybuilder; retention policy object

This commit is contained in:
Stephen Hoogendijk
2015-06-18 13:52:50 +02:00
parent 016c5abc6e
commit 87121d2126
9 changed files with 531 additions and 24 deletions
+4 -3
View File
@@ -1,11 +1,12 @@
# php-influxdb - InfluxDB client library for PHP
# influxdb-php
## InfluxDB client library for PHP
##Overview
###Overview
This library was created to have php port of the python influxdb client.
This way there will be a common abstraction library between different programming languages.
##Usage
###Usage
Initialize a new client object:
+2 -2
View File
@@ -1,5 +1,5 @@
{
"name": "leaseweb/php-influxdb",
"name": "leaseweb/influxdb-php",
"description": "InfluxDB client library for PHP",
"minimum-stability": "dev",
"authors": [
@@ -9,7 +9,7 @@
}
],
"require": {
"php": ">=5.4",
"php": ">=5.3",
"guzzlehttp/guzzle": "~6.0"
},
"scripts": {
Generated
+2 -2
View File
@@ -4,7 +4,7 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "6ef9eaeaea0251d4dc34987cf21167b4",
"hash": "d4cd96b7a6d552bb16ed5847dd32d4e3",
"packages": [
{
"name": "guzzlehttp/guzzle",
@@ -234,7 +234,7 @@
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": ">=5.4"
"php": ">=5.3"
},
"platform-dev": []
}
+143 -7
View File
@@ -5,9 +5,68 @@
namespace Leaseweb\InfluxDB;
/**
* Class Client
*
* @package Leaseweb\InfluxDB
*/
class Client
{
/**
* @var string
*/
protected $host = '';
/**
* @var int
*/
protected $port = 8086;
/**
* @var string
*/
protected $username = '';
/**
* @var string
*/
protected $password = '';
/**
* @var string
*/
protected $database = '';
/**
* @var int
*/
protected $timeout = 0;
/**
* @var bool
*/
protected $scheme = 'http';
/**
* @var bool
*/
protected $verifySSL = false;
/**
* @var bool
*/
protected $useUdp = false;
/**
* @var int
*/
protected $udpPort = 4444;
/**
* @var
*/
protected $baseURI;
/**
* @param string $host
@@ -18,20 +77,97 @@ class Client
* @param bool $ssl
* @param bool $verifySSL
* @param int $timeout
* @param bool $useUdp
* @param int $udpPort
*
* @todo add UDP support
* @todo add SSL support
*/
public function __construct($host,
public function __construct(
$host,
$port = 8086,
$username = '',
$password = '',
$database = '',
$ssl = false,
$verifySSL = false,
$timeout = 0,
$useUdp = false,
$udpPort = 4444)
$timeout = 0
// $useUdp = false,
// $udpPort = 4444
)
{
$this->host = $host;
$this->port = (int) $port;
$this->username = $username;
$this->password = $password;
$this->database = $database;
$this->timeout = $timeout;
$this->verifySSL = (bool) $verifySSL;
if ($ssl) {
$this->scheme = 'https';
}
// 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;
}
/**
* Use the given database
*
* @param string $name
*
* @return Database
*/
public function db($name)
{
if (empty($name)) {
throw new \InvalidArgumentException(sprintf('No database provided'));
}
return new Database($name, $this);
}
/**
* Build the client from a dsn
*
* Example: tcp+influxdb://username:pass@localhost:8086/databasename', timeout=5
*
* @param string $dsn
*
* @todo finish this functionality
*/
public static function fromDSN($dsn)
{
$args = array();
}
/**
* @return mixed
*/
public function getBaseURI()
{
return $this->baseURI;
}
/**
* @param mixed $baseURI
*/
public function setBaseURI($baseURI)
{
$this->baseURI = $baseURI;
}
}
+96
View File
@@ -0,0 +1,96 @@
<?php
/**
* @author Stephen "TheCodeAssassin" Hoogendijk
*/
namespace Leaseweb\InfluxDB;
use GuzzleHttp\Client as httpClient;
use Leaseweb\InfluxDB\Database\RetentionPolicy;
use Leaseweb\InfluxDB\Query\Builder as QueryBuilder;
/**
* Class Database
*
* @todo admin functionality
*
* @package Leaseweb\InfluxDB
*/
class Database
{
/**
* The name of the Database
*
* @var string
*/
protected $name = '';
/**
* @var Client
*/
protected $client;
/**
* @var httpClient
*/
protected $httpClient;
/**
* Construct a database object
*
* @param string $name
* @param Client $client
*/
public function __construct($name, Client $client)
{
$this->client = $client;
$this->httpClient = new Client(array('base_uri' => $this->client->getBaseURI()));
}
/**
* Query influxDB
*
* @param string $query
* @param array $params
*/
public function query($query, $params = array())
{
$params = array_merge(array('q' => $query, $params));
$result = $this->httpClient->get('query', $params);
die(var_dump($result));
}
/**
* Create this database
*
* @param RetentionPolicy $retentionPolicy
*/
public function create(RetentionPolicy $retentionPolicy)
{
return $this->query(sprintf('CREATE DATABASE %s', $this->name));
}
/**
* Drop this database
*/
public function drop()
{
}
/**
* Retrieve the query builder
*
* @return QueryBuilder
*/
public function getQueryBuilder()
{
return new QueryBuilder($this);
}
}
@@ -0,0 +1,44 @@
<?php
/**
* @author Stephen "TheCodeAssassin" Hoogendijk
*/
namespace Leaseweb\InfluxDB\Database;
class RetentionPolicy
{
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $duration;
/**
* @var int
*/
private $replication;
/**
* @var bool
*/
private $default;
/**
* @param string $name
* @param string $duration
* @param int $replication
* @param bool $default
*/
public function __construct($name, $duration = '1d', $replication = 1, $default = false)
{
$this->name = $name;
$this->duration = $duration;
$this->replication = $replication;
$this->default = $default;
}
}
+12
View File
@@ -0,0 +1,12 @@
<?php
/**
* @author Stephen "TheCodeAssassin" Hoogendijk
*/
namespace Leaseweb\InfluxDB;
class Exception extends \Exception
{
}
+228
View File
@@ -0,0 +1,228 @@
<?php
/**
* @author Stephen "TheCodeAssassin" Hoogendijk <[email protected]>
*/
namespace Leaseweb\InfluxDB\Query;
use Leaseweb\InfluxDB\Database;
/**
* Class QueryBuilder
*
* Abstraction class for getting time series out of InfluxDB
*
* Sample usage:
*
* $series = new QueryBuilder($db);
* $series->percentile(95)->setTimeRange($timeFrom, $timeTo)->getResult();
*
* $series->select('*')->from('*')->getResult();
*
* @package Leaseweb\InfluxDB
*/
class Builder
{
protected $db = null;
protected $selection = '*';
protected $where = array();
protected $startTime = null;
protected $endTime = null;
protected $metric = null;
/**
* @param Database $db
*/
public function __construct(Database $db)
{
$this->db = $db;
}
/**
* @param string $metric The metric to select (required)
*
* @return $this
*/
public function from($metric)
{
$this->metric = $metric;
return $this;
}
/**
* Custom select method
*
* example:
*
* $series->select('sum(value)',
*
* @param string $customSelect
*
* @return $this
*/
public function select($customSelect)
{
$this->selection = $customSelect;
return $this;
}
/**
* @param array $conditions
*
* Example: array('time > now()', 'time < now() -1d');
*
* @return $this
*/
public function where(array $conditions)
{
foreach ($conditions as $condition) {
$this->where[] = $condition;
}
return $this;
}
/**
* @return $this
*/
public function selectAll()
{
$this->selection = '*';
return $this;
}
/**
* @param string $field
*
* @return $this
*/
public function count($field = 'type')
{
$this->selection = sprintf('count(%s)', $field);
return $this;
}
/**
* @param string $field
*
* @return $this
*/
public function mean($field = 'type')
{
$this->selection = sprintf('mean(%s)', $field);
return $this;
}
/**
* @param string $field
*
* @return $this
*/
public function sum($field = 'type')
{
$this->selection = sprintf('sum(%s)', $field);
return $this;
}
/**
* @param string $field
*
* @return $this
*/
public function first($field = 'type')
{
$this->selection = sprintf('first(%s)', $field);
return $this;
}
/**
* @param string $field
*
* @return $this
*/
public function last($field = 'type')
{
$this->selection = sprintf('first(%s)', $field);
return $this;
}
/**
* Set's the time range to select data from
*
* @param int $from Unix timestamp from
* @param int $to Unix timestamp to
*
* @return $this
*/
public function setTimeRange($from, $to)
{
$fromDate = date('Y-m-d H:i:s', $from);
$toDate = date('Y-m-d H:i:s', $to);
$this->where(array("time > '$fromDate'", "time < '$toDate'"));
return $this;
}
/**
* @param int $percentile Percentage to select (for example 95 for 95th percentile billing)
*
* @return $this
*/
public function percentile($percentile = 95)
{
$this->selection = sprintf('percentile(value, %d)', (int) $percentile);
return $this;
}
/**
* Gets the result from the database (builds the query)
*
* @param bool $raw always return the ResultSeriesObjects, even when using an aggregation function
*
* @return array|null
*/
public function getResult($raw = false)
{
$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) {
$selection = 'AND';
}
$clause = $this->where[$i];
$query .= ' ' . $selection . ' ' . $clause;
}
$queryResult = $this->db->query($query);
if ($queryResult && $aggregateKey && !$raw) {
return (float) $queryResult[0]->$aggregateKey;
}
return $queryResult;
}
}
-10
View File
@@ -1,10 +0,0 @@
#!/usr/bin/php
<?php
require __DIR__ . '/vendor/autoload.php';
$host = 'localhost';
$port = 8086;
$client = new \Leaseweb\InfluxDB\Client($host, $port);