Merge branch 'feature/option-default-time-range-98'

fixes #98
This commit is contained in:
Alexander A. Klimov 2017-12-18 11:31:13 +01:00
commit daea0c2c78
5 changed files with 81 additions and 24 deletions

View file

@ -17,6 +17,33 @@ class AdvancedForm extends ConfigForm
public function createElements(array $formData)
{
$this->addElements([
[
'number',
'ui_default_time_range',
[
'label' => $this->translate('Default time range'),
'description' => $this->translate('The default time range for graphs'),
'min' => 1,
'value' => 1
]
],
[
'select',
'ui_default_time_range_unit',
[
'label' => $this->translate('Default time range unit'),
'description' => $this->translate('The above range\'s unit'),
'multiOptions' => [
'minutes' => $this->translate('Minutes'),
'hours' => $this->translate('Hours'),
'days' => $this->translate('Days'),
'weeks' => $this->translate('Weeks'),
'months' => $this->translate('Months'),
'years' => $this->translate('Years')
],
'value' => 'hours'
]
],
[
'text',
'icinga_graphite_writer_host_name_template',

View file

@ -146,13 +146,16 @@ class CommonForm extends Form
*/
protected function urlToForm()
{
if ($this->preSelectDefault()) {
return;
}
$params = $this->getRedirectUrl()->getParams();
$seconds = $this->getRelativeSeconds($params);
if ($seconds === null && count(array_intersect_key(
$params->toArray(false),
array_keys($this->getAllRangeParameters())
)) === 0) {
$seconds = $this->getDefaultRelativeTimeRange();
}
if ($seconds !== null) {
if ($seconds !== false) {
foreach ($this->rangeFactors as $unit => $factor) {
@ -191,23 +194,4 @@ class CommonForm extends Form
}
}
}
/**
* If no range is specified, pre-select "1 hour"
*
* @return bool Whether no range is specified
*/
protected function preSelectDefault()
{
$params = $this->getRedirectUrl()->getParams();
foreach (static::getAllRangeParameters() as $parameter) {
if ($params->has($parameter)) {
return false;
}
}
$this->getElement('hours')->setValue('1');
return true;
}
}

View file

@ -2,6 +2,8 @@
namespace Icinga\Module\Graphite\Forms\TimeRangePicker;
use Icinga\Application\Config;
use Icinga\Exception\ConfigurationError;
use Icinga\Web\Url;
use Icinga\Web\UrlParams;
@ -74,4 +76,36 @@ trait TimeRangePickerTrait
return preg_match('/^(?:0|[1-9]\d*)$/', $seconds) ? (int) $seconds : false;
}
/**
* Get the default relative time range for graphs
*
* @return int
*
* @throws ConfigurationError
*/
public static function getDefaultRelativeTimeRange()
{
$rangeFactors = [
'minutes' => 60,
'hours' => 3600,
'days' => 86400,
'weeks' => 604800,
'months' => 2592000,
'years' => 31557600
];
$config = Config::module('graphite');
$unit = $config->get('ui', 'default_time_range_unit', 'hours');
if (! isset($rangeFactors[$unit])) {
throw new ConfigurationError(
'Bad ui.default_time_range_unit %s in file %s',
var_export($unit, true),
var_export($config->getConfigFile(), true)
);
}
return (int) $config->get('ui', 'default_time_range', 1) * $rangeFactors[$unit];
}
}

View file

@ -17,6 +17,14 @@ Open up the Icinga Web 2 frontend and navigate to:
Configuration > Modules > graphite > Advanced
### UI
The settings *Default time range* and *Default time range unit* set the default
time range for displayed graphs both in the graphs lists and in monitored
objects' detail views.
### Icinga 2 (Core)
The settings *Host name template* and *Service name template* both are only
required if you are using a different naming schema than the default Icinga 2
is using. (As outlined [here](https://www.icinga.com/docs/icinga2/latest/doc/14-features/#current-graphite-schema))

View file

@ -308,7 +308,11 @@ abstract class Graphs extends AbstractWidget
}
$absolute = TimeRangePickerTrait::getAbsoluteRangeParameters();
return [$params->get($absolute['start'], '-3600'), $params->get($absolute['end'])];
$start = $params->get($absolute['start']);
return [
$start === null ? -TimeRangePickerTrait::getDefaultRelativeTimeRange() : $start,
$params->get($absolute['end'])
];
}
/**