allow inheriting from TimeSeries class#32
Closed
glaszig wants to merge 1 commit intopalicao:masterfrom
Closed
Conversation
Owner
|
Hi @glaszig, and thank you for your PRs! May I ask you which use case is solved by extending the class, which is not possible to solve using composition? |
Author
|
to quickly implement the patch in #33. i ended up copying the entire class. but nevermind, i moved to use a delegator/decorator. implementation for reference: <?php
namespace App;
use Redis;
use Palicao\PhpRedisTimeSeries\AggregationRule;
use Palicao\PhpRedisTimeSeries\Label;
use Palicao\PhpRedisTimeSeries\Metadata;
use Palicao\PhpRedisTimeSeries\Sample;
use Palicao\PhpRedisTimeSeries\TimeSeries;
use Palicao\PhpRedisTimeSeries\Client\RedisClient;
use Palicao\PhpRedisTimeSeries\Client\RedisConnectionParams;
use RedisException;
class RedisTimeSeries
{
protected $redis, $ts;
public function __construct()
{
$this->redis = new RedisClient(
new Redis(),
new RedisConnectionParams()
);
$this->ts = new TimeSeries($this->redis);
}
public function __call(string $name, array $args)
{
return call_user_func_array([$this->ts, $name], $args);
}
public function info(string $key): Metadata
{
$result = $this->redis->executeCommand(['TS.INFO', $key]);
$result = $this->parseInfo($result);
$labels = [];
foreach ($result['labels'] as $label) {
$labels[] = new Label($label[0], $label[1]);
}
$sourceKey = $result['sourceKey'] === false ? null : $result['sourceKey'];
$rules = [];
foreach ($result['rules'] as $rule) {
$rules[$rule[0]] = new AggregationRule($rule[2], $rule[1]);
}
return Metadata::fromRedis(
$result['lastTimestamp'],
$result['retentionTime'],
$result['chunkCount'],
$result['chunkSize'],
$labels,
$sourceKey,
$rules
);
}
protected function parseInfo($info)
{
$chunks = array_chunk($info, 2);
$props = [];
foreach ($chunks as $chunk) {
$props[$chunk[0]] = $chunk[1];
}
// maxSamplesPerChunk has been replaced with chunkSize in 1.4.4
// https://github.com/RedisTimeSeries/RedisTimeSeries/commit/5896a509e5ec30929c04cd96a7f8b2c9e9651ed9
$props['chunkSize'] = isset($props['chunkSize'])
? $props['chunkSize']
: $props['maxSamplesPerChunk']
;
return $props;
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
i needed to subclass
TimeSeriesto add functionality but then was bitten byprivateness of things as those properties are exclusive to the defining scope.this opens up the class to make it easier to quickly modify behavior.