diff --git a/src/InfluxDB/Adapter/AdapterAbstract.php b/src/InfluxDB/Adapter/AdapterAbstract.php new file mode 100644 index 000000000..cc39dab6f --- /dev/null +++ b/src/InfluxDB/Adapter/AdapterAbstract.php @@ -0,0 +1,34 @@ +options = $options; + } + + /** + * @return Options + */ + public function getOptions() + { + return $this->options; + } + + protected function getMessageDefaults() + { + return [ + "database" => $this->getOptions()->getDatabase(), + ]; + } + + abstract public function send(array $message); +} diff --git a/src/InfluxDB/Adapter/AdapterInterface.php b/src/InfluxDB/Adapter/AdapterInterface.php index b4ac32821..23bdf7a68 100644 --- a/src/InfluxDB/Adapter/AdapterInterface.php +++ b/src/InfluxDB/Adapter/AdapterInterface.php @@ -11,5 +11,5 @@ interface AdapterInterface * @param mixed $message * @param string|boolean $timePrecision */ - public function send($message); + public function send(array $message); } diff --git a/src/InfluxDB/Adapter/GuzzleAdapter.php b/src/InfluxDB/Adapter/GuzzleAdapter.php index fdd0d1680..9a4d1ab78 100644 --- a/src/InfluxDB/Adapter/GuzzleAdapter.php +++ b/src/InfluxDB/Adapter/GuzzleAdapter.php @@ -10,77 +10,46 @@ use InfluxDB\Options; * * @deprecated */ -class GuzzleAdapter implements AdapterInterface, QueryableInterface +class GuzzleAdapter extends AdapterAbstract implements QueryableInterface { - /** - * @var GuzzleHttp\Client - */ private $httpClient; - /** - * @var \InfluxDB\Options - */ - private $options; - - /** - * @param Client $httpClient - * @param Options $options - */ public function __construct(Client $httpClient, Options $options) { + parent::__construct($options); + $this->httpClient = $httpClient; - $this->options = $options; } - /** - * @return Options - */ - public function getOptions() + public function send(array $message) { - return $this->options; - } + $message = array_replace_recursive($this->getMessageDefaults(), $message); - /** - * {@inheritDoc} - */ - public function send($message, $timePrecision = false) - { $httpMessage = [ - "auth" => [$this->options->getUsername(), $this->options->getPassword()], + "auth" => [$this->getOptions()->getUsername(), $this->getOptions()->getPassword()], "body" => json_encode($message) ]; - if ($timePrecision) { - $httpMessage["query"]["time_precision"] = $timePrecision; - } - - $endpoint = $this->options->getHttpSeriesEndpoint(); + $endpoint = $this->getOptions()->getHttpSeriesEndpoint(); return $this->httpClient->post($endpoint, $httpMessage); } - /** - * {@inheritDoc} - */ - public function query($query, $timePrecision = false) + public function query($query) { $options = [ - "auth" => [$this->options->getUsername(), $this->options->getPassword()], + "auth" => [$this->getOptions()->getUsername(), $this->getOptions()->getPassword()], 'query' => [ "q" => $query, "db" => $this->getOptions()->getDatabase(), ] ]; - if ($timePrecision) { - $options["query"]["time_precision"] = $timePrecision; - } - return $this->get($options); } private function get(array $httpMessage) { - $endpoint = $this->options->getHttpQueryEndpoint(); + $endpoint = $this->getOptions()->getHttpQueryEndpoint(); return $this->httpClient->get($endpoint, $httpMessage)->json(); } } diff --git a/src/InfluxDB/Adapter/UdpAdapter.php b/src/InfluxDB/Adapter/UdpAdapter.php index 090098dab..39a9a7570 100644 --- a/src/InfluxDB/Adapter/UdpAdapter.php +++ b/src/InfluxDB/Adapter/UdpAdapter.php @@ -3,38 +3,15 @@ namespace InfluxDB\Adapter; use InfluxDB\Options; -/** - * Clent adapter to call InfluxDb by UDP protocol - * @link http://influxdb.com/docs/v0.6/api/reading_and_writing_data.html#writing-data-through-json-+-udp - */ -class UdpAdapter implements AdapterInterface +final class UdpAdapter extends AdapterAbstract { - private $options; - - /** - * @param Options $options - */ - public function __construct(Options $options) + public function send(array $message) { - $this->options = $options; - } + $message = array_replace_recursive($this->getMessageDefaults(), $message); - /** - * @return Options - */ - public function getOptions() - { - return $this->options; - } - - /** - * {@inheritDoc} - */ - public function send($message) - { $message = json_encode($message); $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); - socket_sendto($socket, $message, strlen($message), 0, $this->options->getHost(), $this->options->getPort()); + socket_sendto($socket, $message, strlen($message), 0, $this->getOptions()->getHost(), $this->getOptions()->getPort()); socket_close($socket); } } diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index cc270d60e..bfaf09fa0 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -29,7 +29,6 @@ class Client if (!is_array($name)) { $data =[]; - $data["database"] = $this->getAdapter()->getOptions()->getDatabase(); $data['points'][0]['name'] = $name; $data['points'][0]['fields'] = $values; } diff --git a/tests/InfluxDB/ClientTest.php b/tests/InfluxDB/ClientTest.php index f86d04055..27a42773d 100644 --- a/tests/InfluxDB/ClientTest.php +++ b/tests/InfluxDB/ClientTest.php @@ -53,7 +53,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase */ public function testGuzzleHttpApiWorksCorrectly() { - $t = $this->object->mark("tcp.test", ["mark" => "element"]); + $this->object->mark("tcp.test", ["mark" => "element"]); sleep(1); @@ -103,7 +103,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase public function testWriteDirectMessages() { $this->object->mark([ - "database" => "tcp.test", "tags" => [ "dc" => "eu-west-1", ], @@ -160,6 +159,47 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals("element", $body["results"][0]["series"][0]["values"][0][1]); } + /** + * @group udp + */ + public function testOverrideDatabaseNameViaMessage() + { + $rawOptions = $this->rawOptions; + $options = new Options(); + $options->setHost($rawOptions["udp"]["host"]); + $options->setUsername($rawOptions["udp"]["username"]); + $options->setPassword($rawOptions["udp"]["password"]); + $options->setPort($rawOptions["udp"]["port"]); + $options->setDatabase("a-wrong-database"); + + $adapter = new UdpAdapter($options); + $object = new Client(); + $object->setAdapter($adapter); + + $object->mark([ + "database" => "{$rawOptions["udp"]["database"]}", + "points" => [ + [ + "name" => "vm-serie", + "fields" => [ + "cpu" => 18.12, + "free" => 712423, + ], + ], + ] + ]); + + sleep(1); + + $this->options->setDatabase($rawOptions["udp"]["database"]); + $body = $this->object->query("select * from \"vm-serie\""); + + $this->assertCount(1, $body["results"][0]["series"][0]["values"]); + $this->assertEquals("cpu", $body["results"][0]["series"][0]["columns"][1]); + $this->assertEquals(18.12, $body["results"][0]["series"][0]["values"][0][1]); + $this->assertEquals(712423, $body["results"][0]["series"][0]["values"][0][2]); + } + /** * @group udp */ @@ -178,7 +218,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase $object->setAdapter($adapter); $object->mark([ - "database" => "udp.test", "points" => [ [ "name" => "vm-serie", @@ -220,7 +259,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase $object->setAdapter($adapter); $object->mark([ - "database" => "udp.test", "tags" => [ "region" => "eu", ],