Added precision; added database exists method

This commit is contained in:
Stephen Hoogendijk
2015-07-20 12:22:39 +02:00
parent 33a7ea7d62
commit fd80291e15
4 changed files with 81 additions and 11 deletions
+26 -5
View File
@@ -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
+6 -2
View File
@@ -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();
+27 -3
View File
@@ -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);
}
/**
+22 -1
View File
@@ -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()
{