icingadb-web/application/controllers/ServiceController.php

132 lines
3.6 KiB
PHP
Raw Normal View History

2019-10-09 11:26:52 -04:00
<?php
namespace Icinga\Module\Eagle\Controllers;
2019-11-03 17:20:46 -05:00
use Exception;
2019-10-09 11:26:52 -04:00
use Icinga\Exception\NotFoundError;
use Icinga\Module\Eagle\Common\CommandActions;
use Icinga\Module\Eagle\Common\Links;
2019-11-03 17:20:46 -05:00
use Icinga\Module\Eagle\Common\ServiceLinks;
use Icinga\Module\Eagle\Model\History;
2019-10-09 11:26:52 -04:00
use Icinga\Module\Eagle\Model\Service;
use Icinga\Module\Eagle\Web\Controller;
2019-11-03 09:34:09 -05:00
use Icinga\Module\Eagle\Widget\Detail\ObjectDetail;
use Icinga\Module\Eagle\Widget\Detail\QuickActions;
2019-11-03 17:20:46 -05:00
use Icinga\Module\Eagle\Widget\ItemList\HistoryList;
2019-11-03 09:34:09 -05:00
use Icinga\Module\Eagle\Widget\ServiceList;
2019-11-03 17:20:46 -05:00
use ipl\Sql\Sql;
2019-10-09 11:26:52 -04:00
class ServiceController extends Controller
{
use CommandActions;
/** @var Service The service object */
protected $service;
public function init()
2019-10-09 11:26:52 -04:00
{
$name = $this->params->shiftRequired('name');
$hostName = $this->params->shiftRequired('host.name');
2019-10-09 11:26:52 -04:00
$query = Service::on($this->getDb())->with([
'state',
2019-10-14 09:13:39 -04:00
'host',
'host.state'
2019-10-09 11:26:52 -04:00
]);
$query->getSelectBase()
->where(['service.name = ?' => $name])
->where(['service_host.name = ?' => $hostName]);
2019-10-09 11:26:52 -04:00
/** @var Service $service */
$service = $query->first();
if ($service === null) {
throw new NotFoundError($this->translate('Service not found'));
}
$this->service = $service;
2019-11-03 17:20:46 -05:00
$this->setTitleTab($this->getRequest()->getActionName());
}
public function getCommandTargetsUrl()
{
return Links::service($this->service, $this->service->host);
}
public function fetchCommandTargets()
{
return [$this->service];
}
public function indexAction()
{
2019-11-03 09:34:09 -05:00
$this->addControl((new ServiceList([$this->service]))->setViewMode('compact'));
$this->addControl(new QuickActions($this->service));
$this->addContent(new ObjectDetail($this->service));
2019-10-09 11:26:52 -04:00
}
2019-11-03 17:20:46 -05:00
public function historyAction()
{
$this->addControl((new ServiceList([$this->service]))->setViewMode('compact'));
$db = $this->getDb();
$history = History::on($db)->with([
'host',
'host.service',
'host.state',
'service',
'service.state',
'service.host',
'service.host.state',
'comment',
'downtime',
'notification',
'state'
]);
$history
->getSelectBase()
->where([
'history_host_service.id = ?' => $this->service->id,
'history_service.id = ?' => $this->service->id
], Sql::ANY);
$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($history);
yield $this->export($history);
$this->addControl($paginationControl);
$this->addControl($limitControl);
$this->addContent(new HistoryList($history));
}
protected function createTabs()
{
return $this
->getTabs()
->add('index', [
'label' => $this->translate('Service'),
'url' => Links::service($this->service, $this->service->host)
])
->add('history', [
'label' => $this->translate('History'),
'url' => ServiceLinks::history($this->service, $this->service->host)
]);
}
protected function setTitleTab($name)
{
$tab = $this->createTabs()->get($name);
if ($tab !== null) {
$tab->setActive();
$this->view->title = $tab->getLabel();
}
}
2019-10-09 11:26:52 -04:00
}