1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
<?php
/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */
namespace Icinga\Module\Icingadb\Controllers;
use Generator;
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Model\Service;
use Icinga\Module\Icingadb\Model\ServicegroupSummary;
use Icinga\Module\Icingadb\Redis\VolatileStateResults;
use Icinga\Module\Icingadb\Web\Control\SearchBar\ObjectSuggestions;
use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher;
use Icinga\Module\Icingadb\Web\Controller;
use Icinga\Module\Icingadb\Widget\Detail\ObjectHeader;
use Icinga\Module\Icingadb\Widget\ItemList\ObjectList;
use ipl\Html\Html;
use ipl\Stdlib\Filter;
use ipl\Web\Control\LimitControl;
use ipl\Web\Control\SortControl;
class ServicegroupController extends Controller
{
/** @var string */
protected $servicegroupName;
public function init()
{
$this->assertRouteAccess('servicegroups');
$this->servicegroupName = $this->params->shiftRequired('name');
}
/**
* Fetch the service group object
*
* @return ServicegroupSummary
*/
protected function fetchServicegroup(): ServicegroupSummary
{
$query = ServicegroupSummary::on($this->getDb());
foreach ($query->getUnions() as $unionPart) {
$unionPart->filter(Filter::equal('servicegroup.name', $this->servicegroupName));
}
$this->applyRestrictions($query);
/** @var ServicegroupSummary $servicegroup */
$servicegroup = $query->first();
if ($servicegroup === null) {
$this->httpNotFound(t('Service group not found'));
}
return $servicegroup;
}
public function indexAction(): Generator
{
$db = $this->getDb();
$servicegroup = $this->fetchServicegroup();
$services = Service::on($db)->with([
'state',
'state.last_comment',
'icon_image',
'host',
'host.state'
]);
$services
->setResultSetClass(VolatileStateResults::class)
->filter(Filter::equal('servicegroup.id', $servicegroup->id));
$this->applyRestrictions($services);
$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($services);
$sortControl = $this->createSortControl(
$services,
[
'service.display_name' => t('Name'),
'service.state.severity desc,service.state.last_state_change desc' => t('Severity'),
'service.state.soft_state' => t('Current State'),
'service.state.last_state_change desc' => t('Last State Change'),
'host.display_name' => t('Host')
]
);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);
$searchBar = $this->createSearchBar($services, [
$limitControl->getLimitParam(),
$sortControl->getSortParam(),
$viewModeSwitcher->getViewModeParam(),
'name'
]);
if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
if ($searchBar->hasBeenSubmitted()) {
$filter = $this->getFilter();
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();
return;
}
} else {
$filter = $searchBar->getFilter();
}
$services->filter($filter);
yield $this->export($services);
$serviceList = (new ObjectList($services))
->setViewMode($viewModeSwitcher->getViewMode())
->setEmptyStateMessage($paginationControl->getEmptyStateMessage());
// ICINGAWEB_EXPORT_FORMAT is not set yet and $this->format is inaccessible, yeah...
if ($this->getRequest()->getParam('format') === 'pdf') {
$this->addContent(new ObjectHeader($servicegroup));
$this->addContent(Html::tag('h2', null, t('Services')));
} else {
$this->addControl(new ObjectHeader($servicegroup));
}
$this->addControl($paginationControl);
$this->addControl($sortControl);
$this->addControl($limitControl);
$this->addControl($viewModeSwitcher);
$this->addControl($searchBar);
$continueWith = $this->createContinueWith(
Links::servicesDetails()
->setFilter(Filter::equal('servicegroup.name', $servicegroup->name)),
$searchBar
);
$this->addContent($serviceList);
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
$this->sendMultipartUpdate($continueWith);
}
$this->addTitleTab(t('Service Group'));
$this->setTitle($servicegroup->display_name);
$this->setAutorefreshInterval(10);
}
public function completeAction(): void
{
$suggestions = new ObjectSuggestions();
$suggestions->setModel(Service::class);
$suggestions->setBaseFilter(Filter::equal('servicegroup.name', $this->servicegroupName));
$suggestions->forRequest($this->getServerRequest());
$this->getDocument()->add($suggestions);
}
public function searchEditorAction(): void
{
$editor = $this->createSearchEditor(Service::on($this->getDb()), [
LimitControl::DEFAULT_LIMIT_PARAM,
SortControl::DEFAULT_SORT_PARAM,
ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM,
'name'
]);
$this->getDocument()->add($editor);
$this->setTitle(t('Adjust Filter'));
}
}
|