Last hour of metric points
This commit is contained in:
@@ -51,9 +51,10 @@ class MetricsComposer
|
||||
|
||||
$metrics->map(function ($metric) use (&$metricData) {
|
||||
$metricData[$metric->id] = [
|
||||
'today' => $this->metricRepository->listPointsToday($metric),
|
||||
'week' => $this->metricRepository->listPointsForWeek($metric),
|
||||
'month' => $this->metricRepository->listPointsForMonth($metric),
|
||||
'last_hour' => $this->metricRepository->listPointsLastHour($metric),
|
||||
'today' => $this->metricRepository->listPointsToday($metric),
|
||||
'week' => $this->metricRepository->listPointsForWeek($metric),
|
||||
'month' => $this->metricRepository->listPointsForMonth($metric),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -52,9 +52,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => 'Sidste 12 timer',
|
||||
'weekly' => 'Uge',
|
||||
'monthly' => 'Måned',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => 'Sidste 12 timer',
|
||||
'weekly' => 'Uge',
|
||||
'monthly' => 'Måned',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -52,9 +52,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => 'Letzte 12 Stunden',
|
||||
'weekly' => 'Wöchentlich',
|
||||
'monthly' => 'Monatlich',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => 'Letzte 12 Stunden',
|
||||
'weekly' => 'Wöchentlich',
|
||||
'monthly' => 'Monatlich',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -52,9 +52,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => 'Last 12 Hours',
|
||||
'weekly' => 'Week',
|
||||
'monthly' => 'Month',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => 'Last 12 Hours',
|
||||
'weekly' => 'Week',
|
||||
'monthly' => 'Month',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -50,9 +50,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => 'Últimas 12 horas',
|
||||
'weekly' => 'Semana',
|
||||
'monthly' => 'Mes',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => 'Últimas 12 horas',
|
||||
'weekly' => 'Semana',
|
||||
'monthly' => 'Mes',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -53,9 +53,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => 'Last 12 Hours',
|
||||
'weekly' => 'Week',
|
||||
'monthly' => 'Month',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => 'Last 12 Hours',
|
||||
'weekly' => 'Week',
|
||||
'monthly' => 'Month',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -52,9 +52,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => 'Last 12 Hours',
|
||||
'weekly' => 'Week',
|
||||
'monthly' => 'Month',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => 'Last 12 Hours',
|
||||
'weekly' => 'Week',
|
||||
'monthly' => 'Month',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -52,9 +52,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => 'Ultime 12 ore',
|
||||
'weekly' => 'Settimana',
|
||||
'monthly' => 'Mese',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => 'Ultime 12 ore',
|
||||
'weekly' => 'Settimana',
|
||||
'monthly' => 'Mese',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -52,9 +52,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => 'Last 12 Hours',
|
||||
'weekly' => 'Week',
|
||||
'monthly' => 'Month',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => 'Last 12 Hours',
|
||||
'weekly' => 'Week',
|
||||
'monthly' => 'Month',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -52,9 +52,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => 'Laatste 12 uren',
|
||||
'weekly' => 'Week',
|
||||
'monthly' => 'Maand',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => 'Laatste 12 uren',
|
||||
'weekly' => 'Week',
|
||||
'monthly' => 'Maand',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -52,9 +52,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => 'Last 12 Hours',
|
||||
'weekly' => 'Week',
|
||||
'monthly' => 'Month',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => 'Last 12 Hours',
|
||||
'weekly' => 'Week',
|
||||
'monthly' => 'Month',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -52,9 +52,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => 'Últimas 12 horas',
|
||||
'weekly' => 'Semana',
|
||||
'monthly' => 'Mês',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => 'Últimas 12 horas',
|
||||
'weekly' => 'Semana',
|
||||
'monthly' => 'Mês',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -51,9 +51,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => 'Последние 12 часов',
|
||||
'weekly' => 'Неделя',
|
||||
'monthly' => 'Месяц',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => 'Последние 12 часов',
|
||||
'weekly' => 'Неделя',
|
||||
'monthly' => 'Месяц',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -52,9 +52,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => '最近12小时',
|
||||
'weekly' => '周',
|
||||
'monthly' => '月',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => '最近12小时',
|
||||
'weekly' => '周',
|
||||
'monthly' => '月',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -52,9 +52,10 @@ return [
|
||||
// Metrics
|
||||
'metrics' => [
|
||||
'filter' => [
|
||||
'hourly' => '最近12小時',
|
||||
'weekly' => '周',
|
||||
'monthly' => '月',
|
||||
'last_hour' => 'Last Hour',
|
||||
'hourly' => '最近12小時',
|
||||
'weekly' => '周',
|
||||
'monthly' => '月',
|
||||
],
|
||||
],
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
<div class="dropdown pull-right">
|
||||
<a href="javascript: void(0);" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class='filter'>{{ trans('cachet.metrics.filter.hourly') }}</span> <span class="caret"></span></a>
|
||||
<ul class="dropdown-menu dropdown-menu-right">
|
||||
<li><a href="#" data-filter-type="last_hour">{{ trans('cachet.metrics.filter.last_hour') }}</a></li>
|
||||
<li><a href="#" data-filter-type="today">{{ trans('cachet.metrics.filter.hourly') }}</a></li>
|
||||
<li><a href="#" data-filter-type="week">{{ trans('cachet.metrics.filter.weekly') }}</a></li>
|
||||
<li><a href="#" data-filter-type="month">{{ trans('cachet.metrics.filter.monthly') }}</a></li>
|
||||
|
||||
Reference in New Issue
Block a user