From c61af25f8aad6e9ec14169876b275bb4aa1b9caf Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Mon, 27 Mar 2023 11:25:23 +0300 Subject: [PATCH] Extended TimeSeries support by implementing TS.MGET command (#1225) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 * 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 * Added stream commands to KeyPrefixProcessor (#1051) Co-authored-by: Vladyslav Vildanov * Fix return type of ReplicationInterface::getSlaves (#1111) * Codestyle fixes * Changed return annotation * Added support for TS.MGET command --------- Co-authored-by: Vladyslav Vildanov Co-authored-by: Till Krüss Co-authored-by: Stephan --- examples/Commands/TimeSeries/ts_mget.php | 41 ++++++ src/ClientContextInterface.php | 2 + src/ClientInterface.php | 2 + .../Argument/TimeSeries/CommonArguments.php | 13 ++ .../Argument/TimeSeries/GetArguments.php | 29 +---- .../Argument/TimeSeries/MGetArguments.php | 40 ++++++ src/Command/Redis/TimeSeries/TSMGET.php | 37 ++++++ .../TimeSeries/CommonArgumentsTest.php | 10 ++ .../Argument/TimeSeries/GetArgumentsTest.php | 38 ------ .../Argument/TimeSeries/MGetArgumentsTest.php | 48 +++++++ .../Command/Redis/TimeSeries/TSMGET_Test.php | 120 ++++++++++++++++++ 11 files changed, 314 insertions(+), 66 deletions(-) create mode 100644 examples/Commands/TimeSeries/ts_mget.php create mode 100644 src/Command/Argument/TimeSeries/MGetArguments.php create mode 100644 src/Command/Redis/TimeSeries/TSMGET.php delete mode 100644 tests/Predis/Command/Argument/TimeSeries/GetArgumentsTest.php create mode 100644 tests/Predis/Command/Argument/TimeSeries/MGetArgumentsTest.php create mode 100644 tests/Predis/Command/Redis/TimeSeries/TSMGET_Test.php diff --git a/examples/Commands/TimeSeries/ts_mget.php b/examples/Commands/TimeSeries/ts_mget.php new file mode 100644 index 00000000..fc3df170 --- /dev/null +++ b/examples/Commands/TimeSeries/ts_mget.php @@ -0,0 +1,41 @@ +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); diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index f68ba8af..d1aef2d4 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -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) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index e5a66fc7..f7590634 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -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) diff --git a/src/Command/Argument/TimeSeries/CommonArguments.php b/src/Command/Argument/TimeSeries/CommonArguments.php index 54724288..b15d5dd4 100644 --- a/src/Command/Argument/TimeSeries/CommonArguments.php +++ b/src/Command/Argument/TimeSeries/CommonArguments.php @@ -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} */ diff --git a/src/Command/Argument/TimeSeries/GetArguments.php b/src/Command/Argument/TimeSeries/GetArguments.php index 6f2224be..765685cb 100644 --- a/src/Command/Argument/TimeSeries/GetArguments.php +++ b/src/Command/Argument/TimeSeries/GetArguments.php @@ -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; - } } diff --git a/src/Command/Argument/TimeSeries/MGetArguments.php b/src/Command/Argument/TimeSeries/MGetArguments.php new file mode 100644 index 00000000..231eedb2 --- /dev/null +++ b/src/Command/Argument/TimeSeries/MGetArguments.php @@ -0,0 +1,40 @@ +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; + } +} diff --git a/src/Command/Redis/TimeSeries/TSMGET.php b/src/Command/Redis/TimeSeries/TSMGET.php new file mode 100644 index 00000000..78c43d2b --- /dev/null +++ b/src/Command/Redis/TimeSeries/TSMGET.php @@ -0,0 +1,37 @@ +toArray(); + + array_push($processedArguments, 'FILTER', ...$arguments); + + parent::setArguments(array_merge( + $commandArguments, + $processedArguments + )); + } +} diff --git a/tests/Predis/Command/Argument/TimeSeries/CommonArgumentsTest.php b/tests/Predis/Command/Argument/TimeSeries/CommonArgumentsTest.php index 70efe4c3..88ecb334 100644 --- a/tests/Predis/Command/Argument/TimeSeries/CommonArgumentsTest.php +++ b/tests/Predis/Command/Argument/TimeSeries/CommonArgumentsTest.php @@ -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()); + } } diff --git a/tests/Predis/Command/Argument/TimeSeries/GetArgumentsTest.php b/tests/Predis/Command/Argument/TimeSeries/GetArgumentsTest.php deleted file mode 100644 index 73a68694..00000000 --- a/tests/Predis/Command/Argument/TimeSeries/GetArgumentsTest.php +++ /dev/null @@ -1,38 +0,0 @@ -arguments = new GetArguments(); - } - - /** - * @return void - */ - public function testCreatesArgumentsWithEncodingModifier(): void - { - $this->arguments->latest(); - - $this->assertSame(['LATEST'], $this->arguments->toArray()); - } -} diff --git a/tests/Predis/Command/Argument/TimeSeries/MGetArgumentsTest.php b/tests/Predis/Command/Argument/TimeSeries/MGetArgumentsTest.php new file mode 100644 index 00000000..7a78196d --- /dev/null +++ b/tests/Predis/Command/Argument/TimeSeries/MGetArgumentsTest.php @@ -0,0 +1,48 @@ +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()); + } +} diff --git a/tests/Predis/Command/Redis/TimeSeries/TSMGET_Test.php b/tests/Predis/Command/Redis/TimeSeries/TSMGET_Test.php new file mode 100644 index 00000000..a1986344 --- /dev/null +++ b/tests/Predis/Command/Redis/TimeSeries/TSMGET_Test.php @@ -0,0 +1,120 @@ +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'], + ], + ]; + } +}