diff --git a/application/forms/Config/AdvancedForm.php b/application/forms/Config/AdvancedForm.php index 896d037..0bb4bec 100644 --- a/application/forms/Config/AdvancedForm.php +++ b/application/forms/Config/AdvancedForm.php @@ -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', diff --git a/application/forms/TimeRangePicker/CommonForm.php b/application/forms/TimeRangePicker/CommonForm.php index b3f099f..8ced5f0 100644 --- a/application/forms/TimeRangePicker/CommonForm.php +++ b/application/forms/TimeRangePicker/CommonForm.php @@ -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; - } } diff --git a/application/forms/TimeRangePicker/TimeRangePickerTrait.php b/application/forms/TimeRangePicker/TimeRangePickerTrait.php index de797ad..276754b 100644 --- a/application/forms/TimeRangePicker/TimeRangePickerTrait.php +++ b/application/forms/TimeRangePicker/TimeRangePickerTrait.php @@ -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]; + } } diff --git a/doc/03-Configuration.md b/doc/03-Configuration.md index d2eeeba..f616317 100644 --- a/doc/03-Configuration.md +++ b/doc/03-Configuration.md @@ -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)) diff --git a/library/Graphite/Web/Widget/Graphs.php b/library/Graphite/Web/Widget/Graphs.php index b36679b..557963e 100644 --- a/library/Graphite/Web/Widget/Graphs.php +++ b/library/Graphite/Web/Widget/Graphs.php @@ -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']) + ]; } /**