2022-08-05 15:41:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Arendsen\FluxQueryBuilder;
|
|
|
|
|
|
2022-08-08 14:42:10 +00:00
|
|
|
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
|
|
|
|
use Arendsen\FluxQueryBuilder\Function\Filter;
|
|
|
|
|
use Arendsen\FluxQueryBuilder\Function\From;
|
|
|
|
|
use Arendsen\FluxQueryBuilder\Function\Range;
|
2022-08-08 15:37:04 +00:00
|
|
|
use Arendsen\FluxQueryBuilder\Function\Reduce;
|
2022-08-08 14:42:10 +00:00
|
|
|
use Exception;
|
|
|
|
|
|
2022-08-05 15:41:38 +00:00
|
|
|
class QueryBuilder {
|
|
|
|
|
|
2022-08-08 14:42:10 +00:00
|
|
|
const FLUX_PART_FROM = 'from';
|
|
|
|
|
const FLUX_PART_RANGE = 'range';
|
|
|
|
|
const FLUX_PART_FILTERS = 'filters';
|
2022-08-08 15:37:04 +00:00
|
|
|
const FLUX_PART_REDUCE = 'reduce';
|
2022-08-08 14:42:10 +00:00
|
|
|
|
2022-08-08 15:07:30 +00:00
|
|
|
const PARTS = [
|
|
|
|
|
self::FLUX_PART_FROM,
|
|
|
|
|
self::FLUX_PART_RANGE,
|
2022-08-08 15:37:04 +00:00
|
|
|
self::FLUX_PART_REDUCE,
|
2022-08-08 15:07:30 +00:00
|
|
|
self::FLUX_PART_FILTERS
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const REQUIRED_INPUT_FROM = 'from';
|
|
|
|
|
const REQUIRED_INPUT_MEASUREMENT = 'measurement';
|
|
|
|
|
const REQUIRED_INPUT_RANGE = 'range';
|
|
|
|
|
|
|
|
|
|
const REQUIRED_INPUT = [
|
|
|
|
|
self::REQUIRED_INPUT_FROM,
|
|
|
|
|
self::REQUIRED_INPUT_MEASUREMENT,
|
|
|
|
|
self::REQUIRED_INPUT_RANGE,
|
|
|
|
|
];
|
|
|
|
|
|
2022-08-08 14:42:10 +00:00
|
|
|
/**
|
|
|
|
|
* @var array $fluxQuery
|
|
|
|
|
*/
|
|
|
|
|
private $fluxQueryParts = [];
|
|
|
|
|
|
2022-08-05 15:41:38 +00:00
|
|
|
/**
|
2022-08-08 15:07:30 +00:00
|
|
|
* @var array $requiredData
|
2022-08-05 15:41:38 +00:00
|
|
|
*/
|
2022-08-08 15:07:30 +00:00
|
|
|
private $requiredData = [];
|
2022-08-05 15:41:38 +00:00
|
|
|
|
|
|
|
|
public function from(array $from): QueryBuilder
|
|
|
|
|
{
|
2022-08-08 15:07:30 +00:00
|
|
|
$this->addRequiredData(self::REQUIRED_INPUT_FROM, $from);
|
2022-08-08 14:42:10 +00:00
|
|
|
$this->addToQuery(
|
|
|
|
|
self::FLUX_PART_FROM,
|
2022-08-08 15:07:30 +00:00
|
|
|
new From($from)
|
2022-08-08 14:42:10 +00:00
|
|
|
);
|
2022-08-05 15:41:38 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 15:07:30 +00:00
|
|
|
public function fromBucket(string $bucket): QueryBuilder
|
|
|
|
|
{
|
2022-08-08 14:42:10 +00:00
|
|
|
$this->from(['bucket' => $bucket]);
|
2022-08-05 15:41:38 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function fromMeasurement(string $measurement): QueryBuilder
|
|
|
|
|
{
|
2022-08-08 15:07:30 +00:00
|
|
|
$this->addRequiredData(self::REQUIRED_INPUT_MEASUREMENT, $measurement);
|
2022-08-08 14:42:10 +00:00
|
|
|
$this->addToQueryArray(
|
|
|
|
|
self::FLUX_PART_FILTERS,
|
2022-08-10 13:30:16 +00:00
|
|
|
new Filter(KeyValue::setEqualTo('_measurement', $measurement))
|
2022-08-08 14:42:10 +00:00
|
|
|
);
|
2022-08-05 15:41:38 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 15:07:30 +00:00
|
|
|
public function addFilter(KeyValue $keyValue): QueryBuilder
|
|
|
|
|
{
|
|
|
|
|
$this->addToQueryArray(
|
|
|
|
|
self::FLUX_PART_FILTERS,
|
|
|
|
|
new Filter($keyValue)
|
|
|
|
|
);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 15:41:38 +00:00
|
|
|
public function addRange(array $range): QueryBuilder
|
|
|
|
|
{
|
2022-08-08 15:07:30 +00:00
|
|
|
$this->addRequiredData(self::REQUIRED_INPUT_RANGE, $range);
|
2022-08-08 14:42:10 +00:00
|
|
|
$this->addToQuery(
|
|
|
|
|
self::FLUX_PART_RANGE,
|
|
|
|
|
new Range($range)
|
|
|
|
|
);
|
2022-08-05 15:41:38 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function addRangeStart(string $rangeStart): QueryBuilder
|
|
|
|
|
{
|
2022-08-08 14:42:10 +00:00
|
|
|
$this->addRange(['start' => $rangeStart]);
|
2022-08-05 15:41:38 +00:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 15:37:04 +00:00
|
|
|
public function addReduce(array $settings, array $identity): QueryBuilder
|
|
|
|
|
{
|
|
|
|
|
$this->addToQueryArray(
|
|
|
|
|
self::FLUX_PART_REDUCE,
|
|
|
|
|
new Reduce($settings, $identity)
|
|
|
|
|
);
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 14:42:10 +00:00
|
|
|
protected function addToQuery($key, $query)
|
|
|
|
|
{
|
|
|
|
|
$this->fluxQueryParts[$key] = $query;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function addToQueryArray($key, $query)
|
|
|
|
|
{
|
|
|
|
|
$this->fluxQueryParts[$key][] = $query;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-05 15:41:38 +00:00
|
|
|
public function build(): string
|
|
|
|
|
{
|
2022-08-08 14:42:10 +00:00
|
|
|
$this->checkRequired();
|
|
|
|
|
|
|
|
|
|
$query = '';
|
|
|
|
|
|
2022-08-08 15:07:30 +00:00
|
|
|
foreach(self::PARTS as $part)
|
|
|
|
|
{
|
|
|
|
|
if(isset($this->fluxQueryParts[$part]))
|
|
|
|
|
{
|
|
|
|
|
if(is_array($this->fluxQueryParts[$part]))
|
|
|
|
|
{
|
|
|
|
|
foreach($this->fluxQueryParts[$part] as $filter) {
|
|
|
|
|
$query .= $filter;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$query .= $this->fluxQueryParts[$part];
|
|
|
|
|
}
|
2022-08-08 14:42:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $query;
|
2022-08-08 15:07:30 +00:00
|
|
|
}
|
2022-08-08 14:42:10 +00:00
|
|
|
|
2022-08-08 15:07:30 +00:00
|
|
|
protected function addRequiredData(string $key, $value) {
|
|
|
|
|
$this->requiredData[$key] = $value;
|
2022-08-08 14:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function checkRequired()
|
|
|
|
|
{
|
2022-08-08 15:07:30 +00:00
|
|
|
foreach(self::REQUIRED_INPUT as $input) {
|
|
|
|
|
if(!isset($this->requiredData[$input]))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception('You need to define the "' . $input . '" part of the query!');
|
|
|
|
|
}
|
2022-08-08 14:42:10 +00:00
|
|
|
}
|
2022-08-05 15:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|