Add additional state history filters for time interval and event type

This commit is contained in:
Matthias Jentsch 2014-09-16 15:58:24 +02:00
parent dc05b2e933
commit 65203fddcf
3 changed files with 186 additions and 37 deletions

View File

@ -28,29 +28,33 @@ class HistoryColorGrid extends AbstractWidget {
private $maxValue = 1;
private $start = null;
private $end = null;
private $data = array();
private $color;
public function __construct($color = '#51e551') {
public function __construct($color = '#51e551', $start = null, $end = null) {
$this->setColor($color);
if (isset($start)) {
$this->start = $this->tsToDateStr($start);
}
if (isset($end)) {
$this->end = $this->tsToDateStr($end);
}
}
/**
* Set the displayed data-set
*
* @param $data array The values to display.
* properties for each entry:
* @param $events array The history events to display as an array of arrays:
* value: The value to display
* caption: The caption on mouse-over
* url: The url to open on click.
*/
public function setData(array $data)
public function setData(array $events)
{
$this->data = $data;
$this->data = $events;
$start = time();
$end = time();
foreach ($this->data as $entry) {
@ -68,8 +72,12 @@ class HistoryColorGrid extends AbstractWidget {
$start = $time;
}
}
$this->start = $this->tsToDateStr($start);
$this->end = $this->tsToDateStr($end);
if (!isset($this->start)) {
$this->start = $this->tsToDateStr($start);
}
if (!isset($this->end)) {
$this->end = $this->tsToDateStr($end);
}
}
/**
@ -130,13 +138,14 @@ class HistoryColorGrid extends AbstractWidget {
{
$weeks = $grid['weeks'];
$months = $grid['months'];
$years = $grid['years'];
$html = '<table class="historycolorgrid">';
$html .= '<tr><th></th>';
$old = -1;
foreach ($months as $month) {
foreach ($months as $week => $month) {
if ($old !== $month) {
$old = $month;
$txt = $this->monthName($month);
$txt = $this->monthName($month, $years[$week]);
} else {
$txt = '';
}
@ -157,6 +166,7 @@ class HistoryColorGrid extends AbstractWidget {
*/
private function renderVertical($grid)
{
$years = $grid['years'];
$weeks = $grid['weeks'];
$months = $grid['months'];
$html = '<table class="historycolorgrid">';
@ -176,7 +186,7 @@ class HistoryColorGrid extends AbstractWidget {
}
if ($old !== $months[$index]) {
$old = $months[$index];
$txt = $this->monthName($old);
$txt = $this->monthName($old, $years[$index]);
} else {
$txt = '';
}
@ -220,6 +230,7 @@ class HistoryColorGrid extends AbstractWidget {
$weeks = array(array());
$week = 0;
$months = array();
$years = array();
$start = strtotime($this->start);
$year = intval(date('Y', $start));
$month = intval(date('n', $start));
@ -232,6 +243,7 @@ class HistoryColorGrid extends AbstractWidget {
$date = $this->toDateStr($day, $month, $year);
$weeks[0][$weekday] = $date;
$years[0] = $year;
$months[0] = $month;
while ($date !== $this->end) {
$day++;
@ -242,6 +254,7 @@ class HistoryColorGrid extends AbstractWidget {
// PRESENT => The last day of week determines the month
if ($this->weekFlow === self::CAL_GROW_INTO_PRESENT) {
$months[$week] = $month;
$years[$week] = $year;
}
$week++;
}
@ -257,21 +270,25 @@ class HistoryColorGrid extends AbstractWidget {
// PAST => The first day of each week determines the month
if ($this->weekFlow === self::CAL_GROW_INTO_PAST) {
$months[$week] = $month;
$years[$week] = $year;
}
}
$date = $this->toDateStr($day, $month, $year);
$weeks[$week][$weekday] = $date;
};
$years[$week] = $year;
$months[$week] = $month;
if ($this->weekFlow == self::CAL_GROW_INTO_PAST) {
return array(
'weeks' => array_reverse($weeks),
'months' => array_reverse($months)
'months' => array_reverse($months),
'years' => array_reverse($years)
);
}
return array(
'weeks' => $weeks,
'months' => $months
'months' => $months,
'years' => $years
);
}
@ -282,9 +299,10 @@ class HistoryColorGrid extends AbstractWidget {
*
* @return string The
*/
private function monthName($month)
private function monthName($month, $year)
{
$dt = DateTimeFactory::create('2000-' . $month . '-01');
// TODO: find a way to render years without messing up the layout
$dt = DateTimeFactory::create($year . '-' . $month . '-01');
return $dt->format('M');
}

View File

@ -14,6 +14,7 @@ use Icinga\Web\Widget\FilterBox;
use Icinga\Web\Widget\Chart\HistoryColorGrid;
use Icinga\Data\Filter\Filter;
use Icinga\Web\Widget;
use Icinga\Module\Monitoring\Web\Widget\SelectBox;
class Monitoring_ListController extends Controller
{
@ -359,15 +360,100 @@ class Monitoring_ListController extends Controller
public function statehistorysummaryAction()
{
$this->addTitleTab('statehistorysummary', 'Critical Events');
$this->view->from = $this->params->shift('from', '3 months ago');
$this->addTitleTab('statehistorysummary', 'State Summary');
$selections = array(
'critical' => array(
'column' => 'cnt_critical',
'filter' => Filter::matchAll(
Filter::expression('object_type', '=', 'service'),
Filter::expression('state', '=', '2')
),
'tooltip' => t('%d critical events on %s'),
'color' => '#ff5566', 'opacity' => '0.9'
),
'warning' => array(
'column' => 'cnt_warning',
'filter' => Filter::matchAll(
Filter::expression('object_type', '=', 'service'),
Filter::expression('state', '=', '1')
),
'tooltip' => t('%d warning events on %s'),
'color' => '#ffaa44', 'opacity' => '1.0'
),
'unknown' => array(
'column' => 'cnt_unknown',
'filter' => Filter::matchAll(
Filter::expression('object_type', '=', 'service'),
Filter::expression('state', '=', '3')
),
'tooltip' => t('%d unknown events on %s'),
'color' => '#cc77ff', 'opacity' => '0.7'
),
'ok' => array(
'column' => 'cnt_ok',
'filter' => Filter::matchAll(
Filter::expression('object_type', '=', 'service'),
Filter::expression('state', '=', '0')
),
'tooltip' => t('%d ok events on %s'),
'color' => '#49DF96', 'opacity' => '0.55'
)
);
$eventBox = new SelectBox(
'statehistoryfilter',
array(
'critical' => t('Critical'),
'warning' => t('Warning'),
'unknown' => t('Unknown'),
'ok' => t('Ok')
),
t('Events'),
'event'
);
$eventBox->applyRequest($this->getRequest());
$orientationBox = new SelectBox(
'orientation',
array(
'0' => t('Vertical'),
'1' => t('Horizontal')
),
t('Orientation'),
'horizontal'
);
$orientationBox->applyRequest($this->getRequest());
$intervalBox = new SelectBox(
'from',
array(
'3 months ago' => t('3 Months'),
'4 months ago' => t('4 Months'),
'8 months ago' => t('8 Months'),
'12 months ago' => t('1 Year'),
'24 months ago' => t('2 Years')
),
t('Interval'),
'from'
);
$intervalBox->applyRequest($this->getRequest());
$eventtype = $this->params->shift('event', 'critical');
$orientation = $this->params->shift('horizontal', 0) ? 'horizontal' : 'vertical';
$selection = $selections[$eventtype];
$query = $this->backend->select()->from(
'stateHistorySummary',
array('day', 'cnt_critical')
)->getQuery()->order('day');
$query->limit(365);
$this->view->summary = $query->fetchAll();
$this->view->grid = new HistoryColorGrid();
$this->handleFormatRequest($query);
array('day', $selection['column'])
);
$this->applyFilters($query);
$this->view->orientationBox = $orientationBox;
$this->view->eventBox = $eventBox;
$this->view->selection = $selection;
$this->view->orientation = $orientation;
$this->view->summary = $query->getQuery()->fetchAll();
$this->view->intervalBox = $intervalBox;
}
public function contactgroupsAction()

View File

@ -1,34 +1,47 @@
<?php
use Icinga\Data\Filter\Filter;
use Icinga\Web\Widget\Chart\HistoryColorGrid;
?>
<? if (!$compact): ?>
<div class="controls">
<?= $this->tabs->render($this); ?>
<div class="fake-controls">
<br />
<?= $intervalBox ?>
<?= $eventBox; ?>
<?= $orientationBox; ?>
</div>
</div>
<? endif; ?>
?><div class="controls">
<?= $this->tabs ?>
<h1>History - Critical Events</h1>
</div>
<div class="content" data-base-target="_next">
<?php
$grid->setColor('#f05060');
$from = strtotime($from);
$to = time();
$data = array();
if (count($summary) === 0) {
echo t('No history entry matching the filter');
echo t('No events in the selected time period.');
}
foreach ($summary as $entry) {
$day = $entry->day;
$value = $entry->cnt_critical;
$caption = $value . ' ' .
t('critical events on ') . $this->dateFormat()->formatDate(strtotime($day));
$value = $entry->{$selection['column']};
$caption = sprintf(
$selection['tooltip'],
$value,
$this->dateFormat()->formatDate(strtotime($day))
);
$filter = Filter::matchAll(
Filter::expression('timestamp', '<', strtotime($day . ' 23:59:59')),
Filter::expression('timestamp', '>', strtotime($day . ' 00:00:00')),
Filter::expression('object_type', '=', 'service'),
Filter::expression('state', '=', '2'),
Filter::matchAny(
Filter::expression('type', '=', 'hard_state'),
Filter::expression('type', '=', 'hard_state')
)
),
$selection['filter']
);
$data[$day] = array(
'value' => $value,
@ -36,7 +49,39 @@ foreach ($summary as $entry) {
'url' => $this->href('monitoring/list/eventhistory?' . $filter->toQueryString())
);
}
$grid->setData($data);
$f = new DateTime();
$f->setTimestamp($from);
$t = new DateTime();
$t->setTimestamp($to);
$diff = $t->diff($f);
$step = 124;
for ($i = 0; $i < $diff->days; $i += $step) {
$end = clone $f;
if ($diff->days - $i > $step) {
// full range, move last day to next chunk
$end->add(new DateInterval('P' . ($step - 1) . 'D'));
} else {
// include last day
$end->add(new DateInterval('P' . ($diff->days - $i) . 'D'));
}
$grid = new HistoryColorGrid(null, $f->getTimestamp(), $end->getTimestamp());
$grid->setColor($selection['color']);
$grid->opacity = $selection['opacity'];
$grid->orientation = $orientation;
$grid->setData($data);
$grids[] = $grid;
$f->add(new DateInterval('P' . $step . 'D'));
}
?>
<?= $grid ?>
<div style="width: 33.5em;">
<? foreach (array_reverse($grids) as $key => $grid) { ?>
<div style=" <?= $this->orientation === 'horizontal' ? '' : 'display: inline-block; vertical-align: top; top; margin: 0.5em;' ?>">
<?= $grid; ?>
<?= $this->orientation === 'horizontal' ? '<br />' : '' ?>
</div>
<? } ?>
</div>
</div>