mirror of
https://github.com/stylersnico/librenms.git
synced 2026-07-27 16:08:55 +02:00
Added base filter functionality
This commit is contained in:
@@ -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([]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace InfluxDB\Filter;
|
||||
|
||||
interface FilterInterface
|
||||
{
|
||||
public function filter($anything);
|
||||
}
|
||||
Reference in New Issue
Block a user