From cc56f6449d006544a7ee0b76ad666e60b75a4955 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sat, 13 Sep 2014 09:17:34 +0200 Subject: [PATCH 1/3] Fixes #1 - Send data with time_precision --- spec/InfluxDB/ClientSpec.php | 19 ++++++++++++++++++- src/InfluxDB/Adapter/AdapterInterface.php | 2 +- src/InfluxDB/Adapter/GuzzleAdapter.php | 6 +++++- src/InfluxDB/Adapter/UdpAdapter.php | 5 ++++- src/InfluxDB/Client.php | 7 +++++-- tests/InfluxDB/ClientTest.php | 15 +++++++++++++++ 6 files changed, 48 insertions(+), 6 deletions(-) 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 */ From e99204088083b369fc5e680b3653b14fef23e7da Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sat, 13 Sep 2014 09:22:14 +0200 Subject: [PATCH 2/3] Updated docs (time precision) --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 5fbc30cb3..17aace4d6 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,18 @@ $client = new Client(); $client->setAdapter($adapter); ``` +### Time Precision write/read queries + +You can set the `time_precision` for query query + +```php +$client->mark("app.search", $points, "s"); //points contains "time" that is in seconds +``` + +```php +$client->query("select * from app.search", "s"); // retrieve points using seconds for time column +``` + ### Query InfluxDB You can query the time series database using the query method. From 05163905ebe7558642158997b74941658bcd2153 Mon Sep 17 00:00:00 2001 From: Walter Dal Mut Date: Sat, 13 Sep 2014 09:23:29 +0200 Subject: [PATCH 3/3] Bumped version 0.1.1 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 6e8bf73aa..17e51c385 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1.0 +0.1.1