From e4f5bb94d6a71e8c4548447cf3970b9f653d9664 Mon Sep 17 00:00:00 2001 From: danibrutal Date: Fri, 19 Jun 2015 11:47:27 +0200 Subject: [PATCH] ResultSet bugfix if tags key is missing in the result --- src/Leaseweb/InfluxDB/Database.php | 15 ++++++-- src/Leaseweb/InfluxDB/ResultSet.php | 2 +- tests/unit/DatabaseTest.php | 2 +- tests/unit/ResultSetTest.php | 18 ++++++++++ tests/unit/result-no-tags.example.json | 47 ++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 tests/unit/result-no-tags.example.json diff --git a/src/Leaseweb/InfluxDB/Database.php b/src/Leaseweb/InfluxDB/Database.php index 0dedc4e582..4f92ba3f36 100644 --- a/src/Leaseweb/InfluxDB/Database.php +++ b/src/Leaseweb/InfluxDB/Database.php @@ -99,13 +99,24 @@ class Database /** * Writes points into INfluxdb - * @param Point[] $points + * + * @param array $points + * @return ResultSet */ public function writePoints(array $points) { + $payload = array(); + foreach ($points as $point) { - //$point->a(); + + if (! $point instanceof Point) { + throw new \InvalidArgumentException('Array of Point should be passed'); + } + + $payload[] = (string) $point; } + + return $this->query(implode("\n", $payload)); } /** diff --git a/src/Leaseweb/InfluxDB/ResultSet.php b/src/Leaseweb/InfluxDB/ResultSet.php index 4380c33f9e..64f8cff908 100644 --- a/src/Leaseweb/InfluxDB/ResultSet.php +++ b/src/Leaseweb/InfluxDB/ResultSet.php @@ -64,7 +64,7 @@ class ResultSet if ((empty($metricName) && empty($tags)) || $serie['name'] == $metricName - || array_intersect($tags, $serie['tags']) + || (isset($serie['tags']) && array_intersect($tags, $serie['tags'])) ) { $points = array_merge($points, $this->getPointsFromSerie($serie)); } diff --git a/tests/unit/DatabaseTest.php b/tests/unit/DatabaseTest.php index 2d357c89e1..cbb255b85b 100644 --- a/tests/unit/DatabaseTest.php +++ b/tests/unit/DatabaseTest.php @@ -52,7 +52,7 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase 'myTime' ); - $this->db->writePoints(array($point1, $point2)); + //$this->db->writePoints(array($point1, $point2)); $this->assertTrue( true diff --git a/tests/unit/ResultSetTest.php b/tests/unit/ResultSetTest.php index d68da1cd85..a85c74f9d5 100644 --- a/tests/unit/ResultSetTest.php +++ b/tests/unit/ResultSetTest.php @@ -60,6 +60,24 @@ EOD; new ResultSet($errorResult); } + /** + * We can get points from measurement + */ + public function testGetPointsFromNameWithoudTags() + { + $resultJsonExample = file_get_contents(dirname(__FILE__) . '/result-no-tags.example.json'); + $this->resultSet = new ResultSet($resultJsonExample); + + $measurementName = 'cpu_load_short'; + $expectedNumberOfPoints = 2; + + $points = $this->resultSet->getPoints($measurementName); + + $this->assertTrue(is_array($points)); + + $this->assertCount($expectedNumberOfPoints, $points); + } + /** * We can get points from measurement */ diff --git a/tests/unit/result-no-tags.example.json b/tests/unit/result-no-tags.example.json new file mode 100644 index 0000000000..feba13f256 --- /dev/null +++ b/tests/unit/result-no-tags.example.json @@ -0,0 +1,47 @@ +{ + "results": [ + { + "series": [ + { + "name": "cpu_load_short", + "columns": [ + "time", + "value" + ], + "values": [ + [ + "2015-01-29T21:51:28.968422294Z", + 0.64 + ] + ] + }, + { + "name": "cpu_load_short", + "columns": [ + "time", + "value" + ], + "values": [ + [ + "2015-01-29T21:51:28.968422294Z", + 0.65 + ] + ] + }, + { + "name": "other_serie", + "columns": [ + "time", + "value" + ], + "values": [ + [ + "2015-01-29T21:51:28.968422294Z", + 0.66 + ] + ] + } + ] + } + ] +} \ No newline at end of file