From fd9667d9287352fb88eca5f02c5fd95ee4005753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20D=2E=20Rodas?= Date: Fri, 15 Nov 2013 00:17:00 -0300 Subject: [PATCH] Added timePrecision Client::setTimePrecision() affects all databases globally, and DB::setTimePrecision() only to the current database object --- lib/InfluxPHP/BaseHTTP.php | 34 ++++++++++++++++++++++++++++++++++ lib/InfluxPHP/DB.php | 2 +- tests/DBTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/lib/InfluxPHP/BaseHTTP.php b/lib/InfluxPHP/BaseHTTP.php index 40e0b59..077a6d5 100644 --- a/lib/InfluxPHP/BaseHTTP.php +++ b/lib/InfluxPHP/BaseHTTP.php @@ -44,6 +44,15 @@ class BaseHTTP protected $user; protected $pass; protected $base; + protected $timePrecision = 's'; + protected $children = array(); + + const SECOND = 's'; + const MILLISECOND = 'm'; + const MICROSECOND = 'u'; + const S = 's'; + const MS = 'm'; + const US = 'u'; protected function inherits(BaseHTTP $c) { @@ -51,6 +60,8 @@ class BaseHTTP $this->pass = $c->pass; $this->port = $c->port; $this->host = $c->host; + $this->timePrecision = $c->timePrecision; + $c->children[] = $this; } protected function getCurl($url, Array $args = []) @@ -85,6 +96,29 @@ class BaseHTTP return $this->execCurl($ch); } + public function getTimePrecision() + { + return $this->timePrecision; + } + + public function setTimePrecision($p) + { + switch ($p) { + case 'm': + case 's': + case 'u': + $this->timePrecision = $p; + if ($this instanceof Client) { + foreach ($this->children as $children) { + $children->timePrecision = $p; + } + } + return $this; + } + + throw new \InvalidArgumentException("Expecting m,s or u"); + } + protected function get($url, Array $args = []) { $ch = $this->getCurl($url, $args); diff --git a/lib/InfluxPHP/DB.php b/lib/InfluxPHP/DB.php index 10d8ac8..523023b 100644 --- a/lib/InfluxPHP/DB.php +++ b/lib/InfluxPHP/DB.php @@ -82,7 +82,7 @@ class DB extends BaseHTTP public function query($sql) { - return new Cursor($this->get('series', ['q' => $sql])); + return new Cursor($this->get('series', ['q' => $sql, 'time_precision' => $this->timePrecision])); } public function createUser($username, $password) diff --git a/tests/DBTest.php b/tests/DBTest.php index a245a88..90e7cba 100644 --- a/tests/DBTest.php +++ b/tests/DBTest.php @@ -47,6 +47,31 @@ class DBTest extends \phpunit_framework_testcase $client->test_xxx->drop(); } + public function testTimePrecision() + { + $client = new Client; + $this->assertEquals('s', $client->getTimePrecision()); + $db = $client->createDatabase("test_yyyy"); + $this->assertEquals('s', $db->getTimePrecision()); + + + $client->setTimePrecision('m'); + $this->assertEquals('m', $client->getTimePrecision()); + $this->assertEquals('m', $db->getTimePrecision()); + + $db1 = $client->createDatabase("test_yyyx"); + $this->assertEquals('m', $db->getTimePrecision()); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidTimePrecision() + { + $client = new Client; + $client->SetTimePrecision([]); + } + public function testQuery() { $client = new Client;