From d8208be17927608c745151d2016eaf9d9da2b130 Mon Sep 17 00:00:00 2001 From: A Date: Mon, 15 Jan 2018 11:13:57 +0100 Subject: [PATCH] Prevents to select metric points in future. The metrics had problem with the points, if I post/save a metric point that is created in the future, it was selected. Example: It is 10:00 am, I post or save a metric point and it's 'created at' 10:30 am. Using the "Last 12 hours" the metric point would be selected. This update is applied on all the SQL queries, on minutes, hours, days. Related to CachetHQ/Cachet#2848 --- app/Repositories/Metric/MySqlRepository.php | 23 ++++++++++++++++--- app/Repositories/Metric/PgSqlRepository.php | 24 +++++++++++++++++--- app/Repositories/Metric/SqliteRepository.php | 24 +++++++++++++++++--- 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/app/Repositories/Metric/MySqlRepository.php b/app/Repositories/Metric/MySqlRepository.php index d3439236..567da504 100644 --- a/app/Repositories/Metric/MySqlRepository.php +++ b/app/Repositories/Metric/MySqlRepository.php @@ -32,7 +32,13 @@ class MySqlRepository extends AbstractMetricRepository implements MetricInterfac public function getPointsSinceMinutes(Metric $metric, $minutes) { $queryType = $this->getQueryType($metric); - $points = DB::select("SELECT DATE_FORMAT({$this->getMetricPointsTable()}.`created_at`, '%H:%i') AS `key`, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id WHERE {$this->getMetricsTable()}.id = :metricId AND {$this->getMetricPointsTable()}.`created_at` >= DATE_SUB(NOW(), INTERVAL :minutes MINUTE) GROUP BY HOUR({$this->getMetricPointsTable()}.`created_at`), MINUTE({$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [ + + $points = DB::select("SELECT DATE_FORMAT({$this->getMetricPointsTable()}.`created_at`, '%H:%i') AS `key`, {$queryType} " . + "FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id " . + "WHERE {$this->getMetricsTable()}.id = :metricId " . + "AND {$this->getMetricPointsTable()}.`created_at` >= DATE_SUB(NOW(), INTERVAL :minutes MINUTE) " . + "AND {$this->getMetricPointsTable()}.`created_at` <= NOW() " . + "GROUP BY HOUR({$this->getMetricPointsTable()}.`created_at`), MINUTE({$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [ 'metricId' => $metric->id, 'minutes' => $minutes, ]); @@ -51,7 +57,13 @@ class MySqlRepository extends AbstractMetricRepository implements MetricInterfac public function getPointsSinceHour(Metric $metric, $hour) { $queryType = $this->getQueryType($metric); - $points = DB::select("SELECT DATE_FORMAT({$this->getMetricPointsTable()}.`created_at`, '%H:00') AS `key`, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id WHERE {$this->getMetricsTable()}.id = :metricId AND {$this->getMetricPointsTable()}.`created_at` >= DATE_SUB(NOW(), INTERVAL :hour HOUR) GROUP BY HOUR({$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [ + $points = DB::select("SELECT DATE_FORMAT({$this->getMetricPointsTable()}.`created_at`, '%H:00') AS `key`, {$queryType} " . + "FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id " . + "WHERE {$this->getMetricsTable()}.id = :metricId " . + "AND {$this->getMetricPointsTable()}.`created_at` >= DATE_SUB(NOW(), INTERVAL :hour HOUR) " . + "AND {$this->getMetricPointsTable()}.`created_at` <= NOW() " . + "GROUP BY HOUR({$this->getMetricPointsTable()}.`created_at`) " . + "ORDER BY {$this->getMetricPointsTable()}.`created_at`", [ 'metricId' => $metric->id, 'hour' => $hour, ]); @@ -70,7 +82,12 @@ class MySqlRepository extends AbstractMetricRepository implements MetricInterfac public function getPointsSinceDay(Metric $metric, $day) { $queryType = $this->getQueryType($metric); - $points = DB::select("SELECT DATE_FORMAT({$this->getMetricPointsTable()}.`created_at`, '%Y-%m-%d') AS `key`, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id WHERE {$this->getMetricsTable()}.id = :metricId AND {$this->getMetricPointsTable()}.`created_at` >= DATE_SUB(NOW(), INTERVAL :day DAY) GROUP BY DATE({$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [ + $points = DB::select("SELECT DATE_FORMAT({$this->getMetricPointsTable()}.`created_at`, '%Y-%m-%d') AS `key`, {$queryType} " . + "FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id " . + "WHERE {$this->getMetricsTable()}.id = :metricId " . + "AND {$this->getMetricPointsTable()}.`created_at` >= DATE_SUB(NOW(), INTERVAL :day DAY) " . + "AND {$this->getMetricPointsTable()}.`created_at` <= NOW() " . + "GROUP BY DATE({$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [ 'metricId' => $metric->id, 'day' => $day, ]); diff --git a/app/Repositories/Metric/PgSqlRepository.php b/app/Repositories/Metric/PgSqlRepository.php index feec9d84..6ffdff53 100644 --- a/app/Repositories/Metric/PgSqlRepository.php +++ b/app/Repositories/Metric/PgSqlRepository.php @@ -33,7 +33,13 @@ class PgSqlRepository extends AbstractMetricRepository implements MetricInterfac public function getPointsSinceMinutes(Metric $metric, $minutes) { $queryType = $this->getQueryType($metric); - $points = DB::select("SELECT to_char({$this->getMetricPointsTable()}.created_at, 'HH24:MI') AS key, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id WHERE {$this->getMetricsTable()}.id = :metricId AND {$this->getMetricPointsTable()}.created_at >= (NOW() - INTERVAL '{$minutes}' MINUTE) GROUP BY to_char({$this->getMetricPointsTable()}.created_at, 'HH24:MI') ORDER BY to_char({$this->getMetricPointsTable()}.created_at, 'HH24:MI')", [ + $points = DB::select("SELECT to_char({$this->getMetricPointsTable()}.created_at, 'HH24:MI') AS key, {$queryType} " . + "FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id " . + "WHERE {$this->getMetricsTable()}.id = :metricId " . + "AND {$this->getMetricPointsTable()}.created_at >= (NOW() - INTERVAL '{$minutes}' MINUTE) " . + "AND {$this->getMetricPointsTable()}.created_at <= NOW() " . + "GROUP BY to_char({$this->getMetricPointsTable()}.created_at, 'HH24:MI') " . + "ORDER BY to_char({$this->getMetricPointsTable()}.created_at, 'HH24:MI')", [ 'metricId' => $metric->id, ]); @@ -51,7 +57,13 @@ class PgSqlRepository extends AbstractMetricRepository implements MetricInterfac public function getPointsSinceHour(Metric $metric, $hour) { $queryType = $this->getQueryType($metric); - $points = DB::select("SELECT to_char({$this->getMetricPointsTable()}.created_at, 'HH24:00') AS key, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id WHERE {$this->getMetricsTable()}.id = :metricId AND {$this->getMetricPointsTable()}.created_at >= (NOW() - INTERVAL '{$hour}' HOUR) GROUP BY to_char({$this->getMetricPointsTable()}.created_at, 'HH24:00') ORDER BY to_char({$this->getMetricPointsTable()}.created_at, 'HH24:00')", [ + $points = DB::select("SELECT to_char({$this->getMetricPointsTable()}.created_at, 'HH24:00') AS key, {$queryType} " . + "FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id " . + "WHERE {$this->getMetricsTable()}.id = :metricId " . + "AND {$this->getMetricPointsTable()}.created_at >= (NOW() - INTERVAL '{$hour}' HOUR) " . + "AND {$this->getMetricPointsTable()}.created_at <= NOW() " . + "GROUP BY to_char({$this->getMetricPointsTable()}.created_at, 'HH24:00') " . + "ORDER BY to_char({$this->getMetricPointsTable()}.created_at, 'HH24:00')", [ 'metricId' => $metric->id, ]); @@ -69,7 +81,13 @@ class PgSqlRepository extends AbstractMetricRepository implements MetricInterfac public function getPointsSinceDay(Metric $metric, $day) { $queryType = $this->getQueryType($metric); - $points = DB::select("SELECT DATE({$this->getMetricPointsTable()}.created_at) AS key, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id WHERE {$this->getMetricsTable()}.id = :metricId AND {$this->getMetricPointsTable()}.created_at >= (DATE(NOW()) - INTERVAL '{$day}' DAY) GROUP BY DATE({$this->getMetricPointsTable()}.created_at) ORDER BY DATE({$this->getMetricPointsTable()}.created_at)", [ + $points = DB::select("SELECT DATE({$this->getMetricPointsTable()}.created_at) AS key, {$queryType} " . + "FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id " . + "WHERE {$this->getMetricsTable()}.id = :metricId " . + "AND {$this->getMetricPointsTable()}.created_at >= (DATE(NOW()) - INTERVAL '{$day}' DAY) " . + "AND {$this->getMetricPointsTable()}.created_at <= DATE(NOW()) " . + "GROUP BY DATE({$this->getMetricPointsTable()}.created_at) " . + "ORDER BY DATE({$this->getMetricPointsTable()}.created_at)", [ 'metricId' => $metric->id, ]); diff --git a/app/Repositories/Metric/SqliteRepository.php b/app/Repositories/Metric/SqliteRepository.php index 5d9c751d..951da5e8 100644 --- a/app/Repositories/Metric/SqliteRepository.php +++ b/app/Repositories/Metric/SqliteRepository.php @@ -33,7 +33,14 @@ class SqliteRepository extends AbstractMetricRepository implements MetricInterfa public function getPointsSinceMinutes(Metric $metric, $minutes) { $queryType = $this->getQueryType($metric); - $points = DB::select("SELECT strftime('%H:%M', {$this->getMetricPointsTable()}.`created_at`) AS `key`, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id WHERE {$this->getMetricsTable()}.id = :metricId AND {$this->getMetricPointsTable()}.`created_at` >= datetime('now', '-{$minutes} minutes') GROUP BY strftime('%H', {$this->getMetricPointsTable()}.`created_at`), strftime('%M', {$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [ + $points = DB::select("SELECT strftime('%H:%M', {$this->getMetricPointsTable()}.`created_at`) AS `key`, {$queryType} " . + "FROM {$this->getMetricsTable()} " . + "INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id " . + "WHERE {$this->getMetricsTable()}.id = :metricId " . + "AND {$this->getMetricPointsTable()}.`created_at` >= datetime('now', '-{$minutes} minutes') " . + "AND {$this->getMetricPointsTable()}.`created_at` <= datetime('now') " . + "GROUP BY strftime('%H', {$this->getMetricPointsTable()}.`created_at`), strftime('%M', {$this->getMetricPointsTable()}.`created_at`) " . + "ORDER BY {$this->getMetricPointsTable()}.`created_at`", [ 'metricId' => $metric->id, ]); @@ -51,7 +58,12 @@ class SqliteRepository extends AbstractMetricRepository implements MetricInterfa public function getPointsSinceHour(Metric $metric, $hour) { $queryType = $this->getQueryType($metric); - $points = DB::select("SELECT strftime('%H:00', {$this->getMetricPointsTable()}.`created_at`) AS `key`, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id WHERE {$this->getMetricsTable()}.id = :metricId AND {$this->getMetricPointsTable()}.`created_at` >= datetime('now', '-{$hour} hours') GROUP BY strftime('%H', {$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [ + $points = DB::select("SELECT strftime('%H:00', {$this->getMetricPointsTable()}.`created_at`) AS `key`, {$queryType} " . + "FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id " . + "WHERE {$this->getMetricsTable()}.id = :metricId " . + "AND {$this->getMetricPointsTable()}.`created_at` >= datetime('now', '-{$hour} hours') " . + "AND {$this->getMetricPointsTable()}.`created_at` <= datetime('now') " . + "GROUP BY strftime('%H', {$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [ 'metricId' => $metric->id, ]); @@ -69,7 +81,13 @@ class SqliteRepository extends AbstractMetricRepository implements MetricInterfa public function getPointsSinceDay(Metric $metric, $day) { $queryType = $this->getQueryType($metric); - $points = DB::select("SELECT strftime('%Y-%m-%d', {$this->getMetricPointsTable()}.`created_at`) AS `key`, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id WHERE {$this->getMetricsTable()}.id = :metricId AND {$this->getMetricPointsTable()}.`created_at` >= datetime('now', '-{$day} days') GROUP BY DATE({$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [ + $points = DB::select("SELECT strftime('%Y-%m-%d', {$this->getMetricPointsTable()}.`created_at`) AS `key`, {$queryType} " . + "FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON {$this->getMetricsTable()}.id = {$this->getMetricPointsTable()}.metric_id " . + "WHERE {$this->getMetricsTable()}.id = :metricId " . + "AND {$this->getMetricPointsTable()}.`created_at` >= datetime('now', '-{$day} days') " . + "AND {$this->getMetricPointsTable()}.`created_at` <= datetime('now') " . + "GROUP BY DATE({$this->getMetricPointsTable()}.`created_at`) " . + "ORDER BY {$this->getMetricPointsTable()}.`created_at`", [ 'metricId' => $metric->id, ]);