Last hour of metric points

This commit is contained in:
James Brooks
2015-12-24 11:27:22 +00:00
parent 8c80565535
commit 3a0c052f8b
21 changed files with 199 additions and 45 deletions
@@ -15,6 +15,17 @@ use CachetHQ\Cachet\Models\Metric;
interface MetricInterface
{
/**
* Returns metrics for the last hour.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
* @param int $hour
* @param int $minute
*
* @return int
*/
public function getPointsLastHour(Metric $metric, $hour, $minute);
/**
* Returns metrics for a given hour.
*
@@ -43,6 +43,27 @@ class MetricRepository
$this->dateTimeZone = SettingFacade::get('app_timezone');
}
/**
* Returns all points as an array, for the last hour.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
*
* @return array
*/
public function listPointsLastHour(Metric $metric)
{
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
$points = [];
$pointKey = $dateTime->format('H:i');
for ($i = 0; $i <= 60; $i++) {
$points[$pointKey] = $this->repository->getPointsLastHour($metric, 0, $i);
$pointKey = $dateTime->sub(new DateInterval('PT1M'))->format('H:i');
}
return array_reverse($points);
}
/**
* Returns all points as an array, by x hours.
*
@@ -36,6 +36,38 @@ class MySqlRepository implements MetricInterface
$this->dateTimeZone = SettingFacade::get('app_timezone');
}
/**
* Returns metrics for the last hour.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
* @param int $hour
* @param int $minute
*
* @return int
*/
public function getPointsLastHour(Metric $metric, $hour, $minute)
{
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
$dateTime->sub(new DateInterval('PT'.$hour.'H'))->sub(new DateInterval('PT'.$minute.'M'));
$timeInterval = $dateTime->format('YmdHi');
$points = $metric->points()
->whereRaw('DATE_FORMAT(created_at, "%Y%m%d%H%i") = '.$timeInterval)
->groupBy(DB::raw('HOUR(created_at), MINUTE(created_at)'));
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
$value = $points->sum('value');
} elseif ($metric->calc_type == Metric::CALC_AVG) {
$value = $points->avg('value');
}
if ($value === 0 && $metric->default_value != $value) {
return $metric->default_value;
}
return round($value, $metric->places);
}
/**
* Returns metrics for a given hour.
*
@@ -36,6 +36,48 @@ class PgSqlRepository implements MetricInterface
$this->dateTimeZone = SettingFacade::get('app_timezone');
}
/**
* Returns metrics for the last hour.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
* @param int $hour
* @param int $minute
*
* @return int
*/
public function getPointsLastHour(Metric $metric, $hour, $minute)
{
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
$dateTime->sub(new DateInterval('PT'.$hour.'H'))->sub(new DateInterval('PT'.$minute.'M'));
$hourInterval = $dateTime->format('YmdHi');
// Default metrics calculations.
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
$queryType = 'sum(metric_points.value)';
} elseif ($metric->calc_type == Metric::CALC_AVG) {
$queryType = 'avg(metric_points.value)';
} else {
$queryType = 'sum(metric_points.value)';
}
$query = DB::select("select {$queryType} as aggregate FROM metrics JOIN metric_points ON metric_points.metric_id = metrics.id WHERE metric_points.metric_id = :metric_id AND to_char(metric_points.created_at, 'YYYYMMDDHHMI24') = :timestamp GROUP BY to_char(metric_points.created_at, 'HI')", [
'metric_id' => $metric->id,
'timestamp' => $hourInterval,
]);
if (isset($query[0])) {
$value = $query[0]->aggregate;
} else {
$value = 0;
}
if ($value === 0 && $metric->default_value != $value) {
return $metric->default_value;
}
return round($value, $metric->places);
}
/**
* Returns metrics for a given hour.
*
@@ -36,6 +36,38 @@ class SqliteRepository implements MetricInterface
$this->dateTimeZone = SettingFacade::get('app_timezone');
}
/**
* Returns metrics for the last hour.
*
* @param \CachetHQ\Cachet\Models\Metric $metric
* @param int $hour
* @param int $minute
*
* @return int
*/
public function getPointsLastHour(Metric $metric, $hour, $minute)
{
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
$dateTime->sub(new DateInterval('PT'.$hour.'H'))->sub(new DateInterval('PT'.$minute.'M'));
$hourInterval = $dateTime->format('YmdHi');
$points = $metric->points()
->whereRaw('strftime("%Y%m%d%H%i", created_at) = "'.$hourInterval.'"')
->groupBy(DB::raw('strftime("%H%i", created_at)'));
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
$value = $points->sum('value');
} elseif ($metric->calc_type == Metric::CALC_AVG) {
$value = $points->avg('value');
}
if ($value === 0 && $metric->default_value != $value) {
return $metric->default_value;
}
return round($value, $metric->places);
}
/**
* Returns metrics for a given hour.
*