Make interval selection being a widget

refs #4190
This commit is contained in:
Johannes Meyer 2014-03-20 15:39:04 +01:00
parent aade2ab7f6
commit 5e945e3194
4 changed files with 157 additions and 68 deletions

View File

@ -36,12 +36,14 @@ use Icinga\Web\Controller\ActionController;
use Icinga\Module\Monitoring\Timeline\TimeLine;
use Icinga\Module\Monitoring\Timeline\TimeEntry;
use Icinga\Module\Monitoring\Timeline\TimeRange;
use Icinga\Module\Monitoring\Web\Widget\TimelineIntervalBox;
use Icinga\Module\Monitoring\DataView\EventHistory as EventHistoryView;
class Monitoring_TimelineController extends ActionController
{
public function showAction()
{
$this->setupIntervalBox();
$timeline = new TimeLine();
$timeline->setName('Timeline');
$timeline->setRequest($this->_request);
@ -49,7 +51,7 @@ class Monitoring_TimelineController extends ActionController
$timeline->buildForm(); // Necessary in order to populate request parameters
$timeline->populate($this->_request->getParams());
$timeline->setAttrib('data-icinga-component', 'monitoring/timelineComponent');
list($displayRange, $forecastRange) = $this->buildTimeRanges($timeline->getInterval());
list($displayRange, $forecastRange) = $this->buildTimeRanges($this->getTimelineInterval());
$timeline->setTimeRange($displayRange);
$timeline->setDisplayData($this->loadData($displayRange));
$timeline->setForecastData($this->loadData($forecastRange));
@ -58,11 +60,11 @@ class Monitoring_TimelineController extends ActionController
public function extendAction()
{
$this->setupIntervalBox();
$timeline = new TimeLine();
$timeline->showLineOnly();
$timeline->setRequest($this->_request);
$timeline->setConfiguration(Config::app());
list($displayRange, $forecastRange) = $this->buildTimeRanges($timeline->getInterval());
list($displayRange, $forecastRange) = $this->buildTimeRanges($this->getTimelineInterval());
$timeline->setTimeRange($displayRange);
$timeline->setDisplayData($this->loadData($displayRange));
$timeline->setForecastData($this->loadData($forecastRange));
@ -72,6 +74,47 @@ class Monitoring_TimelineController extends ActionController
$this->_helper->layout()->disableLayout();
}
/**
* Create a select box the user can choose the timeline interval from
*/
private function setupIntervalBox()
{
$box = new TimelineIntervalBox(
'intervalBox',
array(
'4h' => t('4 Hours'),
'1d' => t('One day'),
'1w' => t('One week'),
'1m' => t('One month'),
'1y' => t('One year')
)
);
$box->applyRequest($this->getRequest());
$this->view->intervalBox = $box;
}
/**
* Return the chosen interval
*
* @return DateInterval The chosen interval
*/
private function getTimelineInterval()
{
switch ($this->view->intervalBox->getInterval())
{
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:
return new DateInterval('PT4H');
}
}
/**
* Return a new display- and forecast time range
*

View File

@ -1 +1,8 @@
<?= $this->timeline ?>
<?php if (!$this->compact): ?>
<div class="controls">
<div style="margin: 1em;" class="dontprint">
<?= $this->intervalBox; ?>
</div>
</div>
<?php endif ?>
<?= $this->timeline; ?>

View File

@ -62,13 +62,6 @@ class TimeLine extends Form
*/
private $forecastData;
/**
* Whether only the timeline is shown
*
* @var bool
*/
private $hideOuterElements = false;
/**
* The maximum diameter each circle can have
*
@ -120,14 +113,6 @@ class TimeLine extends Form
$this->forecastData = $entries;
}
/**
* Define that only the timeline itself should be rendered
*/
public function showLineOnly()
{
$this->hideOuterElements = true;
}
/**
* Set the maximum diameter each circle can have
*
@ -145,31 +130,6 @@ class TimeLine extends Form
}
}
/**
* 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()->getParam('timelineInterval', '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
*/
@ -191,30 +151,6 @@ class TimeLine extends Form
return parent::render($view);
}
/**
* Add form elements
*/
public function create()
{
if (!$this->hideOuterElements) {
$this->addElement(
'select',
'timelineInterval',
array(
'multiOptions' => array(
'4h' => t('4 Hours'),
'1d' => t('One day'),
'1w' => t('One week'),
'1m' => t('One month'),
'1y' => t('One year')
)
)
);
$this->enableAutoSubmit(array('timelineInterval'));
$this->setIgnoreChangeDiscarding(false);
}
}
/**
* Add timeline elements
*/

View File

@ -0,0 +1,103 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Module\Monitoring\Web\Widget;
use Icinga\Web\Form;
use Icinga\Web\Request;
use Icinga\Web\Widget\AbstractWidget;
class TimelineIntervalBox extends AbstractWidget
{
/**
* The name of the form that will be created
*
* @var string
*/
private $name;
/**
* An array containing all intervals with their associated labels
*
* @var array
*/
private $values;
/**
* A request object used for initial form population
*
* @var Request
*/
private $request;
/**
* Create a TimelineIntervalBox
*
* @param string $name The name of the form that will be created
* @param array $values An array containing all intervals with their associated labels
*/
public function __construct($name, array $values)
{
$this->name = $name;
$this->values = $values;
}
/**
* Apply the parameters from the given request on this widget
*
* @param Request $request The request to use for populating the form
*/
public function applyRequest(Request $request)
{
$this->request = $request;
}
/**
* Return the chosen interval value or null
*
* @param Request $request The request to fetch the value from
*
* @return string|null
*/
public function getInterval(Request $request = null)
{
if ($request === null && $this->request) {
$request = $this->request;
}
if ($request) {
return $request->getParam('timelineInterval');
}
}
/**
* Renders this widget and returns the HTML as a string
*
* @return string
*/
public function render()
{
$form = new Form();
$form->setAttrib('class', 'inline');
$form->setMethod('GET');
$form->setTokenDisabled();
$form->setName($this->name);
$form->addElement(
'select',
'timelineInterval',
array(
'label' => 'Timeline Interval',
'multiOptions' => $this->values,
'class' => 'autosubmit'
)
);
if ($this->request) {
$form->setAction($this->request->getRequestUri());
$form->populate($this->request->getParams());
}
return $form;
}
}