From 016c5abc6ef986e9ad671e3d42a3ea3903644368 Mon Sep 17 00:00:00 2001 From: Daniel Martinez Date: Thu, 18 Jun 2015 13:40:16 +0200 Subject: [PATCH] ResultSet - getSeries --- src/Leaseweb/InfluxDB/InfluxDBClientError.php | 12 ++++ src/Leaseweb/InfluxDB/ResultSet.php | 55 +++++++++++++++---- tests/unit/ResultSetTest.php | 19 +++++-- 3 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 src/Leaseweb/InfluxDB/InfluxDBClientError.php diff --git a/src/Leaseweb/InfluxDB/InfluxDBClientError.php b/src/Leaseweb/InfluxDB/InfluxDBClientError.php new file mode 100644 index 000000000..75b8dbc22 --- /dev/null +++ b/src/Leaseweb/InfluxDB/InfluxDBClientError.php @@ -0,0 +1,12 @@ +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; } + } \ No newline at end of file diff --git a/tests/unit/ResultSetTest.php b/tests/unit/ResultSetTest.php index 18a86de4c..82c50b526 100644 --- a/tests/unit/ResultSetTest.php +++ b/tests/unit/ResultSetTest.php @@ -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); } } \ No newline at end of file