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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
<?php
// Icinga Web 2 Cube Module | (c) 2016 Icinga GmbH | GPLv2
namespace Icinga\Module\Cube\Web;
use Icinga\Module\Cube\DimensionParams;
use Icinga\Module\Cube\Forms\DimensionsForm;
use Icinga\Module\Cube\Hook\IcingaDbActionsHook;
use Icinga\Module\Cube\IcingaDb\CustomVariableDimension;
use Icinga\Module\Cube\IcingaDb\IcingaDbCube;
use Icinga\Module\Icingadb\Common\Auth;
use Icinga\Module\Icingadb\Common\Database;
use Icinga\Module\Icingadb\Web\Control\ProblemToggle;
use ipl\Html\FormElement\CheckboxElement;
use ipl\Html\HtmlString;
use ipl\Stdlib\Filter;
use ipl\Stdlib\Str;
use ipl\Web\Compat\CompatController;
use ipl\Web\Compat\SearchControls;
use ipl\Web\Control\SortControl;
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
use ipl\Web\Widget\Tabs;
abstract class Controller extends CompatController
{
use SearchControls;
use Database;
use Auth;
/** @var string[] Preserved params for searchbar and search editor controls */
protected $preserveParams = [
'dimensions',
'showSettings',
'wantNull',
'problems',
'sort'
];
/** @var Filter\Rule Filter from query string parameters */
private $filter;
/**
* Return this controllers' cube
*
* @return IcingaDbCube
*/
abstract protected function getCube(): IcingaDbCube;
/**
* Get the filter created from query string parameters
*
* @return Filter\Rule
*/
public function getFilter(): Filter\Rule
{
if ($this->filter === null) {
$this->filter = QueryString::parse((string) $this->params);
}
return $this->filter;
}
public function detailsAction(): void
{
$cube = $this->prepareCube();
$this->getTabs()->add('details', [
'label' => $this->translate('Cube details'),
'url' => $this->getRequest()->getUrl()
])->activate('details');
$cube->setBaseFilter($this->getFilter());
$this->setTitle($cube->getSlicesLabel());
$this->view->links = IcingaDbActionsHook::renderAll($cube);
$this->addContent(
HtmlString::create($this->view->render('/cube-details.phtml'))
);
}
protected function renderCube(): void
{
$cube = $this->prepareCube();
$this->setTitle(sprintf(
$this->translate('Cube: %s'),
$cube->getPathLabel()
));
$this->params->shift('format');
$showSettings = $this->params->shift('showSettings');
$query = $cube->innerQuery();
$problemsOnly = (bool) $this->params->shift('problems', false);
$problemToggle = (new ProblemToggle($problemsOnly ?: null))
->setIdProtector([$this->getRequest(), 'protectId'])
->on(ProblemToggle::ON_SUCCESS, function (ProblemToggle $form) {
/** @var CheckboxElement $problems */
$problems = $form->getElement('problems');
if (! $problems->isChecked()) {
$this->redirectNow(Url::fromRequest()->remove('problems'));
} else {
$this->redirectNow(Url::fromRequest()->setParam('problems'));
}
})->handleRequest($this->getServerRequest());
$this->addControl($problemToggle);
$sortControl = SortControl::create([
IcingaDbCube::DIMENSION_VALUE_SORT_PARAM => t('Value'),
IcingaDbCube::DIMENSION_SEVERITY_SORT_PARAM . ' desc' => t('Severity'),
]);
$this->params->shift($sortControl->getSortParam());
$cube->sortBy($sortControl->getSort());
$this->addControl($sortControl);
$searchBar = $this->createSearchBar(
$query,
$this->preserveParams
);
if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
if ($searchBar->hasBeenSubmitted()) {
$filter = $this->getFilter();
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();
return;
}
} else {
$filter = $searchBar->getFilter();
}
if ($problemsOnly) {
$filter = Filter::all($filter, Filter::equal('state.is_problem', true));
}
$cube->setBaseFilter($filter);
$cube->problemsOnly($problemsOnly);
$this->addControl($searchBar);
if (count($cube->listDimensions()) > 0) {
$this->view->cube = $cube;
} else {
$showSettings = true;
}
$this->view->url = Url::fromRequest()
->onlyWith($this->preserveParams)
->setFilter($searchBar->getFilter());
if ($showSettings) {
$form = (new DimensionsForm())
->setUrl($this->view->url)
->setCube($cube)
->on(DimensionsForm::ON_SUCCESS, function ($form) {
$this->redirectNow($form->getRedirectUrl());
})
->handleRequest($this->getServerRequest());
$this->view->form = $form;
} else {
$this->setAutorefreshInterval(15);
}
$this->addContent(
HtmlString::create($this->view->render('/cube-index.phtml'))
);
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
$this->sendMultipartUpdate();
}
}
private function prepareCube(): IcingaDbCube
{
$cube = $this->getCube();
$cube->chooseFacts(array_keys($cube->getAvailableFactColumns()));
$dimensions = DimensionParams::fromString(
$this->params->shift('dimensions', '')
)->getDimensions();
if ($this->hasLegacyDimensionParams($dimensions)) {
$this->transformLegacyDimensionParamsAndRedirect($dimensions);
}
$wantNull = $this->params->shift('wantNull');
foreach ($dimensions as $dimension) {
$cube->addDimensionByName($dimension);
if ($wantNull) {
$cube->getDimension($dimension)->wantNull();
}
$sliceParamWithPrefix = rawurlencode($cube::SLICE_PREFIX . $dimension);
if ($this->params->has($sliceParamWithPrefix)) {
$this->preserveParams[] = $sliceParamWithPrefix;
$cube->slice($dimension, $this->params->shift($sliceParamWithPrefix));
}
}
return $cube;
}
/**
* Get whether the given dimension param is legacy dimension param
*
* @param string $dimensionParam
*
* @return bool
*/
private function isLegacyDimensionParam(string $dimensionParam): bool
{
return ! Str::startsWith($dimensionParam, CustomVariableDimension::HOST_PREFIX)
&& ! Str::startsWith($dimensionParam, CustomVariableDimension::SERVICE_PREFIX);
}
/**
* Get whether the dimensions contain legacy dimension
*
* @param array $dimensions
*
* @return bool
*/
private function hasLegacyDimensionParams(array $dimensions): bool
{
foreach ($dimensions as $dimension) {
if ($this->isLegacyDimensionParam($dimension)) {
return true;
}
}
return false;
}
/**
* Transform legacy dimension and slice params and redirect
*
* This adds the new prefix to params and then redirects so that the new URL contains the prefixed params
* Slices are prefixed to differ filter and slice params
*
* @param array $legacyDimensions
*/
private function transformLegacyDimensionParamsAndRedirect(array $legacyDimensions): void
{
$dimensions = [];
$slices = [];
$dimensionPrefix = CustomVariableDimension::HOST_PREFIX;
if ($this->getRequest()->getControllerName() === 'services') {
$dimensionPrefix = CustomVariableDimension::SERVICE_PREFIX;
}
foreach ($legacyDimensions as $param) {
$newParam = $param;
if ($this->isLegacyDimensionParam($param)) {
$newParam = $dimensionPrefix . $param;
}
$slice = $this->params->shift($param);
if ($slice) {
$slices[IcingaDbCube::SLICE_PREFIX . $newParam] = $slice;
}
$dimensions[] = $newParam;
}
$this->redirectNow(
Url::fromRequest()
->setParam('dimensions', DimensionParams::fromArray($dimensions)->getParams())
->addParams($slices)
->without($legacyDimensions)
);
}
public function createTabs(): Tabs
{
$params = Url::fromRequest()
->onlyWith($this->preserveParams)
->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)
]);
}
}
|