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
This commit is contained in:
A
2018-01-15 11:13:57 +01:00
parent 0f6512fbf1
commit d8208be179
3 changed files with 62 additions and 9 deletions

View File

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