diff --git a/spec/InfluxDB/ClientSpec.php b/spec/InfluxDB/ClientSpec.php index b1ff81cb6..8fc4a6827 100644 --- a/spec/InfluxDB/ClientSpec.php +++ b/spec/InfluxDB/ClientSpec.php @@ -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) + { + $filter->filter(Argument::Any())->shouldBeCalledTimes(1)->willReturn([]); + $adapter->query(Argument::Any(), Argument::Any())->willReturn([]); + + $this->setFilter($filter); + $this->setAdapter($adapter); + + $this->query("select * from tcp.test")->shouldReturn([]); + } } diff --git a/spec/InfluxDB/Filter/ColumnsPointsFilterSpec.php b/spec/InfluxDB/Filter/ColumnsPointsFilterSpec.php new file mode 100644 index 000000000..4e85f88fc --- /dev/null +++ b/spec/InfluxDB/Filter/ColumnsPointsFilterSpec.php @@ -0,0 +1,58 @@ +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([]); + } +} diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 3b4c7e95c..94e879f8f 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -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() diff --git a/src/InfluxDB/Filter/ColumnsPointsFilter.php b/src/InfluxDB/Filter/ColumnsPointsFilter.php new file mode 100644 index 000000000..b6e94dc07 --- /dev/null +++ b/src/InfluxDB/Filter/ColumnsPointsFilter.php @@ -0,0 +1,22 @@ +columns; + $response[$metric->name] = []; + + foreach ($metric->points as $point) { + $response[$metric->name][] = array_combine($columns, $point); + } + } + + return $response; + } +} diff --git a/src/InfluxDB/Filter/FilterInterface.php b/src/InfluxDB/Filter/FilterInterface.php new file mode 100644 index 000000000..01bb59fd8 --- /dev/null +++ b/src/InfluxDB/Filter/FilterInterface.php @@ -0,0 +1,7 @@ +