Skip to content

Commit

Permalink
Make TimeEntry creation more flexible
Browse files Browse the repository at this point in the history
TimeEntry objects are now created with the fromArray
constructor instead of the default one.

refs #4190
  • Loading branch information
Johannes Meyer committed Apr 1, 2014
1 parent b168cf8 commit a589460
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 58 deletions.
103 changes: 90 additions & 13 deletions library/Icinga/Timeline/TimeEntry.php
Expand Up @@ -29,6 +29,7 @@
namespace Icinga\Timeline;

use \DateTime;
use \Icinga\Exception\ProgrammingError;

/**
* An event group that is part of a timeline
Expand Down Expand Up @@ -71,18 +72,44 @@ class TimeEntry
private $weight = 1.0;

/**
* Initialize a new event group
* The color of this 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
* @var string
*/
public function __construct($name, $value, DateTime $dateTime, $detailUrl)
private $color;

/**
* Return a new TimeEntry object with the given attributes being set
*
* @param array $attributes The attributes to set
* @return TimeEntry The resulting TimeEntry object
* @throws ProgrammingError If one of the given attributes cannot be set
*/
public static function fromArray(array $attributes)
{
$entry = new TimeEntry();

foreach ($attributes as $name => $value) {
$methodName = 'set' . ucfirst($name);
if (method_exists($entry, $methodName)) {
$entry->{$methodName}($value);
} else {
throw new ProgrammingError(
'Method "' . $methodName . '" does not exist on object of type "' . __CLASS__ . '"'
);
}
}

return $entry;
}

/**
* Set this group's name
*
* @param string $name The name to set
*/
public function setName($name)
{
$this->detailUrl = $detailUrl;
$this->dateTime = $dateTime;
$this->value = $value;
$this->name = $name;
}

Expand All @@ -96,6 +123,16 @@ public function getName()
return $this->name;
}

/**
* Set this group's amount of events
*
* @param int $value The value to set
*/
public function setValue($value)
{
$this->value = intval($value);
}

/**
* Return the amount of events in this group
*
Expand All @@ -106,6 +143,16 @@ public function getValue()
return $this->value;
}

/**
* Set this group's date and time
*
* @param DateTime $dateTime The date and time to set
*/
public function setDateTime(DateTime $dateTime)
{
$this->dateTime = $dateTime;
}

/**
* Return the date and time of this group
*
Expand All @@ -116,6 +163,16 @@ public function getDateTime()
return $this->dateTime;
}

/**
* Set the url to this group's detail view
*
* @param string $detailUrl The url to set
*/
public function setDetailUrl($detailUrl)
{
$this->detailUrl = $detailUrl;
}

/**
* Return the url to this group's detail view
*
Expand All @@ -126,6 +183,16 @@ public function getDetailUrl()
return $this->detailUrl;
}

/**
* Set this group's weight
*
* @param float $weight The weight for this group
*/
public function setWeight($weight)
{
$this->weight = floatval($weight);
}

/**
* Return the weight of this group
*
Expand All @@ -137,12 +204,22 @@ public function getWeight()
}

/**
* Set the weight of this group
* Set this group's color
*
* @param float $weight The weight for this group
* @param string $color The color to set. (The css name or hex-code)
*/
public function setWeight($weight)
public function setColor($color)
{
$this->weight = floatval($weight);
$this->color = $color;
}

/**
* Get the color of this group
*
* @return string
*/
public function getColor()
{
return $this->color;
}
}
109 changes: 64 additions & 45 deletions modules/monitoring/application/controllers/TimelineController.php
Expand Up @@ -42,7 +42,10 @@ class Monitoring_TimelineController extends ActionController
public function showAction()
{
$timeline = new TimeLine();
$timeline->setName('Timeline');
$timeline->setRequest($this->_request);
$timeline->buildForm(); // Necessary in order to populate request parameters
$timeline->populate($this->_request->getParams());
list($displayRange, $forecastRange) = $this->buildTimeRanges($timeline->getInterval());
$timeline->setTimeRange($displayRange);
$timeline->setDisplayData($this->loadData($displayRange));
Expand Down Expand Up @@ -166,16 +169,17 @@ private function getPreloadInterval(DateInterval $interval)
/**
* Groups a set of elements based on a specific range of time
*
* @param TimeRange $range The range of time represented by the timeline
* @param array $elements The elements to group. Each element need to have a ´time´ property
* that defines its position in the given range of time
* @param string $groupName The name of each group
* @param string $groupUrl The url to the detailview of this group's type. Need to contain
* placeholders for both the start- and end time of a specific timeframe
* @return array A list of event groups suitable to pass to the timeline
* @throws ProgrammingError If an element is found that does not match the given range of time
* @param TimeRange $range The range of time represented by the timeline
* @param array $elements The elements to group. Each element need to have a ´time´ property
* that defines its position in the given range of time
* @param array $attributes The attributes to set on each event group. Need to contain at least
* a ´name´ and a ´detailUrl´. The detailUrl need also to contain
* placeholders for both the start- and end time of a specific timeframe
* @return array A list of event groups suitable to pass to the timeline
* @throws ProgrammingError If an element is found that does not match the given range of time
* or one of the required attributes is missing
*/
private function groupResults(TimeRange $range, array $elements, $groupName, $groupUrl)
private function groupResults(TimeRange $range, array $elements, array $attributes)
{
$groupCounts = array();
foreach ($elements as $element) {
Expand All @@ -199,36 +203,26 @@ private function groupResults(TimeRange $range, array $elements, $groupName, $gr
}
}

if (!array_key_exists('name', $attributes) || !array_key_exists('detailUrl', $attributes)) {
throw new ProgrammingError('Missing required event group attribute. Either ´name´ or ´detailUrl´');
}

$groups = array();
$urlTemplate = $attributes['detailUrl'];
foreach ($groupCounts as $timeframeIdentifier => $groupCount) {
$timeframe = $range->getTimeframe($timeframeIdentifier);
$groups[] = new TimeEntry(
$groupName,
$groupCount,
$timeframe->start,
sprintf(
$groupUrl,
$timeframe->start->getTimestamp(),
$timeframe->end->getTimestamp()
)
$attributes['value'] = $groupCount;
$attributes['detailUrl'] = sprintf(
$urlTemplate,
$timeframe->start->getTimestamp(),
$timeframe->end->getTimestamp()
);
$groups[] = TimeEntry::fromArray($attributes);
}

return $groups;
}

/**
* Set the given weight on each passed in event group
*
* @param array $groups A set of event groups for which to set the weight
* @param float $weight The weight to set
* @return array The set of adjusted event groups
*/
private function applyWeight(array $groups, $weight)
{
return array_map(function ($group) use ($weight) { $group->setWeight($weight); }, $groups);
}

/**
* Load the event groups that the timeline should display
*
Expand All @@ -237,7 +231,6 @@ private function applyWeight(array $groups, $weight)
*/
private function loadData(TimeRange $timeRange)
{
// TODO: Should a specific weight for a specific type of group(s) be set here?
$entries = array_merge(
$this->loadInitiatedDowntimes($timeRange),
$this->loadFinishedDowntimes($timeRange),
Expand Down Expand Up @@ -278,8 +271,14 @@ private function loadNotifications(TimeRange $range)
->where('state != 0')
->fetchAll();

$urlFilter = 'timestamp<=%s&timestamp>%s&type=notify&state!=0';
return $this->groupResults($range, $result, 'Notifications', 'monitoring/list/eventhistory?' . $urlFilter);
return $this->groupResults(
$range,
$result,
array(
'name' => t('Notifications'),
'detailUrl' => 'monitoring/list/eventhistory?timestamp<=%s&timestamp>%s&type=notify&state!=0'
)
);
}

/**
Expand All @@ -303,8 +302,14 @@ private function loadStateChanges(TimeRange $range)
->where('state != 0')
->fetchAll();

$urlFilter = 'timestamp<=%s&timestamp>%s&type=hard_state&state!=0';
return $this->groupResults($range, $result, 'Hard states', 'monitoring/list/eventhistory?' . $urlFilter);
return $this->groupResults(
$range,
$result,
array(
'name' => t('Hard states'),
'detailUrl' => 'monitoring/list/eventhistory?timestamp<=%s&timestamp>%s&type=hard_state&state!=0'
)
);
}

/**
Expand All @@ -327,8 +332,14 @@ private function loadComments(TimeRange $range)
->where('type = comment')
->fetchAll();

$urlFilter = 'timestamp<=%s&timestamp>%s&type=comment';
return $this->groupResults($range, $result, 'Comments', 'monitoring/list/eventhistory?' . $urlFilter);
return $this->groupResults(
$range,
$result,
array(
'name' => t('Comments'),
'detailUrl' => 'monitoring/list/eventhistory?timestamp<=%s&timestamp>%s&type=comment'
)
);
}

/**
Expand All @@ -351,8 +362,14 @@ private function loadAcknowledgements(TimeRange $range)
->where('type = ack')
->fetchAll();

$urlFilter = 'timestamp<=%s&timestamp>%s&type=ack';
return $this->groupResults($range, $result, 'Acknowledgements', 'monitoring/list/eventhistory?' . $urlFilter);
return $this->groupResults(
$range,
$result,
array(
'name' => t('Acknowledgements'),
'detailUrl' => 'monitoring/list/eventhistory?timestamp<=%s&timestamp>%s&type=ack'
)
);
}

/**
Expand All @@ -375,12 +392,13 @@ private function loadInitiatedDowntimes(TimeRange $range)
->where('type = dt_start')
->fetchAll();

$urlFilter = 'timestamp<=%s&timestamp>%s&type=dt_start';
return $this->groupResults(
$range,
$result,
'Initiated downtimes',
'monitoring/list/eventhistory?' . $urlFilter
array(
'name' => t('Initiated downtimes'),
'detailUrl' => 'monitoring/list/eventhistory?timestamp<=%s&timestamp>%s&type=dt_start'
)
);
}

Expand All @@ -404,12 +422,13 @@ private function loadFinishedDowntimes(TimeRange $range)
->where('type = dt_end')
->fetchAll();

$urlFilter = 'timestamp<=%s&timestamp>%s&type=dt_end';
return $this->groupResults(
$range,
$result,
'Finished downtimes',
'monitoring/list/eventhistory?' . $urlFilter
array(
'name' => t('Finished downtimes'),
'detailUrl' => 'monitoring/list/eventhistory?timestamp<=%s&timestamp>%s&type=dt_end'
)
);
}
}

0 comments on commit a589460

Please sign in to comment.