1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
<?php
namespace Icinga\Module\Graphite\Util;
use Icinga\Application\Config;
use Icinga\Exception\ConfigurationError;
use Icinga\Web\Url;
use Icinga\Web\UrlParams;
final class TimeRangePickerTools
{
/**
* @return string
*/
public static function getRelativeRangeParameter()
{
return 'graph_range';
}
/**
* @return string[]
*/
public static function getAbsoluteRangeParameters()
{
return ['start' => 'graph_start', 'end' => 'graph_end'];
}
/**
* @return string[]
*/
public static function getAllRangeParameters()
{
return array_values(array_merge([static::getRelativeRangeParameter()], static::getAbsoluteRangeParameters()));
}
/**
* Copy {@link getAllRangeParameters()} from one {@link UrlParams} instance to another
*
* @param UrlParams|null $copy Defaults to a new instance
* @param UrlParams|null $origin Defaults to the current request's params
*
* @return UrlParams The copy
*/
public static function copyAllRangeParameters(UrlParams $copy = null, UrlParams $origin = null)
{
if ($origin === null) {
$origin = Url::fromRequest()->getParams();
}
if ($copy === null) {
$copy = new UrlParams();
}
foreach (self::getAllRangeParameters() as $param) {
$value = $origin->get($param);
if ($value !== null) {
$copy->set($param, $value);
}
}
return $copy;
}
/**
* Extract the relative time range (if any) from the given URL parameters
*
* @param UrlParams $params
*
* @return bool|int|null
*/
public static function getRelativeSeconds(UrlParams $params)
{
$seconds = $params->get(self::getRelativeRangeParameter());
if ($seconds === null) {
return null;
}
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];
}
}
|