mirror of
https://github.com/predis/predis.git
synced 2026-09-07 08:58:33 +00:00
Extended TimeSeries support by implementing TS.ADD command (#1198)
* 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 Time Series commands, TSCREATE command * Fixed param annotation * Added support for TS.ADD 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
c88d8ebce3
commit
08e252a580
@@ -0,0 +1,38 @@
|
||||
<?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\AddArguments;
|
||||
use Predis\Command\Argument\TimeSeries\CommonArguments;
|
||||
use Predis\Command\Argument\TimeSeries\CreateArguments;
|
||||
|
||||
require __DIR__ . '/../../shared.php';
|
||||
|
||||
// Example of TS.CREATE command usage:
|
||||
|
||||
// 1. Create time series
|
||||
$client = new Client();
|
||||
|
||||
$arguments = (new CreateArguments())
|
||||
->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}";
|
||||
@@ -27,6 +27,7 @@ use Predis\Command\Argument\Search\SugGetArguments;
|
||||
use Predis\Command\Argument\Search\SynUpdateArguments;
|
||||
use Predis\Command\Argument\Server\LimitOffsetCount;
|
||||
use Predis\Command\Argument\Server\To;
|
||||
use Predis\Command\Argument\TimeSeries\AddArguments;
|
||||
use Predis\Command\Argument\TimeSeries\CreateArguments as TSCreateArguments;
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Command\Container\FUNCTIONS;
|
||||
@@ -233,6 +234,7 @@ use Predis\Command\Container\Search\FTCONFIG;
|
||||
* @method $this topklist(string $key, bool $withCount = false)
|
||||
* @method $this topkquery(string $key, ...$items)
|
||||
* @method $this topkreserve(string $key, int $topK, int $width = 8, int $depth = 7, float $decay = 0.9)
|
||||
* @method $this tsadd(string $key, int $timestamp, float $value, ?AddArguments $arguments = null)
|
||||
* @method $this tscreate(string $key, ?TSCreateArguments $arguments = null)
|
||||
* @method $this zadd($key, array $membersAndScoresDictionary)
|
||||
* @method $this zcard($key)
|
||||
|
||||
@@ -27,6 +27,7 @@ use Predis\Command\Argument\Search\SugGetArguments;
|
||||
use Predis\Command\Argument\Search\SynUpdateArguments;
|
||||
use Predis\Command\Argument\Server\LimitOffsetCount;
|
||||
use Predis\Command\Argument\Server\To;
|
||||
use Predis\Command\Argument\TimeSeries\AddArguments;
|
||||
use Predis\Command\Argument\TimeSeries\CreateArguments as TSCreateArguments;
|
||||
use Predis\Command\CommandInterface;
|
||||
use Predis\Command\Container\FUNCTIONS;
|
||||
@@ -243,6 +244,7 @@ use Predis\Response\Status;
|
||||
* @method array topklist(string $key, bool $withCount = false)
|
||||
* @method array topkquery(string $key, ...$items)
|
||||
* @method Status topkreserve(string $key, int $topK, int $width = 8, int $depth = 7, float $decay = 0.9)
|
||||
* @method int tsadd(string $key, int $timestamp, float $value, ?AddArguments $arguments = null)
|
||||
* @method Status tscreate(string $key, ?TSCreateArguments $arguments = null)
|
||||
* @method string xadd(string $key, array $dictionary, string $id = '*', array $options = null)
|
||||
* @method int xdel(string $key, string ...$id)
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<?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 AddArguments extends CommonArguments
|
||||
{
|
||||
/**
|
||||
* Is overwrite key and database configuration for DUPLICATE_POLICY,
|
||||
* the policy for handling samples with identical timestamps.
|
||||
*
|
||||
* @param string $policy
|
||||
* @return $this
|
||||
*/
|
||||
public function onDuplicate(string $policy = self::POLICY_BLOCK): self
|
||||
{
|
||||
array_push($this->arguments, 'ON_DUPLICATE', $policy);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,9 @@ class CommonArguments implements ArrayableArgument
|
||||
public const POLICY_MAX = 'MAX';
|
||||
public const POLICY_SUM = 'SUM';
|
||||
|
||||
public const ENCODING_UNCOMPRESSED = 'UNCOMPRESSED';
|
||||
public const ENCODING_COMPRESSED = 'COMPRESSED';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
@@ -80,6 +83,19 @@ class CommonArguments implements ArrayableArgument
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the series samples encoding format.
|
||||
*
|
||||
* @param string $encoding
|
||||
* @return $this
|
||||
*/
|
||||
public function encoding(string $encoding = self::ENCODING_COMPRESSED): self
|
||||
{
|
||||
array_push($this->arguments, 'ENCODING', $encoding);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
||||
@@ -14,19 +14,4 @@ namespace Predis\Command\Argument\TimeSeries;
|
||||
|
||||
class CreateArguments extends CommonArguments
|
||||
{
|
||||
public const ENCODING_UNCOMPRESSED = 'UNCOMPRESSED';
|
||||
public const ENCODING_COMPRESSED = 'COMPRESSED';
|
||||
|
||||
/**
|
||||
* Specifies the series samples encoding format.
|
||||
*
|
||||
* @param string $encoding
|
||||
* @return $this
|
||||
*/
|
||||
public function encoding(string $encoding = self::ENCODING_COMPRESSED): self
|
||||
{
|
||||
array_push($this->arguments, 'ENCODING', $encoding);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?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 TSADD extends RedisCommand
|
||||
{
|
||||
public function getId()
|
||||
{
|
||||
return 'TS.ADD';
|
||||
}
|
||||
|
||||
public function setArguments(array $arguments)
|
||||
{
|
||||
[$key, $timestamp, $value] = $arguments;
|
||||
$commandArguments = (!empty($arguments[3])) ? $arguments[3]->toArray() : [];
|
||||
|
||||
parent::setArguments(array_merge(
|
||||
[$key, $timestamp, $value],
|
||||
$commandArguments
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?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 AddArgumentsTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var AddArguments
|
||||
*/
|
||||
private $arguments;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->arguments = new AddArguments();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreatesArgumentsWithOnDuplicateModifier(): void
|
||||
{
|
||||
$this->arguments->onDuplicate(CommonArguments::POLICY_LAST);
|
||||
|
||||
$this->assertSame(['ON_DUPLICATE', CommonArguments::POLICY_LAST], $this->arguments->toArray());
|
||||
}
|
||||
}
|
||||
@@ -65,4 +65,14 @@ class CommonArgumentsTest extends TestCase
|
||||
|
||||
$this->assertSame(['LABELS', 'label1', 1, 'label2', 2], $this->arguments->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreatesArgumentsWithEncodingModifier(): void
|
||||
{
|
||||
$this->arguments->encoding(CommonArguments::ENCODING_UNCOMPRESSED);
|
||||
|
||||
$this->assertSame(['ENCODING', CommonArguments::ENCODING_UNCOMPRESSED], $this->arguments->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
<?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\AddArguments;
|
||||
use Predis\Command\Argument\TimeSeries\CommonArguments;
|
||||
use Predis\Command\Argument\TimeSeries\CreateArguments;
|
||||
use Predis\Command\Redis\PredisCommandTestCase;
|
||||
|
||||
class TSADD_Test extends PredisCommandTestCase
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedCommand(): string
|
||||
{
|
||||
return TSADD::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getExpectedId(): string
|
||||
{
|
||||
return 'TSADD';
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 testAddSampleIntoTimeSeriesWithGivenConfiguration(): 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)
|
||||
);
|
||||
|
||||
$addArguments = (new AddArguments())
|
||||
->retention(31536000000);
|
||||
|
||||
$this->assertEquals(
|
||||
123123123123,
|
||||
$redis->tsadd('temperature:2:32', 123123123123, 27, $addArguments)
|
||||
);
|
||||
}
|
||||
|
||||
public function argumentsProvider(): array
|
||||
{
|
||||
return [
|
||||
'with default arguments' => [
|
||||
['key', 123123121321, 1.0],
|
||||
['key', 123123121321, 1.0],
|
||||
],
|
||||
'with RETENTION modifier' => [
|
||||
['key', 123123121321, 1.0, (new AddArguments())->retention(100)],
|
||||
['key', 123123121321, 1.0, 'RETENTION', 100],
|
||||
],
|
||||
'with ENCODING modifier' => [
|
||||
['key', 123123121321, 1.0, (new AddArguments())->encoding(CommonArguments::ENCODING_UNCOMPRESSED)],
|
||||
['key', 123123121321, 1.0, 'ENCODING', CommonArguments::ENCODING_UNCOMPRESSED],
|
||||
],
|
||||
'with CHUNK_SIZE modifier' => [
|
||||
['key', 123123121321, 1.0, (new AddArguments())->chunkSize(100)],
|
||||
['key', 123123121321, 1.0, 'CHUNK_SIZE', 100],
|
||||
],
|
||||
'with ON_DUPLICATE modifier' => [
|
||||
['key', 123123121321, 1.0, (new AddArguments())->onDuplicate(CommonArguments::POLICY_FIRST)],
|
||||
['key', 123123121321, 1.0, 'ON_DUPLICATE', CommonArguments::POLICY_FIRST],
|
||||
],
|
||||
'with all modifiers' => [
|
||||
['key', 123123121321, 1.0, (new AddArguments())->retention(100)->encoding(CommonArguments::ENCODING_UNCOMPRESSED)->chunkSize(100)->onDuplicate(CommonArguments::POLICY_FIRST)],
|
||||
['key', 123123121321, 1.0, 'RETENTION', 100, 'ENCODING', CommonArguments::ENCODING_UNCOMPRESSED, 'CHUNK_SIZE', 100, 'ON_DUPLICATE', CommonArguments::POLICY_FIRST],
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user