2013-10-22 17:02:30 +02:00
|
|
|
|
<?php
|
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
2014-03-20 14:30:48 +01:00
|
|
|
|
namespace Icinga\Module\Monitoring\Timeline;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
|
2014-04-17 15:33:57 +02:00
|
|
|
|
use StdClass;
|
|
|
|
|
use Iterator;
|
|
|
|
|
use DateTime;
|
|
|
|
|
use DateInterval;
|
|
|
|
|
use Icinga\Util\Format;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A range of time split into a specific interval
|
|
|
|
|
*
|
|
|
|
|
* @see Iterator
|
|
|
|
|
*/
|
|
|
|
|
class TimeRange implements Iterator
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The start of this time range
|
|
|
|
|
*
|
|
|
|
|
* @var DateTime
|
|
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
|
protected $start;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The end of this time range
|
|
|
|
|
*
|
|
|
|
|
* @var DateTime
|
|
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
|
protected $end;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The interval by which this time range is split
|
|
|
|
|
*
|
|
|
|
|
* @var DateInterval
|
|
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
|
protected $interval;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The current date in the iteration
|
|
|
|
|
*
|
|
|
|
|
* @var DateTime
|
|
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
|
protected $current;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether the date iteration is negative
|
|
|
|
|
*
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
|
protected $negative;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize a new time range
|
|
|
|
|
*
|
|
|
|
|
* @param DateTime $start When the time range should start
|
|
|
|
|
* @param DateTime $end When the time range should end
|
|
|
|
|
* @param DateInterval $interval The interval of the time range
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(DateTime $start, DateTime $end, DateInterval $interval)
|
|
|
|
|
{
|
|
|
|
|
$this->interval = $interval;
|
|
|
|
|
$this->start = $start;
|
|
|
|
|
$this->end = $end;
|
2014-03-24 16:53:18 +01:00
|
|
|
|
$this->negative = $this->start > $this->end;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return when this range of time starts
|
|
|
|
|
*
|
|
|
|
|
* @return DateTime
|
|
|
|
|
*/
|
|
|
|
|
public function getStart()
|
|
|
|
|
{
|
|
|
|
|
return $this->start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return when this range of time ends
|
|
|
|
|
*
|
|
|
|
|
* @return DateTime
|
|
|
|
|
*/
|
|
|
|
|
public function getEnd()
|
|
|
|
|
{
|
|
|
|
|
return $this->end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the interval by which this time range is split
|
|
|
|
|
*
|
|
|
|
|
* @return DateInterval
|
|
|
|
|
*/
|
|
|
|
|
public function getInterval()
|
|
|
|
|
{
|
|
|
|
|
return $this->interval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the appropriate timeframe for the given date and time or null if none could be found
|
|
|
|
|
*
|
|
|
|
|
* @param DateTime $dateTime The date and time for which to search the timeframe
|
|
|
|
|
* @param bool $asTimestamp Whether the start of the timeframe should be returned as timestamp
|
|
|
|
|
* @return StdClass|int An object with a ´start´ and ´end´ property or a timestamp
|
|
|
|
|
*/
|
|
|
|
|
public function findTimeframe(DateTime $dateTime, $asTimestamp = false)
|
|
|
|
|
{
|
2013-10-24 17:46:28 +02:00
|
|
|
|
foreach ($this as $timeframeIdentifier => $timeframe) {
|
2013-10-22 17:02:30 +02:00
|
|
|
|
if ($this->negative) {
|
2014-03-21 12:55:53 +01:00
|
|
|
|
if ($dateTime <= $timeframe->start && $dateTime >= $timeframe->end) {
|
2013-10-24 17:46:28 +02:00
|
|
|
|
return $asTimestamp ? $timeframeIdentifier : $timeframe;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
}
|
2014-03-21 12:55:53 +01:00
|
|
|
|
} elseif ($dateTime >= $timeframe->start && $dateTime <= $timeframe->end) {
|
2013-10-24 17:46:28 +02:00
|
|
|
|
return $asTimestamp ? $timeframeIdentifier : $timeframe;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-24 16:53:18 +01:00
|
|
|
|
/**
|
|
|
|
|
* Return whether the given time is within this range of time
|
|
|
|
|
*
|
|
|
|
|
* @param int|DateTime $time The timestamp or date and time to check
|
|
|
|
|
*/
|
|
|
|
|
public function validateTime($time)
|
|
|
|
|
{
|
|
|
|
|
if ($time instanceof DateTime) {
|
|
|
|
|
$dateTime = $time;
|
|
|
|
|
} else {
|
|
|
|
|
$dateTime = new DateTime();
|
|
|
|
|
$dateTime->setTimestamp($time);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ($this->negative && ($dateTime <= $this->start && $dateTime >= $this->end)) ||
|
|
|
|
|
(!$this->negative && ($dateTime >= $this->start && $dateTime <= $this->end));
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-22 17:02:30 +02:00
|
|
|
|
/**
|
2013-10-24 17:46:28 +02:00
|
|
|
|
* Return the appropriate timeframe for the given timeframe start
|
2013-10-22 17:02:30 +02:00
|
|
|
|
*
|
2013-10-24 17:46:28 +02:00
|
|
|
|
* @param int|DateTime $time The timestamp or date and time for which to return the timeframe
|
2013-10-22 17:02:30 +02:00
|
|
|
|
* @return StdClass An object with a ´start´ and ´end´ property
|
|
|
|
|
*/
|
2013-10-24 17:46:28 +02:00
|
|
|
|
public function getTimeframe($time)
|
2013-10-22 17:02:30 +02:00
|
|
|
|
{
|
2013-10-24 17:46:28 +02:00
|
|
|
|
if ($time instanceof DateTime) {
|
2014-03-24 16:53:18 +01:00
|
|
|
|
$startTime = clone $time;
|
2013-10-24 17:46:28 +02:00
|
|
|
|
} else {
|
|
|
|
|
$startTime = new DateTime();
|
|
|
|
|
$startTime->setTimestamp($time);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-02 10:53:39 +02:00
|
|
|
|
return $this->buildTimeframe($startTime, $this->applyInterval(clone $startTime, 1));
|
|
|
|
|
}
|
2013-10-22 17:02:30 +02:00
|
|
|
|
|
2014-04-02 10:53:39 +02:00
|
|
|
|
/**
|
|
|
|
|
* Apply the current interval to the given date and time
|
|
|
|
|
*
|
|
|
|
|
* @param DateTime $dateTime The date and time to apply the interval to
|
|
|
|
|
* @param int $adjustBy By how much seconds the resulting date and time should be adjusted
|
|
|
|
|
*
|
|
|
|
|
* @return DateTime
|
|
|
|
|
*/
|
|
|
|
|
protected function applyInterval(DateTime $dateTime, $adjustBy)
|
|
|
|
|
{
|
|
|
|
|
if (!$this->interval->y && !$this->interval->m) {
|
|
|
|
|
if ($this->negative) {
|
|
|
|
|
return $dateTime->sub($this->interval)->add(new DateInterval('PT' . $adjustBy . 'S'));
|
|
|
|
|
} else {
|
|
|
|
|
return $dateTime->add($this->interval)->sub(new DateInterval('PT' . $adjustBy . 'S'));
|
|
|
|
|
}
|
|
|
|
|
} elseif ($this->interval->m) {
|
|
|
|
|
for ($i = 0; $i < $this->interval->m; $i++) {
|
|
|
|
|
if ($this->negative) {
|
2014-04-17 15:33:57 +02:00
|
|
|
|
$dateTime->sub(new DateInterval('PT' . Format::secondsByMonth($dateTime) . 'S'));
|
2014-04-02 10:53:39 +02:00
|
|
|
|
} else {
|
2014-04-17 15:33:57 +02:00
|
|
|
|
$dateTime->add(new DateInterval('PT' . Format::secondsByMonth($dateTime) . 'S'));
|
2014-04-02 10:53:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ($this->interval->y) {
|
|
|
|
|
for ($i = 0; $i < $this->interval->y; $i++) {
|
|
|
|
|
if ($this->negative) {
|
2014-04-17 15:33:57 +02:00
|
|
|
|
$dateTime->sub(new DateInterval('PT' . Format::secondsByYear($dateTime) . 'S'));
|
2014-04-02 10:53:39 +02:00
|
|
|
|
} else {
|
2014-04-17 15:33:57 +02:00
|
|
|
|
$dateTime->add(new DateInterval('PT' . Format::secondsByYear($dateTime) . 'S'));
|
2014-04-02 10:53:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-10-22 17:02:30 +02:00
|
|
|
|
}
|
2014-04-02 10:53:39 +02:00
|
|
|
|
$adjustment = new DateInterval('PT' . $adjustBy . 'S');
|
|
|
|
|
return $this->negative ? $dateTime->add($adjustment) : $dateTime->sub($adjustment);
|
2013-10-22 17:02:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return an object representation of the given timeframe
|
|
|
|
|
*
|
|
|
|
|
* @param DateTime $start The start of the timeframe
|
|
|
|
|
* @param DateTime $end The end of the timeframe
|
|
|
|
|
* @return StdClass
|
|
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
|
protected function buildTimeframe(DateTime $start, DateTime $end)
|
2013-10-22 17:02:30 +02:00
|
|
|
|
{
|
|
|
|
|
$timeframe = new StdClass();
|
|
|
|
|
$timeframe->start = $start;
|
|
|
|
|
$timeframe->end = $end;
|
|
|
|
|
return $timeframe;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset the iterator to its initial state
|
|
|
|
|
*/
|
|
|
|
|
public function rewind()
|
|
|
|
|
{
|
|
|
|
|
$this->current = clone $this->start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return whether the current iteration step is valid
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function valid()
|
|
|
|
|
{
|
|
|
|
|
if ($this->negative) {
|
|
|
|
|
return $this->current > $this->end;
|
|
|
|
|
} else {
|
|
|
|
|
return $this->current < $this->end;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the current value in the iteration
|
|
|
|
|
*
|
2013-10-24 17:46:28 +02:00
|
|
|
|
* @return StdClass
|
2013-10-22 17:02:30 +02:00
|
|
|
|
*/
|
|
|
|
|
public function current()
|
|
|
|
|
{
|
2013-10-24 17:46:28 +02:00
|
|
|
|
return $this->getTimeframe($this->current);
|
2013-10-22 17:02:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a unique identifier for the current value in the iteration
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function key()
|
|
|
|
|
{
|
|
|
|
|
return $this->current->getTimestamp();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Advance the iterator position by one
|
|
|
|
|
*/
|
|
|
|
|
public function next()
|
|
|
|
|
{
|
2014-04-02 10:53:39 +02:00
|
|
|
|
$this->applyInterval($this->current, 0);
|
2013-10-22 17:02:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|