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

View File

@@ -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),
];
});
}

View File

@@ -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.
*

View File

@@ -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.
*

View File

@@ -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.
*

View File

@@ -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.
*

View File

@@ -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.
*

View File

@@ -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',
],
],

View File

@@ -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',
],
],

View File

@@ -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',
],
],

View File

@@ -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',
],
],

View File

@@ -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',
],
],

View File

@@ -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',
],
],

View File

@@ -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',
],
],

View File

@@ -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',
],
],

View File

@@ -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',
],
],

View File

@@ -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',
],
],

View File

@@ -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',
],
],

View File

@@ -51,9 +51,10 @@ return [
// Metrics
'metrics' => [
'filter' => [
'hourly' => 'Последние 12 часов',
'weekly' => 'Неделя',
'monthly' => 'Месяц',
'last_hour' => 'Last Hour',
'hourly' => 'Последние 12 часов',
'weekly' => 'Неделя',
'monthly' => 'Месяц',
],
],

View File

@@ -52,9 +52,10 @@ return [
// Metrics
'metrics' => [
'filter' => [
'hourly' => '最近12小时',
'weekly' => '',
'monthly' => '',
'last_hour' => 'Last Hour',
'hourly' => '最近12小时',
'weekly' => '',
'monthly' => '月',
],
],

View File

@@ -52,9 +52,10 @@ return [
// Metrics
'metrics' => [
'filter' => [
'hourly' => '最近12小時',
'weekly' => '',
'monthly' => '',
'last_hour' => 'Last Hour',
'hourly' => '最近12小時',
'weekly' => '',
'monthly' => '月',
],
],

View File

@@ -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>