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
@@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Repositories\Metric;
use CachetHQ\Cachet\Models\Metric;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\Collection;
/**
* This is the abstract metric repository class.
@@ -60,4 +61,49 @@ abstract class AbstractMetricRepository
return $prefix.'metrics';
}
/**
* Return the query type.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
*
* @return string
*/
protected function getQueryType(Metric $metric)
{
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
return 'sum(metric_points.value * metric_points.counter) AS value';
} elseif ($metric->calc_type == Metric::CALC_AVG) {
return 'avg(metric_points.value * metric_points.counter) AS value';
} else {
return 'sum(metric_points.value * metric_points.counter) AS value';
}
}
/**
* Map the result set.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
* @param array $results
*
* @return \Illuminate\Support\Collection
*/
protected function mapResults(Metric $metric, array $results)
{
$results = Collection::make($results);
return $results->map(function ($point) use ($metric) {
if (!$point->value) {
$point->value = $metric->default_value;
}
if ($point->value === 0 && $metric->default_value != $value) {
$point->value = $metric->default_value;
}
$point->value = round($point->value, $metric->places);
return $point;
});
}
}