Merge branch 'release/0.2.0'

This commit is contained in:
Walter Dal Mut
2014-09-14 18:08:05 +02:00
9 changed files with 211 additions and 53 deletions
+50 -49
View File
@@ -5,6 +5,25 @@
Send metrics to InfluxDB and query for any data.
## Install it
Just use composer
```shell
php composer.phar require corley/influxdb-sdk:dev-master
```
Or place it in your require section
```json
{
"require": {
// ...
"corley/influxdb-sdk": "dev-master"
}
}
```
Add new points:
```php
@@ -58,7 +77,7 @@ $client->setAdapter($adapter);
Effectively the client creation is not so simple, for that
reason you can you the factory method provided with the library.
```
```php
$options = [
"adapter" => [
"name" => "InfluxDB\\Adapter\\GuzzleAdapter",
@@ -69,6 +88,11 @@ $options = [
"options" => [
"host" => "my.influx.domain.tld",
],
"filters" => [
"query" => [
"name" => "InfluxDB\\Filter\\ColumnsPointsFilter"
],
],
];
$client = \InfluxDB\ClientFactory::create($options);
@@ -135,6 +159,31 @@ array(1) {
}
```
By default data is returned as is. You can add filters in order to
change a response as you prefer, by default this library carries a
common filter that simplifies the response.
```
$client->setFilter(new ColumnsPointsFilter());
$data = $client->query("select * from hd_used");
```
With the "ColumnsPointsFilter" you get a list of dictionaries,
something like:
```
[
"serie_name" => [
[
"time" => 410545635590,
"sequence_number" => 390001,
"mark" => "element",
],
]
]
```
## Database operations
You can create, list or destroy databases using dedicated methods
@@ -147,51 +196,3 @@ $client->deleteDatabase("my.name"); // delete an existing database with name "my
Actually only queryable adapters can handle databases (implements the `QueryableInterface`)
## Install it
Just use composer
```shell
php composer.phar require corley/influxdb-sdk:dev-master
```
Or place it in your require section
```json
{
"require": {
// ...
"corley/influxdb-sdk": "dev-master"
}
}
```
## Prepare lib dependencies
Use your DiC or Service Locator in order to provide a configured client
```php
<?php
use InfluxDB\Client;
use InfluxDB\Options;
use InfluxDB\Adapter\GuzzleAdapter as InfluxHttpAdapter;
use GuzzleHttp\Client as GuzzleHttpClient;
$options = new Options();
$options->setHost("analytics.mine.domain.tld");
$options->setPort(8086);
$options->setUsername("root");
$options->setPassword("root");
$options->setDatabase("mine");
$guzzleHttp = new GuzzleHttpClient();
$adapter = new InfluxHttpAdapter($guzzleHttp, $options);
$influx = new Client();
$influx->setAdapter($adapter);
$influx->mark("tcp.test", ["mark" => "element"]);
```
+1 -1
View File
@@ -1 +1 @@
0.1.2
0.2.0
+13 -1
View File
@@ -6,7 +6,8 @@ use Prophecy\Argument;
use InfluxDB\Adapter\GuzzleAdapter;
use InfluxDB\Adapter\UdpAdapter;
use InfluxDB\Adapter\AdapterInterface;
use InfluxDB\Adapter\ConnectableInterface;
use InfluxDB\Filter\FilterInterface;
use InfluxDb\Adapter\QueryableInterface;
class ClientSpec extends ObjectBehavior
{
@@ -83,4 +84,15 @@ class ClientSpec extends ObjectBehavior
$this->shouldThrow("\\BadMethodCallException")->duringQuery("select * from tcp.test");
}
function it_should_filter_returned_data(FilterInterface $filter, QueryableInterface $adapter)
{
$adapter->query(Argument::Any(), Argument::Any())->willReturn(null);
$filter->filter(Argument::Any())->shouldBeCalledTimes(1)->willReturn([]);
$this->setFilter($filter);
$this->setAdapter($adapter);
$this->query("select * from tcp.test")->shouldReturn([]);
}
}
@@ -0,0 +1,58 @@
<?php
namespace spec\InfluxDB\Filter;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class ColumnsPointsFilterSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('InfluxDB\Filter\ColumnsPointsFilter');
}
function it_is_a_valid_filter()
{
$this->shouldImplement("InfluxDb\\Filter\\FilterInterface");
}
function it_should_map_columns_with_points()
{
$response = json_decode('[{"name":"hd_used","columns":["time","sequence_number","value","host","mount","time_precision"],"points":[[1410591684,11820001,23.2,"serverA","/mnt","s"]]}]');
$this->filter($response)->shouldBeEqualTo([
"hd_used" => [
[
"time" => 1410591684,
"sequence_number" => 11820001,
"value" => 23.2,
"host" => "serverA",
"mount" => "/mnt",
"time_precision" => "s",
],
],
]);
}
function it_should_map_also_a_series_list()
{
$response = json_decode('[{"name":"list_series_result","columns":["time","name"],"points":[[0,"hd_used"]]}]');
$this->filter($response)->shouldBeEqualTo([
"list_series_result" => [
[
"time" => 0,
"name" => "hd_used",
],
],
]);
}
function it_should_reply_to_an_empty_set()
{
$response = json_decode('[]');
$this->filter($response)->shouldBeEqualTo([]);
}
}
+20 -2
View File
@@ -3,11 +3,23 @@
namespace InfluxDB;
use InfluxDb\Adapter\QueryableInterface;
use InfluxDb\Adapter\ConnectableInterface;
use InfluxDB\Filter\FilterInterface;
class Client
{
private $adapter;
private $filter;
public function setFilter(Filter\FilterInterface $filter)
{
$this->filter = $filter;
return $this;
}
public function getFilter()
{
return $this->filter;
}
public function setAdapter(Adapter\AdapterInterface $adapter)
{
@@ -41,7 +53,13 @@ class Client
$timePrecision = $this->clearTimePrecision($timePrecision);
return $this->getAdapter()->query($query, $timePrecision);
$return = $this->getAdapter()->query($query, $timePrecision);
if ($this->getFilter() instanceOf FilterInterface) {
$return = $this->getFilter()->filter($return);
}
return $return;
}
public function getDatabases()
+7
View File
@@ -12,6 +12,9 @@ abstract class ClientFactory
"options" => [],
],
"options" => [],
"filters" => [
"query" => false
],
];
public static function create(array $options)
@@ -42,6 +45,10 @@ abstract class ClientFactory
$client = new Client();
$client->setAdapter($adapter);
if ($options["filters"]["query"]) {
$client->setFilter(new $options["filters"]["query"]["name"]);
}
return $client;
}
}
@@ -0,0 +1,22 @@
<?php
namespace InfluxDB\Filter;
class ColumnsPointsFilter implements FilterInterface
{
public function filter($metrics)
{
$response = [];
foreach ($metrics as $metric) {
$columns = $metric->columns;
$response[$metric->name] = [];
foreach ($metric->points as $point) {
$response[$metric->name][] = array_combine($columns, $point);
}
}
return $response;
}
}
+7
View File
@@ -0,0 +1,7 @@
<?php
namespace InfluxDB\Filter;
interface FilterInterface
{
public function filter($anything);
}
+33
View File
@@ -54,4 +54,37 @@ class ClientFactoryTest extends \PHPUnit_Framework_TestCase
$this->assertEquals("user", $client->getAdapter()->getOptions()->getUsername());
$this->assertEquals("pass", $client->getAdapter()->getOptions()->getPassword());
}
/**
* @group factory
* @group filters
*/
public function testCreateTcpClientWithFilter()
{
$options = [
"adapter" => [
"name" => "InfluxDB\\Adapter\\GuzzleAdapter",
],
"options" => [
"host" => "127.0.0.1",
"username" => "user",
"password" => "pass",
],
"filters" => [
"query" => [
"name" => "InfluxDB\\Filter\\ColumnsPointsFilter",
],
],
];
$client = ClientFactory::create($options);
$this->assertInstanceOf("InfluxDB\\Client", $client);
$this->assertInstanceOf("InfluxDB\\Adapter\\GuzzleAdapter", $client->getAdapter());
$this->assertEquals("127.0.0.1", $client->getAdapter()->getOptions()->getHost());
$this->assertEquals("user", $client->getAdapter()->getOptions()->getUsername());
$this->assertEquals("pass", $client->getAdapter()->getOptions()->getPassword());
$this->assertInstanceOf("InfluxDB\\Filter\\ColumnsPointsFilter", $client->getFilter());
}
}