icingadb-web/application/controllers/HistoryController.php

148 lines
4.8 KiB
PHP
Raw 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\Widget\ItemList\HistoryList;
use Icinga\Module\Icingadb\Widget\ShowMore;
use Icinga\Module\Icingadb\Widget\ViewModeSwitcher;
2020-03-10 04:26:28 -04:00
use ipl\Sql\Sql;
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->setTitle(t('History'));
$compact = $this->view->compact; // TODO: Find a less-legacy way..
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());
$url = Url::fromPath('icingadb/history')->setParams(clone $this->params);
if (! $this->params->has('page') || ($page = (int) $this->params->shift('page')) < 1) {
$page = 1;
}
2019-11-05 05:52:57 -05:00
$limitControl = $this->createLimitControl();
2019-12-11 07:38:29 -05:00
$sortControl = $this->createSortControl(
$history,
[
'history.event_time desc' => t('Event Time')
2019-12-11 07:38:29 -05:00
]
);
2021-08-03 11:18:08 -04:00
$viewModeSwitcher = $this->createViewModeSwitcher();
2020-11-02 08:30:15 -05:00
$searchBar = $this->createSearchBar($history, [
$limitControl->getLimitParam(),
2021-08-03 11:18:08 -04:00
$sortControl->getSortParam(),
$viewModeSwitcher->getViewModeParam()
2020-11-02 08:30:15 -05:00
]);
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();
$history->limit($limitControl->getLimit());
if ($page > 1) {
if ($compact) {
$history->offset(($page - 1) * $limitControl->getLimit());
} else {
$history->limit($page * $limitControl->getLimit());
}
}
$history->filter(Filter::lessThanOrEqual('event_time', $before));
$this->filter($history, $filter);
2020-03-10 04:26:28 -04:00
$history->getSelectBase()
// Make sure we'll fetch service history entries only for services which still exist
->where(['history.service_id IS NULL', 'history_service.id IS NOT NULL'], Sql::ANY);
2019-11-05 05:52:57 -05:00
yield $this->export($history);
$results = $history->execute();
$showMore = (new ShowMore(
$results,
$url->setParam('page', $page + 1)
->setParam('before', $before)
->setAnchor('page-' . ($page + 1))
))
->setLabel(t('Load More'))
->setAttribute('data-no-icinga-ajax', true);
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
$historyList = (new HistoryList($results))
2021-08-03 11:18:08 -04:00
->setPageSize($limitControl->getLimit())
->setViewMode($viewModeSwitcher->getViewMode());
if ($compact) {
$historyList->setPageNumber($page);
}
// TODO: Dirty, really dirty, find a better solution (And I don't just mean `getContent()` !)
$historyList->add($showMore->setTag('li')->addAttributes(['class' => 'list-item']));
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
}