2013-10-22 17:02:30 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-10-22 17:02:30 +02:00
|
|
|
|
2014-03-20 14:30:48 +01:00
|
|
|
namespace Icinga\Module\Monitoring\Timeline;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
2014-08-27 15:51:49 +02:00
|
|
|
use DateTime;
|
|
|
|
use Exception;
|
|
|
|
use ArrayIterator;
|
2014-08-27 16:03:15 +02:00
|
|
|
use Icinga\Exception\IcingaException;
|
2014-08-27 15:51:49 +02:00
|
|
|
use IteratorAggregate;
|
2014-06-21 03:09:40 +02:00
|
|
|
use Icinga\Data\Filter\Filter;
|
2014-03-24 16:53:18 +01:00
|
|
|
use Icinga\Web\Hook;
|
2014-03-25 14:19:50 +01:00
|
|
|
use Icinga\Web\Session\SessionNamespace;
|
2014-03-24 16:53:18 +01:00
|
|
|
use Icinga\Module\Monitoring\DataView\DataView;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
|
|
|
/**
|
2014-03-24 16:53:18 +01:00
|
|
|
* Represents a set of events in a specific range of time
|
2013-10-22 17:02:30 +02:00
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
class TimeLine implements IteratorAggregate
|
2013-10-22 17:02:30 +02:00
|
|
|
{
|
|
|
|
/**
|
2014-03-24 16:53:18 +01:00
|
|
|
* The resultset returned by the dataview
|
2013-10-22 17:02:30 +02:00
|
|
|
*
|
2014-03-24 16:53:18 +01:00
|
|
|
* @var array
|
2013-10-22 17:02:30 +02:00
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
private $resultset;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
|
|
|
/**
|
2014-03-24 16:53:18 +01:00
|
|
|
* The groups this timeline uses for display purposes
|
2013-10-22 17:02:30 +02:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
private $displayGroups;
|
|
|
|
|
2014-03-25 14:19:50 +01:00
|
|
|
/**
|
|
|
|
* The session to use
|
|
|
|
*
|
|
|
|
* @var SessionNamespace
|
|
|
|
*/
|
|
|
|
protected $session;
|
|
|
|
|
2014-03-24 16:53:18 +01:00
|
|
|
/**
|
|
|
|
* The base that is used to calculate each circle's diameter
|
|
|
|
*
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
protected $calculationBase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The dataview to fetch entries from
|
|
|
|
*
|
|
|
|
* @var DataView
|
|
|
|
*/
|
|
|
|
protected $dataview;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
|
|
|
/**
|
2014-03-24 16:53:18 +01:00
|
|
|
* The names by which to group entries
|
2013-10-22 17:02:30 +02:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
protected $identifiers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The range of time for which to display entries
|
|
|
|
*
|
|
|
|
* @var TimeRange
|
|
|
|
*/
|
|
|
|
protected $displayRange;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The range of time for which to calculate forecasts
|
|
|
|
*
|
|
|
|
* @var TimeRange
|
|
|
|
*/
|
|
|
|
protected $forecastRange;
|
2013-10-22 17:02:30 +02:00
|
|
|
|
2013-10-24 17:49:55 +02:00
|
|
|
/**
|
|
|
|
* The maximum diameter each circle can have
|
|
|
|
*
|
2014-03-24 16:53:18 +01:00
|
|
|
* @var float
|
2013-10-24 17:49:55 +02:00
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
protected $circleDiameter = 100.0;
|
2013-10-24 17:49:55 +02:00
|
|
|
|
2014-03-27 10:19:50 +01:00
|
|
|
/**
|
|
|
|
* The minimum diameter each circle can have
|
|
|
|
*
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
protected $minCircleDiameter = 1.0;
|
|
|
|
|
2013-10-24 17:49:55 +02:00
|
|
|
/**
|
|
|
|
* The unit of a circle's diameter
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
protected $diameterUnit = 'px';
|
2013-10-24 17:49:55 +02:00
|
|
|
|
|
|
|
/**
|
2014-03-24 16:53:18 +01:00
|
|
|
* Return a iterator for this timeline
|
2013-10-24 17:49:55 +02:00
|
|
|
*
|
2014-03-24 16:53:18 +01:00
|
|
|
* @return ArrayIterator
|
2013-10-24 17:49:55 +02:00
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
return new ArrayIterator($this->toArray());
|
|
|
|
}
|
2013-10-24 17:49:55 +02:00
|
|
|
|
2013-10-22 17:02:30 +02:00
|
|
|
/**
|
2014-03-24 16:53:18 +01:00
|
|
|
* Create a new timeline
|
|
|
|
*
|
|
|
|
* The given dataview must provide the following columns:
|
|
|
|
* - name A string identifying an entry (Corresponds to the keys of "$identifiers")
|
|
|
|
* - time A unix timestamp that defines where to place an entry on the timeline
|
2013-10-22 17:02:30 +02:00
|
|
|
*
|
2014-03-24 16:53:18 +01:00
|
|
|
* @param DataView $dataview The dataview to fetch entries from
|
|
|
|
* @param array $identifiers The names by which to group entries
|
2013-10-22 17:02:30 +02:00
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
public function __construct(DataView $dataview, array $identifiers)
|
2013-10-22 17:02:30 +02:00
|
|
|
{
|
2014-03-24 16:53:18 +01:00
|
|
|
$this->dataview = $dataview;
|
|
|
|
$this->identifiers = $identifiers;
|
2013-10-22 17:02:30 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 14:19:50 +01:00
|
|
|
/**
|
|
|
|
* Set the session to use
|
|
|
|
*
|
|
|
|
* @param SessionNamespace $session The session to use
|
|
|
|
*/
|
|
|
|
public function setSession(SessionNamespace $session)
|
|
|
|
{
|
|
|
|
$this->session = $session;
|
|
|
|
}
|
|
|
|
|
2013-10-22 17:02:30 +02:00
|
|
|
/**
|
2014-03-24 16:53:18 +01:00
|
|
|
* Set the range of time for which to display elements
|
2013-10-22 17:02:30 +02:00
|
|
|
*
|
2014-03-24 16:53:18 +01:00
|
|
|
* @param TimeRange $range The range of time for which to display elements
|
2013-10-22 17:02:30 +02:00
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
public function setDisplayRange(TimeRange $range)
|
2013-10-22 17:02:30 +02:00
|
|
|
{
|
2014-03-24 16:53:18 +01:00
|
|
|
$this->displayRange = $range;
|
2013-10-22 17:02:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-24 16:53:18 +01:00
|
|
|
* Set the range of time for which to calculate forecasts
|
2013-10-22 17:02:30 +02:00
|
|
|
*
|
2014-03-24 16:53:18 +01:00
|
|
|
* @param TimeRange $range The range of time for which to calculate forecasts
|
2013-10-22 17:02:30 +02:00
|
|
|
*/
|
2014-03-24 16:53:18 +01:00
|
|
|
public function setForecastRange(TimeRange $range)
|
2013-10-22 17:02:30 +02:00
|
|
|
{
|
2014-03-24 16:53:18 +01:00
|
|
|
$this->forecastRange = $range;
|
2013-10-22 17:02:30 +02:00
|
|
|
}
|
|
|
|
|
2013-10-24 17:49:55 +02:00
|
|
|
/**
|
|
|
|
* Set the maximum diameter each circle can have
|
|
|
|
*
|
2014-03-24 16:53:18 +01:00
|
|
|
* @param string $width The diameter to set, suffixed with its unit
|
|
|
|
*
|
|
|
|
* @throws Exception If the given diameter is invalid
|
2013-10-24 17:49:55 +02:00
|
|
|
*/
|
|
|
|
public function setMaximumCircleWidth($width)
|
|
|
|
{
|
|
|
|
$matches = array();
|
2014-03-27 10:19:50 +01:00
|
|
|
if (preg_match('#([\d|\.]+)([a-z]+|%)#', $width, $matches)) {
|
|
|
|
$this->circleDiameter = floatval($matches[1]);
|
2013-10-24 17:49:55 +02:00
|
|
|
$this->diameterUnit = $matches[2];
|
|
|
|
} else {
|
2014-08-27 16:03:15 +02:00
|
|
|
throw new IcingaException(
|
|
|
|
'Width "%s" is not a valid width',
|
|
|
|
$width
|
|
|
|
);
|
2013-10-24 17:49:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-27 10:19:50 +01:00
|
|
|
/**
|
|
|
|
* Set the minimum diameter each circle can have
|
|
|
|
*
|
|
|
|
* @param string $width The diameter to set, suffixed with its unit
|
|
|
|
*
|
|
|
|
* @throws Exception If the given diameter is invalid or its unit differs from the maximum
|
|
|
|
*/
|
|
|
|
public function setMinimumCircleWidth($width)
|
|
|
|
{
|
|
|
|
$matches = array();
|
|
|
|
if (preg_match('#([\d|\.]+)([a-z]+|%)#', $width, $matches)) {
|
|
|
|
if ($matches[2] === $this->diameterUnit) {
|
|
|
|
$this->minCircleDiameter = floatval($matches[1]);
|
|
|
|
} else {
|
2014-08-27 16:03:15 +02:00
|
|
|
throw new IcingaException(
|
|
|
|
'Unit needs to be in "%s"',
|
|
|
|
$this->diameterUnit
|
|
|
|
);
|
2014-03-27 10:19:50 +01:00
|
|
|
}
|
|
|
|
} else {
|
2014-08-27 16:03:15 +02:00
|
|
|
throw new IcingaException(
|
|
|
|
'Width "%s" is not a valid width',
|
|
|
|
$width
|
|
|
|
);
|
2014-03-27 10:19:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-27 09:38:57 +01:00
|
|
|
/**
|
|
|
|
* Return all known group types (identifiers) with their respective labels and colors as array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getGroupInfo()
|
|
|
|
{
|
|
|
|
$groupInfo = array();
|
|
|
|
foreach ($this->identifiers as $name => $attributes) {
|
|
|
|
$groupInfo[$name]['label'] = $attributes['label'];
|
|
|
|
$groupInfo[$name]['color'] = $attributes['color'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $groupInfo;
|
|
|
|
}
|
|
|
|
|
2014-03-24 16:53:18 +01:00
|
|
|
/**
|
|
|
|
* Return the circle's diameter for the given event group
|
|
|
|
*
|
|
|
|
* @param TimeEntry $group The group for which to return a circle width
|
|
|
|
* @param int $precision Amount of decimal places to preserve
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function calculateCircleWidth(TimeEntry $group, $precision = 0)
|
|
|
|
{
|
|
|
|
$base = $this->getCalculationBase(true);
|
|
|
|
$factor = log($group->getValue() * $group->getWeight(), $base) / 100;
|
2014-03-27 10:19:50 +01:00
|
|
|
$width = $this->circleDiameter * $factor;
|
|
|
|
return sprintf(
|
|
|
|
'%.' . $precision . 'F%s',
|
|
|
|
$width > $this->minCircleDiameter ? $width : $this->minCircleDiameter,
|
|
|
|
$this->diameterUnit
|
|
|
|
);
|
2014-03-24 16:53:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an extrapolated circle width for the given event group
|
|
|
|
*
|
|
|
|
* @param TimeEntry $group The event group for which to return an extrapolated circle width
|
|
|
|
* @param int $precision Amount of decimal places to preserve
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getExtrapolatedCircleWidth(TimeEntry $group, $precision = 0)
|
|
|
|
{
|
|
|
|
$eventCount = 0;
|
|
|
|
foreach ($this->displayGroups as $groups) {
|
|
|
|
if (array_key_exists($group->getName(), $groups)) {
|
|
|
|
$eventCount += $groups[$group->getName()]->getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$extrapolatedCount = (int) $eventCount / count($this->displayGroups);
|
|
|
|
if ($extrapolatedCount < $group->getValue()) {
|
|
|
|
return $this->calculateCircleWidth($group, $precision);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->calculateCircleWidth(
|
|
|
|
TimeEntry::fromArray(
|
|
|
|
array(
|
|
|
|
'value' => $extrapolatedCount,
|
|
|
|
'weight' => $group->getWeight()
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$precision
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the base that should be used to calculate circle widths
|
|
|
|
*
|
|
|
|
* @param bool $create Whether to generate a new base if none is known yet
|
|
|
|
*
|
|
|
|
* @return float|null
|
|
|
|
*/
|
|
|
|
public function getCalculationBase($create)
|
|
|
|
{
|
|
|
|
if ($this->calculationBase === null) {
|
2014-04-01 11:43:03 +02:00
|
|
|
$calculationBase = $this->session !== null ? $this->session->get('calculationBase') : null;
|
2014-03-25 14:19:50 +01:00
|
|
|
|
2014-03-24 16:53:18 +01:00
|
|
|
if ($create) {
|
|
|
|
$new = $this->generateCalculationBase();
|
2014-04-01 11:43:03 +02:00
|
|
|
if ($new > $calculationBase) {
|
2014-03-24 16:53:18 +01:00
|
|
|
$this->calculationBase = $new;
|
2014-03-25 14:19:50 +01:00
|
|
|
|
|
|
|
if ($this->session !== null) {
|
|
|
|
$this->session->calculationBase = $new;
|
|
|
|
}
|
2014-04-01 11:43:03 +02:00
|
|
|
} else {
|
|
|
|
$this->calculationBase = $calculationBase;
|
2014-03-24 16:53:18 +01:00
|
|
|
}
|
2014-04-01 11:43:03 +02:00
|
|
|
} else {
|
|
|
|
return $calculationBase;
|
2014-03-24 16:53:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->calculationBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a new base to calculate circle widths with
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
protected function generateCalculationBase()
|
|
|
|
{
|
|
|
|
$allEntries = $this->groupEntries(
|
|
|
|
array_merge(
|
|
|
|
$this->fetchEntries(),
|
|
|
|
$this->fetchForecasts()
|
|
|
|
),
|
|
|
|
new TimeRange(
|
|
|
|
$this->displayRange->getStart(),
|
|
|
|
$this->forecastRange->getEnd(),
|
|
|
|
$this->displayRange->getInterval()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$highestValue = 0;
|
|
|
|
foreach ($allEntries as $groups) {
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
if ($group->getValue() * $group->getWeight() > $highestValue) {
|
|
|
|
$highestValue = $group->getValue() * $group->getWeight();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 14:19:50 +01:00
|
|
|
return pow($highestValue, 1 / 100); // 100 == 100%
|
2014-03-24 16:53:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch all entries and forecasts by using the dataview associated with this timeline
|
|
|
|
*
|
|
|
|
* @return array The dataview's result
|
|
|
|
*/
|
|
|
|
private function fetchResults()
|
|
|
|
{
|
|
|
|
$hookResults = array();
|
|
|
|
foreach (Hook::all('timeline') as $timelineProvider) {
|
|
|
|
$hookResults = array_merge(
|
|
|
|
$hookResults,
|
|
|
|
$timelineProvider->fetchEntries($this->displayRange),
|
|
|
|
$timelineProvider->fetchForecasts($this->forecastRange)
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($timelineProvider->getIdentifiers() as $identifier => $attributes) {
|
|
|
|
if (!array_key_exists($identifier, $this->identifiers)) {
|
|
|
|
$this->identifiers[$identifier] = $attributes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 03:09:40 +02:00
|
|
|
$query = $this->dataview;
|
|
|
|
$filter = Filter::matchAll(
|
|
|
|
Filter::where('type', array_keys($this->identifiers)),
|
|
|
|
Filter::expression('timestamp', '<=', $this->displayRange->getStart()->getTimestamp()),
|
|
|
|
Filter::expression('timestamp', '>', $this->displayRange->getEnd()->getTimestamp())
|
|
|
|
);
|
|
|
|
$query->applyFilter($filter);
|
|
|
|
return array_merge($query->getQuery()->fetchAll(), $hookResults);
|
2014-03-24 16:53:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch all entries
|
|
|
|
*
|
|
|
|
* @return array The entries to display on the timeline
|
|
|
|
*/
|
|
|
|
protected function fetchEntries()
|
|
|
|
{
|
|
|
|
if ($this->resultset === null) {
|
|
|
|
$this->resultset = $this->fetchResults();
|
|
|
|
}
|
|
|
|
|
|
|
|
$range = $this->displayRange;
|
|
|
|
return array_filter(
|
|
|
|
$this->resultset,
|
|
|
|
function ($e) use ($range) { return $range->validateTime($e->time); }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch all forecasts
|
|
|
|
*
|
|
|
|
* @return array The entries to calculate forecasts with
|
|
|
|
*/
|
|
|
|
protected function fetchForecasts()
|
|
|
|
{
|
|
|
|
if ($this->resultset === null) {
|
|
|
|
$this->resultset = $this->fetchResults();
|
|
|
|
}
|
|
|
|
|
|
|
|
$range = $this->forecastRange;
|
|
|
|
return array_filter(
|
|
|
|
$this->resultset,
|
|
|
|
function ($e) use ($range) { return $range->validateTime($e->time); }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the given entries grouped together
|
|
|
|
*
|
|
|
|
* @param array $entries The entries to group
|
|
|
|
* @param TimeRange $timeRange The range of time to group by
|
|
|
|
*
|
|
|
|
* @return array displayGroups The grouped entries
|
|
|
|
*/
|
|
|
|
protected function groupEntries(array $entries, TimeRange $timeRange)
|
|
|
|
{
|
|
|
|
$counts = array();
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
$entryTime = new DateTime();
|
|
|
|
$entryTime->setTimestamp($entry->time);
|
|
|
|
$timestamp = $timeRange->findTimeframe($entryTime, true);
|
|
|
|
|
|
|
|
if ($timestamp !== null) {
|
|
|
|
if (array_key_exists($entry->name, $counts)) {
|
|
|
|
if (array_key_exists($timestamp, $counts[$entry->name])) {
|
|
|
|
$counts[$entry->name][$timestamp] += 1;
|
|
|
|
} else {
|
|
|
|
$counts[$entry->name][$timestamp] = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$counts[$entry->name][$timestamp] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$groups = array();
|
|
|
|
foreach ($counts as $name => $data) {
|
|
|
|
foreach ($data as $timestamp => $count) {
|
|
|
|
$dateTime = new DateTime();
|
|
|
|
$dateTime->setTimestamp($timestamp);
|
|
|
|
$groups[$timestamp][$name] = TimeEntry::fromArray(
|
|
|
|
array_merge(
|
|
|
|
$this->identifiers[$name],
|
|
|
|
array(
|
|
|
|
'name' => $name,
|
|
|
|
'value' => $count,
|
|
|
|
'dateTime' => $dateTime
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the contents of this timeline as array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function toArray()
|
|
|
|
{
|
|
|
|
$this->displayGroups = $this->groupEntries($this->fetchEntries(), $this->displayRange);
|
|
|
|
|
|
|
|
$array = array();
|
|
|
|
foreach ($this->displayRange as $timestamp => $timeframe) {
|
|
|
|
$array[] = array(
|
|
|
|
$timeframe,
|
|
|
|
array_key_exists($timestamp, $this->displayGroups) ? $this->displayGroups[$timestamp] : array()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
2013-10-22 17:02:30 +02:00
|
|
|
}
|