Add addCount, addFirst, addMax, and addMin functions

Signed-off-by: David Arendsen <darendsen@gamepoint.com>
This commit is contained in:
David Arendsen 2023-02-12 17:18:23 +01:00
commit fc99922700
15 changed files with 463 additions and 23 deletions

View file

@ -8,13 +8,17 @@ 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)
* [addCount()](functions/addCount.md)
* [addDuplicate()](functions/addDuplicate.md)
* [addFieldFilter()](functions/addFieldFilter.md)
* [addFirst()](functions/addFirst.md)
* [addGroup()](functions/addGroup.md)
* [addLast()](functions/addLast.md)
* [addLimit()](functions/addLimit.md)
* [addMap()](functions/addMap.md)
* [addMax()](functions/addMax.md)
* [addMean()](functions/addMean.md)
* [addMin()](functions/addMin.md)
* [addRangeStart()](functions/addRangeStart.md)
or [addRangeInBetween()](functions/addRangeInBetween.md)
* [addReduce()](functions/addReduce.md)

View file

@ -0,0 +1,39 @@
# Flux Query Builder Docs
## Functions &raquo; addCount()
### Parameters:
<table>
<tbody>
<tr>
<th>Name</th>
<th>Required</th>
<th>Data type</th>
<th>Description</th>
</tr>
<tr>
<td>column</td>
<td>No</td>
<td>string</td>
<td>Column to count values in and store the total count.</td>
</tr>
</tbody>
</table>
### Example
```php
->addCount("_value")
```
This will result in the following Flux function part:
```
|> count(column: "_value")
```
### Extra resources
* [Flux documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/count/)

View file

@ -0,0 +1,39 @@
# Flux Query Builder Docs
## Functions &raquo; addFirst()
### Parameters:
<table>
<tbody>
<tr>
<th>Name</th>
<th>Required</th>
<th>Data type</th>
<th>Description</th>
</tr>
<tr>
<td>column</td>
<td>No</td>
<td>string</td>
<td>Column to operate on. Default is '_value'.</td>
</tr>
</tbody>
</table>
### Example
```php
->addFirst("_value")
```
This will result in the following Flux function part:
```
|> first(column: "_value")
```
### Extra resources
* [Flux documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/first/)

39
docs/functions/addMax.md Normal file
View file

@ -0,0 +1,39 @@
# Flux Query Builder Docs
## Functions &raquo; addMax()
### Parameters:
<table>
<tbody>
<tr>
<th>Name</th>
<th>Required</th>
<th>Data type</th>
<th>Description</th>
</tr>
<tr>
<td>column</td>
<td>No</td>
<td>string</td>
<td>Column to return maximum values from. Default is '_value'.</td>
</tr>
</tbody>
</table>
### Example
```php
->addMax("_value")
```
This will result in the following Flux function part:
```
|> max(column: "_value")
```
### Extra resources
* [Flux documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/max/)

39
docs/functions/addMin.md Normal file
View file

@ -0,0 +1,39 @@
# Flux Query Builder Docs
## Functions &raquo; addMin()
### Parameters:
<table>
<tbody>
<tr>
<th>Name</th>
<th>Required</th>
<th>Data type</th>
<th>Description</th>
</tr>
<tr>
<td>column</td>
<td>No</td>
<td>string</td>
<td>Column to return minimum values from. Default is '_value'.</td>
</tr>
</tbody>
</table>
### Example
```php
->addMin("_value")
```
This will result in the following Flux function part:
```
|> min(column: "_value")
```
### Extra resources
* [Flux documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/min/)

View file

@ -4,19 +4,111 @@ namespace Arendsen\FluxQueryBuilder\Builder;
use Arendsen\FluxQueryBuilder\Builder\QueryBuilderInterface;
use Arendsen\FluxQueryBuilder\Functions\AggregateWindow;
use Arendsen\FluxQueryBuilder\Functions\Count;
use Arendsen\FluxQueryBuilder\Functions\Duplicate;
use Arendsen\FluxQueryBuilder\Functions\First;
use Arendsen\FluxQueryBuilder\Functions\Group;
use Arendsen\FluxQueryBuilder\Functions\Last;
use Arendsen\FluxQueryBuilder\Functions\Limit;
use Arendsen\FluxQueryBuilder\Functions\Map;
use Arendsen\FluxQueryBuilder\Functions\Max;
use Arendsen\FluxQueryBuilder\Functions\Mean;
use Arendsen\FluxQueryBuilder\Functions\Min;
use Arendsen\FluxQueryBuilder\Functions\Reduce;
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;
use Arendsen\FluxQueryBuilder\Functions\Sum;
use Arendsen\FluxQueryBuilder\Functions\Window;
trait Universe
{
public function addAggregateWindow($every, $fn, array $options = []): QueryBuilderInterface
{
$this->addToQuery(
new AggregateWindow($every, $fn, $options)
);
return $this;
}
public function addCount(?string $column = null): QueryBuilderInterface
{
$this->addToQuery(
new Count($column)
);
return $this;
}
public function addDuplicate(string $column, string $as): QueryBuilderInterface
{
$this->addToQuery(
new Duplicate($column, $as)
);
return $this;
}
public function addFirst(?string $column = null): QueryBuilderInterface
{
$this->addToQuery(
new First($column)
);
return $this;
}
public function addGroup(array $columns, $mode = 'by'): QueryBuilderInterface
{
$this->addToQuery(
new Group($columns, $mode)
);
return $this;
}
public function addLast(string $column = '_value'): QueryBuilderInterface
{
$this->addToQuery(
new Last($column)
);
return $this;
}
public function addLimit(int $limit, int $offset = 0): QueryBuilderInterface
{
$this->addToQuery(
new Limit($limit, $offset)
);
return $this;
}
public function addMap($query): QueryBuilderInterface
{
$this->addToQuery(
new Map($query)
);
return $this;
}
public function addMax(?string $column = null): QueryBuilderInterface
{
$this->addToQuery(
new Max($column)
);
return $this;
}
public function addMean(?string $column = ''): QueryBuilderInterface
{
$this->addToQuery(
new Mean($column)
);
return $this;
}
public function addMin(?string $column = null): QueryBuilderInterface
{
$this->addToQuery(
new Min($column)
);
return $this;
}
public function addReduce(array $settings, array $identity): QueryBuilderInterface
{
$this->addToQuery(
@ -33,50 +125,10 @@ trait Universe
return $this;
}
public function addMap($query): QueryBuilderInterface
public function addSum(string $column): QueryBuilderInterface
{
$this->addToQuery(
new Map($query)
);
return $this;
}
public function addGroup(array $columns, $mode = 'by'): QueryBuilderInterface
{
$this->addToQuery(
new Group($columns, $mode)
);
return $this;
}
public function addLimit(int $limit, int $offset = 0): QueryBuilderInterface
{
$this->addToQuery(
new Limit($limit, $offset)
);
return $this;
}
public function addWindow($every, array $options = []): QueryBuilderInterface
{
$this->addToQuery(
new Window($every, $options)
);
return $this;
}
public function addDuplicate(string $column, string $as): QueryBuilderInterface
{
$this->addToQuery(
new Duplicate($column, $as)
);
return $this;
}
public function addMean(?string $column = ''): QueryBuilderInterface
{
$this->addToQuery(
new Mean($column)
new Sum($column)
);
return $this;
}
@ -89,26 +141,10 @@ trait Universe
return $this;
}
public function addAggregateWindow($every, $fn, array $options = []): QueryBuilderInterface
public function addWindow($every, array $options = []): QueryBuilderInterface
{
$this->addToQuery(
new AggregateWindow($every, $fn, $options)
);
return $this;
}
public function addSum(string $column): QueryBuilderInterface
{
$this->addToQuery(
new Sum($column)
);
return $this;
}
public function addLast(string $column = '_value'): QueryBuilderInterface
{
$this->addToQuery(
new Last($column)
new Window($every, $options)
);
return $this;
}

27
src/Functions/Count.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type;
class Count extends Base
{
/**
* @var string|null $column
*/
private $column;
public function __construct(?string $column = null)
{
$this->column = $column;
}
public function __toString()
{
$params = new Type(array_filter([
'column' => $this->column
]));
return '|> count(' . $params . ') ';
}
}

27
src/Functions/First.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type;
class First extends Base
{
/**
* @var string|null $column
*/
private $column;
public function __construct(?string $column = null)
{
$this->column = $column;
}
public function __toString()
{
$params = new Type(array_filter([
'column' => $this->column
]));
return '|> first(' . $params . ') ';
}
}

27
src/Functions/Max.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type;
class Max extends Base
{
/**
* @var string|null $column
*/
private $column;
public function __construct(?string $column = null)
{
$this->column = $column;
}
public function __toString()
{
$params = new Type(array_filter([
'column' => $this->column
]));
return '|> max(' . $params . ') ';
}
}

27
src/Functions/Min.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace Arendsen\FluxQueryBuilder\Functions;
use Arendsen\FluxQueryBuilder\Type;
class Min extends Base
{
/**
* @var string|null $column
*/
private $column;
public function __construct(?string $column = null)
{
$this->column = $column;
}
public function __toString()
{
$params = new Type(array_filter([
'column' => $this->column
]));
return '|> min(' . $params . ') ';
}
}

View file

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Count;
use PHPUnit\Framework\TestCase;
final class CountFunctionTest extends TestCase
{
public function testSimpleCount()
{
$expression = new Count();
$query = '|> count() ';
$this->assertEquals($query, $expression->__toString());
}
public function testCountWithColumn()
{
$expression = new Count('_value');
$query = '|> count(column: "_value") ';
$this->assertEquals($query, $expression->__toString());
}
}

View file

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\First;
use PHPUnit\Framework\TestCase;
final class FirstFunctionTest extends TestCase
{
public function testSimpleFirst()
{
$expression = new First();
$query = '|> first() ';
$this->assertEquals($query, $expression->__toString());
}
public function testFirstWithColumn()
{
$expression = new First('something');
$query = '|> first(column: "something") ';
$this->assertEquals($query, $expression->__toString());
}
}

View file

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Max;
use PHPUnit\Framework\TestCase;
final class MaxFunctionTest extends TestCase
{
public function testSimpleMax()
{
$expression = new Max();
$query = '|> max() ';
$this->assertEquals($query, $expression->__toString());
}
public function testMaxWithColumn()
{
$expression = new Max('something');
$query = '|> max(column: "something") ';
$this->assertEquals($query, $expression->__toString());
}
}

View file

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace Tests\Functions;
use Arendsen\FluxQueryBuilder\Functions\Min;
use PHPUnit\Framework\TestCase;
final class MinFunctionTest extends TestCase
{
public function testSimpleMin()
{
$expression = new Min();
$query = '|> min() ';
$this->assertEquals($query, $expression->__toString());
}
public function testMinWithColumn()
{
$expression = new Min('something');
$query = '|> min(column: "something") ';
$this->assertEquals($query, $expression->__toString());
}
}

View file

@ -41,6 +41,11 @@ final class QueryBuilderTest extends TestCase
['20s', 'mean', ['timeDst' => '_time']],
'|> aggregateWindow(every: 20s, fn: mean, timeDst: "_time") '
],
'addCount' => [
'addCount',
['_value'],
'|> count(column: "_value") '
],
'addDuplicate' => [
'addDuplicate',
['old_name', 'new_name'],
@ -61,6 +66,11 @@ final class QueryBuilderTest extends TestCase
[['email', 'username']],
'|> filter(fn: (r) => r._field == "email" or r._field == "username") '
],
'addFirst' => [
'addFirst',
['something'],
'|> first(column: "something") '
],
'addGroup' => [
'addGroup',
[['_field', 'ip']],
@ -81,10 +91,20 @@ final class QueryBuilderTest extends TestCase
['r with name: r.user'],
'|> map(fn: (r) => ({ r with name: r.user })) '
],
'addMax' => [
'addMax',
[],
'|> max() '
],
'addMean' => [
'addMean',
[],
'|> mean() '
['something'],
'|> mean(column: "something") '
],
'addMin' => [
'addMin',
['something'],
'|> min(column: "something") '
],
'addRawFunction' => [
'addRawFunction',