diff --git a/app/Composers/MetricsComposer.php b/app/Composers/MetricsComposer.php index bb1e5c3d..b471db99 100644 --- a/app/Composers/MetricsComposer.php +++ b/app/Composers/MetricsComposer.php @@ -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), ]; }); } diff --git a/app/Repositories/Metric/MetricInterface.php b/app/Repositories/Metric/MetricInterface.php index bde75e47..b355192f 100644 --- a/app/Repositories/Metric/MetricInterface.php +++ b/app/Repositories/Metric/MetricInterface.php @@ -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. * diff --git a/app/Repositories/Metric/MetricRepository.php b/app/Repositories/Metric/MetricRepository.php index 3f0e9d8b..7f1d6f54 100644 --- a/app/Repositories/Metric/MetricRepository.php +++ b/app/Repositories/Metric/MetricRepository.php @@ -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. * diff --git a/app/Repositories/Metric/MySqlRepository.php b/app/Repositories/Metric/MySqlRepository.php index 8c766ac1..261c6b20 100644 --- a/app/Repositories/Metric/MySqlRepository.php +++ b/app/Repositories/Metric/MySqlRepository.php @@ -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. * diff --git a/app/Repositories/Metric/PgSqlRepository.php b/app/Repositories/Metric/PgSqlRepository.php index fb505c0a..f54d3202 100644 --- a/app/Repositories/Metric/PgSqlRepository.php +++ b/app/Repositories/Metric/PgSqlRepository.php @@ -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. * diff --git a/app/Repositories/Metric/SqliteRepository.php b/app/Repositories/Metric/SqliteRepository.php index c2ee70ea..d002865c 100644 --- a/app/Repositories/Metric/SqliteRepository.php +++ b/app/Repositories/Metric/SqliteRepository.php @@ -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. * diff --git a/resources/lang/da/cachet.php b/resources/lang/da/cachet.php index 7c43feb0..61a70b94 100755 --- a/resources/lang/da/cachet.php +++ b/resources/lang/da/cachet.php @@ -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', ], ], diff --git a/resources/lang/de/cachet.php b/resources/lang/de/cachet.php index f3af1682..a7540466 100755 --- a/resources/lang/de/cachet.php +++ b/resources/lang/de/cachet.php @@ -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', ], ], diff --git a/resources/lang/en/cachet.php b/resources/lang/en/cachet.php index 51ecea28..99ad7a37 100755 --- a/resources/lang/en/cachet.php +++ b/resources/lang/en/cachet.php @@ -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', ], ], diff --git a/resources/lang/es/cachet.php b/resources/lang/es/cachet.php index 2f48e582..7cc7f441 100755 --- a/resources/lang/es/cachet.php +++ b/resources/lang/es/cachet.php @@ -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', ], ], diff --git a/resources/lang/fr/cachet.php b/resources/lang/fr/cachet.php index e785f0c4..eed2d7d5 100755 --- a/resources/lang/fr/cachet.php +++ b/resources/lang/fr/cachet.php @@ -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', ], ], diff --git a/resources/lang/id/cachet.php b/resources/lang/id/cachet.php index ed077b15..5090e6b0 100755 --- a/resources/lang/id/cachet.php +++ b/resources/lang/id/cachet.php @@ -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', ], ], diff --git a/resources/lang/it/cachet.php b/resources/lang/it/cachet.php index ec144247..1309f98e 100644 --- a/resources/lang/it/cachet.php +++ b/resources/lang/it/cachet.php @@ -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', ], ], diff --git a/resources/lang/ko/cachet.php b/resources/lang/ko/cachet.php index beff7088..5d7a83f2 100755 --- a/resources/lang/ko/cachet.php +++ b/resources/lang/ko/cachet.php @@ -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', ], ], diff --git a/resources/lang/nl/cachet.php b/resources/lang/nl/cachet.php index 4a9ee3bf..9b2792d8 100755 --- a/resources/lang/nl/cachet.php +++ b/resources/lang/nl/cachet.php @@ -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', ], ], diff --git a/resources/lang/pl/cachet.php b/resources/lang/pl/cachet.php index daa5c361..cc2728ce 100755 --- a/resources/lang/pl/cachet.php +++ b/resources/lang/pl/cachet.php @@ -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', ], ], diff --git a/resources/lang/pt-BR/cachet.php b/resources/lang/pt-BR/cachet.php index 0601371b..6be63801 100755 --- a/resources/lang/pt-BR/cachet.php +++ b/resources/lang/pt-BR/cachet.php @@ -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', ], ], diff --git a/resources/lang/ru/cachet.php b/resources/lang/ru/cachet.php index 295d0a60..afee203f 100644 --- a/resources/lang/ru/cachet.php +++ b/resources/lang/ru/cachet.php @@ -51,9 +51,10 @@ return [ // Metrics 'metrics' => [ 'filter' => [ - 'hourly' => 'Последние 12 часов', - 'weekly' => 'Неделя', - 'monthly' => 'Месяц', + 'last_hour' => 'Last Hour', + 'hourly' => 'Последние 12 часов', + 'weekly' => 'Неделя', + 'monthly' => 'Месяц', ], ], diff --git a/resources/lang/zh-CN/cachet.php b/resources/lang/zh-CN/cachet.php index a22d2008..f199b77c 100755 --- a/resources/lang/zh-CN/cachet.php +++ b/resources/lang/zh-CN/cachet.php @@ -52,9 +52,10 @@ return [ // Metrics 'metrics' => [ 'filter' => [ - 'hourly' => '最近12小时', - 'weekly' => '周', - 'monthly' => '月', + 'last_hour' => 'Last Hour', + 'hourly' => '最近12小时', + 'weekly' => '周', + 'monthly' => '月', ], ], diff --git a/resources/lang/zh-TW/cachet.php b/resources/lang/zh-TW/cachet.php index f1c368da..fe522062 100644 --- a/resources/lang/zh-TW/cachet.php +++ b/resources/lang/zh-TW/cachet.php @@ -52,9 +52,10 @@ return [ // Metrics 'metrics' => [ 'filter' => [ - 'hourly' => '最近12小時', - 'weekly' => '周', - 'monthly' => '月', + 'last_hour' => 'Last Hour', + 'hourly' => '最近12小時', + 'weekly' => '周', + 'monthly' => '月', ], ], diff --git a/resources/views/partials/metrics.blade.php b/resources/views/partials/metrics.blade.php index d0d5ec65..8985bffa 100644 --- a/resources/views/partials/metrics.blade.php +++ b/resources/views/partials/metrics.blade.php @@ -15,6 +15,7 @@