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] 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); + } +}