Files
librenms-librenms/lib/influxdb-php/src/InfluxDB/Database.php
T

224 lines
5.1 KiB
PHP
Raw Normal View History

<?php
2015-06-23 09:19:24 +02:00
namespace InfluxDB;
2015-07-20 09:28:47 -03:00
use InfluxDB\Database\Exception as DatabaseException;
2015-06-23 09:19:24 +02:00
use InfluxDB\Database\RetentionPolicy;
use InfluxDB\Query\Builder as QueryBuilder;
/**
* Class Database
*
2015-06-23 09:19:24 +02:00
* @package InfluxDB
2015-07-20 09:28:47 -03:00
* @author Stephen "TheCodeAssassin" Hoogendijk
*/
class Database
{
/**
* The name of the Database
*
* @var string
*/
protected $name = '';
/**
* @var Client
*/
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
*
* @param string $name
* @param Client $client
*/
public function __construct($name, Client $client)
{
2015-07-20 09:28:47 -03:00
if (empty($name)) {
throw new \InvalidArgumentException('No database name provided');
2015-06-18 16:17:57 +02:00
}
2015-07-20 10:31:41 -03:00
$this->name = (string) $name;
2015-07-20 09:28:47 -03:00
$this->client = $client;
}
2015-06-19 12:04:25 +02:00
/**
2015-07-20 09:28:47 -03:00
* @return string
2015-06-19 12:04:25 +02:00
*/
public function getName()
{
return $this->name;
}
/**
* Query influxDB
*
2015-07-20 09:28:47 -03:00
* @param string $query
* @param array $params
2015-06-18 16:17:57 +02:00
* @return ResultSet
* @throws Exception
*/
public function query($query, $params = [])
{
2015-06-19 15:46:02 +02:00
return $this->client->query($this->name, $query, $params);
}
/**
* Create this database
*
2015-07-20 09:28:47 -03:00
* @param RetentionPolicy $retentionPolicy
* @param bool $createIfNotExists Only create the database if it does not yet exist
*
2015-06-18 16:17:57 +02:00
* @return ResultSet
* @throws DatabaseException
*/
public function create(RetentionPolicy $retentionPolicy = null, $createIfNotExists = true)
2015-06-18 16:17:57 +02:00
{
try {
$query = sprintf(
'CREATE DATABASE %s"%s"',
($createIfNotExists ? 'IF NOT EXISTS ' : ''),
$this->name
);
$this->query($query);
2015-06-18 16:17:57 +02:00
if ($retentionPolicy) {
$this->createRetentionPolicy($retentionPolicy);
2015-06-18 16:17:57 +02:00
}
} catch (\Exception $e) {
throw new DatabaseException(
2015-07-23 09:40:41 +02:00
sprintf('Failed to created database %s', $this->name),
$e->getCode(),
$e
2015-06-18 16:17:57 +02:00
);
}
}
/**
2015-07-20 09:28:47 -03:00
* @param RetentionPolicy $retentionPolicy
* @return ResultSet
*/
public function createRetentionPolicy(RetentionPolicy $retentionPolicy)
{
return $this->query($this->getRetentionPolicyQuery('CREATE', $retentionPolicy));
}
2015-06-19 09:49:58 +02:00
/**
* Writes points into InfluxDB
*
2015-07-20 09:28:47 -03:00
* @param Point[] $points Array of points
* @param string $precision The timestamp precision (defaults to nanoseconds)
* @return bool
* @throws Exception
2015-06-19 09:49:58 +02:00
*/
public function writePoints(array $points, $precision = self::PRECISION_NANOSECONDS)
2015-06-19 09:49:58 +02:00
{
2015-07-20 09:54:00 -03:00
$payload = array_map(
function (Point $point) {
return (string) $point;
},
$points
);
try {
$parameters = [
2015-07-21 00:34:08 +02:00
'url' => sprintf('write?db=%s&precision=%s', $this->name, $precision),
'database' => $this->name,
'method' => 'post'
];
return $this->client->write($parameters, $payload);
} catch (\Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
}
/**
* @return bool
*/
public function exists()
{
$databases = $this->client->listDatabases();
return in_array($this->name, $databases);
2015-06-19 09:49:58 +02:00
}
2015-06-18 16:17:57 +02:00
/**
* @param RetentionPolicy $retentionPolicy
*/
2015-06-18 16:17:57 +02:00
public function alterRetentionPolicy(RetentionPolicy $retentionPolicy)
{
2015-06-18 16:17:57 +02:00
$this->query($this->getRetentionPolicyQuery('ALTER', $retentionPolicy));
}
/**
* @return array
* @throws Exception
*/
public function listRetentionPolicies()
{
return $this->query(sprintf('SHOW RETENTION POLICIES ON "%s"', $this->name))->getPoints();
}
/**
* Drop this database
*/
public function drop()
{
$this->query(sprintf('DROP DATABASE "%s"', $this->name));
}
/**
* Retrieve the query builder
*
* @return QueryBuilder
*/
public function getQueryBuilder()
{
return new QueryBuilder($this);
}
2015-06-22 14:51:48 +02:00
/**
* @return Client
*/
public function getClient()
{
return $this->client;
}
2015-06-18 16:17:57 +02:00
/**
2015-07-20 09:28:47 -03:00
* @param string $method
* @param RetentionPolicy $retentionPolicy
2015-06-18 16:17:57 +02:00
* @return string
*/
protected function getRetentionPolicyQuery($method, RetentionPolicy $retentionPolicy)
{
$query = sprintf(
'%s RETENTION POLICY "%s" ON "%s" DURATION %s REPLICATION %s',
2015-06-18 16:17:57 +02:00
$method,
$retentionPolicy->name,
$this->name,
$retentionPolicy->duration,
$retentionPolicy->replication
);
if ($retentionPolicy->default) {
$query .= " DEFAULT";
}
return $query;
}
2015-07-20 09:28:47 -03:00
}