Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

se puede especificar la precisión cuando hacer /query y /write. #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Adapter/Http/Options.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace InfluxDB\Adapter\Http;

use InfluxDB\Client as InfluxClient;

class Options
{
private $host;
Expand All @@ -12,6 +14,8 @@ class Options
private $retentionPolicy;
private $tags;
private $prefix;
private $epoch;
private $precision;

public function __construct()
{
Expand All @@ -21,7 +25,8 @@ public function __construct()
$this->setPassword("root");
$this->setProtocol("http");
$this->setPrefix("");

$this->setEpoch(InfluxClient::PRECISION_RFC3339);
$this->setPrecision(InfluxClient::PRECISION_NANOSECONDS);
$this->setRetentionPolicy("default");
$this->setTags([]);
}
Expand Down Expand Up @@ -124,4 +129,18 @@ public function setDatabase($database)
$this->database = $database;
return $this;
}

public function getEpoch() { return $this->epoch; } // fin getEpoch()

public function setEpoch($value) {
if (!InfluxClient::validatePrecision($value)) { throw new \Exception("Precisión inválida."); } // fin if
$this->epoch = $value;
} // fin setEpoch()

public function getPrecision() { return $this->precision; } // fin getPrecision()

public function setPrecision($value) {
if (!InfluxClient::validatePrecision($value)) { throw new \Exception("Precisión inválida."); } // fin if
$this->precision = $value;
} // fin setPrecision()
}
20 changes: 13 additions & 7 deletions src/Adapter/Http/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace InfluxDB\Adapter\Http;

use InfluxDB\Adapter\Http\Options;
use InfluxDB\Client as InfluxClient;
use GuzzleHttp\Client;
use InfluxDB\Adapter\QueryableInterface;

Expand All @@ -10,6 +11,8 @@ class Reader implements QueryableInterface
private $httpClient;
private $options;

const ENDPOINT = "query";

public function __construct(Client $httpClient, Options $options)
{
$this->httpClient = $httpClient;
Expand All @@ -23,11 +26,13 @@ public function getOptions()

public function query($query)
{
$objOpts = $this->getOptions();
$options = [
"auth" => [$this->getOptions()->getUsername(), $this->getOptions()->getPassword()],
"auth" => [$objOpts->getUsername(), $objOpts->getPassword()],
'query' => [
"q" => $query,
"db" => $this->getOptions()->getDatabase(),
"db" => $objOpts->getDatabase(),
"epoch"=>InfluxClient::toValidQueryPrecision($objOpts->getEpoch(), self::ENDPOINT)
]
];

Expand All @@ -42,17 +47,18 @@ private function get(array $httpMessage)

protected function getHttpQueryEndpoint()
{
return $this->getHttpEndpoint("query");
return $this->getHttpEndpoint(self::ENDPOINT);
}

private function getHttpEndpoint($operation)
{
$objOpts = $this->getOptions();
$url = sprintf(
"%s://%s:%d%s/%s",
$this->getOptions()->getProtocol(),
$this->getOptions()->getHost(),
$this->getOptions()->getPort(),
$this->getOptions()->getPrefix(),
$objOpts->getProtocol(),
$objOpts->getHost(),
$objOpts->getPort(),
$objOpts->getPrefix(),
$operation
);

Expand Down
24 changes: 15 additions & 9 deletions src/Adapter/Http/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use InfluxDB\Adapter\WriterTrait;
use InfluxDB\Adapter\Http\Options;
use InfluxDB\Adapter\WritableInterface;
use InfluxDB\Client as InfluxClient;

class Writer implements WritableInterface
{
Expand All @@ -13,6 +14,8 @@ class Writer implements WritableInterface
private $httpClient;
private $options;

const ENDPOINT = "write";

public function __construct(Client $httpClient, Options $options)
{
$this->httpClient = $httpClient;
Expand All @@ -26,13 +29,15 @@ public function getOptions()

public function send(array $message)
{
$objOptions = $this->getOptions();
$httpMessage = [
"auth" => [$this->getOptions()->getUsername(), $this->getOptions()->getPassword()],
"auth" => [$objOptions->getUsername(), $objOptions->getPassword()],
'query' => [
"db" => $this->getOptions()->getDatabase(),
"retentionPolicy" => $this->getOptions()->getRetentionPolicy(),
"db" => $objOptions->getDatabase(),
"retentionPolicy" => $objOptions->getRetentionPolicy(),
"precision"=>InfluxClient::toValidQueryPrecision($objOpts->getPrecision(), self::ENDPOINT)
],
"body" => $this->messageToLineProtocol($message, $this->getOptions()->getTags())
"body" => $this->messageToLineProtocol($message, $objOptions->getTags())
];

$endpoint = $this->getHttpSeriesEndpoint();
Expand All @@ -41,17 +46,18 @@ public function send(array $message)

protected function getHttpSeriesEndpoint()
{
return $this->getHttpEndpoint("write");
return $this->getHttpEndpoint(self::ENDPOINT);
}

private function getHttpEndpoint($operation)
{
$objOptions = $this->getOptions();
$url = sprintf(
"%s://%s:%d%s/%s",
$this->getOptions()->getProtocol(),
$this->getOptions()->getHost(),
$this->getOptions()->getPort(),
$this->getOptions()->getPrefix(),
$objOptions->getProtocol(),
$objOptions->getHost(),
$objOptions->getPort(),
$objOptions->getPrefix(),
$operation
);

Expand Down
34 changes: 34 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace InfluxDB;

use InfluxDB\Adapter\Http\Reader as HttpReader;
use InfluxDB\Adapter\Http\Writer as HttpWriter;
use InfluxDB\Adapter\WritableInterface as Writer;
use InfluxDB\Adapter\QueryableInterface as Reader;

Expand All @@ -13,6 +15,16 @@ class Client
private $reader;
private $writer;

// las constantes de precisión
const PRECISION_NANOSECONDS = "ns";
const PRECISION_MICROSECONDS_U = "µ";
const PRECISION_MICROSECONDS = "u";
const PRECISION_MILLISECONDS = "ms";
const PRECISION_SECONDS = "s";
const PRECISION_MINUTES = "m";
const PRECISION_HOURS = "h";
const PRECISION_RFC3339 = "rfc3339";

public function __construct(Reader $reader, Writer $writer)
{
$this->reader = $reader;
Expand Down Expand Up @@ -45,4 +57,26 @@ public function query($query)
{
return $this->getReader()->query($query);
}

public static function validatePrecision($strPrecision) {
return (in_array($strPrecision, [self::PRECISION_HOURS, self::PRECISION_MINUTES, self::PRECISION_SECONDS, self::PRECISION_MILLISECONDS, self::PRECISION_MICROSECONDS, self::PRECISION_MICROSECONDS_U, self::PRECISION_NANOSECONDS, self::PRECISION_RFC3339], TRUE) ? TRUE : FALSE);
} // fin validatePrecision()

public static function toValidQueryPrecision($strPrecision, $type=HttpReader::ENDPOINT) {
switch ($type) {
case HttpReader::ENDPOINT:
switch ($strPrecision) {
case self::PRECISION_RFC3339: return NULL;
case self::PRECISION_MICROSECONDS_U: return self::PRECISION_MICROSECONDS;
} // fin switch
break;
case HttpWriter::ENDPOINT:
switch ($strPrecision) {
case self::PRECISION_RFC3339: return self::PRECISION_NANOSECONDS;
case self::PRECISION_MICROSECONDS_U: return self::PRECISION_MICROSECONDS;
} // fin switch
break;
} // fin switch
return $strPrecision;
} // fin toValidQueryPrecision()
}