icingadb-web/application/controllers/HostsController.php

246 lines
8.2 KiB
PHP
Raw Permalink Normal View History

2019-09-09 08:46:56 -04:00
<?php
2020-03-13 03:38:01 -04:00
/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */
2019-11-04 19:07:30 -05:00
namespace Icinga\Module\Icingadb\Controllers;
2019-09-09 08:46:56 -04:00
2020-11-02 08:31:00 -05:00
use GuzzleHttp\Psr7\ServerRequest;
2019-12-18 08:47:43 -05:00
use Icinga\Module\Icingadb\Common\CommandActions;
use Icinga\Module\Icingadb\Common\Links;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Module\Icingadb\Model\HoststateSummary;
use Icinga\Module\Icingadb\Redis\VolatileStateResults;
use Icinga\Module\Icingadb\Util\FeatureStatus;
2020-11-02 08:31:00 -05:00
use Icinga\Module\Icingadb\Web\Control\SearchBar\ObjectSuggestions;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Web\Controller;
2019-12-18 08:47:43 -05:00
use Icinga\Module\Icingadb\Widget\Detail\MultiselectQuickActions;
use Icinga\Module\Icingadb\Widget\Detail\ObjectsDetail;
2019-12-03 05:37:29 -05:00
use Icinga\Module\Icingadb\Widget\HostStatusBar;
use Icinga\Module\Icingadb\Widget\ItemList\ObjectList;
use Icinga\Module\Icingadb\Widget\ItemTable\HostItemTable;
use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher;
2023-08-09 08:25:44 -04:00
use Icinga\Module\Icingadb\Widget\ShowMore;
use ipl\Orm\Query;
use ipl\Stdlib\Filter;
use ipl\Web\Control\LimitControl;
use ipl\Web\Control\SortControl;
use ipl\Web\Url;
2019-09-09 08:46:56 -04:00
class HostsController extends Controller
{
2019-12-18 08:47:43 -05:00
use CommandActions;
2019-09-09 08:46:56 -04:00
public function indexAction()
{
$this->addTitleTab(t('Hosts'));
$compact = $this->view->compact;
2019-09-09 08:46:56 -04:00
$db = $this->getDb();
$hosts = Host::on($db)->with(['state', 'icon_image', 'state.last_comment']);
$hosts->getWith()['host.state']->setJoinType('INNER');
$hosts->setResultSetClass(VolatileStateResults::class);
$this->handleSearchRequest($hosts, ['address', 'address6']);
2020-11-02 08:31:00 -05:00
$summary = null;
if (! $compact) {
$summary = HoststateSummary::on($db);
}
2019-09-09 08:46:56 -04:00
2019-10-08 18:09:18 -04:00
$limitControl = $this->createLimitControl();
2019-10-15 09:29:36 -04:00
$paginationControl = $this->createPaginationControl($hosts);
2019-12-11 07:55:31 -05:00
$sortControl = $this->createSortControl(
$hosts,
[
'host.display_name' => t('Name'),
'host.state.severity desc,host.state.last_state_change desc' => t('Severity'),
'host.state.soft_state' => t('Current State'),
'host.state.last_state_change desc' => t('Last State Change')
],
['host.state.severity DESC', 'host.state.last_state_change DESC']
2019-12-11 07:55:31 -05:00
);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);
$columns = $this->createColumnControl($hosts, $viewModeSwitcher);
2020-11-02 08:31:00 -05:00
$searchBar = $this->createSearchBar($hosts, [
$limitControl->getLimitParam(),
$sortControl->getSortParam(),
$viewModeSwitcher->getViewModeParam(),
'columns'
]);
2019-10-08 17:58:23 -04:00
if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
if ($searchBar->hasBeenSubmitted()) {
$filter = $this->getFilter();
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();
return;
}
} else {
$filter = $searchBar->getFilter();
}
2020-11-02 08:31:00 -05:00
$hosts->peekAhead($compact);
2019-10-08 17:58:23 -04:00
2020-11-02 08:31:00 -05:00
$this->filter($hosts, $filter);
if (! $compact) {
2020-11-02 08:31:00 -05:00
$this->filter($summary, $filter);
yield $this->export($hosts, $summary);
} else {
yield $this->export($hosts);
}
2019-10-15 09:29:36 -04:00
$this->addControl($paginationControl);
2019-12-11 07:55:31 -05:00
$this->addControl($sortControl);
2019-10-08 18:09:18 -04:00
$this->addControl($limitControl);
$this->addControl($viewModeSwitcher);
2020-11-02 08:31:00 -05:00
$this->addControl($searchBar);
$continueWith = $this->createContinueWith(Links::hostsDetails(), $searchBar);
2019-09-09 08:46:56 -04:00
$results = $hosts->execute();
if ($viewModeSwitcher->getViewMode() === 'tabular') {
$hostList = (new HostItemTable($results, HostItemTable::applyColumnMetaData($hosts, $columns)))
->setSort($sortControl->getSort());
} else {
$hostList = (new ObjectList($results))
->setViewMode($viewModeSwitcher->getViewMode());
}
$hostList->setEmptyStateMessage($paginationControl->getEmptyStateMessage());
2019-09-09 08:46:56 -04:00
$this->addContent($hostList);
if ($compact) {
$this->addContent(
(new ShowMore($results, Url::fromRequest()->without(['showCompact', 'limit', 'view'])))
->setBaseTarget('_next')
->setAttribute('title', sprintf(
t('Show all %d hosts'),
$hosts->count()
))
);
2020-11-02 08:31:00 -05:00
} else {
2023-08-09 08:25:44 -04:00
/** @var HoststateSummary $hostsSummary */
$hostsSummary = $summary->first();
$this->addFooter((new HostStatusBar($hostsSummary))->setBaseFilter($filter));
}
2019-12-03 05:37:29 -05:00
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
2021-05-26 05:44:50 -04:00
$this->sendMultipartUpdate($continueWith);
2020-11-02 08:31:00 -05:00
}
$this->setAutorefreshInterval(10);
2019-09-09 08:46:56 -04:00
}
2019-12-18 08:47:43 -05:00
public function detailsAction()
{
$this->addTitleTab(t('Hosts'));
2019-12-18 08:47:43 -05:00
$db = $this->getDb();
$hosts = Host::on($db)->with(['state', 'icon_image']);
$hosts->setResultSetClass(VolatileStateResults::class);
$summary = HoststateSummary::on($db)->with(['state']);
2019-12-18 08:47:43 -05:00
$this->filter($hosts);
$this->filter($summary);
$hosts->limit(3);
$hosts->peekAhead();
2019-12-18 08:47:43 -05:00
yield $this->export($hosts, $summary);
$results = $hosts->execute();
2019-12-18 08:47:43 -05:00
$summary = $summary->first();
$downtimes = Host::on($db)->with(['downtime']);
2019-12-18 08:47:43 -05:00
$downtimes->getWith()['host.downtime']->setJoinType('INNER');
$this->filter($downtimes);
$summary->downtimes_total = $downtimes->count();
$comments = Host::on($db)->with(['comment']);
2019-12-18 08:47:43 -05:00
$comments->getWith()['host.comment']->setJoinType('INNER');
// TODO: This should be automatically done by the model/resolver and added as ON condition
$comments->filter(Filter::equal('comment.object_type', 'host'));
2019-12-18 08:47:43 -05:00
$this->filter($comments);
$summary->comments_total = $comments->count();
$this->addControl(
(new ObjectList($results))
2019-12-18 08:47:43 -05:00
->setViewMode('minimal')
->setDetailActionsDisabled()
2019-12-18 08:47:43 -05:00
);
$this->addControl(new ShowMore(
$results,
Links::hosts()->setFilter($this->getFilter()),
sprintf(t('Show all %d hosts'), $hosts->count())
));
2019-12-18 08:47:43 -05:00
$this->addControl(
(new MultiselectQuickActions('host', $summary))
2019-12-18 08:47:43 -05:00
->setBaseFilter($this->getFilter())
);
$this->addContent(
(new ObjectsDetail('host', $summary, $hosts))
2019-12-18 08:47:43 -05:00
->setBaseFilter($this->getFilter())
);
}
2020-11-02 08:31:00 -05:00
public function completeAction()
{
$suggestions = new ObjectSuggestions();
$suggestions->setModel(Host::class);
$suggestions->forRequest(ServerRequest::fromGlobals());
$this->getDocument()->add($suggestions);
}
public function searchEditorAction()
{
$editor = $this->createSearchEditor(Host::on($this->getDb()), [
LimitControl::DEFAULT_LIMIT_PARAM,
SortControl::DEFAULT_SORT_PARAM,
ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM,
'columns'
]);
$this->getDocument()->add($editor);
$this->setTitle(t('Adjust Filter'));
}
protected function fetchCommandTargets(): Query
2019-12-18 08:47:43 -05:00
{
$db = $this->getDb();
$hosts = Host::on($db)->with('state');
$hosts->setResultSetClass(VolatileStateResults::class);
2019-12-18 08:47:43 -05:00
switch ($this->getRequest()->getActionName()) {
case 'acknowledge':
$hosts->filter(Filter::equal('state.is_problem', 'y'))
->filter(Filter::equal('state.is_acknowledged', 'n'));
2019-12-18 08:47:43 -05:00
break;
}
$this->filter($hosts);
return $hosts;
}
protected function getCommandTargetsUrl(): Url
2019-12-18 08:47:43 -05:00
{
return Links::hostsDetails()->setFilter($this->getFilter());
2019-12-18 08:47:43 -05:00
}
protected function getFeatureStatus()
{
$summary = HoststateSummary::on($this->getDb());
$this->filter($summary);
return new FeatureStatus('host', $summary->first());
}
2019-09-09 08:46:56 -04:00
}