diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:40 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:36:40 +0000 |
commit | a0901c4b7f2db488cb4fb3be2dd921a0308f4659 (patch) | |
tree | fafb393cf330a60df129ff10d0059eb7b14052a7 /application/controllers/HistoryController.php | |
parent | Initial commit. (diff) | |
download | icingadb-web-a0901c4b7f2db488cb4fb3be2dd921a0308f4659.tar.xz icingadb-web-a0901c4b7f2db488cb4fb3be2dd921a0308f4659.zip |
Adding upstream version 1.0.2.upstream/1.0.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | application/controllers/HistoryController.php | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/application/controllers/HistoryController.php b/application/controllers/HistoryController.php new file mode 100644 index 0000000..b4fc6df --- /dev/null +++ b/application/controllers/HistoryController.php @@ -0,0 +1,138 @@ +<?php + +/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */ + +namespace Icinga\Module\Icingadb\Controllers; + +use GuzzleHttp\Psr7\ServerRequest; +use Icinga\Module\Icingadb\Model\History; +use Icinga\Module\Icingadb\Web\Control\SearchBar\ObjectSuggestions; +use Icinga\Module\Icingadb\Web\Controller; +use Icinga\Module\Icingadb\Widget\ItemList\HistoryList; +use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher; +use ipl\Stdlib\Filter; +use ipl\Web\Control\LimitControl; +use ipl\Web\Control\SortControl; +use ipl\Web\Filter\QueryString; +use ipl\Web\Url; + +class HistoryController extends Controller +{ + public function indexAction() + { + $this->addTitleTab(t('History')); + $compact = $this->view->compact; // TODO: Find a less-legacy way.. + + $preserveParams = [ + LimitControl::DEFAULT_LIMIT_PARAM, + SortControl::DEFAULT_SORT_PARAM, + ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM + ]; + + $db = $this->getDb(); + + $history = History::on($db)->with([ + 'host', + 'host.state', + 'service', + 'service.state', + 'comment', + 'downtime', + 'flapping', + 'notification', + 'acknowledgement', + 'state' + ]); + + $before = $this->params->shift('before', time()); + + $limitControl = $this->createLimitControl(); + $paginationControl = $this->createPaginationControl($history); + $sortControl = $this->createSortControl( + $history, + [ + 'history.event_time desc' => t('Event Time') + ] + ); + $viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl, true); + $searchBar = $this->createSearchBar($history, $preserveParams); + + if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) { + if ($searchBar->hasBeenSubmitted()) { + $filter = $this->getFilter(); + } else { + $this->addControl($searchBar); + $this->sendMultipartUpdate(); + return; + } + } else { + $filter = $searchBar->getFilter(); + } + + $history->peekAhead(); + + $page = $paginationControl->getCurrentPageNumber(); + + if ($page > 1 && ! $compact) { + $history->limit($page * $limitControl->getLimit()); + } + + $history->filter(Filter::lessThanOrEqual('event_time', $before)); + $this->filter($history, $filter); + + $history->getWith()['history.host']->setJoinType('LEFT'); + $history->filter(Filter::any( + // Because of LEFT JOINs, make sure we'll fetch history entries only for items which still exist: + Filter::like('host.id', '*'), + Filter::like('service.id', '*') + )); + + yield $this->export($history); + + $this->addControl($sortControl); + $this->addControl($limitControl); + $this->addControl($viewModeSwitcher); + $this->addControl($searchBar); + + $url = Url::fromRequest()->onlyWith($preserveParams); + $url->setQueryString(QueryString::render($filter) . '&' . $url->getParams()->toString()); + + $historyList = (new HistoryList($history->execute())) + ->setPageSize($limitControl->getLimit()) + ->setViewMode($viewModeSwitcher->getViewMode()) + ->setLoadMoreUrl($url->setParam('before', $before)); + if ($compact) { + $historyList->setPageNumber($page); + } + + if ($compact && $page > 1) { + $this->document->addFrom($historyList); + } else { + $this->addContent($historyList); + } + + if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) { + $this->sendMultipartUpdate(); + } + } + + public function completeAction() + { + $suggestions = new ObjectSuggestions(); + $suggestions->setModel(History::class); + $suggestions->forRequest(ServerRequest::fromGlobals()); + $this->getDocument()->add($suggestions); + } + + public function searchEditorAction() + { + $editor = $this->createSearchEditor(History::on($this->getDb()), [ + LimitControl::DEFAULT_LIMIT_PARAM, + SortControl::DEFAULT_SORT_PARAM, + ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM + ]); + + $this->getDocument()->add($editor); + $this->setTitle(t('Adjust Filter')); + } +} |