mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Added new HttpAdapter
This commit is contained in:
@@ -77,21 +77,28 @@ $client = new Client();
|
||||
$client->setAdapter($adapter);
|
||||
```
|
||||
|
||||
### Using HTTP Adapter
|
||||
### Using HTTP Adapters
|
||||
|
||||
Actually Guzzle is used as HTTP client library
|
||||
|
||||
```php
|
||||
<?php
|
||||
$guzzle = new \GuzzleHttp\Client();
|
||||
|
||||
$options = new Options();
|
||||
$adapter = new GuzzleAdapter($guzzle, $options);
|
||||
$adapter = new HttpAdapter($options);
|
||||
|
||||
$client = new Client();
|
||||
$client->setAdapter($adapter);
|
||||
```
|
||||
|
||||
#### Supported types of exceptions
|
||||
|
||||
* InfluxGeneralException
|
||||
* InfluxAuthorizationException (extends InfluxGeneralException)
|
||||
* InfluxBadResponseException (extends InfluxGeneralException)
|
||||
* InfluxNoSeriesException (extends InfluxGeneralException)
|
||||
* InfluxUnexpectedResponseException (extends InfluxGeneralException)
|
||||
|
||||
### Create your client with the factory method
|
||||
|
||||
Effectively the client creation is not so simple, for that
|
||||
@@ -100,7 +107,7 @@ reason you can you the factory method provided with the library.
|
||||
```php
|
||||
$options = [
|
||||
"adapter" => [
|
||||
"name" => "InfluxDB\\Adapter\\GuzzleAdapter",
|
||||
"name" => "InfluxDB\\Adapter\\HttpAdapter",
|
||||
"options" => [
|
||||
// guzzle options
|
||||
],
|
||||
@@ -144,7 +151,7 @@ $influx->query("select * from mine", "s"); // with time_precision
|
||||
```
|
||||
|
||||
You can query the database only if the adapter is queryable (implements
|
||||
`QueryableInterface`), actually `GuzzleAdapter`.
|
||||
`QueryableInterface`), actually `HttpAdapter`.
|
||||
|
||||
The adapter returns the json decoded body of the InfluxDB response, something
|
||||
like:
|
||||
|
||||
Executable
+194
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
namespace spec\InfluxDB\Adapter;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Message\Response;
|
||||
use GuzzleHttp\Stream\Stream;
|
||||
use InfluxDB\Adapter\HttpAdapter;
|
||||
use InfluxDB\Exception\InfluxAuthorizationException;
|
||||
use InfluxDB\Exception\InfluxBadResponseException;
|
||||
use InfluxDB\Exception\InfluxGeneralException;
|
||||
use InfluxDB\Exception\InfluxNoSeriesException;
|
||||
use InfluxDB\Exception\InfluxUnexpectedResponseException;
|
||||
use InfluxDB\Options;
|
||||
use PhpSpec\ObjectBehavior;
|
||||
use Prophecy\Argument;
|
||||
|
||||
class HttpAdapterSpec extends ObjectBehavior
|
||||
{
|
||||
function it_is_initializable()
|
||||
{
|
||||
$this->shouldHaveType('InfluxDB\Adapter\HttpAdapter');
|
||||
}
|
||||
|
||||
function let(Options $options, Client $client)
|
||||
{
|
||||
$options->getHttpSeriesEndpoint()->willReturn("localhost");
|
||||
$options->getHttpDatabaseEndpoint()->willReturn("localhost");
|
||||
$options->getUsername()->willReturn("one");
|
||||
$options->getPassword()->willReturn("two");
|
||||
$this->beConstructedWith($options, $client);
|
||||
}
|
||||
|
||||
function it_should_send_data_via_post(Client $client)
|
||||
{
|
||||
$responseBody = ['key'=>'value'];
|
||||
$response = new Response(200,[], Stream::factory(json_encode($responseBody)));
|
||||
$client->post("localhost", [
|
||||
'auth' => ["one", "two"],
|
||||
'exceptions' => false,
|
||||
'body' => json_encode(['pippo'])
|
||||
])->willReturn($response)
|
||||
->shouldBeCalledTimes(1);
|
||||
$this->send(["pippo"])->shouldReturn($responseBody);
|
||||
}
|
||||
|
||||
function it_should_query_data(Client $client, Options $options)
|
||||
{
|
||||
$client->get(
|
||||
"localhost",
|
||||
[
|
||||
"auth" => ["one", "two"],
|
||||
"exceptions" => false,
|
||||
"query" => [
|
||||
"q" => "select * from tcp.test",
|
||||
]
|
||||
]
|
||||
)->willReturn(new Response(200,[],null));
|
||||
$this->query("select * from tcp.test")->shouldReturn(null);
|
||||
}
|
||||
|
||||
function it_should_query_data_with_time_precision(Client $client, Options $options)
|
||||
{
|
||||
$client->get(
|
||||
"localhost",
|
||||
[
|
||||
"auth" => ["one", "two"],
|
||||
"exceptions" => false,
|
||||
"query" => [
|
||||
"time_precision" => "s",
|
||||
"q" => "select * from tcp.test",
|
||||
]
|
||||
]
|
||||
)->willReturn(new Response(200, [], null));
|
||||
$this->query("select * from tcp.test", "s")->shouldReturn(null);
|
||||
}
|
||||
|
||||
function it_should_list_all_databases(Client $client, Options $options)
|
||||
{
|
||||
$client->get(
|
||||
"localhost",
|
||||
[
|
||||
"auth" => ["one", "two"],
|
||||
"exceptions" => false,
|
||||
]
|
||||
)->shouldBeCalledTimes(1)->willReturn(new Response(200, [], null));
|
||||
|
||||
$this->getDatabases()->shouldReturn(null);
|
||||
}
|
||||
|
||||
function it_should_create_a_new_database(Client $client, Options $options)
|
||||
{
|
||||
$client->post(
|
||||
"localhost",
|
||||
[
|
||||
"auth" => ["one", "two"],
|
||||
"exceptions" => false,
|
||||
"body" => json_encode(["name" => "db_name"])
|
||||
]
|
||||
)->shouldBeCalledTimes(1)->willReturn(new Response(201, [], null));
|
||||
|
||||
$this->createDatabase("db_name")->shouldReturn(true);
|
||||
}
|
||||
|
||||
function it_should_return_true_with_success(Client $client) {
|
||||
foreach ([201,204,299] as $code) {
|
||||
$client->post(Argument::any(), Argument::any(), Argument::any())->willReturn(new Response($code, [], null));
|
||||
|
||||
$this->createDatabase("db_name")->shouldReturn(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
function it_should_throw_no_series_exception (Client $client)
|
||||
{
|
||||
$client->get(
|
||||
"localhost",
|
||||
[
|
||||
"auth" => ["one", "two"],
|
||||
"exceptions" => false,
|
||||
"query" => [
|
||||
"q" => "select * from tcp.test",
|
||||
]
|
||||
]
|
||||
)->willReturn(new Response(HttpAdapter::STATUS_CODE_BAD_REQUEST,[], Stream::factory("Couldn't find series: tcp.test")));
|
||||
$this->shouldThrow(new InfluxNoSeriesException("Couldn't find series: tcp.test", HttpAdapter::STATUS_CODE_BAD_REQUEST))
|
||||
->during("query", ["select * from tcp.test"]);
|
||||
}
|
||||
|
||||
function it_should_throw_authorization_exception (Client $client)
|
||||
{
|
||||
$codes = [HttpAdapter::STATUS_CODE_UNAUTHORIZED, HttpAdapter::STATUS_CODE_FORBIDDEN];
|
||||
foreach ($codes as $code) {
|
||||
$client->get(
|
||||
"localhost",
|
||||
[
|
||||
"auth" => ["one", "two"],
|
||||
"exceptions" => false,
|
||||
"query" => [
|
||||
"q" => "select * from tcp.test",
|
||||
]
|
||||
]
|
||||
)->willReturn(new Response($code,[], Stream::factory("Message")));
|
||||
$this->shouldThrow(new InfluxAuthorizationException("Message", $code))
|
||||
->during("query", ["select * from tcp.test"]);
|
||||
}
|
||||
}
|
||||
|
||||
function it_should_throw_general_exception (Client $client)
|
||||
{
|
||||
$client->get(
|
||||
"localhost",
|
||||
[
|
||||
"auth" => ["one", "two"],
|
||||
"exceptions" => false,
|
||||
"query" => [
|
||||
"q" => "select * from tcp.test",
|
||||
]
|
||||
]
|
||||
)->willReturn(new Response(409,[], Stream::factory("Message")));
|
||||
$this->shouldThrow(new InfluxGeneralException("Message", 409))
|
||||
->during("query", ["select * from tcp.test"]);
|
||||
}
|
||||
|
||||
function it_should_throw_general_exception_with_default_message (Client $client)
|
||||
{
|
||||
$client->get(Argument::any(), Argument::any())->willReturn(new Response(409));
|
||||
$this->shouldThrow(new InfluxGeneralException("Conflict", 409))
|
||||
->during("query", ["select * from tcp.test"]);
|
||||
}
|
||||
|
||||
function it_should_throw_bad_response_exception(Client $client)
|
||||
{
|
||||
$response = new Response(200,[], Stream::factory('bad response'));
|
||||
$client->post("localhost", [
|
||||
'auth' => ["one", "two"],
|
||||
'exceptions' => false,
|
||||
'body' => json_encode(['pippo'])
|
||||
])->willReturn($response)
|
||||
->shouldBeCalledTimes(1);
|
||||
$this->shouldThrow(new InfluxBadResponseException("Unable to parse JSON data: JSON_ERROR_SYNTAX - Syntax error, malformed JSON; Response is 'bad response'", 0))
|
||||
->during("send", [["pippo"]]);
|
||||
}
|
||||
|
||||
function it_should_throw_unexpected_response_exception (Client $client)
|
||||
{
|
||||
foreach ([0, 300, 500] as $code) {
|
||||
$client->get(Argument::any(), Argument::any())->willReturn(new Response($code, [], Stream::factory("Message")));
|
||||
$this->shouldThrow(new InfluxUnexpectedResponseException("Message", $code))
|
||||
->during("query", ["select * from tcp.test"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,12 @@ namespace InfluxDB\Adapter;
|
||||
use GuzzleHttp\Client;
|
||||
use InfluxDB\Options;
|
||||
|
||||
/**
|
||||
* Class GuzzleAdapter
|
||||
* @package InfluxDB\Adapter
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
class GuzzleAdapter implements AdapterInterface, QueryableInterface
|
||||
{
|
||||
private $httpClient;
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
namespace InfluxDB\Adapter;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\ParseException;
|
||||
use GuzzleHttp\Message\ResponseInterface;
|
||||
use InfluxDB\Exception\InfluxAuthorizationException;
|
||||
use InfluxDB\Exception\InfluxBadResponseException;
|
||||
use InfluxDB\Exception\InfluxGeneralException;
|
||||
use InfluxDB\Exception\InfluxNoSeriesException;
|
||||
use InfluxDB\Exception\InfluxUnexpectedResponseException;
|
||||
use InfluxDB\Options;
|
||||
|
||||
class HttpAdapter implements AdapterInterface, QueryableInterface
|
||||
{
|
||||
const STATUS_CODE_OK = 200;
|
||||
const STATUS_CODE_UNAUTHORIZED = 401;
|
||||
const STATUS_CODE_FORBIDDEN = 403;
|
||||
const STATUS_CODE_BAD_REQUEST = 400;
|
||||
|
||||
/**
|
||||
* @var \InfluxDB\Options
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @var Client
|
||||
*/
|
||||
private $client;
|
||||
|
||||
/**
|
||||
* @param Options $options
|
||||
*/
|
||||
public function __construct(Options $options, Client $client = null)
|
||||
{
|
||||
$this->options = $options;
|
||||
$this->client = $client ?: new Client();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Options
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $body
|
||||
* @param array $query
|
||||
* @param bool $timePrecision
|
||||
* @return array
|
||||
*/
|
||||
protected function getRequest(array $body = [], array $query = [], $timePrecision = false)
|
||||
{
|
||||
$request = [
|
||||
"auth" => [$this->options->getUsername(), $this->options->getPassword()],
|
||||
"exceptions" => false
|
||||
];
|
||||
if (count($body)) {
|
||||
$request['body'] = json_encode($body);
|
||||
}
|
||||
if (count($query)) {
|
||||
$request['query'] = $query;
|
||||
}
|
||||
if ($timePrecision) {
|
||||
$request["query"]["time_precision"] = $timePrecision;
|
||||
}
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
* @return mixed
|
||||
* @throws \InfluxDB\Exception\InfluxGeneralException
|
||||
* @throws \InfluxDB\Exception\InfluxAuthorizationException
|
||||
* @throws \InfluxDB\Exception\InfluxNoSeriesException
|
||||
*/
|
||||
protected function parseResponse(ResponseInterface $response)
|
||||
{
|
||||
$statusCode = $response->getStatusCode();
|
||||
if ($statusCode >= 400 && $statusCode < 500) {
|
||||
$message = (string)$response->getBody();
|
||||
if (!$message) {
|
||||
$message = $response->getReasonPhrase();
|
||||
}
|
||||
switch ($statusCode) {
|
||||
case self::STATUS_CODE_UNAUTHORIZED:
|
||||
case self::STATUS_CODE_FORBIDDEN:
|
||||
throw new InfluxAuthorizationException($message, $statusCode);
|
||||
case self::STATUS_CODE_BAD_REQUEST:
|
||||
if (strpos($message, "Couldn't find series:") !== false) {
|
||||
throw new InfluxNoSeriesException($message, $statusCode);
|
||||
}
|
||||
}
|
||||
throw new InfluxGeneralException($message, $statusCode);
|
||||
} else if ($statusCode == self::STATUS_CODE_OK) {
|
||||
try {
|
||||
return $response->json();
|
||||
} catch (ParseException $ex) {
|
||||
throw new InfluxBadResponseException(
|
||||
sprintf("%s; Response is '%s'", $ex->getMessage(), (string)$response->getBody()),
|
||||
$ex->getCode(), $ex
|
||||
);
|
||||
}
|
||||
} else if ($statusCode > 200 && $statusCode < 300) {
|
||||
return true;
|
||||
}
|
||||
throw new InfluxUnexpectedResponseException((string)$response->getBody(), $statusCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
* @param bool $timePrecision
|
||||
* @return \GuzzleHttp\Message\ResponseInterface
|
||||
*/
|
||||
public function send($message, $timePrecision = false)
|
||||
{
|
||||
try {
|
||||
$response = $this->client->post(
|
||||
$this->options->getHttpSeriesEndpoint(),
|
||||
$this->getRequest($message, [], $timePrecision)
|
||||
);
|
||||
} catch (\Exception $ex) {
|
||||
throw new InfluxGeneralException($ex->getMessage(), $ex->getCode(), $ex);
|
||||
}
|
||||
return $this->parseResponse($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $query
|
||||
* @param bool $timePrecision
|
||||
* @return mixed
|
||||
*/
|
||||
public function query($query, $timePrecision = false)
|
||||
{
|
||||
try {
|
||||
$response = $this->client->get(
|
||||
$this->options->getHttpSeriesEndpoint(),
|
||||
$this->getRequest([], ["q" => $query], $timePrecision)
|
||||
);
|
||||
} catch (\Exception $ex) {
|
||||
throw new InfluxGeneralException($ex->getMessage(), $ex->getCode(), $ex);
|
||||
}
|
||||
return $this->parseResponse($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDatabases()
|
||||
{
|
||||
try {
|
||||
$response = $this->client->get(
|
||||
$this->options->getHttpDatabaseEndpoint(),
|
||||
$this->getRequest()
|
||||
);
|
||||
} catch (\Exception $ex) {
|
||||
throw new InfluxGeneralException($ex->getMessage(), $ex->getCode(), $ex);
|
||||
}
|
||||
return $this->parseResponse($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function createDatabase($name)
|
||||
{
|
||||
try {
|
||||
$response = $this->client->post(
|
||||
$this->options->getHttpDatabaseEndpoint(),
|
||||
$this->getRequest(["name" => $name])
|
||||
);
|
||||
} catch (\Exception $ex) {
|
||||
throw new InfluxGeneralException($ex->getMessage(), $ex->getCode(), $ex);
|
||||
}
|
||||
return $this->parseResponse($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function deleteDatabase($name)
|
||||
{
|
||||
try {
|
||||
$response = $this->client->delete(
|
||||
$this->options->getHttpDatabaseEndpoint($name),
|
||||
$this->getRequest()
|
||||
);
|
||||
} catch (\Exception $ex) {
|
||||
throw new InfluxGeneralException($ex->getMessage(), $ex->getCode(), $ex);
|
||||
}
|
||||
return $this->parseResponse($response);
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,9 @@ abstract class ClientFactory
|
||||
case 'InfluxDB\\Adapter\\GuzzleAdapter':
|
||||
$adapter = new $adapterName(new GuzzleClient($options["adapter"]["options"]), $adapterOptions);
|
||||
break;
|
||||
case 'InfluxDB\\Adapter\\HttpAdapter':
|
||||
$adapter = new $adapterName($adapterOptions, new GuzzleClient($options["adapter"]["options"]));
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException("Missing adapter {$adapter}");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace InfluxDB\Exception;
|
||||
|
||||
class InfluxAuthorizationException extends InfluxGeneralException
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace InfluxDB\Exception;
|
||||
|
||||
class InfluxBadResponseException extends InfluxGeneralException
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace InfluxDB\Exception;
|
||||
|
||||
class InfluxGeneralException extends \RuntimeException
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace InfluxDB\Exception;
|
||||
|
||||
class InfluxNoSeriesException extends InfluxGeneralException
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace InfluxDB\Exception;
|
||||
|
||||
class InfluxUnexpectedResponseException extends InfluxGeneralException
|
||||
{
|
||||
|
||||
}
|
||||
@@ -61,12 +61,13 @@ class ClientFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* @group factory
|
||||
* @group tcp
|
||||
* @dataProvider getTcpAdapters
|
||||
*/
|
||||
public function testCreateTcpClient()
|
||||
public function testCreateTcpClient($adapter)
|
||||
{
|
||||
$options = [
|
||||
"adapter" => [
|
||||
"name" => "InfluxDB\\Adapter\\GuzzleAdapter",
|
||||
"name" => $adapter,
|
||||
],
|
||||
"options" => [
|
||||
"host" => "127.0.0.1",
|
||||
@@ -78,21 +79,30 @@ class ClientFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
$client = ClientFactory::create($options);
|
||||
$this->assertInstanceOf("InfluxDB\\Client", $client);
|
||||
|
||||
$this->assertInstanceOf("InfluxDB\\Adapter\\GuzzleAdapter", $client->getAdapter());
|
||||
$this->assertInstanceOf($adapter, $client->getAdapter());
|
||||
$this->assertEquals("127.0.0.1", $client->getAdapter()->getOptions()->getHost());
|
||||
$this->assertEquals("user", $client->getAdapter()->getOptions()->getUsername());
|
||||
$this->assertEquals("pass", $client->getAdapter()->getOptions()->getPassword());
|
||||
}
|
||||
|
||||
public function getTcpAdapters()
|
||||
{
|
||||
return [
|
||||
["InfluxDB\\Adapter\\GuzzleAdapter"],
|
||||
["InfluxDB\\Adapter\\HttpAdapter"],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @group factory
|
||||
* @group filters
|
||||
* @dataProvider getTcpAdapters
|
||||
*/
|
||||
public function testCreateTcpClientWithFilter()
|
||||
public function testCreateTcpClientWithFilter($adapter)
|
||||
{
|
||||
$options = [
|
||||
"adapter" => [
|
||||
"name" => "InfluxDB\\Adapter\\GuzzleAdapter",
|
||||
"name" => $adapter,
|
||||
],
|
||||
"options" => [
|
||||
"host" => "127.0.0.1",
|
||||
@@ -109,7 +119,7 @@ class ClientFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
$client = ClientFactory::create($options);
|
||||
$this->assertInstanceOf("InfluxDB\\Client", $client);
|
||||
|
||||
$this->assertInstanceOf("InfluxDB\\Adapter\\GuzzleAdapter", $client->getAdapter());
|
||||
$this->assertInstanceOf($adapter, $client->getAdapter());
|
||||
$this->assertEquals("127.0.0.1", $client->getAdapter()->getOptions()->getHost());
|
||||
$this->assertEquals("user", $client->getAdapter()->getOptions()->getUsername());
|
||||
$this->assertEquals("pass", $client->getAdapter()->getOptions()->getPassword());
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
namespace InfluxDB;
|
||||
|
||||
use InfluxDB\Adapter\HttpAdapter;
|
||||
use InfluxDB\Adapter\UdpAdapter;
|
||||
|
||||
class HttpAdapterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
private $rawOptions;
|
||||
private $object;
|
||||
private $options;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$options = include __DIR__ . '/../bootstrap.php';
|
||||
$this->rawOptions = $options;
|
||||
|
||||
$tcpOptions = $options["tcp"];
|
||||
|
||||
$options = new Options();
|
||||
$options->setHost($tcpOptions["host"]);
|
||||
$options->setPort($tcpOptions["port"]);
|
||||
$options->setUsername($tcpOptions["username"]);
|
||||
$options->setPassword($tcpOptions["password"]);
|
||||
$options->setDatabase($tcpOptions["database"]);
|
||||
|
||||
$this->options = $options;
|
||||
|
||||
$adapter = new HttpAdapter($options);
|
||||
|
||||
$influx = new Client();
|
||||
$influx->setAdapter($adapter);
|
||||
$this->object = $influx;
|
||||
|
||||
$databases = $this->object->getDatabases();
|
||||
foreach ($databases as $database) {
|
||||
$this->object->deleteDatabase($database["name"]);
|
||||
}
|
||||
|
||||
$this->object->createDatabase($this->rawOptions["tcp"]["database"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group tcp
|
||||
*/
|
||||
public function testApiWorksCorrectly()
|
||||
{
|
||||
$this->object->mark("tcp.test", ["mark" => "element"]);
|
||||
|
||||
$body = $this->object->query("select * from tcp.test");
|
||||
$this->assertCount(1, $body[0]["points"]);
|
||||
$this->assertEquals("element", $body[0]["points"][0][2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group tcp
|
||||
*/
|
||||
public function testQueryApiWorksCorrectly()
|
||||
{
|
||||
$this->object->mark("tcp.test", ["mark" => "element"]);
|
||||
|
||||
$body = $this->object->query("select * from tcp.test");
|
||||
|
||||
$this->assertCount(1, $body);
|
||||
$this->assertEquals("tcp.test", $body[0]["name"]);
|
||||
$this->assertEquals("element", $body[0]["points"][0][2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group tcp
|
||||
*/
|
||||
public function testQueryApiWithMultipleData()
|
||||
{
|
||||
$this->object->mark("tcp.test", ["mark" => "element"]);
|
||||
$this->object->mark("tcp.test", ["mark" => "element2"]);
|
||||
$this->object->mark("tcp.test", ["mark" => "element3"]);
|
||||
|
||||
$body = $this->object->query("select mark from tcp.test", "s");
|
||||
|
||||
$this->assertCount(3, $body[0]["points"]);
|
||||
$this->assertEquals("tcp.test", $body[0]["name"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group tcp
|
||||
*/
|
||||
public function testQueryApiWithTimePrecision()
|
||||
{
|
||||
$this->object->mark("tcp.test", ["mark" => "element"]);
|
||||
|
||||
$body = $this->object->query("select mark from tcp.test", "s");
|
||||
|
||||
$this->assertCount(1, $body[0]["points"]);
|
||||
$this->assertEquals("tcp.test", $body[0]["name"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group tcp
|
||||
*/
|
||||
public function testWriteApiWithTimePrecision()
|
||||
{
|
||||
$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]);
|
||||
}
|
||||
|
||||
public function testListActiveDatabses()
|
||||
{
|
||||
$databases = $this->object->getDatabases();
|
||||
|
||||
$this->assertCount(1, $databases);
|
||||
}
|
||||
|
||||
public function testCreateANewDatabase()
|
||||
{
|
||||
$this->object->createDatabase("walter");
|
||||
$databases = $this->object->getDatabases();
|
||||
|
||||
$this->assertCount(2, $databases);
|
||||
|
||||
$this->object->deleteDatabase("walter");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user