Rewrite the Metric repository implementation. Fixes #1900

This commit is contained in:
James Brooks
2016-06-09 14:38:13 +01:00
committed by James Brooks
parent 0e0a7d9db2
commit 74c646e2f4
9 changed files with 205 additions and 315 deletions

View File

@@ -15,6 +15,11 @@ use CachetHQ\Cachet\Dates\DateFactory;
use CachetHQ\Cachet\Models\Metric;
use DateInterval;
/**
* This is the metric repository class.
*
* @author James Brooks <james@alt-three.com>
*/
class MetricRepository
{
/**
@@ -50,22 +55,25 @@ class MetricRepository
*
* @param \CachetHQ\Cachet\Models\Metric $metric
*
* @return array
* @return \Illuminate\Support\Collection
*/
public function listPointsLastHour(Metric $metric)
{
$dateTime = $this->dates->make();
$points = [];
$pointKey = $dateTime->format('H:i');
$points = $this->repository->getPointsSinceMinutes($metric, 60)->pluck('value', 'key');
for ($i = 0; $i <= 60; $i++) {
$points[$pointKey] = $this->repository->getPointsLastHour($metric, 0, $i);
if (!$points->has($pointKey)) {
$points->put($pointKey, $metric->default_value);
}
$pointKey = $dateTime->sub(new DateInterval('PT1M'))->format('H:i');
}
return array_reverse($points);
return $points->sortBy(function ($point, $key) use ($points) {
return $key;
});
}
/**
@@ -79,17 +87,20 @@ class MetricRepository
public function listPointsToday(Metric $metric, $hours = 12)
{
$dateTime = $this->dates->make();
$points = [];
$pointKey = $dateTime->format('H:00');
$points = $this->repository->getPointsSinceHour($metric, $hours)->pluck('value', 'key');
for ($i = 0; $i <= $hours; $i++) {
$points[$pointKey] = $this->repository->getPointsByHour($metric, $i);
if (!$points->has($pointKey)) {
$points->put($pointKey, $metric->default_value);
}
$pointKey = $dateTime->sub(new DateInterval('PT1H'))->format('H:00');
}
return array_reverse($points);
return $points->sortBy(function ($point, $key) use ($points) {
return $key;
});
}
/**
@@ -102,17 +113,20 @@ class MetricRepository
public function listPointsForWeek(Metric $metric)
{
$dateTime = $this->dates->make();
$points = [];
$pointKey = $dateTime->format('D jS M');
$pointKey = $dateTime->format('Y-m-d');
$points = $this->repository->getPointsSinceDay($metric, 7)->pluck('value', 'key');
for ($i = 0; $i <= 7; $i++) {
$points[$pointKey] = $this->repository->getPointsForDayInWeek($metric, $i);
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('D jS M');
if (!$points->has($pointKey)) {
$points->put($pointKey, $metric->default_value);
}
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('Y-m-d');
}
return array_reverse($points);
return $points->sortBy(function ($point, $key) use ($points) {
return $key;
});
}
/**
@@ -125,18 +139,20 @@ class MetricRepository
public function listPointsForMonth(Metric $metric)
{
$dateTime = $this->dates->make();
$pointKey = $dateTime->format('Y-m-d');
$daysInMonth = $dateTime->format('t');
$points = [];
$pointKey = $dateTime->format('jS M');
$points = $this->repository->getPointsSinceDay($metric, $daysInMonth)->pluck('value', 'key');
for ($i = 0; $i <= $daysInMonth; $i++) {
$points[$pointKey] = $this->repository->getPointsForDayInWeek($metric, $i);
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('jS M');
if (!$points->has($pointKey)) {
$points->put($pointKey, $metric->default_value);
}
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('Y-m-d');
}
return array_reverse($points);
return $points->sortBy(function ($point, $key) use ($points) {
return $key;
});
}
}