Add Group, Limit, Map, Sort functions
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
1922f33c5c
commit
147b2d9017
11 changed files with 252 additions and 2 deletions
32
src/Function/Group.php
Normal file
32
src/Function/Group.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace Arendsen\FluxQueryBuilder\Function;
|
||||
|
||||
class Group extends Base {
|
||||
|
||||
/**
|
||||
* @var array $columns
|
||||
*/
|
||||
private $columns;
|
||||
|
||||
/**
|
||||
* @var string $mode
|
||||
*/
|
||||
private $mode;
|
||||
|
||||
public function __construct(array $columns, string $mode = 'by')
|
||||
{
|
||||
$this->columns = $columns;
|
||||
$this->mode = $mode;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
$columns = array_map(function($column) {
|
||||
return '"' . $column . '"';
|
||||
}, $this->columns);
|
||||
|
||||
return '|> group(columns: [' . implode(', ', $columns) . '], mode: "' . $this->mode . '") ';
|
||||
}
|
||||
|
||||
}
|
||||
22
src/Function/Limit.php
Normal file
22
src/Function/Limit.php
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace Arendsen\FluxQueryBuilder\Function;
|
||||
|
||||
class Limit extends Base {
|
||||
|
||||
/**
|
||||
* @var int $limit
|
||||
*/
|
||||
private $limit;
|
||||
|
||||
public function __construct(int $limit)
|
||||
{
|
||||
$this->limit = $limit;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return '|> limit(n:' . (string)$this->limit . ') ';
|
||||
}
|
||||
|
||||
}
|
||||
22
src/Function/Map.php
Normal file
22
src/Function/Map.php
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace Arendsen\FluxQueryBuilder\Function;
|
||||
|
||||
class Map extends Base {
|
||||
|
||||
/**
|
||||
* @var array $query
|
||||
*/
|
||||
private $query;
|
||||
|
||||
public function __construct(string $query)
|
||||
{
|
||||
$this->query = $query;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return '|> map(fn: (r) => ({ ' . $this->query . ' })) ';
|
||||
}
|
||||
|
||||
}
|
||||
33
src/Function/Sort.php
Normal file
33
src/Function/Sort.php
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Arendsen\FluxQueryBuilder\Function;
|
||||
|
||||
class Sort extends Base {
|
||||
|
||||
/**
|
||||
* @var array $columns
|
||||
*/
|
||||
private $columns;
|
||||
|
||||
/**
|
||||
* @var bool $desc
|
||||
*/
|
||||
private $desc;
|
||||
|
||||
public function __construct(array $columns, bool $desc = false)
|
||||
{
|
||||
$this->columns = $columns;
|
||||
$this->desc = $desc;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
$columns = array_map(function($column) {
|
||||
return '"' . $column . '"';
|
||||
}, $this->columns);
|
||||
$desc = $this->desc ? 'true' : 'false';
|
||||
|
||||
return '|> sort(columns: [' . implode(', ', $columns) . '], desc: ' . $desc . ') ';
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -7,6 +7,10 @@ use Arendsen\FluxQueryBuilder\Function\Filter;
|
|||
use Arendsen\FluxQueryBuilder\Function\From;
|
||||
use Arendsen\FluxQueryBuilder\Function\Range;
|
||||
use Arendsen\FluxQueryBuilder\Function\Reduce;
|
||||
use Arendsen\FluxQueryBuilder\Function\Sort;
|
||||
use Arendsen\FluxQueryBuilder\Function\Map;
|
||||
use Arendsen\FluxQueryBuilder\Function\Group;
|
||||
use Arendsen\FluxQueryBuilder\Function\Limit;
|
||||
use Exception;
|
||||
|
||||
class QueryBuilder {
|
||||
|
|
@ -15,12 +19,20 @@ class QueryBuilder {
|
|||
const FLUX_PART_RANGE = 'range';
|
||||
const FLUX_PART_FILTERS = 'filters';
|
||||
const FLUX_PART_REDUCE = 'reduce';
|
||||
const FLUX_PART_MAP = 'map';
|
||||
const FLUX_PART_SORT = 'sort';
|
||||
const FLUX_PART_GROUP = 'group';
|
||||
const FLUX_PART_LIMIT = 'limit';
|
||||
|
||||
const PARTS = [
|
||||
self::FLUX_PART_FROM,
|
||||
self::FLUX_PART_RANGE,
|
||||
self::FLUX_PART_REDUCE,
|
||||
self::FLUX_PART_FILTERS
|
||||
self::FLUX_PART_FILTERS,
|
||||
self::FLUX_PART_MAP,
|
||||
self::FLUX_PART_SORT,
|
||||
self::FLUX_PART_GROUP,
|
||||
self::FLUX_PART_LIMIT,
|
||||
];
|
||||
|
||||
const REQUIRED_INPUT_FROM = 'from';
|
||||
|
|
@ -103,6 +115,42 @@ class QueryBuilder {
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function addSort(array $columns, $desc): QueryBuilder
|
||||
{
|
||||
$this->addToQueryArray(
|
||||
self::FLUX_PART_SORT,
|
||||
new Sort($columns, $desc)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addMap(string $query): QueryBuilder
|
||||
{
|
||||
$this->addToQueryArray(
|
||||
self::FLUX_PART_MAP,
|
||||
new Map($query)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addGroup(array $columns, $mode = 'by'): QueryBuilder
|
||||
{
|
||||
$this->addToQueryArray(
|
||||
self::FLUX_PART_GROUP,
|
||||
new Group($columns, $mode)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addLimit(int $limit): QueryBuilder
|
||||
{
|
||||
$this->addToQueryArray(
|
||||
self::FLUX_PART_LIMIT,
|
||||
new Limit($limit)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function addToQuery($key, $query)
|
||||
{
|
||||
$this->fluxQueryParts[$key] = $query;
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ final class KeyValueExpressionTest extends TestCase {
|
|||
$keyvalue = KeyValue::setEqualTo('_measurement', 'test_measurement')
|
||||
->andEqualTo('_field', 'user')
|
||||
->or('count', '>=', '1')
|
||||
->and('user', '==', 'my_username')
|
||||
->and('user', KeyValue::EQUAL_TO, 'my_username')
|
||||
->orNotEqualTo('test', 'world');
|
||||
|
||||
$query = 'r._measurement == "test_measurement" and r._field == "user" or ' .
|
||||
|
|
|
|||
18
tests/Function/GroupFunctionTest.php
Normal file
18
tests/Function/GroupFunctionTest.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Function\Group;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class GroupFunctionTest extends TestCase {
|
||||
|
||||
public function testSimpleGroup()
|
||||
{
|
||||
$expression = new Group(['foo', 'bar'], 'by');
|
||||
|
||||
$query = '|> group(columns: ["foo", "bar"], mode: "by") ';
|
||||
|
||||
$this->assertEquals($query, $expression->__toString());
|
||||
}
|
||||
|
||||
}
|
||||
18
tests/Function/LimitFunctionTest.php
Normal file
18
tests/Function/LimitFunctionTest.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Function\Limit;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class LimitFunctionTest extends TestCase {
|
||||
|
||||
public function testSimpleLimit()
|
||||
{
|
||||
$expression = new Limit(1);
|
||||
|
||||
$query = '|> limit(n:1) ';
|
||||
|
||||
$this->assertEquals($query, $expression->__toString());
|
||||
}
|
||||
|
||||
}
|
||||
18
tests/Function/MapFunctionTest.php
Normal file
18
tests/Function/MapFunctionTest.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Function\Map;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MapFunctionTest extends TestCase {
|
||||
|
||||
public function testSimpleMap()
|
||||
{
|
||||
$expression = new Map('r with name: r.user');
|
||||
|
||||
$query = '|> map(fn: (r) => ({ r with name: r.user })) ';
|
||||
|
||||
$this->assertEquals($query, $expression->__toString());
|
||||
}
|
||||
|
||||
}
|
||||
18
tests/Function/SortFunctionTest.php
Normal file
18
tests/Function/SortFunctionTest.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Function\Sort;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class SortFunctionTest extends TestCase {
|
||||
|
||||
public function testSimpleSort()
|
||||
{
|
||||
$expression = new Sort(['foo', 'bar'], true);
|
||||
|
||||
$query = '|> sort(columns: ["foo", "bar"], desc: true) ';
|
||||
|
||||
$this->assertEquals($query, $expression->__toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -87,4 +87,25 @@ final class QueryBuilderTest extends TestCase {
|
|||
];
|
||||
}
|
||||
|
||||
public function testComplexQuery()
|
||||
{
|
||||
$queryBuilder = new QueryBuilder();
|
||||
$queryBuilder->fromBucket('test_bucket')
|
||||
->fromMeasurement('test_measurement')
|
||||
->addRangeStart('-3h')
|
||||
->addFilter(KeyValue::setEqualTo('_field', 'username'))
|
||||
->addMap('r with name: r.user')
|
||||
->addGroup(['_field', 'ip'])
|
||||
->addReduce(['count' => 'accumulator.count + 1'], ['count' => 0])
|
||||
->addFilter(KeyValue::setGreaterOrEqualTo('count', '1'));
|
||||
|
||||
$expectedQuery = 'from(bucket: "test_bucket") |> range(start: "-3h") ' .
|
||||
'|> reduce(fn: (r, accumulator) => ({count: accumulator.count + 1}), identity: {count: 0}) ' .
|
||||
'|> filter(fn: (r) => r._measurement == "test_measurement") |> filter(fn: (r) => r._field == "username") ' .
|
||||
'|> filter(fn: (r) => r.count >= "1") |> map(fn: (r) => ({ r with name: r.user })) ' .
|
||||
'|> group(columns: ["_field", "ip"], mode: "by") ';
|
||||
|
||||
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue