Add window() function to Query Builder
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
9bea683e89
commit
ab03c2b56e
6 changed files with 141 additions and 1 deletions
|
|
@ -39,6 +39,8 @@ composer test
|
|||
|
||||
## Coding style
|
||||
|
||||
Run the following commands to check the coding style. We're using the PSR12 standard.
|
||||
|
||||
```
|
||||
composer check
|
||||
composer format
|
||||
|
|
|
|||
85
src/Functions/Window.php
Normal file
85
src/Functions/Window.php
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
namespace Arendsen\FluxQueryBuilder\Functions;
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Type;
|
||||
use Arendsen\FluxQueryBuilder\Type\ArrayType;
|
||||
use Arendsen\FluxQueryBuilder\Type\DurationType;
|
||||
|
||||
class Window extends Base
|
||||
{
|
||||
/**
|
||||
* @var string $every
|
||||
*/
|
||||
private $every;
|
||||
|
||||
/**
|
||||
* @var string|null $period
|
||||
*/
|
||||
private $period;
|
||||
|
||||
/**
|
||||
* @var string|null $offset
|
||||
*/
|
||||
private $offset;
|
||||
|
||||
/**
|
||||
* @var string|null $location
|
||||
*/
|
||||
private $location;
|
||||
|
||||
/**
|
||||
* @var string|null $timeColumn
|
||||
*/
|
||||
private $timeColumn;
|
||||
|
||||
/**
|
||||
* @var string|null $startColumn
|
||||
*/
|
||||
private $startColumn;
|
||||
|
||||
/**
|
||||
* @var string|null $stopColumn
|
||||
*/
|
||||
private $stopColumn;
|
||||
|
||||
/**
|
||||
* @var bool $createEmpty
|
||||
*/
|
||||
private $createEmpty;
|
||||
|
||||
public function __construct(
|
||||
$every,
|
||||
?string $period = null,
|
||||
?string $offset = null,
|
||||
?string $location = null,
|
||||
?string $timeColumn = null,
|
||||
?string $startColumn = null,
|
||||
?string $stopColumn = null,
|
||||
bool $createEmpty = false
|
||||
) {
|
||||
$this->every = $every;
|
||||
$this->period = $period;
|
||||
$this->offset = $offset;
|
||||
$this->location = $location;
|
||||
$this->timeColumn = $timeColumn;
|
||||
$this->startColumn = $startColumn;
|
||||
$this->stopColumn = $stopColumn;
|
||||
$this->createEmpty = $createEmpty;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
$input = new ArrayType(array_filter([
|
||||
'every' => new DurationType($this->every),
|
||||
'period' => $this->period ? new DurationType($this->period) : null,
|
||||
'offset' => $this->offset ? new DurationType($this->offset) : null,
|
||||
'location' => $this->location ? new Type($this->location) : null,
|
||||
'timeColumn' => $this->timeColumn ? new Type($this->timeColumn) : null,
|
||||
'startColumn' => $this->startColumn ? new Type($this->startColumn) : null,
|
||||
'stopColumn' => $this->stopColumn ? new Type($this->stopColumn) : null,
|
||||
'createEmpty' => $this->createEmpty ? new Type($this->createEmpty) : null,
|
||||
]));
|
||||
return '|> window(' . $input . ') ';
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@ use Arendsen\FluxQueryBuilder\Functions\Sort;
|
|||
use Arendsen\FluxQueryBuilder\Functions\Map;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Group;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Limit;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Window;
|
||||
|
||||
class QueryBuilder
|
||||
{
|
||||
|
|
@ -24,6 +25,7 @@ class QueryBuilder
|
|||
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 PARTS = [
|
||||
self::FLUX_PART_FROM,
|
||||
|
|
@ -32,6 +34,7 @@ class QueryBuilder
|
|||
self::FLUX_PART_FILTERS,
|
||||
self::FLUX_PART_MAP,
|
||||
self::FLUX_PART_SORT,
|
||||
self::FLUX_PART_WINDOW,
|
||||
self::FLUX_PART_GROUP,
|
||||
self::FLUX_PART_LIMIT,
|
||||
];
|
||||
|
|
@ -167,6 +170,23 @@ class QueryBuilder
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function addWindow(
|
||||
$every,
|
||||
?string $period = null,
|
||||
?string $offset = null,
|
||||
?string $location = null,
|
||||
?string $timeColumn = null,
|
||||
?string $startColumn = null,
|
||||
?string $stopColumn = null,
|
||||
bool $createEmpty = false
|
||||
): QueryBuilder {
|
||||
$this->addToQueryArray(
|
||||
self::FLUX_PART_WINDOW,
|
||||
new Window($every, $period, $offset, $location, $timeColumn, $startColumn, $stopColumn, $createEmpty)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function addToQuery($key, $query)
|
||||
{
|
||||
$this->fluxQueryParts[$key] = $query;
|
||||
|
|
|
|||
|
|
@ -39,6 +39,8 @@ class Type
|
|||
return new BooleanType($this->value);
|
||||
case 'array':
|
||||
return new ArrayType($this->value, $this->settings);
|
||||
case 'NULL':
|
||||
return 'null';
|
||||
default:
|
||||
return (string)$this->value;
|
||||
}
|
||||
|
|
|
|||
30
tests/Functions/WindowFunctionTest.php
Normal file
30
tests/Functions/WindowFunctionTest.php
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Functions;
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Functions\Window;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class WindowFunctionTest extends TestCase
|
||||
{
|
||||
public function testSimpleWindow()
|
||||
{
|
||||
$expression = new Window('20s');
|
||||
|
||||
$query = '|> window(every: 20s) ';
|
||||
|
||||
$this->assertEquals($query, $expression->__toString());
|
||||
}
|
||||
|
||||
public function testAllParameters()
|
||||
{
|
||||
$expression = new Window('20s', 'every', '0s', 'location', '_time', '_start', '_stop', true);
|
||||
|
||||
$query = '|> window(every: 20s, period: every, offset: 0s, location: "location", ' .
|
||||
'timeColumn: "_time", startColumn: "_start", stopColumn: "_stop", createEmpty: true) ';
|
||||
|
||||
$this->assertEquals($query, $expression->__toString());
|
||||
}
|
||||
}
|
||||
|
|
@ -103,6 +103,7 @@ final class QueryBuilderTest extends TestCase
|
|||
->addFieldFilter(['username', 'ip'])
|
||||
->addMap('r with name: r.user')
|
||||
->addGroup(['_field', 'ip'])
|
||||
->addWindow('20s')
|
||||
->addReduce(['count' => new MathType('accumulator.count + 1')], ['count' => 0])
|
||||
->addFilter(KeyValue::setGreaterOrEqualTo('count', 1)->andGreaterOrEqualTo('count2', 2));
|
||||
|
||||
|
|
@ -110,7 +111,7 @@ final class QueryBuilderTest extends TestCase
|
|||
'|> reduce(fn: (r, accumulator) => ({count: accumulator.count + 1}), identity: {count: 0}) ' .
|
||||
'|> 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) ' .
|
||||
'|> map(fn: (r) => ({ r with name: r.user })) |> group(columns: ["_field", "ip"], mode: "by") ';
|
||||
'|> map(fn: (r) => ({ r with name: r.user })) |> window(every: 20s) |> group(columns: ["_field", "ip"], mode: "by") ';
|
||||
|
||||
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
editor.link_modal.header
Reference in a new issue