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;