Add sum() Flux function + docs
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
ee0ae4ca20
commit
2257e4f9cd
6 changed files with 108 additions and 0 deletions
|
|
@ -16,6 +16,7 @@ or [fromBucket()](functions/fromBucket.md)
|
|||
* [addRangeStart()](functions/addRangeStart.md)
|
||||
or [addRangeInBetween()](functions/addRangeInBetween.md)
|
||||
* [addReduce()](functions/addReduce.md)
|
||||
* [addSum()](functions/addSum.md)
|
||||
|
||||
Anything missing here? You can use this method to add anything to the query:
|
||||
* [addRawFunction()](functions/addRawFunction.md)
|
||||
39
docs/functions/addSum.md
Normal file
39
docs/functions/addSum.md
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# Flux Query Builder Docs
|
||||
|
||||
## Functions » addSum()
|
||||
|
||||
### Parameters:
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Required</th>
|
||||
<th>Data type</th>
|
||||
<th>Description</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>column</td>
|
||||
<td>Yes</td>
|
||||
<td>string</td>
|
||||
<td>Column to operate on. Default is "_value".</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
|
||||
### Example
|
||||
|
||||
```php
|
||||
->addSum('_value')
|
||||
```
|
||||
|
||||
This will result in the following Flux function part:
|
||||
|
||||
```
|
||||
|> sum(column: "_value")
|
||||
```
|
||||
|
||||
### Extra resources
|
||||
|
||||
* [Flux documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/sum/)
|
||||
|
|
@ -12,6 +12,7 @@ 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;
|
||||
|
||||
trait Universe
|
||||
{
|
||||
|
|
@ -94,4 +95,12 @@ trait Universe
|
|||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addSum(string $column): QueryBuilderInterface
|
||||
{
|
||||
$this->addToQuery(
|
||||
new Sum($column)
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
21
src/Functions/Sum.php
Normal file
21
src/Functions/Sum.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace Arendsen\FluxQueryBuilder\Functions;
|
||||
|
||||
class Sum extends Base
|
||||
{
|
||||
/**
|
||||
* @var string $column
|
||||
*/
|
||||
private $column;
|
||||
|
||||
public function __construct(string $column)
|
||||
{
|
||||
$this->column = $column;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return '|> sum(column: "' . (string)$this->column . '") ';
|
||||
}
|
||||
}
|
||||
20
tests/Functions/SumFunctionTest.php
Normal file
20
tests/Functions/SumFunctionTest.php
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Functions;
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Functions\Sum;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class SumFunctionTest extends TestCase
|
||||
{
|
||||
public function testSimpleSum()
|
||||
{
|
||||
$expression = new Sum('_value');
|
||||
|
||||
$query = '|> sum(column: "_value") ';
|
||||
|
||||
$this->assertEquals($query, $expression->__toString());
|
||||
}
|
||||
}
|
||||
|
|
@ -188,4 +188,22 @@ final class QueryBuilderTest extends TestCase
|
|||
|
||||
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
||||
}
|
||||
|
||||
public function testQueryWithSum()
|
||||
{
|
||||
$queryBuilder = new QueryBuilder();
|
||||
$queryBuilder->fromBucket('test_bucket')
|
||||
->addRangeStart(new DateTime('2022-08-12 17:31:00'))
|
||||
->fromMeasurement('test_measurement')
|
||||
->addKeyFilter(KeyFilter::setGreaterOrEqualTo('count', 1)->andGreaterOrEqualTo('count2', 2))
|
||||
->addSum('_value')
|
||||
->addFieldFilter(['username', 'ip']);
|
||||
|
||||
$expectedQuery = 'from(bucket: "test_bucket") |> range(start: time(v: 2022-08-12T17:31:00Z)) ' .
|
||||
'|> filter(fn: (r) => r._measurement == "test_measurement") ' .
|
||||
'|> filter(fn: (r) => r.count >= 1 and r.count2 >= 2) |> sum(column: "_value") ' .
|
||||
'|> filter(fn: (r) => r._field == "username" or r._field == "ip") ';
|
||||
|
||||
$this->assertEquals($expectedQuery, $queryBuilder->build());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
editor.link_modal.header
Reference in a new issue