diff --git a/spec/InfluxDB/ClientSpec.php b/spec/InfluxDB/ClientSpec.php index a0ee5ae02..b1ff81cb6 100644 --- a/spec/InfluxDB/ClientSpec.php +++ b/spec/InfluxDB/ClientSpec.php @@ -28,7 +28,7 @@ class ClientSpec extends ObjectBehavior "points" => [ ["Guccini", "Autogrill"] ] - ]])->shouldBeCalledTimes(1); + ]], false)->shouldBeCalledTimes(1); $this->mark("video.search", [ "author" => "Guccini", @@ -36,6 +36,23 @@ class ClientSpec extends ObjectBehavior ]); } + function it_should_send_data_with_time_precision(AdapterInterface $adapter) + { + $adapter->send([[ + "name" => "video.search", + "columns" => ["time", "author", "title"], + "points" => [ + ["1410591552", "Guccini", "Autogrill"] + ] + ]], "s")->shouldBeCalledTimes(1); + + $this->mark("video.search", [ + "time" => "1410591552", + "author" => "Guccini", + "title" => "Autogrill" + ], "s"); + } + function it_should_query_on_querable_adapter(GuzzleAdapter $adapter) { $this->setAdapter($adapter); diff --git a/src/InfluxDB/Adapter/AdapterInterface.php b/src/InfluxDB/Adapter/AdapterInterface.php index d0b9e539a..79ed18670 100644 --- a/src/InfluxDB/Adapter/AdapterInterface.php +++ b/src/InfluxDB/Adapter/AdapterInterface.php @@ -3,5 +3,5 @@ namespace InfluxDB\Adapter; interface AdapterInterface { - public function send($message); + public function send($message, $timePrecision = false); } diff --git a/src/InfluxDB/Adapter/GuzzleAdapter.php b/src/InfluxDB/Adapter/GuzzleAdapter.php index 45ad43ea3..96a109a4a 100644 --- a/src/InfluxDB/Adapter/GuzzleAdapter.php +++ b/src/InfluxDB/Adapter/GuzzleAdapter.php @@ -17,13 +17,17 @@ class GuzzleAdapter implements AdapterInterface, QueryableInterface $this->options = $options; } - public function send($message) + public function send($message, $timePrecision = false) { $httpMessage = [ "auth" => [$this->options->getUsername(), $this->options->getPassword()], "body" => json_encode($message) ]; + if ($timePrecision) { + $httpMessage["query"]["time_precision"] = $timePrecision; + } + $endpoint = $this->options->getHttpSeriesEndpoint(); return $this->httpClient->post($endpoint, $httpMessage); } diff --git a/src/InfluxDB/Adapter/UdpAdapter.php b/src/InfluxDB/Adapter/UdpAdapter.php index 88827173f..4cc08c775 100644 --- a/src/InfluxDB/Adapter/UdpAdapter.php +++ b/src/InfluxDB/Adapter/UdpAdapter.php @@ -12,7 +12,10 @@ class UdpAdapter implements AdapterInterface $this->options = $options; } - public function send($message) + /** + * @todo Handle timePrecision parameter + */ + public function send($message, $timePrecision = false) { $message = json_encode($message); $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 6971d71ba..3b4c7e95c 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -20,14 +20,17 @@ class Client return $this->adapter; } - public function mark($name, array $values) + public function mark($name, array $values, $timePrecision = false) { $data =[]; + + $timePrecision = $this->clearTimePrecision($timePrecision); + $data['name'] = $name; $data['columns'] = array_keys($values); $data['points'][] = array_values($values); - return $this->getAdapter()->send([$data]); + return $this->getAdapter()->send([$data], $timePrecision); } public function query($query, $timePrecision = false) diff --git a/tests/InfluxDB/ClientTest.php b/tests/InfluxDB/ClientTest.php index 5d0dc8af1..45ea0397e 100644 --- a/tests/InfluxDB/ClientTest.php +++ b/tests/InfluxDB/ClientTest.php @@ -100,6 +100,21 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals("tcp.test", $body[0]["name"]); } + /** + * @group tcp + */ + public function testGuzzleHttpWriteApiWithTimePrecision() + { + $this->object->mark("tcp.test", ["time" => 1410591552, "mark" => "element"], "s"); + + $body = $this->object->query("select mark from tcp.test", "ms"); + + $this->assertCount(1, $body[0]["points"]); + $this->assertEquals("tcp.test", $body[0]["name"]); + + $this->assertEquals("1410591552000", $body[0]["points"][0][0]); + } + /** * @group udp */