From b7aad42d273d8948dd73e48e615c39f074711b0c Mon Sep 17 00:00:00 2001 From: Percy Mamedy Date: Wed, 4 Dec 2019 10:45:28 +0400 Subject: [PATCH] Issue #7 - Added a precision method onto Target class. --- README.md | 6 +++++ src/Metrics/Target.php | 35 ++++++++++++++++++++++++++- src/Series/DataPoints.php | 4 +-- src/Series/Point.php | 5 ++-- tests/Feature/Renderer/PointsTest.php | 22 +++++++++++++++++ 5 files changed, 67 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4faeeff..2b4d234 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,12 @@ use OceanDBA\Graphitti\Metrics\Target; $target = Target::make('oceandba.server1.load', 'Server-load-1'); ``` +You may also add a precision to the Target object which will serve to round DataPoints when retrieved. + +```php +$target = Target::make('oceandba.server1.load', 'Server-load-1')->precision(2); +``` + The ```Target``` takes two parameters : 1) The path 2) An optional name for the Target diff --git a/src/Metrics/Target.php b/src/Metrics/Target.php index 9a10398..25b457c 100644 --- a/src/Metrics/Target.php +++ b/src/Metrics/Target.php @@ -27,16 +27,25 @@ class Target implements UrlParameter, Metric */ protected $target; + /** + * The final precision when rendering to DataPoints. + * + * @var int + */ + protected $precision; + /** * Target constructor. * * @param string $target * @param string $name + * @param int $precision */ - public function __construct(string $target, string $name = null) + public function __construct(string $target, string $name = null, int $precision = null) { $this->target = $target; $this->name = $name; + $this->precision = $precision; } /** @@ -74,6 +83,30 @@ public function name(): string return $this->name; } + /** + * Return the Precision used for this Target. + * + * @param int $precision + * + * @return self + */ + public function precision(int $precision): self + { + $this->precision = $precision; + + return $this; + } + + /** + * Return the Precision used for this Target. + * + * @return int|null + */ + public function getPrecision(): ?int + { + return $this->precision; + } + /** * Get the current value of the parameter as string for Graphite. * diff --git a/src/Series/DataPoints.php b/src/Series/DataPoints.php index b555fc0..b03a000 100644 --- a/src/Series/DataPoints.php +++ b/src/Series/DataPoints.php @@ -49,8 +49,8 @@ public function __construct(Target $target, Collection $points) public static function make(Target $target, array $data = null): self { $data = $data ?? []; - $points = collect($data['datapoints'] ?? [])->transform(function ($point) { - return Point::make($point[1], $point[0] ?? 0.0); + $points = collect($data['datapoints'] ?? [])->transform(function ($point) use ($target) { + return Point::make($point[1], $point[0] ?? 0.0, $target->getPrecision()); }); return new static($target, $points); diff --git a/src/Series/Point.php b/src/Series/Point.php index 4fa8fc8..5de6738 100644 --- a/src/Series/Point.php +++ b/src/Series/Point.php @@ -60,13 +60,14 @@ public function value(): float * * @param int $timestamp * @param float $value + * @param int $precision * * @return Point */ - public static function make(int $timestamp, float $value): self + public static function make(int $timestamp, float $value, int $precision = null): self { $time = Carbon::createFromTimestamp($timestamp); - $value = round($value, 2); + $value = is_null($precision) ? $value : round($value, $precision); return new static($time, $value); } diff --git a/tests/Feature/Renderer/PointsTest.php b/tests/Feature/Renderer/PointsTest.php index 33104bb..fc24292 100644 --- a/tests/Feature/Renderer/PointsTest.php +++ b/tests/Feature/Renderer/PointsTest.php @@ -27,6 +27,28 @@ public function test_it_returns_datapoints_collection() $this->assertCount(3, $dataPointsCollection); } + public function test_it_follows_defined_precision() + { + /** @var Points $points */ + $points = app(Points::class)->handler($this->mockPoints()); + $dataPointsCollection = $points->addTarget(Target::make('oceandba.server1.load', 'Server-load-1')->precision(2)) + ->addTarget(Target::make('oceandba.server2.load', 'Server-load-2')) + ->from('-1h') + ->until('now') + ->addParameter('maxDataPoints', 50) + ->render(); + + $this->assertEquals( + [0.54, 1.07, 0.0], + $dataPointsCollection->first()->points()->map->value()->all() + ); + + $this->assertEquals( + [0.48166666666666674, 0.6614285714285714, 0.7287499999999999], + $dataPointsCollection->last()->points()->map->value()->all() + ); + } + /** * Return mocks of datapoints. *