diff --git a/src/Leaseweb/InfluxDB/ResultSet.php b/src/Leaseweb/InfluxDB/ResultSet.php index 7cde9dd164..0e92dc6b85 100644 --- a/src/Leaseweb/InfluxDB/ResultSet.php +++ b/src/Leaseweb/InfluxDB/ResultSet.php @@ -41,8 +41,33 @@ class ResultSet * * @return array $points */ - public function getPoints($metricName = '', $tags = array()) + public function getPoints($metricName = '', array $tags = array()) { - return array(); + $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); + } + } + } + + return $points; + } + + /** + * @param array $serie + * @return array + */ + private function getPointsFromSerie(array $serie) + { + return array_combine( + $serie['columns'], + array_shift($serie['values']) + ); } } \ No newline at end of file diff --git a/tests/unit/ResultSetTest.php b/tests/unit/ResultSetTest.php index 1f6016fddc..18a86de4c9 100644 --- a/tests/unit/ResultSetTest.php +++ b/tests/unit/ResultSetTest.php @@ -14,14 +14,46 @@ class ResultSetTest extends \PHPUnit_Framework_TestCase $this->resultSet = new ResultSet($resultJsonExample); } + /** + * @expectedException \InvalidArgumentException + */ + public function testThrowsExceptionIfJSONisNotValid() + { + $invalidJSON = 'foo'; + + new ResultSet($invalidJSON); + } + + /** + * We can get points for a measurement + */ public function testGetPointsFromMeasurementName() { $measurementName = 'cpu_load_short'; + $expectedNumberOfPoints = 2; + $expectedValueFromFirstPoint = 0.64; $points = $this->resultSet->getPoints($measurementName); $this->assertTrue( is_array($points) ); + + $this->assertCount($expectedNumberOfPoints, $points); + + $somePoint = array_shift($points); + + $this->assertEquals($expectedValueFromFirstPoint, $somePoint['value']); + } + + public function testGetPointsFromTags() + { + $tags = array("host" => "server01"); + + $points = $this->resultSet->getPoints('', $tags); + + $this->assertTrue( + is_array($points) + ); } } \ No newline at end of file