Add DateTime option to Range function
Signed-off-by: davidarendsen <davidarendsen@hey.com>
This commit is contained in:
parent
41cdf265dc
commit
15a3f8fcde
6 changed files with 71 additions and 11 deletions
|
|
@ -18,4 +18,13 @@ final class FormattersTest extends TestCase {
|
|||
$this->assertEquals($expected, Formatters::toFluxArrayString($array));
|
||||
}
|
||||
|
||||
public function testDateTimeToString()
|
||||
{
|
||||
$dateTime = new DateTime('2022-08-12 17:31:00');
|
||||
|
||||
$expected = 'time(v: 2022-08-12T17:31:00Z)';
|
||||
|
||||
$this->assertEquals($expected, Formatters::dateTimeToString($dateTime));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use Arendsen\FluxQueryBuilder\Exception\FunctionRequiredSettingMissingException;
|
||||
use Arendsen\FluxQueryBuilder\Formatters;
|
||||
use Arendsen\FluxQueryBuilder\Functions\Range;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
|
|
@ -25,6 +26,18 @@ final class RangeFunctionTest extends TestCase {
|
|||
$this->assertEquals($query, $expression->__toString());
|
||||
}
|
||||
|
||||
public function testRangeInBetween()
|
||||
{
|
||||
$expression = new Range([
|
||||
'start' => new DateTime('2022-08-12 17:31:00'),
|
||||
'stop' => new DateTime('2022-08-12 18:31:00'),
|
||||
]);
|
||||
|
||||
$expected = '|> range(start: time(v: 2022-08-12T17:31:00Z), stop: time(v: 2022-08-12T18:31:00Z)) ';
|
||||
|
||||
$this->assertEquals($expected, $expression->__toString());
|
||||
}
|
||||
|
||||
public function testThrowsExceptionWhenStartIsMissing()
|
||||
{
|
||||
$this->expectException(FunctionRequiredSettingMissingException::class);
|
||||
|
|
|
|||
|
|
@ -92,14 +92,14 @@ final class QueryBuilderTest extends TestCase {
|
|||
$queryBuilder = new QueryBuilder();
|
||||
$queryBuilder->fromBucket('test_bucket')
|
||||
->fromMeasurement('test_measurement')
|
||||
->addRangeStart('-3h')
|
||||
->addRangeStart(new DateTime('2022-08-12 17:31:00'))
|
||||
->addFilter(KeyValue::setEqualTo('_field', 'username'))
|
||||
->addMap('r with name: r.user')
|
||||
->addGroup(['_field', 'ip'])
|
||||
->addReduce(['count' => 'accumulator.count + 1'], ['count' => 0])
|
||||
->addFilter(KeyValue::setGreaterOrEqualTo('count', 1)->andGreaterOrEqualTo('count2', 2));
|
||||
|
||||
$expectedQuery = 'from(bucket: "test_bucket") |> range(start: -3h) ' .
|
||||
$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}) ' .
|
||||
'|> filter(fn: (r) => r._measurement == "test_measurement") |> filter(fn: (r) => r._field == "username") ' .
|
||||
'|> filter(fn: (r) => r.count >= 1 and r.count2 >= 2) |> map(fn: (r) => ({ r with name: r.user })) ' .
|
||||
|
|
|
|||
Loading…
Add table
editor.link_modal.header
Reference in a new issue