From 87121d212640cabe904c3bc60ec93ebee9715171 Mon Sep 17 00:00:00 2001 From: Stephen Hoogendijk Date: Thu, 18 Jun 2015 13:52:50 +0200 Subject: [PATCH] Intermediate commit; added querybuilder; retention policy object --- README.md | 7 +- composer.json | 4 +- composer.lock | 4 +- src/Leaseweb/InfluxDB/Client.php | 150 +++++++++++- src/Leaseweb/InfluxDB/Database.php | 96 ++++++++ .../InfluxDB/Database/RetentionPolicy.php | 44 ++++ src/Leaseweb/InfluxDB/Exception.php | 12 + src/Leaseweb/InfluxDB/Query/Builder.php | 228 ++++++++++++++++++ test.php | 10 - 9 files changed, 531 insertions(+), 24 deletions(-) create mode 100644 src/Leaseweb/InfluxDB/Database.php create mode 100644 src/Leaseweb/InfluxDB/Database/RetentionPolicy.php create mode 100644 src/Leaseweb/InfluxDB/Exception.php create mode 100644 src/Leaseweb/InfluxDB/Query/Builder.php delete mode 100644 test.php diff --git a/README.md b/README.md index 69a09f65f0..600553ba60 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/composer.json b/composer.json index eebde4a546..5308120ac7 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/composer.lock b/composer.lock index 7710aa2426..0613c1ffef 100644 --- a/composer.lock +++ b/composer.lock @@ -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": [] } diff --git a/src/Leaseweb/InfluxDB/Client.php b/src/Leaseweb/InfluxDB/Client.php index 180bfffa9c..7854e10aaf 100644 --- a/src/Leaseweb/InfluxDB/Client.php +++ b/src/Leaseweb/InfluxDB/Client.php @@ -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; } } \ No newline at end of file diff --git a/src/Leaseweb/InfluxDB/Database.php b/src/Leaseweb/InfluxDB/Database.php new file mode 100644 index 0000000000..f62f3bdcc3 --- /dev/null +++ b/src/Leaseweb/InfluxDB/Database.php @@ -0,0 +1,96 @@ +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); + } + + +} \ No newline at end of file diff --git a/src/Leaseweb/InfluxDB/Database/RetentionPolicy.php b/src/Leaseweb/InfluxDB/Database/RetentionPolicy.php new file mode 100644 index 0000000000..6549f809f3 --- /dev/null +++ b/src/Leaseweb/InfluxDB/Database/RetentionPolicy.php @@ -0,0 +1,44 @@ +name = $name; + $this->duration = $duration; + $this->replication = $replication; + $this->default = $default; + } +} \ No newline at end of file diff --git a/src/Leaseweb/InfluxDB/Exception.php b/src/Leaseweb/InfluxDB/Exception.php new file mode 100644 index 0000000000..c8f674344c --- /dev/null +++ b/src/Leaseweb/InfluxDB/Exception.php @@ -0,0 +1,12 @@ + + */ + +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; + } +} \ No newline at end of file diff --git a/test.php b/test.php deleted file mode 100644 index e8efd10e59..0000000000 --- a/test.php +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/php - -