ResultSet - getSeries

This commit is contained in:
Daniel Martinez
2015-06-18 13:40:16 +02:00
parent 413cbf6eb2
commit 016c5abc6e
3 changed files with 70 additions and 16 deletions
@@ -0,0 +1,12 @@
<?php
namespace Leaseweb\InfluxDB;
/**
* Class InfluxDBClientError
* @package Leaseweb\InfluxDB
*/
class InfluxDBClientError extends \RuntimeException
{
}
+43 -12
View File
@@ -17,12 +17,16 @@ class ResultSet
*/
protected $raw = '';
/**
* @var array|mixed
*/
protected $parsedResults = array();
/**
* @param $raw
*
* @throws \InvalidArgumentException
* @throws InfluxDBClientError
*/
public function __construct($raw)
{
@@ -33,6 +37,11 @@ class ResultSet
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \InvalidArgumentException("Invalid JSON");
}
// There was an error in the query thrown by influxdb
if (isset($this->parsedResults['error'])) {
throw new InfluxDBClientError($this->parsedResults['error']);
}
}
/**
@@ -45,29 +54,51 @@ class ResultSet
{
$points = array();
// todo: we are considering always have a metricName
foreach ($this->parsedResults['results'] as $result) {
foreach ($result['series'] as $serie) {
if ($serie['measurement'] == $metricName) {
$points[] = $this->getPointsFromSerie($serie);
}
foreach ($this->getSeries() as $serie) {
if ($serie['measurement'] == $metricName || array_intersect($tags, $serie['tags'])) {
$points = array_merge($points, $this->getPointsFromSerie($serie));
}
}
return $points;
}
/**
* @see: https://influxdb.com/docs/v0.9/concepts/reading_and_writing_data.html
*
* results is an array of objects, one for each query,
* each containing the keys for a series
*
* @return array $series
*/
private function getSeries()
{
$pickSeries = function ($object) {
return $object['series'];
};
// Foreach object, pick series key
return array_shift(
array_map($pickSeries, $this->parsedResults['results'])
);
}
/**
* @param array $serie
* @return array
*/
private function getPointsFromSerie(array $serie)
{
return array_combine(
$serie['columns'],
array_shift($serie['values'])
);
$points = array();
foreach ($serie['values'] as $point) {
$points[] = array_combine(
$serie['columns'],
$point
);
}
return $points;
}
}
+15 -4
View File
@@ -25,7 +25,7 @@ class ResultSetTest extends \PHPUnit_Framework_TestCase
}
/**
* We can get points for a measurement
* We can get points from measurement
*/
public function testGetPointsFromMeasurementName()
{
@@ -49,11 +49,22 @@ class ResultSetTest extends \PHPUnit_Framework_TestCase
public function testGetPointsFromTags()
{
$tags = array("host" => "server01");
$expectedNumberOfPoints = 2;
$points = $this->resultSet->getPoints('', $tags);
$this->assertTrue(
is_array($points)
);
$this->assertTrue(is_array($points));
$this->assertCount($expectedNumberOfPoints, $points);
}
public function testGetPointsFromNameAndTags()
{
$tags = array("host" => "server01");
$expectedNumberOfPoints = 2;
$points = $this->resultSet->getPoints('', $tags);
$this->assertTrue(is_array($points));
$this->assertCount($expectedNumberOfPoints, $points);
}
}