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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
<?php
// Icinga Web 2 Cube Module | (c) 2019 Icinga GmbH | GPLv2
namespace Icinga\Module\Cube\Web;
use Icinga\Application\Modules\Module;
use Icinga\Module\Cube\DimensionParams;
use Icinga\Module\Cube\Forms\DimensionsForm;
use Icinga\Module\Cube\IcingaDb\CustomVariableDimension;
use Icinga\Module\Cube\IcingaDb\IcingaDbCube;
use Icinga\Module\Cube\Ido\IdoCube;
use ipl\Stdlib\Str;
use ipl\Web\Compat\CompatController;
use ipl\Web\Url;
use ipl\Web\Widget\Tabs;
abstract class IdoController extends CompatController
{
/**
* Return this controllers' cube
*
* @return IdoCube
*/
abstract protected function getCube(): IdoCube;
public function detailsAction(): void
{
$cube = $this->prepareCube();
$this->getTabs()->add('details', [
'label' => $this->translate('Cube details'),
'url' => $this->getRequest()->getUrl()
])->activate('details');
$this->view->title = $cube->getSlicesLabel();
$this->view->links = ActionLinks::renderAll($cube, $this->view);
$this->render('cube-details', null, true);
}
protected function renderCube(): void
{
$this->params->shift('format');
$showSettings = $this->params->shift('showSettings');
$cube = $this->prepareCube();
$this->view->title = sprintf(
$this->translate('Cube: %s'),
$cube->getPathLabel()
);
if (count($cube->listDimensions()) > 0) {
$this->view->cube = $cube;
} else {
$showSettings = true;
}
$this->view->url = Url::fromRequest();
if ($showSettings) {
$form = (new DimensionsForm())
->setUrl($this->view->url)
->setCube($cube)
->setUrl(Url::fromRequest())
->on(DimensionsForm::ON_SUCCESS, function ($form) {
$this->redirectNow($form->getRedirectUrl());
})
->handleRequest($this->getServerRequest());
$this->view->form = $form;
} else {
$this->setAutorefreshInterval(15);
}
$this->render('cube-index', null, true);
}
private function prepareCube(): IdoCube
{
$cube = $this->getCube();
$cube->chooseFacts(array_keys($cube->getAvailableFactColumns()));
$vars = DimensionParams::fromString($this->params->shift('dimensions', ''))->getDimensions();
$resolved = $this->params->shift('resolved', false);
if (
! $resolved
&& Module::exists('icingadb')
&& $this->hasIcingadbDimensionParams($vars)
) {
$this->transformIcingadbDimensionParamsAndRedirect($vars);
} elseif ($resolved) {
$this->redirectNow(Url::fromRequest()->without('resolved'));
}
$wantNull = $this->params->shift('wantNull');
foreach ($vars as $var) {
$cube->addDimensionByName($var);
if ($wantNull) {
$cube->getDimension($var)->wantNull();
}
}
foreach ($this->params->toArray() as $param) {
$cube->slice(rawurldecode($param[0]), rawurldecode($param[1]));
}
return $cube;
}
/**
* Get whether the dimensions contain icingadb dimension
*
* @param array $dimensions
*
* @return bool
*/
private function hasIcingadbDimensionParams(array $dimensions): bool
{
foreach ($dimensions as $dimension) {
if (
Str::startsWith($dimension, CustomVariableDimension::HOST_PREFIX)
|| Str::startsWith($dimension, CustomVariableDimension::SERVICE_PREFIX)
) {
return true;
}
}
return false;
}
/**
* Transform icingadb dimension and slice params and redirect
*
* This remove the new icingadb prefix from params and remove sort, problems-only, filter params
*
* @param array $icingadbDimensions
*/
private function transformIcingadbDimensionParamsAndRedirect(array $icingadbDimensions): void
{
$dimensions = [];
$slices = [];
$toRemoveSlices = [];
$prefix = CustomVariableDimension::HOST_PREFIX;
if ($this->getRequest()->getControllerName() === 'ido-services') {
$prefix = CustomVariableDimension::SERVICE_PREFIX;
}
foreach ($icingadbDimensions as $param) {
$newParam = $param;
if (strpos($param, $prefix) !== false) {
$newParam = substr($param, strlen($prefix));
}
$slice = $this->params->shift(IcingaDbCube::SLICE_PREFIX . $param);
if ($slice) {
$slices[$newParam] = $slice;
$toRemoveSlices[] = IcingaDbCube::SLICE_PREFIX . $param;
}
$dimensions[] = $newParam;
}
$icingadbParams = array_merge(
$icingadbDimensions,
$toRemoveSlices,
array_keys($this->params->toArray(false))
);
$this->redirectNow(
Url::fromRequest()
->setParam('dimensions', DimensionParams::fromArray($dimensions)->getParams())
->addParams($slices)
->addParams(['resolved' => true])
->without($icingadbParams)
);
}
public function createTabs(): Tabs
{
$params = Url::fromRequest()->getParams()->toString();
return $this->getTabs()
->add('cube/hosts', [
'label' => $this->translate('Hosts'),
'url' => 'cube/hosts' . ($params === '' ? '' : '?' . $params)
])
->add('cube/services', [
'label' => $this->translate('Services'),
'url' => 'cube/services' . ($params === '' ? '' : '?' . $params)
]);
}
}
|