mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Merged leaseweb/influxdb-php into influxdb/influxdb-php
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
/* CONTRIBUTE */
|
||||
|
||||
This is the contribute.md of influxdb-php. Great to have you here.
|
||||
|
||||
Here are a few ways you can help make this project better.
|
||||
|
||||
# Contribute.md
|
||||
|
||||
## Team members
|
||||
|
||||
Stephen "TheCodeAssassin" Hoogendijk
|
||||
Daniel "danibrutal" Martinez
|
||||
|
||||
## Helping out
|
||||
|
||||
We appreciate any efforts to help us writing this library. You can contribute in any of the following ways:
|
||||
|
||||
* Documentation
|
||||
* Unit tests
|
||||
* New features
|
||||
* Bug fixed
|
||||
* Reviewing pull requests
|
||||
|
||||
## Guidelines
|
||||
|
||||
In order for your pull requests to get accepted we hold all the code to the following criteria:
|
||||
|
||||
* PSR-1/PSR-2 compliant
|
||||
* Do not use left hand conditions such as false == $something
|
||||
* New features need to be documented
|
||||
* Breaking changes should be well highlighted and explained in the PR
|
||||
|
||||
The following is optional but encouraged:
|
||||
|
||||
* Code should be documented
|
||||
* Code should be unit tested
|
||||
* Do not write conditions like false === $something, rather $something === false.
|
||||
@@ -2,6 +2,7 @@
|
||||
## InfluxDB client library for PHP
|
||||
[](https://travis-ci.org/LeaseWeb/influxdb-php)
|
||||
[](https://codeclimate.com/github/LeaseWeb/influxdb-php)
|
||||
[](https://codeclimate.com/github/LeaseWeb/influxdb-php/coverage)
|
||||
|
||||
### Overview
|
||||
|
||||
@@ -39,8 +40,8 @@ To fetch records from InfluxDB you can do a query directly on a database:
|
||||
|
||||
```php
|
||||
|
||||
// fetch the db
|
||||
$database = $client->db('influx_test_db');
|
||||
// fetch the selectDB
|
||||
$database = $client->selectDB('influx_test_db');
|
||||
|
||||
// executing a query will yield a resultset object
|
||||
$result = $database->query('select * from test_metric LIMIT 5');
|
||||
@@ -116,8 +117,8 @@ This library makes it easy to provide a retention policy when creating a databas
|
||||
// create the client
|
||||
$client = new \InfluxDB\Client($host, $port, '', '');
|
||||
|
||||
// select the db
|
||||
$database = $client->db('influx_test_db');
|
||||
// select the selectDB
|
||||
$database = $client->selectDB('influx_test_db');
|
||||
|
||||
// create the database with a retention policy
|
||||
$result = $database->create(new RetentionPolicy('test', '5d', 1, true));
|
||||
@@ -151,6 +152,12 @@ Some functions are too general for a database. So these are available in the cli
|
||||
|
||||
## Changelog
|
||||
|
||||
####0.1
|
||||
####0.1.1
|
||||
* Merged repository to influxdb/influxdb-php
|
||||
* Added unit test for createRetentionPolicy
|
||||
* -BREAKING CHANGE- changed $client->db to $client->selectDB
|
||||
|
||||
|
||||
####0.1.0
|
||||
------
|
||||
* Initial release
|
||||
|
||||
@@ -14,6 +14,10 @@
|
||||
{
|
||||
"name": "Stephen Hoogendijk",
|
||||
"email": "[email protected]"
|
||||
},
|
||||
{
|
||||
"name": "Daniel Martinez",
|
||||
"email": "[email protected]"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
|
||||
+2
-10
@@ -124,7 +124,7 @@ class Client
|
||||
*
|
||||
* @return Database
|
||||
*/
|
||||
public function db($name)
|
||||
public function selectDB($name)
|
||||
{
|
||||
|
||||
if (empty($name)) {
|
||||
@@ -261,7 +261,7 @@ class Client
|
||||
$timeout
|
||||
);
|
||||
|
||||
return ($dbName ? $client->db($dbName) : $client);
|
||||
return ($dbName ? $client->selectDB($dbName) : $client);
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
@@ -294,12 +294,4 @@ class Client
|
||||
|
||||
return $names;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Guzzle\Http\Client
|
||||
*/
|
||||
public function getHttpClient()
|
||||
{
|
||||
return $this->httpClient;
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ class Database
|
||||
$this->client = $client;
|
||||
|
||||
if (!$name) {
|
||||
throw new DatabaseException('No database name provided');
|
||||
throw new \InvalidArgumentException('No database name provided');
|
||||
}
|
||||
|
||||
$this->name = $name;
|
||||
@@ -105,7 +105,7 @@ class Database
|
||||
*/
|
||||
public function createRetentionPolicy(RetentionPolicy $retentionPolicy)
|
||||
{
|
||||
$this->query($this->getRetentionPolicyQuery('CREATE', $retentionPolicy));
|
||||
return $this->query($this->getRetentionPolicyQuery('CREATE', $retentionPolicy));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace InfluxDB;
|
||||
|
||||
use InfluxDB\Database\Exception;
|
||||
use InfluxDB\Database\Exception as DatabaseException;
|
||||
|
||||
/**
|
||||
* Class Point
|
||||
@@ -35,13 +35,13 @@ class Point
|
||||
* @param array $additionalFields Array of optional fields
|
||||
* @param int $timestamp Optional timestamp
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function __construct($measurement, $value, array $tags = array(), array $additionalFields = array(), $timestamp = null)
|
||||
{
|
||||
|
||||
if (empty($measurement)) {
|
||||
throw new Exception('Invalid measurement name provided');
|
||||
throw new DatabaseException('Invalid measurement name provided');
|
||||
}
|
||||
|
||||
$this->measurement = (string) $measurement;
|
||||
@@ -51,7 +51,7 @@ class Point
|
||||
$this->fields += array('value' => (float) $value);
|
||||
|
||||
if ($timestamp && !$this->isValidTimeStamp($timestamp)) {
|
||||
throw new Exception(sprintf('%s is not a valid timestamp', $timestamp));
|
||||
throw new DatabaseException(sprintf('%s is not a valid timestamp', $timestamp));
|
||||
}
|
||||
|
||||
$this->timestamp = $timestamp;
|
||||
|
||||
@@ -6,6 +6,8 @@ namespace InfluxDB\Test;
|
||||
use InfluxDB\Client;
|
||||
use InfluxDB\Database;
|
||||
use InfluxDB\Point;
|
||||
use InfluxDB\ResultSet;
|
||||
use PHPUnit_Framework_MockObject_MockObject;
|
||||
|
||||
class DatabaseTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@@ -13,24 +15,85 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase
|
||||
/** @var Database $db */
|
||||
protected $db = null;
|
||||
|
||||
/** @var Client $client */
|
||||
/** @var Client|PHPUnit_Framework_MockObject_MockObject $client */
|
||||
protected $mockClient;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $dataToInsert;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $resultData;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
static $emptyResult = '{"results":[{}]}';
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
protected $mockResultSet;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->mockClient = $this->getMockBuilder('\InfluxDB\Client')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$this->resultData = file_get_contents(dirname(__FILE__) . '/result.example.json');
|
||||
|
||||
$this->mockClient->expects($this->any())
|
||||
->method('getBaseURI')
|
||||
->will($this->returnValue($this->equalTo('http://localhost:8086')));
|
||||
|
||||
$this->mockClient->expects($this->any())
|
||||
->method('query')
|
||||
->will($this->returnValue(new ResultSet($this->resultData)));
|
||||
|
||||
$this->db = new Database('influx_test_db', $this->mockClient);
|
||||
|
||||
$this->dataToInsert = file_get_contents(dirname(__FILE__) . '/input.example.json');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function testQuery()
|
||||
{
|
||||
$testResultSet = new ResultSet($this->resultData);
|
||||
$this->assertEquals($this->db->query('SELECT * FROM test_metric'), $testResultSet);
|
||||
}
|
||||
|
||||
public function testCreateRetentionPolicy()
|
||||
{
|
||||
$retentionPolicy = new Database\RetentionPolicy('test', '1d', 1, true);
|
||||
|
||||
$mockClient = $this->getMockBuilder('\InfluxDB\Client')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$mockClient->expects($this->once())
|
||||
->method('query')
|
||||
->will($this->returnValue(new ResultSet(self::$emptyResult)));
|
||||
|
||||
|
||||
|
||||
$database = new Database('test', $mockClient);
|
||||
|
||||
$this->assertEquals($database->createRetentionPolicy($retentionPolicy), new ResultSet(self::$emptyResult));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testEmptyDatabaseName()
|
||||
{
|
||||
new Database(null, $this->mockClient);
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +116,8 @@ class DatabaseTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->mockClient->expects($this->once())
|
||||
->method('write')
|
||||
->with($this->equalTo($this->db->getName()), $this->equalTo($payloadExpected));
|
||||
->with($this->equalTo($this->db->getName()), $this->equalTo($payloadExpected))
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$this->db->writePoints(array($point1, $point2));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user