When selecting the time of metric's points only the time was selected, not the date. The issue with this is mainly in the view "Last 12 hours". Example: It's 11:10 am and I choose the "Last 12 hours" view, the metrics points will be from 23:00 yesterday to 11:00 am today. When giving all these datas and labels to ChartJS, it sorts the points by label from lower to highter. It means 23:00 (from yesterday) will be after the datas of today, it doesn't have sense. To fix it, I've added in the repositories the date in addition to the time. So it's no longer 11:00 that is selected but 2018-01-15 11:00. I've updated the Metric vue in order to cut the label when displaying so it doesn't change the displaying. Because there are metric views that are based on date but not time, there is a condition in the Metric vue to cut the string only if the time is present. Related to CachetHQ/Cachet#2848
97 lines
3.9 KiB
PHP
97 lines
3.9 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Cachet.
|
|
*
|
|
* (c) Alt Three Services Limited
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace CachetHQ\Cachet\Repositories\Metric;
|
|
|
|
use CachetHQ\Cachet\Models\Metric;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Jenssegers\Date\Date;
|
|
|
|
/**
|
|
* This is the pgsql repository class.
|
|
*
|
|
* @author James Brooks <james@alt-three.com>
|
|
*/
|
|
class PgSqlRepository extends AbstractMetricRepository implements MetricInterface
|
|
{
|
|
/**
|
|
* Returns metrics since given minutes.
|
|
*
|
|
* @param \CachetHQ\Cachet\Models\Metric $metric
|
|
* @param int $minutes
|
|
*
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
public function getPointsSinceMinutes(Metric $metric, $minutes)
|
|
{
|
|
$queryType = $this->getQueryType($metric);
|
|
$points = DB::select("SELECT to_char({$this->getMetricPointsTable()}.created_at, 'YYYY-MM-DD 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,
|
|
]);
|
|
|
|
return $this->mapResults($metric, $points);
|
|
}
|
|
|
|
/**
|
|
* Returns metrics since given hour.
|
|
*
|
|
* @param \CachetHQ\Cachet\Models\Metric $metric
|
|
* @param int $hour
|
|
*
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
public function getPointsSinceHour(Metric $metric, $hour)
|
|
{
|
|
$queryType = $this->getQueryType($metric);
|
|
$points = DB::select("SELECT to_char({$this->getMetricPointsTable()}.created_at, 'YYYY-MM-DD 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,
|
|
]);
|
|
|
|
return $this->mapResults($metric, $points);
|
|
}
|
|
|
|
/**
|
|
* Returns metrics since given day.
|
|
*
|
|
* @param \CachetHQ\Cachet\Models\Metric $metric
|
|
* @param int $day
|
|
*
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
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) " .
|
|
"AND {$this->getMetricPointsTable()}.created_at <= DATE(NOW()) " .
|
|
"GROUP BY DATE({$this->getMetricPointsTable()}.created_at) " .
|
|
"ORDER BY DATE({$this->getMetricPointsTable()}.created_at)", [
|
|
'metricId' => $metric->id,
|
|
]);
|
|
|
|
return $this->mapResults($metric, $points);
|
|
}
|
|
}
|