Extract general QueryBuilder logic into an abstract class to make it more maintainable
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
dbc83d596c
commit
df6ff71375
5 changed files with 218 additions and 176 deletions
161
src/Builder/AbstractQueryBuilder.php
Normal file
161
src/Builder/AbstractQueryBuilder.php
Normal file
|
|
@ -0,0 +1,161 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Arendsen\FluxQueryBuilder\Builder;
|
||||||
|
|
||||||
|
use DateTime;
|
||||||
|
use Exception;
|
||||||
|
use Arendsen\FluxQueryBuilder\Builder\FluxPart;
|
||||||
|
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
||||||
|
use Arendsen\FluxQueryBuilder\Expression\KeyFilter;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Duplicate;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Filter;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\From;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Range;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Reduce;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Sort;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Map;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Group;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Limit;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Mean;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\RawFunction;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Window;
|
||||||
|
|
||||||
|
abstract class AbstractQueryBuilder
|
||||||
|
{
|
||||||
|
public const REQUIRED_INPUT_FROM = 'from';
|
||||||
|
public const REQUIRED_INPUT_RANGE = 'range';
|
||||||
|
public const REQUIRED_INPUT_MEASUREMENT = 'measurement';
|
||||||
|
|
||||||
|
public const REQUIRED_INPUT = [
|
||||||
|
self::REQUIRED_INPUT_FROM,
|
||||||
|
self::REQUIRED_INPUT_RANGE,
|
||||||
|
self::REQUIRED_INPUT_MEASUREMENT,
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int $currentFluxQueryPart
|
||||||
|
*/
|
||||||
|
private $currentFluxQueryPart = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array $fluxQuery
|
||||||
|
*/
|
||||||
|
private $fluxQueryParts = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array $requiredData
|
||||||
|
*/
|
||||||
|
private $requiredData = [];
|
||||||
|
|
||||||
|
public function from(array $from): AbstractQueryBuilder
|
||||||
|
{
|
||||||
|
$this->addRequiredData(self::REQUIRED_INPUT_FROM, $from);
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::FROM,
|
||||||
|
new From($from)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fromBucket(string $bucket): AbstractQueryBuilder
|
||||||
|
{
|
||||||
|
$this->from(['bucket' => $bucket]);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function fromMeasurement(string $measurement): AbstractQueryBuilder
|
||||||
|
{
|
||||||
|
$this->addRequiredData(self::REQUIRED_INPUT_MEASUREMENT, $measurement);
|
||||||
|
$this->addKeyFilter(KeyFilter::setEqualTo('_measurement', $measurement));
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*
|
||||||
|
* @param KeyValue $keyValue
|
||||||
|
* @return QueryBuilder
|
||||||
|
*/
|
||||||
|
public function addFilter(KeyValue $keyValue): AbstractQueryBuilder
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::FILTERS,
|
||||||
|
new Filter($keyValue)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addKeyFilter(KeyFilter $keyFilter): AbstractQueryBuilder
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::FILTERS,
|
||||||
|
new Filter($keyFilter)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addFieldFilter(array $fields): AbstractQueryBuilder
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::FILTERS,
|
||||||
|
new Filter($fields)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addRange(DateTime $start, ?DateTime $stop = null): AbstractQueryBuilder
|
||||||
|
{
|
||||||
|
$this->addRequiredData(self::REQUIRED_INPUT_RANGE, [$start, $stop]);
|
||||||
|
$this->addToQuery(
|
||||||
|
FluxPart::RANGE,
|
||||||
|
new Range($start, $stop)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addRangeStart(DateTime $start): AbstractQueryBuilder
|
||||||
|
{
|
||||||
|
$this->addRange($start);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addRangeInBetween(DateTime $start, DateTime $stop)
|
||||||
|
{
|
||||||
|
$this->addRange($start, $stop);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function addToQuery($key, $query)
|
||||||
|
{
|
||||||
|
$this->fluxQueryParts[$this->currentFluxQueryPart] = $query;
|
||||||
|
$this->currentFluxQueryPart++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function build(): string
|
||||||
|
{
|
||||||
|
$this->checkRequired();
|
||||||
|
|
||||||
|
$query = '';
|
||||||
|
|
||||||
|
foreach ($this->fluxQueryParts as $part) {
|
||||||
|
$query .= $part;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function addRequiredData(string $key, $value)
|
||||||
|
{
|
||||||
|
$this->requiredData[][$key] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function checkRequired()
|
||||||
|
{
|
||||||
|
foreach (self::REQUIRED_INPUT as $key => $input) {
|
||||||
|
if (!isset($this->requiredData[$key][$input])) {
|
||||||
|
throw new Exception('You need to define the "' . $input . '" part of the query!');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
src/Builder/FluxPart.php
Normal file
21
src/Builder/FluxPart.php
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Arendsen\FluxQueryBuilder\Builder;
|
||||||
|
|
||||||
|
class FluxPart
|
||||||
|
{
|
||||||
|
public const FROM = 'from';
|
||||||
|
public const RANGE = 'range';
|
||||||
|
public const FILTERS = 'filters';
|
||||||
|
public const REDUCE = 'reduce';
|
||||||
|
public const MAP = 'map';
|
||||||
|
public const SORT = 'sort';
|
||||||
|
public const GROUP = 'group';
|
||||||
|
public const LIMIT = 'limit';
|
||||||
|
public const WINDOW = 'window';
|
||||||
|
public const MEAN = 'mean';
|
||||||
|
public const DUPLICATE = 'duplicate';
|
||||||
|
public const UNWINDOW = 'unwindow';
|
||||||
|
public const AGGREGATEWINDOW = 'aggregateWindow';
|
||||||
|
public const RAWFUNCTION = 'raw';
|
||||||
|
}
|
||||||
|
|
@ -2,15 +2,10 @@
|
||||||
|
|
||||||
namespace Arendsen\FluxQueryBuilder;
|
namespace Arendsen\FluxQueryBuilder;
|
||||||
|
|
||||||
use DateTime;
|
use Arendsen\FluxQueryBuilder\Builder\AbstractQueryBuilder;
|
||||||
use Exception;
|
use Arendsen\FluxQueryBuilder\Builder\FluxPart;
|
||||||
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
|
||||||
use Arendsen\FluxQueryBuilder\Expression\KeyFilter;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
|
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Duplicate;
|
use Arendsen\FluxQueryBuilder\Functions\Duplicate;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Filter;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\From;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Range;
|
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Reduce;
|
use Arendsen\FluxQueryBuilder\Functions\Reduce;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Sort;
|
use Arendsen\FluxQueryBuilder\Functions\Sort;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Map;
|
use Arendsen\FluxQueryBuilder\Functions\Map;
|
||||||
|
|
@ -20,130 +15,12 @@ use Arendsen\FluxQueryBuilder\Functions\Mean;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\RawFunction;
|
use Arendsen\FluxQueryBuilder\Functions\RawFunction;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Window;
|
use Arendsen\FluxQueryBuilder\Functions\Window;
|
||||||
|
|
||||||
class QueryBuilder
|
class QueryBuilder extends AbstractQueryBuilder
|
||||||
{
|
{
|
||||||
public const FLUX_PART_FROM = 'from';
|
|
||||||
public const FLUX_PART_RANGE = 'range';
|
|
||||||
public const FLUX_PART_FILTERS = 'filters';
|
|
||||||
public const FLUX_PART_REDUCE = 'reduce';
|
|
||||||
public const FLUX_PART_MAP = 'map';
|
|
||||||
public const FLUX_PART_SORT = 'sort';
|
|
||||||
public const FLUX_PART_GROUP = 'group';
|
|
||||||
public const FLUX_PART_LIMIT = 'limit';
|
|
||||||
public const FLUX_PART_WINDOW = 'window';
|
|
||||||
public const FLUX_PART_MEAN = 'mean';
|
|
||||||
public const FLUX_PART_DUPLICATE = 'duplicate';
|
|
||||||
public const FLUX_PART_UNWINDOW = 'unwindow';
|
|
||||||
public const FLUX_PART_AGGREGATEWINDOW = 'aggregateWindow';
|
|
||||||
public const FLUX_PART_RAWFUNCTION = 'raw';
|
|
||||||
|
|
||||||
public const REQUIRED_INPUT_FROM = 'from';
|
|
||||||
public const REQUIRED_INPUT_RANGE = 'range';
|
|
||||||
public const REQUIRED_INPUT_MEASUREMENT = 'measurement';
|
|
||||||
|
|
||||||
public const REQUIRED_INPUT = [
|
|
||||||
self::REQUIRED_INPUT_FROM,
|
|
||||||
self::REQUIRED_INPUT_RANGE,
|
|
||||||
self::REQUIRED_INPUT_MEASUREMENT,
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int $currentFluxQueryPart
|
|
||||||
*/
|
|
||||||
private $currentFluxQueryPart = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array $fluxQuery
|
|
||||||
*/
|
|
||||||
private $fluxQueryParts = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array $requiredData
|
|
||||||
*/
|
|
||||||
private $requiredData = [];
|
|
||||||
|
|
||||||
public function from(array $from): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addRequiredData(self::REQUIRED_INPUT_FROM, $from);
|
|
||||||
$this->addToQuery(
|
|
||||||
self::FLUX_PART_FROM,
|
|
||||||
new From($from)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function fromBucket(string $bucket): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->from(['bucket' => $bucket]);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function fromMeasurement(string $measurement): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addRequiredData(self::REQUIRED_INPUT_MEASUREMENT, $measurement);
|
|
||||||
$this->addKeyFilter(KeyFilter::setEqualTo('_measurement', $measurement));
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @deprecated
|
|
||||||
*
|
|
||||||
* @param KeyValue $keyValue
|
|
||||||
* @return QueryBuilder
|
|
||||||
*/
|
|
||||||
public function addFilter(KeyValue $keyValue): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
self::FLUX_PART_FILTERS,
|
|
||||||
new Filter($keyValue)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addKeyFilter(KeyFilter $keyFilter): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
self::FLUX_PART_FILTERS,
|
|
||||||
new Filter($keyFilter)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addFieldFilter(array $fields): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addToQuery(
|
|
||||||
self::FLUX_PART_FILTERS,
|
|
||||||
new Filter($fields)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addRange(DateTime $start, ?DateTime $stop = null): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addRequiredData(self::REQUIRED_INPUT_RANGE, [$start, $stop]);
|
|
||||||
$this->addToQuery(
|
|
||||||
self::FLUX_PART_RANGE,
|
|
||||||
new Range($start, $stop)
|
|
||||||
);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addRangeStart(DateTime $start): QueryBuilder
|
|
||||||
{
|
|
||||||
$this->addRange($start);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addRangeInBetween(DateTime $start, DateTime $stop)
|
|
||||||
{
|
|
||||||
$this->addRange($start, $stop);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addReduce(array $settings, array $identity): QueryBuilder
|
public function addReduce(array $settings, array $identity): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_REDUCE,
|
FluxPart::REDUCE,
|
||||||
new Reduce($settings, $identity)
|
new Reduce($settings, $identity)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -152,7 +29,7 @@ class QueryBuilder
|
||||||
public function addSort(array $columns, $desc): QueryBuilder
|
public function addSort(array $columns, $desc): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_SORT,
|
FluxPart::SORT,
|
||||||
new Sort($columns, $desc)
|
new Sort($columns, $desc)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -161,7 +38,7 @@ class QueryBuilder
|
||||||
public function addMap($query): QueryBuilder
|
public function addMap($query): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_MAP,
|
FluxPart::MAP,
|
||||||
new Map($query)
|
new Map($query)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -170,17 +47,17 @@ class QueryBuilder
|
||||||
public function addGroup(array $columns, $mode = 'by'): QueryBuilder
|
public function addGroup(array $columns, $mode = 'by'): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_GROUP,
|
FluxPart::GROUP,
|
||||||
new Group($columns, $mode)
|
new Group($columns, $mode)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addLimit(int $limit): QueryBuilder
|
public function addLimit(int $limit, int $offset = 0): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_LIMIT,
|
FluxPart::LIMIT,
|
||||||
new Limit($limit)
|
new Limit($limit, $offset)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
@ -188,7 +65,7 @@ class QueryBuilder
|
||||||
public function addWindow($every, array $options = []): QueryBuilder
|
public function addWindow($every, array $options = []): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_WINDOW,
|
FluxPart::WINDOW,
|
||||||
new Window($every, $options)
|
new Window($every, $options)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -197,7 +74,7 @@ class QueryBuilder
|
||||||
public function addDuplicate(string $column, string $as): QueryBuilder
|
public function addDuplicate(string $column, string $as): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_DUPLICATE,
|
FluxPart::DUPLICATE,
|
||||||
new Duplicate($column, $as)
|
new Duplicate($column, $as)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -206,7 +83,7 @@ class QueryBuilder
|
||||||
public function addMean(?string $column = ''): QueryBuilder
|
public function addMean(?string $column = ''): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_MEAN,
|
FluxPart::MEAN,
|
||||||
new Mean($column)
|
new Mean($column)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -215,7 +92,7 @@ class QueryBuilder
|
||||||
public function addUnWindow(): QueryBuilder
|
public function addUnWindow(): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_UNWINDOW,
|
FluxPart::UNWINDOW,
|
||||||
new Window('inf')
|
new Window('inf')
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -224,7 +101,7 @@ class QueryBuilder
|
||||||
public function addAggregateWindow($every, $fn, array $options = []): QueryBuilder
|
public function addAggregateWindow($every, $fn, array $options = []): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_AGGREGATEWINDOW,
|
FluxPart::AGGREGATEWINDOW,
|
||||||
new AggregateWindow($every, $fn, $options)
|
new AggregateWindow($every, $fn, $options)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -233,42 +110,9 @@ class QueryBuilder
|
||||||
public function addRawFunction(string $input): QueryBuilder
|
public function addRawFunction(string $input): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_RAWFUNCTION,
|
FluxPart::RAWFUNCTION,
|
||||||
new RawFunction($input)
|
new RawFunction($input)
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function addToQuery($key, $query)
|
|
||||||
{
|
|
||||||
$this->fluxQueryParts[$this->currentFluxQueryPart] = $query;
|
|
||||||
$this->currentFluxQueryPart++;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function build(): string
|
|
||||||
{
|
|
||||||
$this->checkRequired();
|
|
||||||
|
|
||||||
$query = '';
|
|
||||||
|
|
||||||
foreach ($this->fluxQueryParts as $part) {
|
|
||||||
$query .= $part;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $query;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function addRequiredData(string $key, $value)
|
|
||||||
{
|
|
||||||
$this->requiredData[][$key] = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function checkRequired()
|
|
||||||
{
|
|
||||||
foreach (self::REQUIRED_INPUT as $key => $input) {
|
|
||||||
if (!isset($this->requiredData[$key][$input])) {
|
|
||||||
throw new Exception('You need to define the "' . $input . '" part of the query!');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||||
namespace Tests\Functions;
|
namespace Tests\Functions;
|
||||||
|
|
||||||
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
||||||
|
use Arendsen\FluxQueryBuilder\Expression\KeyFilter;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Filter;
|
use Arendsen\FluxQueryBuilder\Functions\Filter;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
|
@ -31,4 +32,17 @@ final class FilterFunctionTest extends TestCase
|
||||||
|
|
||||||
$this->assertEquals($expression->__toString(), $query);
|
$this->assertEquals($expression->__toString(), $query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testKeyFilter()
|
||||||
|
{
|
||||||
|
$expression = new Filter(KeyFilter::setEqualTo('_measurement', 'test_measurement')
|
||||||
|
->andEqualTo('_field', 'user')
|
||||||
|
->orEqualTo('_field', 'field2')
|
||||||
|
->andEqualTo('user', 'my_username'));
|
||||||
|
|
||||||
|
$query = '|> filter(fn: (r) => r._measurement == "test_measurement" and r._field == "user" or ' .
|
||||||
|
'r._field == "field2" and r.user == "my_username") ';
|
||||||
|
|
||||||
|
$this->assertEquals($expression->__toString(), $query);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -117,13 +117,14 @@ final class QueryBuilderTest extends TestCase
|
||||||
->addFieldFilter(['username', 'ip'])
|
->addFieldFilter(['username', 'ip'])
|
||||||
->addKeyFilter(KeyFilter::setGreaterOrEqualTo('count', 1)->andGreaterOrEqualTo('count2', 2))
|
->addKeyFilter(KeyFilter::setGreaterOrEqualTo('count', 1)->andGreaterOrEqualTo('count2', 2))
|
||||||
->addMap('r with name: r.user')
|
->addMap('r with name: r.user')
|
||||||
->addGroup(['_field', 'ip']);
|
->addGroup(['_field', 'ip'])
|
||||||
|
->addLimit(1);
|
||||||
|
|
||||||
$expectedQuery = 'from(bucket: "test_bucket") |> range(start: time(v: 2022-08-12T17:31:00Z)) ' .
|
$expectedQuery = 'from(bucket: "test_bucket") |> range(start: time(v: 2022-08-12T17:31:00Z)) ' .
|
||||||
'|> reduce(fn: (r, accumulator) => ({count: accumulator.count + 1}), identity: {count: 0}) ' .
|
'|> reduce(fn: (r, accumulator) => ({count: accumulator.count + 1}), identity: {count: 0}) ' .
|
||||||
'|> filter(fn: (r) => r._measurement == "test_measurement") |> filter(fn: (r) => ' .
|
'|> filter(fn: (r) => r._measurement == "test_measurement") |> filter(fn: (r) => ' .
|
||||||
'r._field == "username" or r._field == "ip") |> filter(fn: (r) => r.count >= 1 and r.count2 >= 2) ' .
|
'r._field == "username" or r._field == "ip") |> filter(fn: (r) => r.count >= 1 and r.count2 >= 2) ' .
|
||||||
'|> map(fn: (r) => ({ r with name: r.user })) |> group(columns: ["_field", "ip"], mode: "by") ';
|
'|> map(fn: (r) => ({ r with name: r.user })) |> group(columns: ["_field", "ip"], mode: "by") |> limit(n: 1, offset: 0) ';
|
||||||
|
|
||||||
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
||||||
}
|
}
|
||||||
|
|
@ -176,12 +177,13 @@ final class QueryBuilderTest extends TestCase
|
||||||
->addRangeStart(new DateTime('2022-08-12 17:31:00'))
|
->addRangeStart(new DateTime('2022-08-12 17:31:00'))
|
||||||
->addReduce(['count' => new MathType('accumulator.count + 1')], ['count' => 0])
|
->addReduce(['count' => new MathType('accumulator.count + 1')], ['count' => 0])
|
||||||
->fromMeasurement('test_measurement')
|
->fromMeasurement('test_measurement')
|
||||||
->addRawFunction('|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time")');
|
->addRawFunction('|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time")')
|
||||||
|
->addLimit(50, 100);
|
||||||
|
|
||||||
$expectedQuery = 'from(bucket: "test_bucket") |> range(start: time(v: 2022-08-12T17:31:00Z)) ' .
|
$expectedQuery = 'from(bucket: "test_bucket") |> range(start: time(v: 2022-08-12T17:31:00Z)) ' .
|
||||||
'|> reduce(fn: (r, accumulator) => ({count: accumulator.count + 1}), identity: {count: 0}) ' .
|
'|> reduce(fn: (r, accumulator) => ({count: accumulator.count + 1}), identity: {count: 0}) ' .
|
||||||
'|> filter(fn: (r) => r._measurement == "test_measurement") ' .
|
'|> filter(fn: (r) => r._measurement == "test_measurement") ' .
|
||||||
'|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time") ';
|
'|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time") |> limit(n: 50, offset: 100) ';
|
||||||
|
|
||||||
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue