Add unwindow function to Query Builder

Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
davidarendsen 2022-09-01 09:16:38 +00:00
commit 68ae5f2760
2 changed files with 35 additions and 3 deletions

View file

@ -26,17 +26,19 @@ class QueryBuilder
public const FLUX_PART_GROUP = 'group';
public const FLUX_PART_LIMIT = 'limit';
public const FLUX_PART_WINDOW = 'window';
public const FLUX_PART_UNWINDOW = 'unwindow';
public const PARTS = [
self::FLUX_PART_FROM,
self::FLUX_PART_RANGE,
self::FLUX_PART_REDUCE,
self::FLUX_PART_WINDOW,
self::FLUX_PART_FILTERS,
self::FLUX_PART_MAP,
self::FLUX_PART_SORT,
self::FLUX_PART_WINDOW,
self::FLUX_PART_GROUP,
self::FLUX_PART_LIMIT,
self::FLUX_PART_UNWINDOW,
];
public const REQUIRED_INPUT_FROM = 'from';
@ -187,6 +189,15 @@ class QueryBuilder
return $this;
}
public function addUnWindow()
{
$this->addToQueryArray(
self::FLUX_PART_UNWINDOW,
new Window('inf')
);
return $this;
}
protected function addToQuery($key, $query)
{
$this->fluxQueryParts[$key] = $query;

View file

@ -109,9 +109,30 @@ final class QueryBuilderTest extends TestCase
$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}) ' .
'|> filter(fn: (r) => r._measurement == "test_measurement") |> filter(fn: (r) => ' .
'|> window(every: 20s) |> 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 })) |> window(every: 20s) |> group(columns: ["_field", "ip"], mode: "by") ';
'|> map(fn: (r) => ({ r with name: r.user })) |> group(columns: ["_field", "ip"], mode: "by") ';
$this->assertEquals($expectedQuery, $queryBuilder->build());
}
public function testQueryWithUnWindow()
{
$queryBuilder = new QueryBuilder();
$queryBuilder->fromBucket('test_bucket')
->fromMeasurement('test_measurement')
->addRangeStart(new DateTime('2022-08-12 17:31:00'))
->addFieldFilter(['username', 'ip'])
->addWindow('20s')
->addReduce(['count' => new MathType('accumulator.count + 1')], ['count' => 0])
->addFilter(KeyValue::setGreaterOrEqualTo('count', 1)->andGreaterOrEqualTo('count2', 2))
->addUnWindow();
$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}) ' .
'|> window(every: 20s) |> 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) |> window(every: inf) ';
$this->assertEquals($expectedQuery, $queryBuilder->build());
}