mirror of
https://github.com/predis/predis.git
synced 2026-09-18 14:28:54 +00:00
Extended TimeSeries support by implementing TS.MGET command (#1225)
* Added support for new arguments for BITPOS, BITCOUNT commands (#1045) * Added support for new arguments for EXPIRE, EXPIREAT commands (#1046) * Extended core support by implementing SORT_RO command (#1044) * Added support for SORT_RO command * Codestyle fixes * Added command description --------- Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local> * fix deprecated call * Added support for container commands (#1049) * Added support for container commands FUNCTION LOAD, FUNCTION DELETE and FCALL * Changed ContainerInterface and AbstractContainer * Re-implement logic of abstract methods --------- Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local> * Added stream commands to KeyPrefixProcessor (#1051) Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local> * Fix return type of ReplicationInterface::getSlaves (#1111) * Codestyle fixes * Changed return annotation * Added support for TS.MGET command --------- Co-authored-by: Vladyslav Vildanov <vladyslavvildanov@Vladyslav-Vildanov-MacBook-Pro.local> Co-authored-by: Till Krüss <till@kruss.io> Co-authored-by: Stephan <glaubinix@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
2fbb7ffdfd
commit
c61af25f8a
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) 2009-2020 Daniele Alessandri
|
||||
* (c) 2021-2023 Till Krüss
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Predis\Client;
|
||||
use Predis\Command\Argument\TimeSeries\CommonArguments;
|
||||
use Predis\Command\Argument\TimeSeries\CreateArguments;
|
||||
use Predis\Command\Argument\TimeSeries\MGetArguments;
|
||||
|
||||
require __DIR__ . '/../../shared.php';
|
||||
|
||||
// Example of TS.MGET command usage:
|
||||
|
||||
// 1. Create time series
|
||||
$client = new Client();
|
||||
|
||||
$arguments = (new CreateArguments())
|
||||
->retention(60000)
|
||||
->duplicatePolicy(CommonArguments::POLICY_MAX)
|
||||
->labels('type', 'temp', 'sensor_id', 2, 'area_id', 32);
|
||||
|
||||
$client->tscreate('temperature:2:32', $arguments);
|
||||
$client->tscreate('temperature:2:33', $arguments);
|
||||
|
||||
// 2. Add samples into time series
|
||||
$client->tsadd('temperature:2:32', 123123123123, 27);
|
||||
$client->tsadd('temperature:2:33', 123123123124, 27);
|
||||
|
||||
// 3. Get sample from multiple time series matching given filter expression, with selected labels only
|
||||
$response = $client->tsmget((new MGetArguments())->selectedLabels('type'), 'type=temp');
|
||||
|
||||
echo "Sample from time series, with label = 'type':\n";
|
||||
print_r($response);
|
||||
@@ -33,6 +33,7 @@ use Predis\Command\Argument\TimeSeries\CreateArguments as TSCreateArguments;
|
||||
use Predis\Command\Argument\TimeSeries\DecrByArguments;
|
||||
use Predis\Command\Argument\TimeSeries\GetArguments;
|
||||
use Predis\Command\Argument\TimeSeries\IncrByArguments;
|
||||
use Predis\Command\Argument\TimeSeries\MGetArguments;
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Command\Container\FUNCTIONS;
|
||||
use Predis\Command\Container\Json\JSONDEBUG;
|
||||
@@ -246,6 +247,7 @@ use Predis\Command\Container\Search\FTCONFIG;
|
||||
* @method $this tsget(string $key, GetArguments $arguments = null)
|
||||
* @method $this tsincrby(string $key, float $value, ?IncrByArguments $arguments = null)
|
||||
* @method $this tsmadd(mixed ...$keyTimestampValue)
|
||||
* @method $this tsmget(MGetArguments $arguments, string ...$filterExpression)
|
||||
* @method $this zadd($key, array $membersAndScoresDictionary)
|
||||
* @method $this zcard($key)
|
||||
* @method $this zcount($key, $min, $max)
|
||||
|
||||
@@ -33,6 +33,7 @@ use Predis\Command\Argument\TimeSeries\CreateArguments as TSCreateArguments;
|
||||
use Predis\Command\Argument\TimeSeries\DecrByArguments;
|
||||
use Predis\Command\Argument\TimeSeries\GetArguments;
|
||||
use Predis\Command\Argument\TimeSeries\IncrByArguments;
|
||||
use Predis\Command\Argument\TimeSeries\MGetArguments;
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Command\Container\FUNCTIONS;
|
||||
use Predis\Command\Container\Json\JSONDEBUG;
|
||||
@@ -256,6 +257,7 @@ use Predis\Response\Status;
|
||||
* @method array tsget(string $key, GetArguments $arguments = null)
|
||||
* @method int tsincrby(string $key, float $value, ?IncrByArguments $arguments = null)
|
||||
* @method array tsmadd(mixed ...$keyTimestampValue)
|
||||
* @method array tsmget(MGetArguments $arguments, string ...$filterExpression)
|
||||
* @method string xadd(string $key, array $dictionary, string $id = '*', array $options = null)
|
||||
* @method int xdel(string $key, string ...$id)
|
||||
* @method int xlen(string $key)
|
||||
|
||||
@@ -96,6 +96,19 @@ class CommonArguments implements ArrayableArgument
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is used when a time series is a compaction.
|
||||
* With LATEST, TS.GET reports the compacted value of the latest, possibly partial, bucket.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function latest(): self
|
||||
{
|
||||
$this->arguments[] = 'LATEST';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
||||
@@ -12,33 +12,6 @@
|
||||
|
||||
namespace Predis\Command\Argument\TimeSeries;
|
||||
|
||||
use Predis\Command\Argument\ArrayableArgument;
|
||||
|
||||
class GetArguments implements ArrayableArgument
|
||||
class GetArguments extends CommonArguments
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $arguments = [];
|
||||
|
||||
/**
|
||||
* Is used when a time series is a compaction.
|
||||
* With LATEST, TS.GET reports the compacted value of the latest, possibly partial, bucket.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function latest(): self
|
||||
{
|
||||
$this->arguments[] = 'LATEST';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return $this->arguments;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) 2009-2020 Daniele Alessandri
|
||||
* (c) 2021-2023 Till Krüss
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Command\Argument\TimeSeries;
|
||||
|
||||
class MGetArguments extends CommonArguments
|
||||
{
|
||||
/**
|
||||
* Includes in the reply all label-value pairs representing metadata labels of the time series.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function withLabels(): self
|
||||
{
|
||||
$this->arguments[] = 'WITHLABELS';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a subset of the label-value pairs that represent metadata labels of the time series.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function selectedLabels(string ...$labels): self
|
||||
{
|
||||
array_push($this->arguments, 'SELECTED_LABELS', ...$labels);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) 2009-2020 Daniele Alessandri
|
||||
* (c) 2021-2023 Till Krüss
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Command\Redis\TimeSeries;
|
||||
|
||||
use Predis\Command\Command as RedisCommand;
|
||||
|
||||
class TSMGET extends RedisCommand
|
||||
{
|
||||
public function getId()
|
||||
{
|
||||
return 'TS.MGET';
|
||||
}
|
||||
|
||||
public function setArguments(array $arguments)
|
||||
{
|
||||
$processedArguments = [];
|
||||
$argumentsObject = array_shift($arguments);
|
||||
$commandArguments = $argumentsObject->toArray();
|
||||
|
||||
array_push($processedArguments, 'FILTER', ...$arguments);
|
||||
|
||||
parent::setArguments(array_merge(
|
||||
$commandArguments,
|
||||
$processedArguments
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -75,4 +75,14 @@ class CommonArgumentsTest extends TestCase
|
||||
|
||||
$this->assertSame(['ENCODING', CommonArguments::ENCODING_UNCOMPRESSED], $this->arguments->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreatesArgumentsWithLatestModifier(): void
|
||||
{
|
||||
$this->arguments->latest();
|
||||
|
||||
$this->assertSame(['LATEST'], $this->arguments->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) 2009-2020 Daniele Alessandri
|
||||
* (c) 2021-2023 Till Krüss
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Command\Argument\TimeSeries;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class GetArgumentsTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var GetArguments
|
||||
*/
|
||||
private $arguments;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->arguments = new GetArguments();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreatesArgumentsWithEncodingModifier(): void
|
||||
{
|
||||
$this->arguments->latest();
|
||||
|
||||
$this->assertSame(['LATEST'], $this->arguments->toArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) 2009-2020 Daniele Alessandri
|
||||
* (c) 2021-2023 Till Krüss
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Command\Argument\TimeSeries;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MGetArgumentsTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var MGetArguments
|
||||
*/
|
||||
private $arguments;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->arguments = new MGetArguments();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreatesArgumentsWithWithLabelsModifier(): void
|
||||
{
|
||||
$this->arguments->withLabels();
|
||||
|
||||
$this->assertSame(['WITHLABELS'], $this->arguments->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreatesArgumentsWithSelectedLabelsModifier(): void
|
||||
{
|
||||
$this->arguments->selectedLabels('label1', 'label2');
|
||||
|
||||
$this->assertSame(['SELECTED_LABELS', 'label1', 'label2'], $this->arguments->toArray());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Predis package.
|
||||
*
|
||||
* (c) 2009-2020 Daniele Alessandri
|
||||
* (c) 2021-2023 Till Krüss
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Predis\Command\Redis\TimeSeries;
|
||||
|
||||
use Predis\Command\Argument\TimeSeries\CommonArguments;
|
||||
use Predis\Command\Argument\TimeSeries\CreateArguments;
|
||||
use Predis\Command\Argument\TimeSeries\MGetArguments;
|
||||
use Predis\Command\Redis\PredisCommandTestCase;
|
||||
|
||||
class TSMGET_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedCommand(): string
|
||||
{
|
||||
return TSMGET::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedId(): string
|
||||
{
|
||||
return 'TSMGET';
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
* @dataProvider argumentsProvider
|
||||
*/
|
||||
public function testFilterArguments(array $actualArguments, array $expectedArguments): void
|
||||
{
|
||||
$command = $this->getCommand();
|
||||
$command->setArguments($actualArguments);
|
||||
|
||||
$this->assertSameValues($expectedArguments, $command->getArguments());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group disconnected
|
||||
*/
|
||||
public function testParseResponse(): void
|
||||
{
|
||||
$this->assertSame(1, $this->getCommand()->parseResponse(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group connected
|
||||
* @return void
|
||||
* @requiresRedisTimeSeriesVersion >= 1.0.0
|
||||
*/
|
||||
public function testGetSampleFromMultipleTimeSeriesMatchingGivenPattern(): void
|
||||
{
|
||||
$redis = $this->getClient();
|
||||
$expectedResponse = [
|
||||
['temperature:2:32', [['type', 'temp']], [123123123123, '27']],
|
||||
['temperature:2:33', [['type', 'temp']], [123123123124, '27']],
|
||||
];
|
||||
|
||||
$createArguments = (new CreateArguments())
|
||||
->retention(60000)
|
||||
->duplicatePolicy(CommonArguments::POLICY_MAX)
|
||||
->labels('type', 'temp', 'sensor_id', 2, 'area_id', 32);
|
||||
|
||||
$this->assertEquals(
|
||||
'OK',
|
||||
$redis->tscreate('temperature:2:32', $createArguments)
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
'OK',
|
||||
$redis->tscreate('temperature:2:33', $createArguments)
|
||||
);
|
||||
|
||||
$redis->tsadd('temperature:2:32', 123123123123, 27);
|
||||
$redis->tsadd('temperature:2:33', 123123123124, 27);
|
||||
|
||||
$this->assertEquals(
|
||||
$expectedResponse,
|
||||
$redis->tsmget((new MGetArguments())->selectedLabels('type'), 'type=temp')
|
||||
);
|
||||
}
|
||||
|
||||
public function argumentsProvider(): array
|
||||
{
|
||||
return [
|
||||
'with default arguments' => [
|
||||
[(new MGetArguments())->withLabels(), 'filterExpression1', 'filterExpression2'],
|
||||
['WITHLABELS', 'FILTER', 'filterExpression1', 'filterExpression2'],
|
||||
],
|
||||
'with LATEST modifier' => [
|
||||
[(new MGetArguments())->latest(), 'filterExpression1', 'filterExpression2'],
|
||||
['LATEST', 'FILTER', 'filterExpression1', 'filterExpression2'],
|
||||
],
|
||||
'with WITHLABELS modifier' => [
|
||||
[(new MGetArguments())->withLabels(), 'filterExpression1', 'filterExpression2'],
|
||||
['WITHLABELS', 'FILTER', 'filterExpression1', 'filterExpression2'],
|
||||
],
|
||||
'with SELECTED_LABELS modifier' => [
|
||||
[(new MGetArguments())->selectedLabels('label1', 'label2'), 'filterExpression1', 'filterExpression2'],
|
||||
['SELECTED_LABELS', 'label1', 'label2', 'FILTER', 'filterExpression1', 'filterExpression2'],
|
||||
],
|
||||
'with all modifiers' => [
|
||||
[(new MGetArguments())->latest()->selectedLabels('label1', 'label2'), 'filterExpression1', 'filterExpression2'],
|
||||
['LATEST', 'SELECTED_LABELS', 'label1', 'label2', 'FILTER', 'filterExpression1', 'filterExpression2'],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user