Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

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

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
22 changes: 15 additions & 7 deletions src/InfluxDB/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InfluxDB\Client\Admin;
use InfluxDB\Client\Exception as ClientException;
use InfluxDB\Database;
use InfluxDB\Driver\DriverInterface;
use InfluxDB\Driver\Exception as DriverException;
use InfluxDB\Driver\Guzzle;
Expand Down Expand Up @@ -88,6 +89,7 @@ class Client
*/
protected $driver;

protected $precision = Database::PRECISION_NANOSECONDS;

/**
* Stores the last query that ran
Expand All @@ -114,7 +116,8 @@ public function __construct(
$ssl = false,
$verifySSL = false,
$timeout = 0,
$connectTimeout = 0
$connectTimeout = 0,
$precision=Database::PRECISION_NANOSECONDS
) {
$this->host = (string) $host;
$this->port = (int) $port;
Expand All @@ -123,7 +126,8 @@ public function __construct(
$this->timeout = (float) $timeout;
$this->connectTimeout = (float) $connectTimeout;
$this->verifySSL = (bool) $verifySSL;

if (!Database::validatePrecision($precision)) { throw new \Exception("Precisión inválida."); } // fin if
$this->precision = $precision;
if ($ssl) {
$this->scheme = 'https';
$this->options['verify'] = $verifySSL;
Expand Down Expand Up @@ -171,9 +175,12 @@ public function query($database, $query, $parameters = [])
if ($database) {
$parameters['db'] = $database;
}

if (isset($parameters['epoch'])) {
if (!Database::validatePrecision($parameters['epoch'])) { throw new \Exception("Precisión inválida."); } // fin if
$parameters['epoch'] = Database::toValidQueryPrecision($parameters['epoch']);
} // fin if
$parameters = [
'url' => 'query?' . http_build_query(array_merge(['q' => $query], $parameters)),
'url' => Database::ENDPOINT_QUERY.'?' . http_build_query(array_merge(['q' => $query, 'epoch'=>Database::toValidQueryPrecision($this->precision)], $parameters)),
'database' => $database,
'method' => 'get'
];
Expand All @@ -193,7 +200,7 @@ public function query($database, $query, $parameters = [])
return $driver->query();

} catch (\Exception $e) {
throw new Exception('Query has failed: ' . $e->getMessage(), $e->getCode(), $e);
throw new Exception('Query has failed', $e->getCode(), $e);
}
}

Expand Down Expand Up @@ -267,7 +274,7 @@ public function listUsers()
* @return Client|Database
* @throws ClientException
*/
public static function fromDSN($dsn, $timeout = 0, $verifySSL = false, $connectTimeout = 0)
public static function fromDSN($dsn, $timeout = 0, $verifySSL = false, $connectTimeout = 0, $precision=Database::PRECISION_NANOSECONDS)
{
$connParams = parse_url($dsn);

Expand Down Expand Up @@ -300,7 +307,8 @@ public static function fromDSN($dsn, $timeout = 0, $verifySSL = false, $connectT
$ssl,
$verifySSL,
$timeout,
$connectTimeout
$connectTimeout,
$precision
);

// set the UDP driver when the DSN specifies UDP
Expand Down
41 changes: 35 additions & 6 deletions src/InfluxDB/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
class Database
{
const ENDPOINT_QUERY = "/query";
const ENDPOINT_WRITE = "/write";

/**
* The name of the Database
*
Expand All @@ -32,12 +35,14 @@ class Database
/**
* Precision constants
*/
const PRECISION_NANOSECONDS = '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";

/**
* Construct a database object
Expand Down Expand Up @@ -161,16 +166,18 @@ function (Point $point) {
*/
public function writePayload($payload, $precision = self::PRECISION_NANOSECONDS, $retentionPolicy = null)
{
$precision = self::toValidQueryPrecision($precision, self::ENDPOINT_WRITE);
try {
$query_params = ['db'=>$this->name, 'precision'=>$precision];
$parameters = [
'url' => sprintf('write?db=%s&precision=%s', $this->name, $precision),
'url' => self::ENDPOINT_WRITE."?",
'database' => $this->name,
'method' => 'post'
];
if ($retentionPolicy !== null) {
$parameters['url'] .= sprintf('&rp=%s', $retentionPolicy);
$query_params['rp'] = $retentionPolicy;
}

$parameters['url'] .= http_build_query($query_params);
return $this->client->write($parameters, $payload);

} catch (Exception $e) {
Expand Down Expand Up @@ -218,9 +225,9 @@ public function drop()
*
* @return QueryBuilder
*/
public function getQueryBuilder()
public function getQueryBuilder(array $parameters=[])
{
return new QueryBuilder($this);
return new QueryBuilder($this, $parameters);
}

/**
Expand Down Expand Up @@ -253,4 +260,26 @@ protected function getRetentionPolicyQuery($method, RetentionPolicy $retentionPo

return $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=self::ENDPOINT_QUERY) {
switch ($type) {
case self::ENDPOINT_QUERY:
switch ($strPrecision) {
case self::PRECISION_RFC3339: return NULL;
case self::PRECISION_MICROSECONDS_U: return self::PRECISION_MICROSECONDS;
} // fin switch
break;
case self::ENDPOINT_WRITE:
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()
}
7 changes: 5 additions & 2 deletions src/InfluxDB/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ class Builder
*/
protected $orderBy;

protected $parameters;

/**
* @param Database $db
*/
public function __construct(Database $db)
public function __construct(Database $db, array $parameters=[])
{
$this->db = $db;
$this->parameters = $parameters;
}

/**
Expand Down Expand Up @@ -297,7 +300,7 @@ public function getQuery()
*/
public function getResultSet()
{
return $this->db->query($this->parseQuery());
return $this->db->query($this->parseQuery(), $this->parameters);
}

/**
Expand Down