Add mean() method to Query Builder
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
ff3c757543
commit
f2b3871b21
4 changed files with 74 additions and 3 deletions
29
src/Functions/Mean.php
Normal file
29
src/Functions/Mean.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Arendsen\FluxQueryBuilder\Functions;
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Type;
|
||||
use Arendsen\FluxQueryBuilder\Type\ArrayType;
|
||||
|
||||
class Mean extends Base
|
||||
{
|
||||
/**
|
||||
* @var string $column
|
||||
*/
|
||||
private $column;
|
||||
|
||||
public function __construct(string $column = '_value')
|
||||
{
|
||||
$this->column = $column;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
$input = new ArrayType(array_filter([
|
||||
'column' => !empty($this->column) && $this->column !== '_value' ?
|
||||
new Type($this->column) : null
|
||||
]));
|
||||
|
||||
return '|> mean(' . $input . ') ';
|
||||
}
|
||||
}
|
||||
|
|
@ -14,6 +14,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\Mean;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Window;
|
||||
|
||||
class QueryBuilder
|
||||
|
|
@ -27,6 +28,7 @@ class QueryBuilder
|
|||
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_UNWINDOW = 'unwindow';
|
||||
public const FLUX_PART_AGGREGATEWINDOW = 'aggregateWindow';
|
||||
|
||||
|
|
@ -35,6 +37,7 @@ class QueryBuilder
|
|||
self::FLUX_PART_RANGE,
|
||||
self::FLUX_PART_REDUCE,
|
||||
self::FLUX_PART_WINDOW,
|
||||
self::FLUX_PART_MEAN,
|
||||
self::FLUX_PART_FILTERS,
|
||||
self::FLUX_PART_MAP,
|
||||
self::FLUX_PART_SORT,
|
||||
|
|
@ -177,16 +180,25 @@ class QueryBuilder
|
|||
|
||||
public function addWindow($every, array $options = []): QueryBuilder
|
||||
{
|
||||
$this->addToQueryArray(
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_WINDOW,
|
||||
new Window($every, $options)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addMean(?string $column = '')
|
||||
{
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_MEAN,
|
||||
new Mean($column)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addUnWindow()
|
||||
{
|
||||
$this->addToQueryArray(
|
||||
$this->addToQuery(
|
||||
self::FLUX_PART_UNWINDOW,
|
||||
new Window('inf')
|
||||
);
|
||||
|
|
|
|||
29
tests/Functions/MeanFunctionTest.php
Normal file
29
tests/Functions/MeanFunctionTest.php
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Functions;
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Functions\Mean;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class MeanFunctionTest extends TestCase
|
||||
{
|
||||
public function testSimpleMean()
|
||||
{
|
||||
$expression = new Mean();
|
||||
|
||||
$query = '|> mean() ';
|
||||
|
||||
$this->assertEquals($query, $expression->__toString());
|
||||
}
|
||||
|
||||
public function testWithParameterColumn()
|
||||
{
|
||||
$expression = new Mean('test');
|
||||
|
||||
$query = '|> mean(column: "test") ';
|
||||
|
||||
$this->assertEquals($query, $expression->__toString());
|
||||
}
|
||||
}
|
||||
|
|
@ -123,12 +123,13 @@ final class QueryBuilderTest extends TestCase
|
|||
->addRangeStart(new DateTime('2022-08-12 17:31:00'))
|
||||
->addWindow('20s')
|
||||
->addReduce(['count' => new MathType('accumulator.count + 1')], ['count' => 0])
|
||||
->addMean()
|
||||
->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") ' .
|
||||
'|> window(every: 20s) |> mean() |> filter(fn: (r) => r._measurement == "test_measurement") ' .
|
||||
'|> filter(fn: (r) => r.count >= 1 and r.count2 >= 2) |> window(every: inf) ';
|
||||
|
||||
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
||||
|
|
|
|||
Loading…
Add table
editor.link_modal.header
Reference in a new issue