Change optional parameters of aggregateWindow into an array

Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
davidarendsen 2022-09-01 12:30:10 +00:00
commit 5951c9b8b1
4 changed files with 31 additions and 70 deletions

View file

@ -15,74 +15,35 @@ class AggregateWindow extends Base
private $every; private $every;
/** /**
* @var string|null $period * @var string $fn
*/ */
private $period; private $fn;
/** /**
* @var string|null $offset * @var array $options
*/ */
private $offset; private $options;
/** public function __construct($every, $fn, array $options = [])
* @var string|null $location {
*/
private $location;
/**
* @var string|null $column
*/
private $column;
/**
* @var string|null $timeSrc
*/
private $timeSrc;
/**
* @var string|null $timeDst
*/
private $timeDst;
/**
* @var bool $createEmpty
*/
private $createEmpty;
public function __construct(
$every,
?string $period = null,
?string $offset = null,
$fn,
?string $location = null,
?string $column = null,
?string $timeSrc = null,
?string $timeDst = null,
bool $createEmpty = true
) {
$this->every = $every; $this->every = $every;
$this->period = $period;
$this->offset = $offset;
$this->fn = $fn; $this->fn = $fn;
$this->location = $location; $this->options = $options;
$this->column = $column;
$this->timeSrc = $timeSrc;
$this->timeDst = $timeDst;
$this->createEmpty = $createEmpty;
} }
public function __toString() public function __toString()
{ {
$input = new ArrayType(array_filter([ $input = new ArrayType(array_filter([
'every' => new DurationType($this->every), 'every' => new DurationType($this->every),
'period' => $this->period ? new DurationType($this->period) : null, 'period' => isset($this->options['period']) ? new DurationType($this->options['period']) : null,
'offset' => $this->offset ? new DurationType($this->offset) : null, 'offset' => isset($this->options['offset']) ? new DurationType($this->options['offset']) : null,
'fn' => new FnType($this->fn), 'fn' => new FnType($this->fn),
'location' => $this->location ? new Type($this->location) : null, 'location' => isset($this->options['location']) ? new Type($this->options['location']) : null,
'column' => $this->column ? new Type($this->column) : null, 'column' => isset($this->options['column']) ? new Type($this->options['column']) : null,
'timeSrc' => $this->timeSrc ? new Type($this->timeSrc) : null, 'timeSrc' => isset($this->options['timeSrc']) ? new Type($this->options['timeSrc']) : null,
'timeDst' => $this->timeDst ? new Type($this->timeDst) : null, 'timeDst' => isset($this->options['timeDst']) ? new Type($this->options['timeDst']) : null,
'createEmpty' => !$this->createEmpty ? new Type($this->createEmpty) : null, 'createEmpty' => isset($this->options['createEmpty']) && !$this->options['createEmpty'] ?
new Type($this->options['createEmpty']) : null,
])); ]));
return '|> aggregateWindow(' . $input . ') '; return '|> aggregateWindow(' . $input . ') ';
} }

View file

@ -201,20 +201,11 @@ class QueryBuilder
return $this; return $this;
} }
public function addAggregateWindow( public function addAggregateWindow($every, $fn, array $options = []): QueryBuilder
$every, {
?string $period = null,
?string $offset = null,
$fn,
?string $location = null,
?string $column = null,
?string $timeSrc = null,
?string $timeDst = null,
bool $createEmpty = true
): QueryBuilder {
$this->addToQuery( $this->addToQuery(
self::FLUX_PART_AGGREGATEWINDOW, self::FLUX_PART_AGGREGATEWINDOW,
new AggregateWindow($every, $period, $offset, $fn, $location, $column, $timeSrc, $timeDst, $createEmpty) new AggregateWindow($every, $fn, $options)
); );
return $this; return $this;
} }

View file

@ -11,7 +11,7 @@ final class AggregateWindowFunctionTest extends TestCase
{ {
public function testSimpleWindow() public function testSimpleWindow()
{ {
$expression = new AggregateWindow('20s', null, null, 'mean'); $expression = new AggregateWindow('20s', 'mean');
$query = '|> aggregateWindow(every: 20s, fn: mean) '; $query = '|> aggregateWindow(every: 20s, fn: mean) ';
@ -20,7 +20,15 @@ final class AggregateWindowFunctionTest extends TestCase
public function testAllParameters() public function testAllParameters()
{ {
$expression = new AggregateWindow('20s', 'every', '0s', 'mean', 'location', '_value', '_stop', '_time', false); $expression = new AggregateWindow('20s', 'mean', [
'period' => 'every',
'offset' => '0s',
'location' => 'location',
'column' => '_value',
'timeSrc' => '_stop',
'timeDst' => '_time',
'createEmpty' => false
]);
$query = '|> aggregateWindow(every: 20s, period: every, offset: 0s, fn: mean, location: "location", ' . $query = '|> aggregateWindow(every: 20s, period: every, offset: 0s, fn: mean, location: "location", ' .
'column: "_value", timeSrc: "_stop", timeDst: "_time", createEmpty: false) '; 'column: "_value", timeSrc: "_stop", timeDst: "_time", createEmpty: false) ';

View file

@ -141,12 +141,13 @@ final class QueryBuilderTest extends TestCase
$queryBuilder->fromBucket('test_bucket') $queryBuilder->fromBucket('test_bucket')
->fromMeasurement('test_measurement') ->fromMeasurement('test_measurement')
->addRangeStart(new DateTime('2022-08-12 17:31:00')) ->addRangeStart(new DateTime('2022-08-12 17:31:00'))
->addAggregateWindow('20s', null, null, 'mean') ->addAggregateWindow('20s', 'mean', ['timeDst' => '_time'])
->addReduce(['count' => new MathType('accumulator.count + 1')], ['count' => 0]); ->addReduce(['count' => new MathType('accumulator.count + 1')], ['count' => 0]);
$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}) ' .
'|> filter(fn: (r) => r._measurement == "test_measurement") |> aggregateWindow(every: 20s, fn: mean) '; '|> filter(fn: (r) => r._measurement == "test_measurement") ' .
'|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time") ';
$this->assertEquals($expectedQuery, $queryBuilder->build()); $this->assertEquals($expectedQuery, $queryBuilder->build());
} }