diff --git a/README.md b/README.md index 75f457e3e..d8c0ea9b0 100644 --- a/README.md +++ b/README.md @@ -93,26 +93,43 @@ Writing data is done by providing an array of points to the writePoints method o 0.64, array('host' => 'server01', 'region' => 'us-west'), array('cpucount' => 10), - 1435255849 + 1435255849 // Time precision has to be set to seconds! ), new Point( 'test_metric', 0.84, array('host' => 'server01', 'region' => 'us-west'), array('cpucount' => 10), - 1435255850 + 1435255850 // Time precision has to be set to seconds! ) ); - $newPoints = $database->writePoints($points); + // we are writing unix timestamps, which have a second precision + $newPoints = $database->writePoints($points, Database::PRECISION_SECONDS); ``` +It's possible to add multiple [fields](https://influxdb.com/docs/v0.9/concepts/key_concepts.html) when writing +measurements to InfluxDB. The point class allows one to easily write data in batches to influxDB. + The name of a measurement and the value are mandatory. Additional fields, tags and a timestamp are optional. InfluxDB takes the current time as the default timestamp. -It's possible to add multiple [fields](https://influxdb.com/docs/v0.9/concepts/key_concepts.html) when writing -measurements to InfluxDB. The point class allows one to easily write data in batches to influxDB. +#### Timestamp precision + +It's important to provide the correct precision when adding a timestamp to a Point object. This is because +if you specify a timestamp in seconds and the default (nanosecond) precision is set; the entered timestamp will be invalid. + +```php + // Points will require a nanosecond precision (this is default as per influxdb standard) + $newPoints = $database->writePoints($points); + + // Points will require second precision + $newPoints = $database->writePoints($points, Database::PRECISION_SECONDS); + + // Points will require microsecond precision + $newPoints = $database->writePoints($points, Database::PRECISION_MICROSECONDS); +``` ### Creating databases @@ -177,6 +194,10 @@ Some functions are too general for a database. So these are available in the cli ## Changelog +####0.1.2 +* Added exists method to Database class +* Added time precision to database class + ####0.1.1 * Merged repository to influxdb/influxdb-php * Added unit test for createRetentionPolicy diff --git a/src/InfluxDB/Client.php b/src/InfluxDB/Client.php index 0aa51dff4..694309fce 100644 --- a/src/InfluxDB/Client.php +++ b/src/InfluxDB/Client.php @@ -184,17 +184,21 @@ class Client * * @param string $database * @param string $data + * @param string $precision The timestamp precision * * @return bool * * @internal Internal method, do not use directly * @throws Exception */ - public function write($database, $data) + public function write($database, $data, $precision) { try { - $result = $this->httpClient->post($this->getBaseURI() . '/write?db=' . $database, null, $data, + $result = $this->httpClient->post($this->getBaseURI() + . '/write?db=' . $database + . '&precision=' . $precision + , null, $data, array('timeout' => $this->getTimeout()) )->send(); diff --git a/src/InfluxDB/Database.php b/src/InfluxDB/Database.php index 660e02e8a..f5d58d78a 100644 --- a/src/InfluxDB/Database.php +++ b/src/InfluxDB/Database.php @@ -31,6 +31,17 @@ class Database */ protected $client; + + /** + * Precision constants + */ + const PRECISION_NANOSECONDS = 'n'; + const PRECISION_MICROSECONDS = 'u'; + const PRECISION_MILLISECONDS = 'ms'; + const PRECISION_SECONDS = 's'; + const PRECISION_MINUTES = 'm'; + const PRECISION_HOURS = 'h'; + /** * Construct a database object * @@ -102,6 +113,8 @@ class Database /** * @param RetentionPolicy $retentionPolicy + * + * @return ResultSet */ public function createRetentionPolicy(RetentionPolicy $retentionPolicy) { @@ -111,12 +124,13 @@ class Database /** * Writes points into InfluxDB * - * @param array $points + * @param Point[] $points Array of points + * @param string $precision The timestamp precision (defaults to nanoseconds) * * @return bool * @throws Exception */ - public function writePoints(array $points) + public function writePoints(array $points, $precision = self::PRECISION_NANOSECONDS) { $payload = array(); @@ -129,7 +143,17 @@ class Database $payload[] = (string) $point; } - return $this->client->write($this->name, implode(PHP_EOL, $payload)); + return $this->client->write($this->name, implode(PHP_EOL, $payload), $precision); + } + + /** + * @return bool + */ + public function exists() + { + $databases = $this->client->listDatabases(); + + return in_array($this->name, $databases); } /** diff --git a/tests/unit/DatabaseTest.php b/tests/unit/DatabaseTest.php index 890c03e45..1b16f598f 100644 --- a/tests/unit/DatabaseTest.php +++ b/tests/unit/DatabaseTest.php @@ -8,8 +8,9 @@ use InfluxDB\Database; use InfluxDB\Point; use InfluxDB\ResultSet; use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_TestCase; -class DatabaseTest extends \PHPUnit_Framework_TestCase +class DatabaseTest extends PHPUnit_Framework_TestCase { /** @var Database $db */ @@ -54,6 +55,12 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase ->method('query') ->will($this->returnValue(new ResultSet($this->resultData))); + + $this->mockClient->expects($this->any()) + ->method('listDatabases') + ->will($this->returnValue(array('test123', 'test'))); + + $this->db = new Database('influx_test_db', $this->mockClient); $this->dataToInsert = file_get_contents(dirname(__FILE__) . '/input.example.json'); @@ -96,6 +103,20 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase new Database(null, $this->mockClient); } + public function testExists() + { + $database = new Database('test', $this->mockClient); + + $this->assertEquals($database->exists(), true); + } + + + public function testNotExists() + { + $database = new Database('test_not_exists', $this->mockClient); + + $this->assertEquals($database->exists(), false); + } public function testWritePointsInASingleCall() {