Add duplicate() function to Query Builder
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
f2b3871b21
commit
2cca3daa17
4 changed files with 72 additions and 3 deletions
35
src/Functions/Duplicate.php
Normal file
35
src/Functions/Duplicate.php
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Arendsen\FluxQueryBuilder\Functions;
|
||||||
|
|
||||||
|
use Arendsen\FluxQueryBuilder\Type;
|
||||||
|
use Arendsen\FluxQueryBuilder\Type\ArrayType;
|
||||||
|
|
||||||
|
class Duplicate extends Base
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string $column
|
||||||
|
*/
|
||||||
|
private $column;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string $as
|
||||||
|
*/
|
||||||
|
private $as;
|
||||||
|
|
||||||
|
public function __construct(string $column, string $as)
|
||||||
|
{
|
||||||
|
$this->column = $column;
|
||||||
|
$this->as = $as;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
$input = new ArrayType([
|
||||||
|
'column' => $this->column,
|
||||||
|
'as' => $this->as
|
||||||
|
]);
|
||||||
|
|
||||||
|
return '|> duplicate(' . $input . ') ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ use DateTime;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
|
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Duplicate;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Filter;
|
use Arendsen\FluxQueryBuilder\Functions\Filter;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\From;
|
use Arendsen\FluxQueryBuilder\Functions\From;
|
||||||
use Arendsen\FluxQueryBuilder\Functions\Range;
|
use Arendsen\FluxQueryBuilder\Functions\Range;
|
||||||
|
|
@ -29,6 +30,7 @@ class QueryBuilder
|
||||||
public const FLUX_PART_LIMIT = 'limit';
|
public const FLUX_PART_LIMIT = 'limit';
|
||||||
public const FLUX_PART_WINDOW = 'window';
|
public const FLUX_PART_WINDOW = 'window';
|
||||||
public const FLUX_PART_MEAN = 'mean';
|
public const FLUX_PART_MEAN = 'mean';
|
||||||
|
public const FLUX_PART_DUPLICATE = 'duplicate';
|
||||||
public const FLUX_PART_UNWINDOW = 'unwindow';
|
public const FLUX_PART_UNWINDOW = 'unwindow';
|
||||||
public const FLUX_PART_AGGREGATEWINDOW = 'aggregateWindow';
|
public const FLUX_PART_AGGREGATEWINDOW = 'aggregateWindow';
|
||||||
|
|
||||||
|
|
@ -38,6 +40,7 @@ class QueryBuilder
|
||||||
self::FLUX_PART_REDUCE,
|
self::FLUX_PART_REDUCE,
|
||||||
self::FLUX_PART_WINDOW,
|
self::FLUX_PART_WINDOW,
|
||||||
self::FLUX_PART_MEAN,
|
self::FLUX_PART_MEAN,
|
||||||
|
self::FLUX_PART_DUPLICATE,
|
||||||
self::FLUX_PART_FILTERS,
|
self::FLUX_PART_FILTERS,
|
||||||
self::FLUX_PART_MAP,
|
self::FLUX_PART_MAP,
|
||||||
self::FLUX_PART_SORT,
|
self::FLUX_PART_SORT,
|
||||||
|
|
@ -187,7 +190,16 @@ class QueryBuilder
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addMean(?string $column = '')
|
public function addDuplicate(string $column, string $as): QueryBuilder
|
||||||
|
{
|
||||||
|
$this->addToQuery(
|
||||||
|
self::FLUX_PART_DUPLICATE,
|
||||||
|
new Duplicate($column, $as)
|
||||||
|
);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addMean(?string $column = ''): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_MEAN,
|
self::FLUX_PART_MEAN,
|
||||||
|
|
@ -196,7 +208,7 @@ class QueryBuilder
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addUnWindow()
|
public function addUnWindow(): QueryBuilder
|
||||||
{
|
{
|
||||||
$this->addToQuery(
|
$this->addToQuery(
|
||||||
self::FLUX_PART_UNWINDOW,
|
self::FLUX_PART_UNWINDOW,
|
||||||
|
|
|
||||||
20
tests/Functions/DuplicateFunctionTest.php
Normal file
20
tests/Functions/DuplicateFunctionTest.php
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Functions;
|
||||||
|
|
||||||
|
use Arendsen\FluxQueryBuilder\Functions\Duplicate;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
final class DuplicateFunctionTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testSimpleDuplicate()
|
||||||
|
{
|
||||||
|
$expression = new Duplicate('tag', 'tag_dup');
|
||||||
|
|
||||||
|
$query = '|> duplicate(column: "tag", as: "tag_dup") ';
|
||||||
|
|
||||||
|
$this->assertEquals($query, $expression->__toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -124,12 +124,14 @@ final class QueryBuilderTest extends TestCase
|
||||||
->addWindow('20s')
|
->addWindow('20s')
|
||||||
->addReduce(['count' => new MathType('accumulator.count + 1')], ['count' => 0])
|
->addReduce(['count' => new MathType('accumulator.count + 1')], ['count' => 0])
|
||||||
->addMean()
|
->addMean()
|
||||||
|
->addDuplicate('tag', 'tag_dup')
|
||||||
->addFilter(KeyValue::setGreaterOrEqualTo('count', 1)->andGreaterOrEqualTo('count2', 2))
|
->addFilter(KeyValue::setGreaterOrEqualTo('count', 1)->andGreaterOrEqualTo('count2', 2))
|
||||||
->addUnWindow();
|
->addUnWindow();
|
||||||
|
|
||||||
$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}) ' .
|
||||||
'|> window(every: 20s) |> mean() |> filter(fn: (r) => r._measurement == "test_measurement") ' .
|
'|> window(every: 20s) |> mean() |> duplicate(column: "tag", as: "tag_dup") ' .
|
||||||
|
'|> filter(fn: (r) => r._measurement == "test_measurement") ' .
|
||||||
'|> filter(fn: (r) => r.count >= 1 and r.count2 >= 2) |> window(every: inf) ';
|
'|> filter(fn: (r) => r.count >= 1 and r.count2 >= 2) |> window(every: inf) ';
|
||||||
|
|
||||||
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
||||||
|
|
|
||||||
Loading…
Add table
editor.link_modal.header
Reference in a new issue