Add addBottom function

Signed-off-by: David Arendsen <darendsen@gamepoint.com>
This commit is contained in:
David Arendsen 2023-03-08 19:20:26 +01:00
commit 9e2bde4217
5 changed files with 91 additions and 0 deletions

View file

@ -8,6 +8,7 @@ On this page you will find the methods you can use in the query builder.
or [fromBucket()](functions/fromBucket.md)
* [fromMeasurement()](functions/fromMeasurement.md)
* [addAggregateWindow()](functions/addAggregateWindow.md)
* [addBottom()](functions/addBottom.md)
* [addCount()](functions/addCount.md)
* [addDuplicate()](functions/addDuplicate.md)
* [addFieldFilter()](functions/addFieldFilter.md)

View file

@ -0,0 +1,45 @@
# Flux Query Builder Docs
## Functions &raquo; addBottom()
### Parameters:
<table>
<tbody>
<tr>
<th>Name</th>
<th>Required</th>
<th>Data type</th>
<th>Description</th>
</tr>
<tr>
<td>n</td>
<td>Yes</td>
<td>string</td>
<td>Number of rows to return from each input table.</td>
</tr>
<tr>
<td>columns</td>
<td>No</td>
<td>array</td>
<td>List of columns to sort by. Default is ["_value"].</td>
</tr>
</tbody>
</table>
### Example
```php
->addBottom(2, ['_value'])
```
This will result in the following Flux function part:
```
|> bottom(n: 2, columns: ["_value"])
```
### Extra resources
* [Flux documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/bottom/)

View file

@ -4,6 +4,7 @@ namespace Arendsen\FluxQueryBuilder\Builder;
use Arendsen\FluxQueryBuilder\Builder\QueryBuilderInterface;
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
use Arendsen\FluxQueryBuilder\Functions\Bottom;
use Arendsen\FluxQueryBuilder\Functions\Count;
use Arendsen\FluxQueryBuilder\Functions\Duplicate;
use Arendsen\FluxQueryBuilder\Functions\First;
@ -30,6 +31,12 @@ trait Universe
return $this;
}
public function addBottom(int $n, array $columns = []): QueryBuilderInterface
{
$this->addToQuery(new Bottom($n, $columns));
return $this;
}
public function addCount(?string $column = null): QueryBuilderInterface
{
$this->addToQuery(

33
src/Functions/Bottom.php Normal file
View file

@ -0,0 +1,33 @@
<?php
namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type\ArrayType;
class Bottom extends Base
{
/**
* @var int $n
*/
private $n;
/**
* @var array $columns
*/
private $columns;
public function __construct($n, array $columns = [])
{
$this->n = $n;
$this->columns = $columns;
}
public function __toString()
{
$input = new ArrayType(array_filter([
'n' => $this->n,
'columns' => $this->columns ?: null,
]));
return '|> bottom(' . $input . ') ';
}
}

View file

@ -41,6 +41,11 @@ final class QueryBuilderTest extends TestCase
['20s', 'mean', ['timeDst' => '_time']],
'|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time") '
],
'addBottom' => [
'addBottom',
[2, ['_value']],
'|> bottom(n: 2, columns: ["_value"]) '
],
'addCount' => [
'addCount',
['_value'],