From b41672ad0b6325e6b63f67ae377b3de5cbe20800 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Fri, 24 Mar 2023 15:59:50 +0200 Subject: [PATCH 1/3] Extended TimeSeries support by implementing TS.DECRBY command (#1223) 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.DECRBY command --------- Co-authored-by: Vladyslav Vildanov Co-authored-by: Till Krüss Co-authored-by: Stephan --- examples/Commands/TimeSeries/ts_decrby.php | 45 +++++ src/ClientContextInterface.php | 2 + src/ClientInterface.php | 2 + .../Argument/TimeSeries/DecrByArguments.php | 17 ++ src/Command/Redis/TimeSeries/TSDECRBY.php | 41 ++++ .../Redis/TimeSeries/TSDECRBY_Test.php | 184 ++++++++++++++++++ 6 files changed, 291 insertions(+) create mode 100644 examples/Commands/TimeSeries/ts_decrby.php create mode 100644 src/Command/Argument/TimeSeries/DecrByArguments.php create mode 100644 src/Command/Redis/TimeSeries/TSDECRBY.php create mode 100644 tests/Predis/Command/Redis/TimeSeries/TSDECRBY_Test.php diff --git a/examples/Commands/TimeSeries/ts_decrby.php b/examples/Commands/TimeSeries/ts_decrby.php new file mode 100644 index 00000000..80862c67 --- /dev/null +++ b/examples/Commands/TimeSeries/ts_decrby.php @@ -0,0 +1,45 @@ +retention(60000) + ->duplicatePolicy(CommonArguments::POLICY_MAX) + ->labels('sensor_id', 2, 'area_id', 32); + +$client->tscreate('temperature:2:32', $arguments); + +// 2. Add sample into newly created time series +$addArguments = (new AddArguments()) + ->retention(31536000000); + +$response = $client->tsadd('temperature:2:32', 123123123123, 27, $addArguments); + +echo "Timeseries was added with timestamp: {$response}\n"; + +// 3. Increasing value and timestamp +$client->tsdecrby('temperature:2:32', 1, (new DecrByArguments())->timestamp(123123123124)); +$response = $client->tsget('temperature:2:32'); + +echo "Decreased value to - {$response[1]} and timestamp to {$response[0]}"; diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index db855bbc..85dfb717 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -30,6 +30,7 @@ use Predis\Command\Argument\Server\To; use Predis\Command\Argument\TimeSeries\AddArguments; use Predis\Command\Argument\TimeSeries\AlterArguments as TSAlterArguments; 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\CommandInterface; @@ -240,6 +241,7 @@ use Predis\Command\Container\Search\FTCONFIG; * @method $this tsadd(string $key, int $timestamp, float $value, ?AddArguments $arguments = null) * @method $this tsalter(string $key, ?TSAlterArguments $arguments = null) * @method $this tscreate(string $key, ?TSCreateArguments $arguments = null) + * @method $this tsdecrby(string $key, float $value, ?DecrByArguments $arguments = null) * @method $this tsdel(string $key, int $fromTimestamp, int $toTimestamp) * @method $this tsget(string $key, GetArguments $arguments = null) * @method $this tsincrby(string $key, float $value, ?IncrByArguments $arguments = null) diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 364248d6..dd184720 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -30,6 +30,7 @@ use Predis\Command\Argument\Server\To; use Predis\Command\Argument\TimeSeries\AddArguments; use Predis\Command\Argument\TimeSeries\AlterArguments as TSAlterArguments; 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\CommandInterface; @@ -250,6 +251,7 @@ use Predis\Response\Status; * @method int tsadd(string $key, int $timestamp, float $value, ?AddArguments $arguments = null) * @method Status tsalter(string $key, ?TSAlterArguments $arguments = null) * @method Status tscreate(string $key, ?TSCreateArguments $arguments = null) + * @method int tsdecrby(string $key, float $value, ?DecrByArguments $arguments = null) * @method int tsdel(string $key, int $fromTimestamp, int $toTimestamp) * @method array tsget(string $key, GetArguments $arguments = null) * @method int tsincrby(string $key, float $value, ?IncrByArguments $arguments = null) diff --git a/src/Command/Argument/TimeSeries/DecrByArguments.php b/src/Command/Argument/TimeSeries/DecrByArguments.php new file mode 100644 index 00000000..c0da3f0b --- /dev/null +++ b/src/Command/Argument/TimeSeries/DecrByArguments.php @@ -0,0 +1,17 @@ +toArray() : []; + + parent::setArguments(array_merge( + [$key, $value], + $commandArguments + )); + } +} diff --git a/tests/Predis/Command/Redis/TimeSeries/TSDECRBY_Test.php b/tests/Predis/Command/Redis/TimeSeries/TSDECRBY_Test.php new file mode 100644 index 00000000..5c792c9b --- /dev/null +++ b/tests/Predis/Command/Redis/TimeSeries/TSDECRBY_Test.php @@ -0,0 +1,184 @@ +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 testDecrByDecreasesValueAndTimestampOfExistingSample(): void + { + $redis = $this->getClient(); + + $arguments = (new CreateArguments()) + ->retention(60000) + ->duplicatePolicy(CommonArguments::POLICY_MAX) + ->labels('sensor_id', 2, 'area_id', 32); + + $this->assertEquals( + 'OK', + $redis->tscreate('temperature:2:32', $arguments) + ); + + $addArguments = (new AddArguments()) + ->retention(31536000000); + + $this->assertEquals( + 123123123123, + $redis->tsadd('temperature:2:32', 123123123123, 27, $addArguments) + ); + + $this->assertEquals( + 123123123124, + $redis->tsdecrby('temperature:2:32', 1, (new DecrByArguments())->timestamp(123123123124)) + ); + } + + /** + * @group connected + * @return void + * @requiresRedisTimeSeriesVersion >= 1.0.0 + */ + public function testDecrByCreateNewSampleIfNotExists(): void + { + $redis = $this->getClient(); + + $arguments = (new CreateArguments()) + ->retention(60000) + ->duplicatePolicy(CommonArguments::POLICY_MAX) + ->labels('sensor_id', 2, 'area_id', 32); + + $this->assertEquals( + 'OK', + $redis->tscreate('temperature:2:32', $arguments) + ); + + $this->assertEquals( + 123123123123, + $redis->tsdecrby('temperature:2:32', 27, (new DecrByArguments())->timestamp(123123123123)) + ); + } + + /** + * @group connected + * @return void + * @requiresRedisTimeSeriesVersion >= 1.0.0 + */ + public function testThrowsExceptionOnOlderTimestampGiven(): void + { + $redis = $this->getClient(); + + $arguments = (new CreateArguments()) + ->retention(60000) + ->duplicatePolicy(CommonArguments::POLICY_MAX) + ->labels('sensor_id', 2, 'area_id', 32); + + $this->assertEquals( + 'OK', + $redis->tscreate('temperature:2:32', $arguments) + ); + + $addArguments = (new AddArguments()) + ->retention(31536000000); + + $this->assertEquals( + 123123123123, + $redis->tsadd('temperature:2:32', 123123123123, 27, $addArguments) + ); + + $this->expectException(ServerException::class); + $this->expectExceptionMessage('TSDB: for incrby/decrby, timestamp should be newer than the'); + + $redis->tsdecrby('temperature:2:32', 27, (new DecrByArguments())->timestamp(123123123122)); + } + + public function argumentsProvider(): array + { + return [ + 'with default arguments' => [ + ['key', 1.0], + ['key', 1.0], + ], + 'with TIMESTAMP modifier' => [ + ['key', 1.0, (new DecrByArguments())->timestamp(10)], + ['key', 1.0, 'TIMESTAMP', 10], + ], + 'with RETENTION modifier' => [ + ['key', 1.0, (new DecrByArguments())->retention(100)], + ['key', 1.0, 'RETENTION', 100], + ], + 'with UNCOMPRESSED modifier' => [ + ['key', 1.0, (new DecrByArguments())->uncompressed()], + ['key', 1.0, 'UNCOMPRESSED'], + ], + 'with CHUNK_SIZE modifier' => [ + ['key', 1.0, (new DecrByArguments())->chunkSize(100)], + ['key', 1.0, 'CHUNK_SIZE', 100], + ], + 'with LABELS modifier' => [ + ['key', 1.0, (new DecrByArguments())->labels('label1', 1, 'label2', 2)], + ['key', 1.0, 'LABELS', 'label1', 1, 'label2', 2], + ], + 'with all modifiers' => [ + ['key', 1.0, (new DecrByArguments())->timestamp(10)->retention(100)->uncompressed()->chunkSize(100)->labels('label1', 1, 'label2', 2)], + ['key', 1.0, 'TIMESTAMP', 10, 'RETENTION', 100, 'UNCOMPRESSED', 'CHUNK_SIZE', 100, 'LABELS', 'label1', 1, 'label2', 2], + ], + ]; + } +} From 2fbb7ffdfdeac853e3681b2a00f57e5d36d2b304 Mon Sep 17 00:00:00 2001 From: Vladyslav Vildanov <117659936+vladvildanov@users.noreply.github.com> Date: Fri, 24 Mar 2023 16:44:44 +0200 Subject: [PATCH 2/3] Extended TimeSeries support by implementing TS.MADD command (#1224) 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.MADD command * Fixed exception message --------- Co-authored-by: Vladyslav Vildanov Co-authored-by: Till Krüss Co-authored-by: Stephan --- examples/Commands/TimeSeries/ts_add.php | 2 +- examples/Commands/TimeSeries/ts_madd.php | 36 ++++++ src/ClientContextInterface.php | 1 + src/ClientInterface.php | 1 + src/Command/Redis/TimeSeries/TSMADD.php | 28 +++++ .../Command/Redis/TimeSeries/TSMADD_Test.php | 104 ++++++++++++++++++ 6 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 examples/Commands/TimeSeries/ts_madd.php create mode 100644 src/Command/Redis/TimeSeries/TSMADD.php create mode 100644 tests/Predis/Command/Redis/TimeSeries/TSMADD_Test.php diff --git a/examples/Commands/TimeSeries/ts_add.php b/examples/Commands/TimeSeries/ts_add.php index 9b63f1b7..c4713f09 100644 --- a/examples/Commands/TimeSeries/ts_add.php +++ b/examples/Commands/TimeSeries/ts_add.php @@ -17,7 +17,7 @@ use Predis\Command\Argument\TimeSeries\CreateArguments; require __DIR__ . '/../../shared.php'; -// Example of TS.CREATE command usage: +// Example of TS.ADD command usage: // 1. Create time series $client = new Client(); diff --git a/examples/Commands/TimeSeries/ts_madd.php b/examples/Commands/TimeSeries/ts_madd.php new file mode 100644 index 00000000..606c4f16 --- /dev/null +++ b/examples/Commands/TimeSeries/ts_madd.php @@ -0,0 +1,36 @@ +retention(60000) + ->duplicatePolicy(CommonArguments::POLICY_MAX) + ->labels('sensor_id', 2, 'area_id', 32); + +$client->tscreate('temperature:2:32', $arguments); +$client->tscreate('temperature:2:33', $arguments); + +// 2. Add samples into few time series +$response = $client->tsmadd('temperature:2:32', 123123123123, 27, 'temperature:2:33', 123123123124, 28); +$stringResponse = implode(', ', $response); + +echo "Samples was added to time series - timestamps: {$stringResponse}"; diff --git a/src/ClientContextInterface.php b/src/ClientContextInterface.php index 85dfb717..f68ba8af 100644 --- a/src/ClientContextInterface.php +++ b/src/ClientContextInterface.php @@ -245,6 +245,7 @@ use Predis\Command\Container\Search\FTCONFIG; * @method $this tsdel(string $key, int $fromTimestamp, int $toTimestamp) * @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 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 dd184720..e5a66fc7 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -255,6 +255,7 @@ use Predis\Response\Status; * @method int tsdel(string $key, int $fromTimestamp, int $toTimestamp) * @method array tsget(string $key, GetArguments $arguments = null) * @method int tsincrby(string $key, float $value, ?IncrByArguments $arguments = null) + * @method array tsmadd(mixed ...$keyTimestampValue) * @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/Redis/TimeSeries/TSMADD.php b/src/Command/Redis/TimeSeries/TSMADD.php new file mode 100644 index 00000000..08522469 --- /dev/null +++ b/src/Command/Redis/TimeSeries/TSMADD.php @@ -0,0 +1,28 @@ +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 testAddSamplesIntoFewTimeSeries(): void + { + $redis = $this->getClient(); + + $createArguments = (new CreateArguments()) + ->retention(60000) + ->duplicatePolicy(CommonArguments::POLICY_MAX) + ->labels('sensor_id', 2, 'area_id', 32); + + $this->assertEquals( + 'OK', + $redis->tscreate('temperature:2:32', $createArguments) + ); + + $this->assertEquals( + 'OK', + $redis->tscreate('temperature:2:33', $createArguments) + ); + + $this->assertEquals( + [123123123123, 123123123124], + $redis->tsmadd('temperature:2:32', 123123123123, 27, 'temperature:2:33', 123123123124, 28) + ); + } + + /** + * @group connected + * @return void + * @requiresRedisTimeSeriesVersion >= 1.0.0 + */ + public function testThrowsExceptionOnNonWrongArgumentsNumber(): void + { + $redis = $this->getClient(); + + $this->expectException(ServerException::class); + $this->expectExceptionMessage("ERR wrong number of arguments for 'TS.MADD' command"); + + $redis->tsmadd('temperature:2:32', 123123123123, 27, 'temperature:2:33', 123123123124); + } +} 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 3/3] 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'], + ], + ]; + } +}