From d61b7618d9c04ff90fdf8d3b584ad5976faedad9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 15:16:36 +0200 Subject: Adding upstream version 1.3.2. Signed-off-by: Daniel Baumann --- library/Cube/Web/IdoController.php | 198 +++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 library/Cube/Web/IdoController.php (limited to 'library/Cube/Web/IdoController.php') diff --git a/library/Cube/Web/IdoController.php b/library/Cube/Web/IdoController.php new file mode 100644 index 0000000..a9feec9 --- /dev/null +++ b/library/Cube/Web/IdoController.php @@ -0,0 +1,198 @@ +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) + ]); + } +} -- cgit v1.2.3