2015-08-30 22:35:16 +01:00
< ? 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 ;
2016-07-06 18:42:31 +01:00
/**
* This is the mysql repository class .
*
* @ author James Brooks < james @ alt - three . com >
*/
class MySqlRepository extends AbstractMetricRepository implements MetricInterface
2015-08-30 22:35:16 +01:00
{
2015-12-24 11:27:22 +00:00
/**
2016-06-09 14:38:13 +01:00
* Returns metrics since given minutes .
2015-12-24 11:27:22 +00:00
*
* @ param \CachetHQ\Cachet\Models\Metric $metric
2016-06-09 14:38:13 +01:00
* @ param int $minutes
2015-12-24 11:27:22 +00:00
*
2016-06-09 14:38:13 +01:00
* @ return \Illuminate\Support\Collection
2015-12-24 11:27:22 +00:00
*/
2016-06-09 14:38:13 +01:00
public function getPointsSinceMinutes ( Metric $metric , $minutes )
2015-12-24 11:27:22 +00:00
{
2016-06-09 14:38:13 +01:00
$queryType = $this -> getQueryType ( $metric );
2018-01-15 11:13:57 +01:00
2018-01-21 20:46:11 +00:00
$points = DB :: select ( " SELECT DATE_FORMAT( { $this -> getMetricPointsTable () } .`created_at`, '%Y-%m-%d %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() " .
2018-01-15 11:13:57 +01:00
" GROUP BY HOUR( { $this -> getMetricPointsTable () } .`created_at`), MINUTE( { $this -> getMetricPointsTable () } .`created_at`) ORDER BY { $this -> getMetricPointsTable () } .`created_at` " , [
2016-06-09 14:38:13 +01:00
'metricId' => $metric -> id ,
'minutes' => $minutes ,
2016-03-02 12:09:57 +00:00
]);
2016-06-09 14:38:13 +01:00
return $this -> mapResults ( $metric , $points );
2015-12-24 11:27:22 +00:00
}
2015-08-30 22:35:16 +01:00
/**
2016-06-09 14:38:13 +01:00
* Returns metrics since given hour .
2015-08-30 22:35:16 +01:00
*
* @ param \CachetHQ\Cachet\Models\Metric $metric
* @ param int $hour
*
2016-06-09 14:38:13 +01:00
* @ return \Illuminate\Support\Collection
2015-08-30 22:35:16 +01:00
*/
2016-06-09 14:38:13 +01:00
public function getPointsSinceHour ( Metric $metric , $hour )
2015-08-30 22:35:16 +01:00
{
2016-06-09 14:38:13 +01:00
$queryType = $this -> getQueryType ( $metric );
2018-01-21 20:46:11 +00:00
$points = DB :: select ( " SELECT DATE_FORMAT( { $this -> getMetricPointsTable () } .`created_at`, '%Y-%m-%d %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`) " .
2018-01-15 11:13:57 +01:00
" ORDER BY { $this -> getMetricPointsTable () } .`created_at` " , [
2016-06-09 14:38:13 +01:00
'metricId' => $metric -> id ,
'hour' => $hour ,
2016-03-02 12:09:57 +00:00
]);
2016-06-09 14:38:13 +01:00
return $this -> mapResults ( $metric , $points );
2015-08-30 22:35:16 +01:00
}
/**
2016-06-09 14:38:13 +01:00
* Returns metrics since given day .
2015-08-30 22:35:16 +01:00
*
* @ param \CachetHQ\Cachet\Models\Metric $metric
2016-06-09 14:38:13 +01:00
* @ param int $day
2015-08-30 22:35:16 +01:00
*
2016-06-09 14:38:13 +01:00
* @ return \Illuminate\Support\Collection
2015-08-30 22:35:16 +01:00
*/
2016-06-09 14:38:13 +01:00
public function getPointsSinceDay ( Metric $metric , $day )
2015-08-30 22:35:16 +01:00
{
2016-06-09 14:38:13 +01:00
$queryType = $this -> getQueryType ( $metric );
2018-01-21 20:46:11 +00:00
$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() " .
2018-01-15 11:13:57 +01:00
" GROUP BY DATE( { $this -> getMetricPointsTable () } .`created_at`) ORDER BY { $this -> getMetricPointsTable () } .`created_at` " , [
2016-06-09 14:38:13 +01:00
'metricId' => $metric -> id ,
'day' => $day ,
2016-03-02 12:09:57 +00:00
]);
2016-06-09 14:38:13 +01:00
return $this -> mapResults ( $metric , $points );
2015-08-30 22:35:16 +01:00
}
}