icingadb-web/application/controllers/ServicesController.php

237 lines
7.5 KiB
PHP
Raw Normal View History

2019-10-09 11:25:39 -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-10-09 11:25:39 -04:00
2020-11-02 08:32:19 -05:00
use GuzzleHttp\Psr7\ServerRequest;
2019-12-18 08:47:55 -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\Service;
use Icinga\Module\Icingadb\Model\ServicestateSummary;
use Icinga\Module\Icingadb\Util\FeatureStatus;
2020-11-02 08:32:19 -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:55 -05:00
use Icinga\Module\Icingadb\Widget\Detail\MultiselectQuickActions;
use Icinga\Module\Icingadb\Widget\Detail\ObjectsDetail;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Widget\ServiceList;
2019-12-03 05:37:36 -05:00
use Icinga\Module\Icingadb\Widget\ServiceStatusBar;
use Icinga\Module\Icingadb\Widget\ShowMore;
use Icinga\Module\Icingadb\Widget\ViewModeSwitcher;
use ipl\Stdlib\Filter;
use ipl\Web\Control\LimitControl;
use ipl\Web\Control\SortControl;
2020-11-02 08:32:19 -05:00
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
2019-10-09 11:25:39 -04:00
class ServicesController extends Controller
{
2019-12-18 08:47:55 -05:00
use CommandActions;
2019-10-09 11:25:39 -04:00
public function indexAction()
{
$this->setTitle(t('Services'));
$compact = $this->view->compact;
2019-10-09 11:25:39 -04:00
$db = $this->getDb();
$services = Service::on($db)->with([
'state',
2019-10-10 09:13:27 -04:00
'host',
'host.state'
2019-10-09 11:25:39 -04:00
]);
2020-11-02 08:32:19 -05:00
$this->handleSearchRequest($services);
$summary = null;
if (! $compact) {
$summary = ServicestateSummary::on($db)->with('state');
}
2019-10-09 11:25:39 -04:00
$limitControl = $this->createLimitControl();
2019-10-15 09:29:36 -04:00
$paginationControl = $this->createPaginationControl($services);
2019-12-11 07:59:39 -05:00
$sortControl = $this->createSortControl(
$services,
[
'service.display_name, host.display_name' => t('Name'),
'service.state.severity desc' => t('Severity'),
'service.state.soft_state' => t('Current State'),
'service.state.last_state_change desc' => t('Last State Change'),
'host.display_name, service.display_name' => t('Host')
2019-12-11 07:59:39 -05:00
]
);
2019-10-15 09:29:36 -04:00
$viewModeSwitcher = $this->createViewModeSwitcher();
2020-11-02 08:32:19 -05:00
$searchBar = $this->createSearchBar($services, [
$limitControl->getLimitParam(),
$sortControl->getSortParam(),
$viewModeSwitcher->getViewModeParam()
]);
2019-10-09 11:25:39 -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:32:19 -05:00
$services->peekAhead($compact);
2020-11-02 08:32:19 -05:00
$this->filter($services, $filter);
if (! $compact) {
$this->filter($summary, $filter);
yield $this->export($services, $summary);
} else {
yield $this->export($services);
}
2019-10-15 09:29:36 -04:00
$this->addControl($paginationControl);
2019-12-11 07:59:39 -05:00
$this->addControl($sortControl);
2019-10-09 11:25:39 -04:00
$this->addControl($limitControl);
$this->addControl($viewModeSwitcher);
2020-11-02 08:32:19 -05:00
$this->addControl($searchBar);
$continueWith = $this->createContinueWith(Links::servicesDetails(), $searchBar);
2019-10-09 11:25:39 -04:00
$results = $services->execute();
$serviceList = (new ServiceList($results))
->setViewMode($viewModeSwitcher->getViewMode());
2019-10-09 11:25:39 -04:00
$this->addContent($serviceList);
if ($compact) {
$this->addContent(
(new ShowMore($results, Url::fromRequest()->without(['showCompact', 'limit'])))
->setBaseTarget('_next')
->setAttribute('title', sprintf(
t('Show all %d services'),
$services->count()
))
);
2020-11-02 08:32:19 -05:00
} else {
$this->addFooter((new ServiceStatusBar($summary->first()))->setBaseFilter($filter));
}
2019-12-03 05:37:36 -05:00
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
2021-05-26 05:44:50 -04:00
$this->sendMultipartUpdate($continueWith);
2020-11-02 08:32:19 -05:00
}
$this->setAutorefreshInterval(10);
2019-10-09 11:25:39 -04:00
}
2019-12-18 08:47:55 -05:00
public function detailsAction()
{
$this->setTitle(t('Services'));
2019-12-18 08:47:55 -05:00
$db = $this->getDb();
$services = Service::on($db)->with([
'state',
'host',
'host.state'
]);
2019-12-18 08:47:55 -05:00
$summary = ServicestateSummary::on($db)->with(['state']);
$this->filter($services);
$this->filter($summary);
$services->limit(3);
$services->peekAhead();
2019-12-18 08:47:55 -05:00
yield $this->export($services, $summary);
$results = $services->execute();
2019-12-18 08:47:55 -05:00
$summary = $summary->first();
$downtimes = Service::on($db)->with(['downtime']);
$downtimes->getWith()['service.downtime']->setJoinType('INNER');
$this->filter($downtimes);
$summary->downtimes_total = $downtimes->count();
$comments = Service::on($db)->with(['comment']);
$comments->getWith()['service.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', 'service'));
2019-12-18 08:47:55 -05:00
$this->filter($comments);
$summary->comments_total = $comments->count();
$this->addControl(
(new ServiceList($results))
2019-12-18 08:47:55 -05:00
->setViewMode('minimal')
);
$this->addControl(new ShowMore(
$results,
Links::services()->setQueryString(QueryString::render($this->getFilter())),
sprintf(t('Show all %d services'), $services->count())
));
2019-12-18 08:47:55 -05:00
$this->addControl(
(new MultiselectQuickActions('service', $summary))
2019-12-18 08:47:55 -05:00
->setBaseFilter($this->getFilter())
);
$this->addContent(
(new ObjectsDetail('service', $summary))
2019-12-18 08:47:55 -05:00
->setBaseFilter($this->getFilter())
);
}
2020-11-02 08:32:19 -05:00
public function completeAction()
{
$suggestions = new ObjectSuggestions();
$suggestions->setModel(Service::class);
$suggestions->forRequest(ServerRequest::fromGlobals());
$this->getDocument()->add($suggestions);
}
2019-12-18 08:47:55 -05:00
public function searchEditorAction()
{
$editor = $this->createSearchEditor(Service::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-12-18 08:47:55 -05:00
public function fetchCommandTargets()
{
$db = $this->getDb();
$services = Service::on($db)->with([
'state',
'host',
'host.state'
]);
switch ($this->getRequest()->getActionName()) {
case 'acknowledge':
$services->filter(Filter::equal('state.is_problem', 'y'))
->filter(Filter::equal('state.is_acknowledged', 'n'));
2019-12-18 08:47:55 -05:00
break;
}
$this->filter($services);
return $services;
}
public function getCommandTargetsUrl()
{
return Links::servicesDetails()->setQueryString(QueryString::render($this->getFilter()));
2019-12-18 08:47:55 -05:00
}
protected function getFeatureStatus()
{
$summary = ServicestateSummary::on($this->getDb())->with(['state']);
$this->filter($summary);
return new FeatureStatus('service', $summary->first());
}
2019-10-09 11:25:39 -04:00
}