Add aggregateWindow() function to Query Builder

Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
davidarendsen 2022-09-01 09:49:14 +00:00
commit 075857a83b
5 changed files with 172 additions and 2 deletions

View file

@ -0,0 +1,89 @@
<?php
namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type;
use Arendsen\FluxQueryBuilder\Type\ArrayType;
use Arendsen\FluxQueryBuilder\Type\DurationType;
use Arendsen\FluxQueryBuilder\Type\FnType;
class AggregateWindow 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 $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->period = $period;
$this->offset = $offset;
$this->fn = $fn;
$this->location = $location;
$this->column = $column;
$this->timeSrc = $timeSrc;
$this->timeDst = $timeDst;
$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,
'fn' => new FnType($this->fn),
'location' => $this->location ? new Type($this->location) : null,
'column' => $this->column ? new Type($this->column) : null,
'timeSrc' => $this->timeSrc ? new Type($this->timeSrc) : null,
'timeDst' => $this->timeDst ? new Type($this->timeDst) : null,
'createEmpty' => !$this->createEmpty ? new Type($this->createEmpty) : null,
]));
return '|> aggregateWindow(' . $input . ') ';
}
}

View file

@ -5,6 +5,7 @@ namespace Arendsen\FluxQueryBuilder;
use DateTime;
use Exception;
use Arendsen\FluxQueryBuilder\Expression\KeyValue;
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
use Arendsen\FluxQueryBuilder\Functions\Filter;
use Arendsen\FluxQueryBuilder\Functions\From;
use Arendsen\FluxQueryBuilder\Functions\Range;
@ -27,11 +28,13 @@ class QueryBuilder
public const FLUX_PART_LIMIT = 'limit';
public const FLUX_PART_WINDOW = 'window';
public const FLUX_PART_UNWINDOW = 'unwindow';
public const FLUX_PART_AGGREGATEWINDOW = 'aggregateWindow';
public const PARTS = [
self::FLUX_PART_FROM,
self::FLUX_PART_RANGE,
self::FLUX_PART_REDUCE,
self::FLUX_PART_AGGREGATEWINDOW,
self::FLUX_PART_WINDOW,
self::FLUX_PART_FILTERS,
self::FLUX_PART_MAP,
@ -198,6 +201,24 @@ class QueryBuilder
return $this;
}
public function addAggregateWindow(
$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(
self::FLUX_PART_AGGREGATEWINDOW,
new AggregateWindow($every, $period, $offset, $fn, $location, $column, $timeSrc, $timeDst, $createEmpty)
);
return $this;
}
protected function addToQuery($key, $query)
{
$this->fluxQueryParts[$key] = $query;

16
src/Type/FnType.php Normal file
View file

@ -0,0 +1,16 @@
<?php
namespace Arendsen\FluxQueryBuilder\Type;
class FnType implements TypeInterface
{
public function __construct(string $value)
{
$this->value = $value;
}
public function __toString(): string
{
return $this->value;
}
}