mirror of
https://github.com/CachetHQ/Cachet.git
synced 2025-01-17 21:49:01 +01:00
Improved metric repository for per-db methods. Closes #921
This commit is contained in:
parent
062a16ca5b
commit
7136457b49
@ -13,20 +13,20 @@ namespace CachetHQ\Cachet\Composers;
|
||||
|
||||
use CachetHQ\Cachet\Facades\Setting;
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use CachetHQ\Cachet\Repositories\MetricRepository;
|
||||
use CachetHQ\Cachet\Repositories\Metric\MetricRepository;
|
||||
use Illuminate\Contracts\View\View;
|
||||
|
||||
class MetricsComposer
|
||||
{
|
||||
/**
|
||||
* @var \CachetHQ\Cachet\Repositories\MetricRepository
|
||||
* @var \CachetHQ\Cachet\Repositories\Metric\MetricRepository
|
||||
*/
|
||||
protected $metricRepository;
|
||||
|
||||
/**
|
||||
* Construct a new home controller instance.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Repositories\MetricRepository $metricRepository
|
||||
* @param \CachetHQ\Cachet\Repositories\Metric\MetricRepository $metricRepository
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
|
55
app/Providers/RepositoryServiceProvider.php
Normal file
55
app/Providers/RepositoryServiceProvider.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?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\Providers;
|
||||
|
||||
use CachetHQ\Cachet\Repositories\Metric\MetricRepository;
|
||||
use CachetHQ\Cachet\Repositories\Metric\MySqlRepository as MetricMySqlRepository;
|
||||
use CachetHQ\Cachet\Repositories\Metric\PgSqlRepository as MetricPgSqlRepository;
|
||||
use CachetHQ\Cachet\Repositories\Metric\SqliteRepository as MetricSqliteRepository;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class RepositoryServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->registerMetricRepository();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the metric repository.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function registerMetricRepository()
|
||||
{
|
||||
$this->app->singleton('cachet.metricrepository', function ($app) {
|
||||
$dbDriver = $app['config']->get('database.default');
|
||||
|
||||
if ($dbDriver == 'mysql') {
|
||||
$repository = new MetricMySqlRepository();
|
||||
} elseif ($dbDriver == 'pgsql') {
|
||||
$repository = new MetricPgSqlRepository();
|
||||
} elseif ($dbDriver == 'sqlite') {
|
||||
$repository = new MetricSqliteRepository();
|
||||
}
|
||||
|
||||
return new MetricRepository($repository);
|
||||
});
|
||||
|
||||
$this->app->alias('cachet.metricrepository', 'CachetHQ\Cachet\Repositories\Metric\MetricRepository');
|
||||
}
|
||||
}
|
36
app/Repositories/Metric/MetricInterface.php
Normal file
36
app/Repositories/Metric/MetricInterface.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?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;
|
||||
|
||||
interface MetricInterface
|
||||
{
|
||||
/**
|
||||
* Returns metrics for a given hour.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
* @param int $hour
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPointsByHour(Metric $metric, $hour);
|
||||
|
||||
/**
|
||||
* Returns metrics for the week.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPointsForDayInWeek(Metric $metric, $day);
|
||||
}
|
110
app/Repositories/Metric/MetricRepository.php
Normal file
110
app/Repositories/Metric/MetricRepository.php
Normal file
@ -0,0 +1,110 @@
|
||||
<?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\Facades\Setting as SettingFacade;
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use DateInterval;
|
||||
use Jenssegers\Date\Date;
|
||||
|
||||
class MetricRepository
|
||||
{
|
||||
/**
|
||||
* Metric repository.
|
||||
*
|
||||
* @var \CachetHQ\Cachet\Repositories\Metric\MetricInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* The timezone the status page is showing in.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dateTimeZone;
|
||||
|
||||
/**
|
||||
* Create a new metric repository class.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Repositories\Metric\MetricInterface $repository
|
||||
*/
|
||||
public function __construct(MetricInterface $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
$this->dateTimeZone = SettingFacade::get('app_timezone');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all points as an array, by x hours.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
* @param int $hours
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function listPointsToday(Metric $metric, $hours = 12)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$points = [];
|
||||
|
||||
$pointKey = $dateTime->format('H:00');
|
||||
for ($i = 0; $i <= $hours; $i++) {
|
||||
$points[$pointKey] = $this->repository->getPointsByHour($metric, $i + 1);
|
||||
$pointKey = $dateTime->sub(new DateInterval('PT1H'))->format('H:00');
|
||||
}
|
||||
|
||||
return array_reverse($points);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all points as an array, in the last week.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function listPointsForWeek(Metric $metric)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$points = [];
|
||||
|
||||
$pointKey = $dateTime->format('jS M');
|
||||
for ($i = 0; $i <= 7; $i++) {
|
||||
$points[$pointKey] = $this->repository->getPointsForDayInWeek($metric, $i);
|
||||
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('D jS M');
|
||||
}
|
||||
|
||||
return array_reverse($points);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all points as an array, in the last month.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function listPointsForMonth(Metric $metric)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$daysInMonth = $dateTime->format('t');
|
||||
$points = [];
|
||||
|
||||
$pointKey = $dateTime->format('jS M');
|
||||
for ($i = 0; $i <= $daysInMonth; $i++) {
|
||||
$points[$pointKey] = $this->repository->getPointsForDayInWeek($metric, $i);
|
||||
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('jS M');
|
||||
}
|
||||
|
||||
return array_reverse($points);
|
||||
}
|
||||
}
|
99
app/Repositories/Metric/MySqlRepository.php
Normal file
99
app/Repositories/Metric/MySqlRepository.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?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\Facades\Setting as SettingFacade;
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use DateInterval;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Jenssegers\Date\Date;
|
||||
|
||||
class MySqlRepository implements MetricInterface
|
||||
{
|
||||
/**
|
||||
* The timezone the status page is showing in.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dateTimeZone;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the metric repository.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->dateTimeZone = SettingFacade::get('app_timezone');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns metrics for a given hour.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
* @param int $hour
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPointsByHour(Metric $metric, $hour)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$dateTime->sub(new DateInterval('PT'.$hour.'H'));
|
||||
$hourInterval = $dateTime->format('YmdH');
|
||||
|
||||
$points = $metric->points()
|
||||
->whereRaw('DATE_FORMAT(created_at, "%Y%m%d%H") = '.$hourInterval)
|
||||
->groupBy(DB::raw('HOUR(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 the week.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPointsForDayInWeek(Metric $metric, $day)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$dateTime->sub(new DateInterval('P'.$day.'D'));
|
||||
|
||||
$points = $metric->points()
|
||||
->whereRaw('created_at BETWEEN DATE_SUB(created_at, INTERVAL 1 WEEK) AND NOW()')
|
||||
->whereRaw('DATE_FORMAT(created_at, "%Y%m%d") = '.$dateTime->format('Ymd'))
|
||||
->groupBy(DB::raw('DATE_FORMAT(created_at, "%Y%m%d")'));
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
113
app/Repositories/Metric/PgSqlRepository.php
Normal file
113
app/Repositories/Metric/PgSqlRepository.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?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\Facades\Setting as SettingFacade;
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use DateInterval;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Jenssegers\Date\Date;
|
||||
|
||||
class PgSqlRepository implements MetricInterface
|
||||
{
|
||||
/**
|
||||
* The timezone the status page is showing in.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dateTimeZone;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the metric repository.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->dateTimeZone = SettingFacade::get('app_timezone');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns metrics for a given hour.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
* @param int $hour
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPointsByHour(Metric $metric, $hour)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$dateTime->sub(new DateInterval('PT'.$hour.'H'));
|
||||
$hourInterval = $dateTime->format('YmdH');
|
||||
|
||||
// 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, 'YYYYMMDDHH24') = :timestamp GROUP BY to_char(metric_points.created_at, 'H')", [
|
||||
'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 the week.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPointsForDayInWeek(Metric $metric, $day)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$dateTime->sub(new DateInterval('P'.$day.'D'));
|
||||
|
||||
// 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, 'YYYYMMDD') = :timestamp GROUP BY to_char(metric_points.created_at, 'YYYYMMDD')", [
|
||||
'timestamp' => $hourInterval,
|
||||
]);
|
||||
|
||||
if (isset($query[0])) {
|
||||
$value = $query[0]->aggregate;
|
||||
} else {
|
||||
$value = 0;
|
||||
}
|
||||
|
||||
return round($value, $metric->places);
|
||||
}
|
||||
}
|
99
app/Repositories/Metric/SqliteRepository.php
Normal file
99
app/Repositories/Metric/SqliteRepository.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?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\Facades\Setting as SettingFacade;
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use DateInterval;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Jenssegers\Date\Date;
|
||||
|
||||
class SqliteRepository implements MetricInterface
|
||||
{
|
||||
/**
|
||||
* The timezone the status page is showing in.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dateTimeZone;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the metric repository.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->dateTimeZone = SettingFacade::get('app_timezone');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns metrics for a given hour.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
* @param int $hour
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPointsByHour(Metric $metric, $hour)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$dateTime->sub(new DateInterval('PT'.$hour.'H'));
|
||||
$hourInterval = $dateTime->format('YmdH');
|
||||
|
||||
$points = $metric->points()
|
||||
->whereRaw('strftime("%Y%m%d%H", created_at) = "'.$hourInterval.'"')
|
||||
->groupBy(DB::raw('strftime("%H", 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 the week.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPointsForDayInWeek(Metric $metric, $day)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$dateTime->sub(new DateInterval('P'.$day.'D'));
|
||||
|
||||
$points = $metric->points()
|
||||
->whereRaw('created_at > date("now", "-7 day")')
|
||||
->whereRaw('strftime("%Y%m%d", created_at) = "'.$dateTime->format('Ymd').'"')
|
||||
->groupBy(DB::raw('strftime("%Y%m%d", 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);
|
||||
}
|
||||
}
|
@ -1,207 +0,0 @@
|
||||
<?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;
|
||||
|
||||
use CachetHQ\Cachet\Facades\Setting as SettingFacade;
|
||||
use CachetHQ\Cachet\Models\Metric;
|
||||
use DateInterval;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Jenssegers\Date\Date;
|
||||
|
||||
class MetricRepository
|
||||
{
|
||||
/**
|
||||
* The timezone the status page is showing in.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dateTimeZone;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the metric repository.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->dateTimeZone = SettingFacade::get('app_timezone');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all points as an array, by x hours.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
* @param int $hours
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function listPointsToday(Metric $metric, $hours = 12)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$points = [];
|
||||
|
||||
$pointKey = $dateTime->format('H:00');
|
||||
for ($i = 0; $i <= $hours; $i++) {
|
||||
$points[$pointKey] = $this->getPointsByHour($metric, $i + 1);
|
||||
$pointKey = $dateTime->sub(new DateInterval('PT1H'))->format('H:00');
|
||||
}
|
||||
|
||||
return array_reverse($points);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all points as an array, in the last week.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function listPointsForWeek(Metric $metric)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$points = [];
|
||||
|
||||
$pointKey = $dateTime->format('jS M');
|
||||
for ($i = 0; $i <= 7; $i++) {
|
||||
$points[$pointKey] = $this->getPointsForDayInWeek($metric, $i);
|
||||
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('D jS M');
|
||||
}
|
||||
|
||||
return array_reverse($points);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all points as an array, in the last month.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function listPointsForMonth(Metric $metric)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$daysInMonth = $dateTime->format('t');
|
||||
$points = [];
|
||||
|
||||
$pointKey = $dateTime->format('jS M');
|
||||
for ($i = 0; $i <= $daysInMonth; $i++) {
|
||||
$points[$pointKey] = $this->getPointsForDayInWeek($metric, $i);
|
||||
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('jS M');
|
||||
}
|
||||
|
||||
return array_reverse($points);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns metrics for a given hour.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
* @param int $hour
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getPointsByHour(Metric $metric, $hour)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$dateTime->sub(new DateInterval('PT'.$hour.'H'));
|
||||
$hourInterval = $dateTime->format('YmdH');
|
||||
|
||||
if (Config::get('database.default') === 'mysql') {
|
||||
$points = $metric->points()
|
||||
->whereRaw('DATE_FORMAT(created_at, "%Y%m%d%H") = '.$hourInterval)
|
||||
->groupBy(DB::raw('HOUR(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');
|
||||
}
|
||||
} else {
|
||||
// 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, 'YYYYMMDDHH24') = :timestamp GROUP BY to_char(metric_points.created_at, 'H')", [
|
||||
'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 the week.
|
||||
*
|
||||
* @param \CachetHQ\Cachet\Models\Metric $metric
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function getPointsForDayInWeek(Metric $metric, $day)
|
||||
{
|
||||
$dateTime = (new Date())->setTimezone($this->dateTimeZone);
|
||||
$dateTime->sub(new DateInterval('P'.$day.'D'));
|
||||
|
||||
if (Config::get('database.default') === 'mysql') {
|
||||
$points = $metric->points()
|
||||
->whereRaw('created_at BETWEEN DATE_SUB(created_at, INTERVAL 1 WEEK) AND NOW()')
|
||||
->whereRaw('DATE_FORMAT(created_at, "%Y%m%d") = '.$dateTime->format('Ymd'))
|
||||
->groupBy(DB::raw('DATE_FORMAT(created_at, "%Y%m%d")'));
|
||||
|
||||
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');
|
||||
}
|
||||
} else {
|
||||
// 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, 'YYYYMMDD') = :timestamp GROUP BY to_char(metric_points.created_at, 'YYYYMMDD')", [
|
||||
'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);
|
||||
}
|
||||
}
|
@ -171,6 +171,7 @@ return [
|
||||
'CachetHQ\Cachet\Providers\ConfigServiceProvider',
|
||||
'CachetHQ\Cachet\Providers\ConsoleServiceProvider',
|
||||
'CachetHQ\Cachet\Providers\EventServiceProvider',
|
||||
'CachetHQ\Cachet\Providers\RepositoryServiceProvider',
|
||||
'CachetHQ\Cachet\Providers\RouteServiceProvider',
|
||||
|
||||
],
|
||||
|
Loading…
x
Reference in New Issue
Block a user