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;
|
2017-01-30 13:31:07 +00:00
|
|
|
use CachetHQ\Cachet\Services\Dates\DateFactory;
|
2015-08-30 22:35:16 +01:00
|
|
|
use DateInterval;
|
|
|
|
|
2016-06-09 14:38:13 +01:00
|
|
|
/**
|
|
|
|
* This is the metric repository class.
|
|
|
|
*
|
|
|
|
* @author James Brooks <james@alt-three.com>
|
|
|
|
*/
|
2015-08-30 22:35:16 +01:00
|
|
|
class MetricRepository
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Metric repository.
|
|
|
|
*
|
|
|
|
* @var \CachetHQ\Cachet\Repositories\Metric\MetricInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
2016-02-02 20:46:46 +00:00
|
|
|
* The date factory instance.
|
2015-08-30 22:35:16 +01:00
|
|
|
*
|
2017-01-30 13:30:45 +00:00
|
|
|
* @var \CachetHQ\Cachet\Services\Dates\DateFactory
|
2015-08-30 22:35:16 +01:00
|
|
|
*/
|
2016-02-02 20:46:46 +00:00
|
|
|
protected $dates;
|
2015-08-30 22:35:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new metric repository class.
|
|
|
|
*
|
|
|
|
* @param \CachetHQ\Cachet\Repositories\Metric\MetricInterface $repository
|
2017-01-30 13:31:07 +00:00
|
|
|
* @param \CachetHQ\Cachet\Services\Dates\DateFactory $dates
|
2016-02-02 20:46:46 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2015-08-30 22:35:16 +01:00
|
|
|
*/
|
2016-02-02 20:46:46 +00:00
|
|
|
public function __construct(MetricInterface $repository, DateFactory $dates)
|
2015-08-30 22:35:16 +01:00
|
|
|
{
|
|
|
|
$this->repository = $repository;
|
2016-02-02 20:46:46 +00:00
|
|
|
$this->dates = $dates;
|
2015-08-30 22:35:16 +01:00
|
|
|
}
|
|
|
|
|
2015-12-24 11:27:22 +00:00
|
|
|
/**
|
|
|
|
* Returns all points as an array, for the last hour.
|
|
|
|
*
|
|
|
|
* @param \CachetHQ\Cachet\Models\Metric $metric
|
|
|
|
*
|
2016-06-09 14:38:13 +01:00
|
|
|
* @return \Illuminate\Support\Collection
|
2015-12-24 11:27:22 +00:00
|
|
|
*/
|
|
|
|
public function listPointsLastHour(Metric $metric)
|
|
|
|
{
|
2016-02-02 20:46:46 +00:00
|
|
|
$dateTime = $this->dates->make();
|
2018-01-15 11:28:41 +01:00
|
|
|
$pointKey = $dateTime->format('Y-m-d H:i');
|
2018-03-27 16:02:46 +02:00
|
|
|
$nrOfMinutes = 61;
|
|
|
|
$points = $this->repository->getPointsSinceMinutes($metric, $nrOfMinutes + $metric->threshold)->pluck('value', 'key')->take(-$nrOfMinutes);
|
2016-02-02 20:46:46 +00:00
|
|
|
|
2018-03-27 16:02:46 +02:00
|
|
|
$timeframe = $nrOfMinutes;
|
|
|
|
for ($i = 0; $i < $timeframe; $i++) {
|
2016-06-09 14:38:13 +01:00
|
|
|
if (!$points->has($pointKey)) {
|
2018-03-27 16:02:46 +02:00
|
|
|
if ($i >= $metric->threshold) {
|
|
|
|
$points->put($pointKey, $metric->default_value);
|
|
|
|
} else {
|
|
|
|
// The point not found is still within the threshold, so it is ignored and
|
|
|
|
// the timeframe is shifted by one minute
|
|
|
|
$timeframe++;
|
|
|
|
}
|
2016-06-09 14:38:13 +01:00
|
|
|
}
|
|
|
|
|
2018-01-15 11:28:41 +01:00
|
|
|
$pointKey = $dateTime->sub(new DateInterval('PT1M'))->format('Y-m-d H:i');
|
2015-12-24 11:27:22 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 21:19:07 +02:00
|
|
|
return $points->sortBy(function ($point, $key) {
|
2016-06-09 14:38:13 +01:00
|
|
|
return $key;
|
|
|
|
});
|
2015-12-24 11:27:22 +00:00
|
|
|
}
|
|
|
|
|
2015-08-30 22:35:16 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2016-02-02 20:46:46 +00:00
|
|
|
$dateTime = $this->dates->make();
|
2018-01-15 11:28:41 +01:00
|
|
|
$pointKey = $dateTime->format('Y-m-d H:00');
|
2016-06-09 14:38:13 +01:00
|
|
|
$points = $this->repository->getPointsSinceHour($metric, $hours)->pluck('value', 'key');
|
2016-02-02 20:46:46 +00:00
|
|
|
|
2015-08-30 22:35:16 +01:00
|
|
|
for ($i = 0; $i <= $hours; $i++) {
|
2016-06-09 14:38:13 +01:00
|
|
|
if (!$points->has($pointKey)) {
|
|
|
|
$points->put($pointKey, $metric->default_value);
|
|
|
|
}
|
|
|
|
|
2018-01-15 11:28:41 +01:00
|
|
|
$pointKey = $dateTime->sub(new DateInterval('PT1H'))->format('Y-m-d H:00');
|
|
|
|
}
|
2018-01-21 20:46:11 +00:00
|
|
|
|
2017-10-11 21:19:07 +02:00
|
|
|
return $points->sortBy(function ($point, $key) {
|
2016-06-09 14:38:13 +01:00
|
|
|
return $key;
|
|
|
|
});
|
2015-08-30 22:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all points as an array, in the last week.
|
|
|
|
*
|
|
|
|
* @param \CachetHQ\Cachet\Models\Metric $metric
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function listPointsForWeek(Metric $metric)
|
|
|
|
{
|
2016-02-02 20:46:46 +00:00
|
|
|
$dateTime = $this->dates->make();
|
2016-06-09 14:38:13 +01:00
|
|
|
$pointKey = $dateTime->format('Y-m-d');
|
|
|
|
$points = $this->repository->getPointsSinceDay($metric, 7)->pluck('value', 'key');
|
2016-02-02 20:46:46 +00:00
|
|
|
|
2015-08-30 22:35:16 +01:00
|
|
|
for ($i = 0; $i <= 7; $i++) {
|
2016-06-09 14:38:13 +01:00
|
|
|
if (!$points->has($pointKey)) {
|
|
|
|
$points->put($pointKey, $metric->default_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('Y-m-d');
|
2015-08-30 22:35:16 +01:00
|
|
|
}
|
|
|
|
|
2017-10-11 21:19:07 +02:00
|
|
|
return $points->sortBy(function ($point, $key) {
|
2016-06-09 14:38:13 +01:00
|
|
|
return $key;
|
|
|
|
});
|
2015-08-30 22:35:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all points as an array, in the last month.
|
|
|
|
*
|
|
|
|
* @param \CachetHQ\Cachet\Models\Metric $metric
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function listPointsForMonth(Metric $metric)
|
|
|
|
{
|
2016-02-02 20:46:46 +00:00
|
|
|
$dateTime = $this->dates->make();
|
2016-06-09 14:38:13 +01:00
|
|
|
$pointKey = $dateTime->format('Y-m-d');
|
2015-08-30 22:35:16 +01:00
|
|
|
$daysInMonth = $dateTime->format('t');
|
2016-06-09 14:38:13 +01:00
|
|
|
$points = $this->repository->getPointsSinceDay($metric, $daysInMonth)->pluck('value', 'key');
|
2016-02-02 20:46:46 +00:00
|
|
|
|
2015-08-30 22:35:16 +01:00
|
|
|
for ($i = 0; $i <= $daysInMonth; $i++) {
|
2016-06-09 14:38:13 +01:00
|
|
|
if (!$points->has($pointKey)) {
|
|
|
|
$points->put($pointKey, $metric->default_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('Y-m-d');
|
2015-08-30 22:35:16 +01:00
|
|
|
}
|
|
|
|
|
2017-10-11 21:19:07 +02:00
|
|
|
return $points->sortBy(function ($point, $key) {
|
2016-06-09 14:38:13 +01:00
|
|
|
return $key;
|
|
|
|
});
|
2015-08-30 22:35:16 +01:00
|
|
|
}
|
|
|
|
}
|