icingadb-web/application/controllers/HistoryController.php

140 lines
4.3 KiB
PHP
Raw Permalink Normal View History

2019-11-05 05:52:57 -05:00
<?php
2020-03-13 03:38:01 -04:00
/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */
2019-11-05 05:52:57 -05:00
namespace Icinga\Module\Icingadb\Controllers;
2020-11-02 08:30:15 -05:00
use GuzzleHttp\Psr7\ServerRequest;
2019-11-05 05:52:57 -05:00
use Icinga\Module\Icingadb\Model\History;
2020-11-02 08:30:15 -05:00
use Icinga\Module\Icingadb\Web\Control\SearchBar\ObjectSuggestions;
2019-11-05 05:52:57 -05:00
use Icinga\Module\Icingadb\Web\Controller;
use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher;
use Icinga\Module\Icingadb\Widget\ItemList\LoadMoreObjectList;
use ipl\Stdlib\Filter;
use ipl\Web\Control\LimitControl;
use ipl\Web\Control\SortControl;
use ipl\Web\Url;
2019-11-05 05:52:57 -05:00
class HistoryController extends Controller
{
public function indexAction()
{
$this->addTitleTab(t('History'));
$compact = $this->view->compact; // TODO: Find a less-legacy way..
2019-11-05 05:52:57 -05:00
$preserveParams = [
LimitControl::DEFAULT_LIMIT_PARAM,
SortControl::DEFAULT_SORT_PARAM,
ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM
];
2019-11-05 05:52:57 -05:00
$db = $this->getDb();
$history = History::on($db)->with([
'host',
'host.state',
'service',
'service.state',
'comment',
'downtime',
'flapping',
2019-11-05 05:52:57 -05:00
'notification',
'acknowledgement',
2019-11-05 05:52:57 -05:00
'state'
]);
$before = $this->params->shift('before', time());
2019-11-05 05:52:57 -05:00
$limitControl = $this->createLimitControl();
2021-08-23 09:23:34 -04:00
$paginationControl = $this->createPaginationControl($history);
2019-12-11 07:38:29 -05:00
$sortControl = $this->createSortControl(
$history,
[
2023-05-23 10:25:19 -04:00
'history.event_time desc, history.event_type desc' => t('Event Time')
2019-12-11 07:38:29 -05:00
]
);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl, true);
$searchBar = $this->createSearchBar($history, $preserveParams);
2019-11-05 05:52:57 -05:00
if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
if ($searchBar->hasBeenSubmitted()) {
$filter = $this->getFilter();
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();
return;
}
} else {
$filter = $searchBar->getFilter();
}
$history->peekAhead();
2021-08-23 09:23:34 -04:00
$page = $paginationControl->getCurrentPageNumber();
if ($page > 1 && ! $compact) {
$history->resetOffset();
2021-08-23 09:23:34 -04:00
$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', '*')
));
2019-11-05 05:52:57 -05:00
yield $this->export($history);
2019-12-11 07:38:29 -05:00
$this->addControl($sortControl);
2019-11-05 05:52:57 -05:00
$this->addControl($limitControl);
2021-08-03 11:18:08 -04:00
$this->addControl($viewModeSwitcher);
2020-11-02 08:30:15 -05:00
$this->addControl($searchBar);
2019-11-05 05:52:57 -05:00
$url = Url::fromRequest()
->onlyWith($preserveParams)
->setFilter($filter);
$historyList = (new LoadMoreObjectList($history->execute()))
2021-08-03 11:18:08 -04:00
->setPageSize($limitControl->getLimit())
2021-08-19 09:14:06 -04:00
->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);
}
2020-11-02 08:30:15 -05:00
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
2020-11-02 08:30:15 -05:00
$this->sendMultipartUpdate();
}
}
public function completeAction()
{
$suggestions = new ObjectSuggestions();
$suggestions->setModel(History::class);
$suggestions->forRequest(ServerRequest::fromGlobals());
$this->getDocument()->add($suggestions);
2019-11-05 05:52:57 -05:00
}
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'));
}
2019-11-05 05:52:57 -05:00
}