icingadb-web/library/Icingadb/View/NotificationRenderer.php

201 lines
6.8 KiB
PHP
Raw Permalink Normal View History

2019-10-23 10:54:12 -04:00
<?php
/* Icinga DB Web | (c) 2025 Icinga GmbH | GPLv2 */
2020-03-13 03:38:01 -04:00
namespace Icinga\Module\Icingadb\View;
2019-10-23 10:54:12 -04:00
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Common\HostLink;
use Icinga\Module\Icingadb\Common\HostStates;
use Icinga\Module\Icingadb\Common\Icons;
2021-06-08 07:40:17 -04:00
use Icinga\Module\Icingadb\Common\Links;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Common\ServiceLink;
use Icinga\Module\Icingadb\Common\ServiceStates;
use Icinga\Module\Icingadb\Model\NotificationHistory;
2021-08-19 10:58:17 -04:00
use Icinga\Module\Icingadb\Util\PluginOutput;
use Icinga\Module\Icingadb\Widget\PluginOutputContainer;
2021-08-04 04:11:49 -04:00
use Icinga\Module\Icingadb\Widget\StateChange;
use InvalidArgumentException;
use ipl\Html\Attributes;
use ipl\Html\HtmlDocument;
2021-06-08 07:40:17 -04:00
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\I18n\Translation;
use ipl\Web\Common\ItemRenderer;
use ipl\Web\Widget\EmptyState;
2019-10-23 10:54:12 -04:00
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;
use ipl\Web\Widget\StateBall;
use ipl\Web\Widget\TimeAgo;
2019-10-23 10:54:12 -04:00
/** @implements ItemRenderer<NotificationHistory> */
class NotificationRenderer implements ItemRenderer
2019-10-23 10:54:12 -04:00
{
use Translation;
2019-10-23 10:54:12 -04:00
use HostLink;
use ServiceLink;
public function assembleAttributes($item, Attributes $attributes, string $layout): void
{
$attributes->get('class')->addValue('notification');
}
public function assembleVisual($item, HtmlDocument $visual, string $layout): void
{
$ballSize = StateBall::SIZE_LARGE;
if ($layout === 'minimal' || $layout === 'header') {
$ballSize = StateBall::SIZE_BIG;
}
2019-10-23 10:54:12 -04:00
switch ($item->type) {
case 'acknowledgement':
2021-08-04 04:11:49 -04:00
$visual->addHtml(HtmlElement::create(
'div',
['class' => ['icon-ball', 'ball-size-' . $ballSize]],
2021-08-04 04:11:49 -04:00
new Icon(Icons::IS_ACKNOWLEDGED)
));
2019-10-23 10:54:12 -04:00
break;
case 'custom':
2021-08-04 04:11:49 -04:00
$visual->addHtml(HtmlElement::create(
'div',
['class' => ['icon-ball', 'ball-size-' . $ballSize]],
2021-08-04 04:11:49 -04:00
new Icon(Icons::NOTIFICATION)
));
2019-10-23 10:54:12 -04:00
break;
case 'downtime_end':
case 'downtime_removed':
case 'downtime_start':
2021-08-04 04:11:49 -04:00
$visual->addHtml(HtmlElement::create(
'div',
['class' => ['icon-ball', 'ball-size-' . $ballSize]],
2021-08-04 04:11:49 -04:00
new Icon(Icons::IN_DOWNTIME)
));
2019-10-23 10:54:12 -04:00
break;
case 'flapping_end':
case 'flapping_start':
2021-08-04 04:11:49 -04:00
$visual->addHtml(HtmlElement::create(
'div',
['class' => ['icon-ball', 'ball-size-' . $ballSize]],
2021-08-04 04:11:49 -04:00
new Icon(Icons::IS_FLAPPING)
));
2019-10-23 10:54:12 -04:00
break;
case 'problem':
case 'recovery':
if ($item->object_type === 'host') {
$state = HostStates::text($item->state);
$previousHardState = HostStates::text($item->previous_hard_state);
2019-10-23 10:54:12 -04:00
} else {
$state = ServiceStates::text($item->state);
$previousHardState = ServiceStates::text($item->previous_hard_state);
2019-10-23 10:54:12 -04:00
}
2021-08-04 04:11:49 -04:00
$visual->addHtml(new StateChange($state, $previousHardState));
2019-10-23 10:54:12 -04:00
break;
}
}
public function assembleTitle($item, HtmlDocument $title, string $layout): void
2019-10-23 10:54:12 -04:00
{
if ($layout === 'header') {
2021-06-08 07:40:17 -04:00
$title->addHtml(HtmlElement::create(
'span',
['class' => 'subject'],
sprintf($this->phraseForType($item->type), ucfirst($item->object_type))
2021-06-08 07:40:17 -04:00
));
} else {
$title->addHtml(new Link(
sprintf($this->phraseForType($item->type), ucfirst($item->object_type)),
Links::event($item->history),
2021-06-08 07:40:17 -04:00
['class' => 'subject']
));
}
2019-10-23 10:54:12 -04:00
if ($item->object_type === 'host') {
$link = $this->createHostLink($item->host, true);
2019-10-23 10:54:12 -04:00
} else {
$link = $this->createServiceLink($item->service, $item->host, true);
2019-10-23 10:54:12 -04:00
}
2021-06-08 07:40:17 -04:00
$title->addHtml(Text::create(' '), $link);
2019-10-23 10:54:12 -04:00
}
public function assembleCaption($item, HtmlDocument $caption, string $layout): void
{
if (in_array($item->type, ['flapping_end', 'flapping_start', 'problem', 'recovery'])) {
$commandName = $item->object_type === 'host'
? $item->host->checkcommand_name
: $item->service->checkcommand_name;
if (isset($commandName)) {
if (empty($item->text)) {
$caption->addHtml(new EmptyState($this->translate('Output unavailable.')));
} else {
$caption->addHtml(new PluginOutputContainer(
(new PluginOutput($item->text))
->setCommandName($commandName)
));
}
} else {
$caption->addHtml(new EmptyState($this->translate('Waiting for Icinga DB to synchronize the config.')));
}
} else {
$caption->add([
new Icon(Icons::USER),
$item->author,
': ',
$item->text
]);
}
}
public function assembleExtendedInfo($item, HtmlDocument $info, string $layout): void
{
$info->addHtml(new TimeAgo($item->send_time->getTimestamp()));
}
public function assembleFooter($item, HtmlDocument $footer, string $layout): void
{
}
public function assemble($item, string $name, HtmlDocument $element, string $layout): bool
2019-10-23 10:54:12 -04:00
{
return false; // no custom sections
}
/**
* Get a localized phrase for the given notification type
*
* @param string $type
*
* @return string
*/
protected function phraseForType(string $type): string
{
switch ($type) {
case 'acknowledgement':
return $this->translate('Problem acknowledged');
case 'custom':
return $this->translate('Custom Notification triggered');
case 'downtime_end':
return $this->translate('Downtime ended');
case 'downtime_removed':
return $this->translate('Downtime removed');
case 'downtime_start':
return $this->translate('Downtime started');
case 'flapping_end':
return $this->translate('Flapping stopped');
case 'flapping_start':
return $this->translate('Flapping started');
case 'problem':
return $this->translate('%s ran into a problem');
case 'recovery':
return $this->translate('%s recovered');
default:
throw new InvalidArgumentException(sprintf('Type %s is not a valid notification type', $type));
}
2019-10-23 10:54:12 -04:00
}
}