Skip to content

Commit f9fc8c8

Browse files
authored
Merge pull request #33 from NETWAYS/refactor/duplicate-code
Remove some duplicate code
2 parents 88b90b4 + 345f7dd commit f9fc8c8

File tree

9 files changed

+115
-158
lines changed

9 files changed

+115
-158
lines changed

application/controllers/ConfigController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Icinga\Module\Grafana\Controllers;
44

5-
use Icinga\Web\Controller;
65
use Icinga\Module\Grafana\Forms\Config\GeneralConfigForm;
76

7+
use Icinga\Web\Controller;
8+
89
class ConfigController extends Controller
910
{
1011
public function init()

application/controllers/DashboardController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Icinga\Module\Grafana\Controllers;
44

5-
use Icinga\Application\Modules\Module;
65
use Icinga\Module\Grafana\ProvidedHook\Icingadb\IcingadbSupport;
7-
use Icinga\Module\Monitoring\Object\Host;
6+
7+
use Icinga\Application\Modules\Module;
88
use Icinga\Module\Monitoring\Controller;
9+
use Icinga\Module\Monitoring\Object\Host;
910
use Icinga\Module\Monitoring\Object\Service;
11+
1012
use ipl\Web\Url;
1113

1214
class DashboardController extends Controller

application/controllers/GraphController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Icinga\Module\Grafana\Controllers;
44

5+
use Icinga\Module\Grafana\Forms\Graph\GraphForm;
6+
57
use Icinga\Exception\NotFoundError;
68
use Icinga\Forms\ConfirmRemovalForm;
7-
use Icinga\Module\Grafana\Forms\Graph\GraphForm;
89
use Icinga\Web\Controller;
910
use Icinga\Web\Notification;
1011

application/controllers/IcingadbdashboardController.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace Icinga\Module\Grafana\Controllers;
44

5-
use Icinga\Exception\NotFoundError;
65
use Icinga\Module\Grafana\ProvidedHook\Icingadb\HostDetailExtension;
76
use Icinga\Module\Grafana\ProvidedHook\Icingadb\ServiceDetailExtension;
87
use Icinga\Module\Grafana\Web\Controller\IcingadbGrafanaController;
8+
99
use Icinga\Module\Icingadb\Model\Host;
10-
use Icinga\Module\Icingadb\Model\Service;
11-
use ipl\Stdlib\Filter;
10+
1211
use ipl\Web\Url;
1312

1413
class IcingadbdashboardController extends IcingadbGrafanaController
@@ -38,29 +37,9 @@ public function indexAction()
3837
$serviceName = $this->params->get('service');
3938

4039
if ($serviceName != null) {
41-
$query = Service::on($this->getDb())->with([
42-
'state',
43-
'icon_image',
44-
'host',
45-
'host.state'
46-
]);
47-
$query->filter(
48-
Filter::all(
49-
Filter::equal('service.name', $serviceName),
50-
Filter::equal('host.name', $hostName)
51-
)
52-
);
40+
$object = $this->getServiceObject($serviceName, $hostName);
5341
} else {
54-
$query = Host::on($this->getDb())->with(['state', 'icon_image']);
55-
$query->filter(Filter::equal('host.name', $hostName));
56-
}
57-
58-
$this->applyRestrictions($query);
59-
60-
$object = $query->first();
61-
62-
if ($object === null) {
63-
throw new NotFoundError(t('Host or Service not found'));
42+
$object = $this->getHostObject($hostName);
6443
}
6544

6645
if ($object instanceof Host) {

application/controllers/IcingadbimgController.php

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
namespace Icinga\Module\Grafana\Controllers;
44

5-
use Icinga\Exception\NotFoundError;
65
use Icinga\Module\Grafana\Web\Controller\IcingadbGrafanaController;
6+
77
use Icinga\Application\Config;
8-
use Icinga\Exception\ConfigurationError;
98
use Icinga\Authentication\Auth;
9+
use Icinga\Exception\ConfigurationError;
1010
use Icinga\Module\Grafana\Helpers\Util;
1111
use Icinga\Module\Icingadb\Model\CustomvarFlat;
12-
use Icinga\Module\Icingadb\Model\Host;
13-
use Icinga\Module\Icingadb\Model\Service;
12+
1413
use ipl\Stdlib\Filter;
1514
use ipl\Web\Url;
1615

@@ -93,7 +92,7 @@ public function init()
9392
$this->defaultDashboardPanelId
9493
);
9594
$this->defaultOrgId = $this->myConfig->get('defaultorgid', $this->defaultOrgId);
96-
$this->grafanaTheme = $this->getUserThemeMode();
95+
$this->grafanaTheme = Util::getUserThemeMode(Auth::getInstance()->getUser());
9796
$this->height = $this->myConfig->get('height', $this->height);
9897
$this->width = $this->myConfig->get('width', $this->width);
9998
$this->proxyTimeout = $this->myConfig->get('proxytimeout', $this->proxyTimeout);
@@ -165,13 +164,13 @@ public function indexAction()
165164
->orderBy('flatname');
166165

167166
if ($this->hasParam('service') && ! is_null($this->getParam('service'))) {
168-
$service = $this->getServiceObject();
167+
$service = $this->getServiceObject(urldecode($this->getParam('service')), urldecode($this->getParam('host')));
169168
$this->object = $service;
170169
$serviceName = $this->object->name;
171170
$hostName = $this->object->host->name;
172171
$varsFlat->filter(Filter::equal('service.id', $this->object->id));
173172
} else {
174-
$host = $this->getHostObject();
173+
$host = $this->getHostObject(urldecode($this->getParam('host')));
175174
$this->object = $host;
176175
$serviceName = $this->object->checkcommand_name;
177176
$hostName = $this->object->name;
@@ -245,43 +244,6 @@ public function indexAction()
245244
exit;
246245
}
247246

248-
private function getHostObject()
249-
{
250-
$query = Host::on($this->getDb())->with(['state', 'icon_image']);
251-
$query->filter(Filter::equal('name', urldecode($this->getParam('host'))));
252-
253-
$this->applyRestrictions($query);
254-
255-
$host = $query->first();
256-
if ($host === null) {
257-
throw new NotFoundError(t('Service not found'));
258-
}
259-
260-
return $host;
261-
}
262-
263-
private function getServiceObject()
264-
{
265-
$query = Service::on($this->getDb())->with([
266-
'state',
267-
'icon_image',
268-
'host',
269-
'host.state'
270-
]);
271-
$query->filter(Filter::equal('name', urldecode($this->getParam('service'))));
272-
$query->filter(Filter::equal('host.name', urldecode($this->getParam('host'))));
273-
274-
$this->applyRestrictions($query);
275-
276-
/** @var Service $service */
277-
$service = $query->first();
278-
if ($service === null) {
279-
throw new NotFoundError(t('Service not found'));
280-
}
281-
282-
return $service;
283-
}
284-
285247
private function setGraphConf($serviceName, $serviceCommand = null)
286248
{
287249
$graphConfig = Config::module('grafana', 'graphs');
@@ -392,26 +354,4 @@ private function getMyimageHtml($serviceName, $hostName, &$imageHtml)
392354
$imageHtml = $result;
393355
return true;
394356
}
395-
396-
/**
397-
* getUserThemeMode returns the users configured Theme Mode.
398-
* Since we cannot handle the 'system' setting (it's client-side),
399-
* we default to 'dark'.
400-
* @return string
401-
*/
402-
private function getUserThemeMode(): string
403-
{
404-
$mode = 'dark';
405-
406-
if ($user = Auth::getInstance()->getUser()) {
407-
$mode = $user->getPreferences()->getValue('icingaweb', 'theme_mode', $mode);
408-
}
409-
410-
// Could be system, which we cannot handle since it's browser-side
411-
if (!in_array($mode, ['dark', 'light'])) {
412-
$mode = 'dark';
413-
}
414-
415-
return $mode;
416-
}
417357
}

application/controllers/IcingadbshowController.php

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace Icinga\Module\Grafana\Controllers;
44

5-
use Icinga\Exception\NotFoundError;
65
use Icinga\Module\Grafana\ProvidedHook\Icingadb\HostDetailExtension;
76
use Icinga\Module\Grafana\ProvidedHook\Icingadb\ServiceDetailExtension;
87
use Icinga\Module\Grafana\Web\Controller\IcingadbGrafanaController;
8+
9+
use Icinga\Application\Config;
10+
use Icinga\Module\Grafana\Helpers\Timeranges;
911
use Icinga\Module\Icingadb\Model\CustomvarFlat;
10-
use Icinga\Module\Icingadb\Model\Host;
1112
use Icinga\Module\Icingadb\Model\Service;
12-
use Icinga\Module\Grafana\Helpers\Timeranges;
13-
use Icinga\Application\Config;
13+
1414
use ipl\Html\HtmlDocument;
1515
use ipl\Html\HtmlElement;
1616
use ipl\Html\HtmlString;
@@ -115,36 +115,4 @@ public function indexAction()
115115
unset($this->object);
116116
unset($customVars);
117117
}
118-
119-
120-
public function getHostObject(string $host): Host
121-
{
122-
$query = Host::on($this->getDb())->with(['state', 'icon_image']);
123-
$query->filter(Filter::equal('name', $host));
124-
$this->applyRestrictions($query);
125-
$host = $query->first();
126-
127-
if ($host === null) {
128-
throw new NotFoundError(t('Host not found'));
129-
}
130-
131-
return $host;
132-
}
133-
134-
public function getServiceObject(string $service, string $host): Service
135-
{
136-
$query = Service::on($this->getDb());
137-
138-
$query->filter(Filter::equal('name', $service));
139-
$query->filter(Filter::equal('host.name', $host));
140-
$this->applyRestrictions($query);
141-
142-
$service = $query->first();
143-
144-
if ($service === null) {
145-
throw new NotFoundError(t('Service not found'));
146-
}
147-
148-
return $service;
149-
}
150118
}

library/Grafana/Helpers/Util.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Icinga\Module\Grafana\Helpers;
44

5+
use Icinga\User;
6+
57
class Util
68
{
79
public static function graphiteReplace(string $string = ''): string
@@ -10,4 +12,28 @@ public static function graphiteReplace(string $string = ''): string
1012

1113
return $string;
1214
}
15+
16+
/**
17+
* getUserThemeMode returns the users configured Theme Mode.
18+
* Since we cannot handle the 'system' setting (it's client-side),
19+
* we default to 'dark'.
20+
*
21+
* @param User $user
22+
* @return string
23+
*/
24+
public static function getUserThemeMode(User $user): string
25+
{
26+
$mode = 'dark';
27+
28+
if (isset($user)) {
29+
$mode = $user->getPreferences()->getValue('icingaweb', 'theme_mode', $mode);
30+
}
31+
32+
// Could be system, which we cannot handle since it's browser-side
33+
if (!in_array($mode, ['dark', 'light'])) {
34+
$mode = 'dark';
35+
}
36+
37+
return $mode;
38+
}
1339
}

library/Grafana/ProvidedHook/Icingadb/IcingaDbGrapher.php

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
namespace Icinga\Module\Grafana\ProvidedHook\Icingadb;
44

5+
use Icinga\Module\Grafana\Helpers\Timeranges;
6+
use Icinga\Module\Grafana\Helpers\Util;
7+
58
use Icinga\Application\Config;
69
use Icinga\Application\Icinga;
710
use Icinga\Authentication\Auth;
811
use Icinga\Exception\ConfigurationError;
9-
use Icinga\Module\Grafana\Helpers\Timeranges;
10-
use Icinga\Module\Grafana\Helpers\Util;
1112
use Icinga\Module\Icingadb\Common\Auth as IcingaDbAuth;
1213
use Icinga\Module\Icingadb\Common\Database;
1314
use Icinga\Module\Icingadb\Common\Links;
1415
use Icinga\Module\Icingadb\Model\CustomvarFlat;
1516
use Icinga\Module\Icingadb\Model\Host;
1617
use Icinga\Module\Icingadb\Model\Service;
18+
1719
use ipl\Html\Html;
1820
use ipl\Html\HtmlDocument;
1921
use ipl\Html\HtmlElement;
@@ -22,8 +24,8 @@
2224
use ipl\Orm\Model;
2325
use ipl\Stdlib\Filter;
2426
use ipl\Web\Url;
25-
use ipl\Web\Widget\Link;
2627
use ipl\Web\Widget\Icon;
28+
use ipl\Web\Widget\Link;
2729

2830
/**
2931
* IcingaDbGrapher contains methods for retrieving and rendering the data from Grafana
@@ -125,7 +127,7 @@ protected function init()
125127
);
126128

127129
$this->defaultOrgId = $this->config->get('defaultorgid', $this->defaultOrgId);
128-
$this->grafanaTheme = $this->getUserThemeMode();
130+
$this->grafanaTheme = Util::getUserThemeMode(Auth::getInstance()->getUser());
129131
$this->height = $this->config->get('height', $this->height);
130132
$this->width = $this->config->get('width', $this->width);
131133

@@ -779,26 +781,4 @@ private function createDebugTable()
779781

780782
return $grafanaTable;
781783
}
782-
783-
/**
784-
* getUserThemeMode returns the users configured Theme Mode.
785-
* Since we cannot handle the 'system' setting (it's client-side),
786-
* we default to 'dark'.
787-
* @return string
788-
*/
789-
private function getUserThemeMode(): string
790-
{
791-
$mode = 'dark';
792-
793-
if ($user = Auth::getInstance()->getUser()) {
794-
$mode = $user->getPreferences()->getValue('icingaweb', 'theme_mode', $mode);
795-
}
796-
797-
// Could be system, which we cannot handle since it's browser-side
798-
if (!in_array($mode, ['dark', 'light'])) {
799-
$mode = 'dark';
800-
}
801-
802-
return $mode;
803-
}
804784
}

0 commit comments

Comments
 (0)