Rewrite the Metric repository implementation. Fixes #1900
This commit is contained in:
committed by
James Brooks
parent
0e0a7d9db2
commit
74c646e2f4
@@ -24,113 +24,56 @@ use Jenssegers\Date\Date;
|
||||
class PgSqlRepository extends AbstractMetricRepository implements MetricInterface
|
||||
{
|
||||
/**
|
||||
* Returns metrics for the last hour.
|
||||
* Returns metrics since given minutes.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
* @param int $hour
|
||||
* @param int $minute
|
||||
* @param int $minutes
|
||||
*
|
||||
* @return int
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function getPointsLastHour(Metric $metric, $hour, $minute)
|
||||
public function getPointsSinceMinutes(Metric $metric, $minutes)
|
||||
{
|
||||
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'))->sub(new DateInterval('PT'.$minute.'M'));
|
||||
|
||||
// Default metrics calculations.
|
||||
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
|
||||
$queryType = 'sum(metric_points.value * metric_points.counter)';
|
||||
} elseif ($metric->calc_type == Metric::CALC_AVG) {
|
||||
$queryType = 'avg(metric_points.value * metric_points.counter)';
|
||||
} else {
|
||||
$queryType = 'sum(metric_points.value * metric_points.counter)';
|
||||
}
|
||||
|
||||
$value = 0;
|
||||
$query = DB::select("select {$queryType} as value FROM {$this->getTableName()} m JOIN metric_points ON metric_points.metric_id = m.id WHERE m.id = :metricId AND to_char(metric_points.created_at, 'YYYYMMDDHH24MI') = :timeInterval GROUP BY to_char(metric_points.created_at, 'HHMI')", [
|
||||
'metricId' => $metric->id,
|
||||
'timeInterval' => $dateTime->format('YmdHi'),
|
||||
$queryType = $this->getQueryType($metric);
|
||||
$points = DB::select("SELECT to_char(metric_points.created_at, 'HH24:MI') AS key, {$queryType} FROM {$this->getTableName()} INNER JOIN metric_points ON metrics.id = metric_points.metric_id WHERE metrics.id = :metricId AND metric_points.created_at >= (NOW() - INTERVAL '{$minutes}' MINUTE) GROUP BY to_char(metric_points.created_at, 'HH24:MI') ORDER BY to_char(metric_points.created_at, 'HH24:MI')", [
|
||||
'metricId' => $metric->id,
|
||||
]);
|
||||
|
||||
if (isset($query[0])) {
|
||||
$value = $query[0]->value;
|
||||
}
|
||||
|
||||
if ($value === 0 && $metric->default_value != $value) {
|
||||
return $metric->default_value;
|
||||
}
|
||||
|
||||
return round($value, $metric->places);
|
||||
return $this->mapResults($metric, $points);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns metrics for a given hour.
|
||||
* Returns metrics since given hour.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
* @param int $hour
|
||||
*
|
||||
* @return int
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function getPointsByHour(Metric $metric, $hour)
|
||||
public function getPointsSinceHour(Metric $metric, $hour)
|
||||
{
|
||||
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'));
|
||||
|
||||
// Default metrics calculations.
|
||||
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
|
||||
$queryType = 'sum(metric_points.value * metric_points.counter)';
|
||||
} elseif ($metric->calc_type == Metric::CALC_AVG) {
|
||||
$queryType = 'avg(metric_points.value * metric_points.counter)';
|
||||
} else {
|
||||
$queryType = 'sum(metric_points.value * metric_points.counter)';
|
||||
}
|
||||
|
||||
$value = 0;
|
||||
$query = DB::select("select {$queryType} as value FROM {$this->getTableName()} m JOIN metric_points ON metric_points.metric_id = m.id WHERE metric_points.metric_id = :metricId AND to_char(metric_points.created_at, 'YYYYMMDDHH24') = :timeInterval GROUP BY to_char(metric_points.created_at, 'H')", [
|
||||
'metricId' => $metric->id,
|
||||
'timeInterval' => $dateTime->format('YmdH'),
|
||||
$queryType = $this->getQueryType($metric);
|
||||
$points = DB::select("SELECT to_char(metric_points.created_at, 'HH24:00') AS key, {$queryType} FROM {$this->getTableName()} INNER JOIN metric_points ON metrics.id = metric_points.metric_id WHERE metrics.id = :metricId AND metric_points.created_at >= (NOW() - INTERVAL '{$hour}' HOUR) GROUP BY to_char(metric_points.created_at, 'HH24:00') ORDER BY to_char(metric_points.created_at, 'HH24:00')", [
|
||||
'metricId' => $metric->id,
|
||||
]);
|
||||
|
||||
if (isset($query[0])) {
|
||||
$value = $query[0]->value;
|
||||
}
|
||||
|
||||
if ($value === 0 && $metric->default_value != $value) {
|
||||
return $metric->default_value;
|
||||
}
|
||||
|
||||
return round($value, $metric->places);
|
||||
return $this->mapResults($metric, $points);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns metrics for the week.
|
||||
* Returns metrics since given day.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
* @param int $day
|
||||
*
|
||||
* @return int
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function getPointsForDayInWeek(Metric $metric, $day)
|
||||
public function getPointsSinceDay(Metric $metric, $day)
|
||||
{
|
||||
$dateTime = (new Date())->sub(new DateInterval('P'.$day.'D'));
|
||||
|
||||
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
|
||||
$queryType = 'sum(mp.value * mp.counter) AS value';
|
||||
} elseif ($metric->calc_type == Metric::CALC_AVG) {
|
||||
$queryType = 'avg(mp.value * mp.counter) AS value';
|
||||
}
|
||||
|
||||
$value = 0;
|
||||
$points = DB::select("SELECT {$queryType} FROM {$this->getTableName()} m INNER JOIN metric_points mp ON m.id = mp.metric_id WHERE m.id = :metricId AND mp.created_at BETWEEN (mp.created_at - interval '1 week') AND (now() + interval '1 day') AND to_char(mp.created_at, 'YYYYMMDD') = :timeInterval GROUP BY to_char(mp.created_at, 'YYYYMMDD')", [
|
||||
'metricId' => $metric->id,
|
||||
'timeInterval' => $dateTime->format('Ymd'),
|
||||
$queryType = $this->getQueryType($metric);
|
||||
$points = DB::select("SELECT DATE(metric_points.created_at) AS key, {$queryType} FROM {$this->getTableName()} INNER JOIN metric_points ON metrics.id = metric_points.metric_id WHERE metrics.id = :metricId AND metric_points.created_at >= (DATE(NOW()) - INTERVAL '{$day}' DAY) GROUP BY DATE(metric_points.created_at) ORDER BY DATE(metric_points.created_at)", [
|
||||
'metricId' => $metric->id,
|
||||
]);
|
||||
|
||||
if (isset($points[0]) && !($value = $points[0]->value)) {
|
||||
$value = 0;
|
||||
}
|
||||
|
||||
if ($value === 0 && $metric->default_value != $value) {
|
||||
return $metric->default_value;
|
||||
}
|
||||
|
||||
return round($value, $metric->places);
|
||||
return $this->mapResults($metric, $points);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user