Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #7 #18

Merged
merged 1 commit into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 34 additions & 1 deletion src/Metrics/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Series/DataPoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/Series/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/Renderer/PointsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down