Add timeline helper classes

#refs 4190
This commit is contained in:
Johannes Meyer 2013-10-22 17:02:30 +02:00
parent f98a39fd5a
commit ba541b402d
4 changed files with 582 additions and 0 deletions

View File

@ -0,0 +1,148 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Timeline;
use \DateTime;
/**
* An event group that is part of a timeline
*/
class TimeEntry
{
/**
* The name of this group
*
* @var string
*/
private $name;
/**
* The amount of events that are part of this group
*
* @var int
*/
private $value;
/**
* The date and time of this group
*
* @var DateTime
*/
private $dateTime;
/**
* The url to this group's detail view
*
* @var string
*/
private $detailUrl;
/**
* The weight of this group
*
* @var float
*/
private $weight = 1.0;
/**
* Initialize a new event group
*
* @param string $name The name of the group
* @param int $value The amount of events
* @param DateTime $dateTime The date and time of the group
* @param string $detailUrl The url to the detail view
*/
public function __construct($name, $value, DateTime $dateTime, $detailUrl)
{
$this->detailUrl = $detailUrl;
$this->dateTime = $dateTime;
$this->value = $value;
$this->name = $name;
}
/**
* Return the name of this group
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Return the amount of events in this group
*
* @return int
*/
public function getValue()
{
return $this->value;
}
/**
* Return the date and time of this group
*
* @return DateTime
*/
public function getDateTime()
{
return $this->dateTime;
}
/**
* Return the url to this group's detail view
*
* @return string
*/
public function getDetailUrl()
{
return $this->detailUrl;
}
/**
* Return the weight of this group
*
* @return float
*/
public function getWeight()
{
return $this->weight;
}
/**
* Set the weight of this group
*
* @param float $weight The weight for this group
*/
public function setWeight($weight)
{
$this->weight = floatval($weight);
}
}

View File

@ -0,0 +1,147 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Timeline;
use \DateInterval;
use \Icinga\Web\Form;
/**
* Represents a set of events in a specific time range
*
* @see Form
*/
class TimeLine extends Form
{
/**
* The range of time represented by this timeline
*
* @var TimeRange
*/
private $range;
/**
* The event groups this timeline will display
*
* @var array
*/
private $displayData;
/**
* The event groups this timeline uses to calculate forecasts
*
* @var array
*/
private $forecastData;
/**
* Whether only the timeline is shown
*
* @var bool
*/
private $hideOuterElements = false;
/**
* Set the range of time to represent
*
* @param TimeRange $range The range of time to represent
*/
public function setTimeRange(TimeRange $range)
{
$this->range = $range;
}
/**
* Set the groups this timeline should display
*
* @param array $entries The TimeEntry objects
*/
public function setDisplayData(array $entries)
{
$this->displayData = $entries;
}
/**
* Set the groups this timeline should use to calculate forecasts
*
* @param array $entries The TimeEntry objects
*/
public function setForecastData(array $entries)
{
$this->forecastData = $entries;
}
/**
* Define that only the timeline itself should be rendered
*/
public function showLineOnly()
{
$this->hideOuterElements = true;
}
/**
* Return the chosen interval
*
* @return DateInterval The chosen interval
* @throws Exception If an invalid interval is given in the current request
*/
public function getInterval()
{
switch ($this->getRequest()->getPost('interval', '4h'))
{
case '4h':
return new DateInterval('PT4H');
case '1d':
return new DateInterval('P1D');
case '1w':
return new DateInterval('P1W');
case '1m':
return new DateInterval('P1M');
case '1y':
return new DateInterval('P1Y');
default:
throw new Exception('Invalid interval given in request');
}
}
/**
* Disable the CSRF token
*/
public function init()
{
$this->setTokenDisabled();
}
/**
* Build the timeline
*/
public function create()
{
}
}

View File

@ -0,0 +1,240 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Timeline;
use \StdClass;
use \Iterator;
use \DateTime;
use \DateInterval;
/**
* A range of time split into a specific interval
*
* @see Iterator
*/
class TimeRange implements Iterator
{
/**
* The start of this time range
*
* @var DateTime
*/
private $start;
/**
* The end of this time range
*
* @var DateTime
*/
private $end;
/**
* The interval by which this time range is split
*
* @var DateInterval
*/
private $interval;
/**
* The current date in the iteration
*
* @var DateTime
*/
private $current;
/**
* Whether the date iteration is negative
*
* @var bool
*/
private $negative;
/**
* 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;
}
/**
* 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)
{
foreach ($this as $timeframeIdentifier => $timeframeStart) {
$timeframeEnd = clone $timeframeStart;
if ($this->negative) {
$timeframeEnd->sub($this->interval);
if ($dateTime <= $timeframeStart && $dateTime > $timeframeEnd) {
return $asTimestamp ? $timeframeIdentifier : $this->buildTimeframe($timeframeStart, $timeframeEnd);
}
} else {
$timeframeEnd->add($this->interval);
if ($dateTime >= $timeframeStart && $dateTime < $timeframeEnd) {
return $asTimestamp ? $timeframeIdentifier : $this->buildTimeframe($timeframeStart, $timeframeEnd);
}
}
}
}
/**
* Return the appropriate timeframe for the given timestamp
*
* @param int $timestamp The timestamp for which to return the timeframe
* @return StdClass An object with a ´start´ and ´end´ property
*/
public function getTimeframe($timestamp)
{
$startTime = new DateTime();
$startTime->setTimestamp($timestamp);
$endTime = clone $startTime;
if ($this->negative) {
$endTime->sub($this->interval);
} else {
$endTime->add($this->interval);
}
return $this->buildTimeframe($startTime, $endTime);
}
/**
* 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
*/
private function buildTimeframe(DateTime $start, DateTime $end)
{
$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;
$this->negative = $this->start > $this->end;
}
/**
* 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
*
* @return DateTime
*/
public function current()
{
return $this->current;
}
/**
* 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()
{
if ($this->negative) {
$this->current->sub($this->interval);
} else {
$this->current->add($this->interval);
}
}
}

View File

@ -0,0 +1,47 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga 2 Web.
*
* Icinga 2 Web - Head for multiple monitoring backends.
* Copyright (C) 2013 Icinga Development Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright 2013 Icinga Development Team <info@icinga.org>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
* @author Icinga Development Team <info@icinga.org>
*/
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Web\Hook;
use \Zend_Controller_Request_Abstract;
use \Icinga\Timeline\TimeRange;
/**
* Base class for TimeEntry providers
*/
abstract class TimelineProvider
{
/**
* Return a set of TimeEntry objects for the given range of time
*
* @param TimeRange $range The range of time for which to fetch entries
* @param Zend_Controller_Request_Abstract $request The current request
* @return array
*/
abstract public function fetchTimeEntries(TimeRange $range, Zend_Controller_Request_Abstract $request);
}