Rename HistoryRenderer to EventRenderer

- use `NotificationRenderer` instance as class property

NotificationRenderer:
- Make the phraseForType() method non-static and protected, as it is only used internally.
This commit is contained in:
Sukhwinder Dhillon 2025-03-26 11:43:23 +01:00 committed by Johannes Meyer
parent 9febe3bf36
commit 0d9bcc985f
4 changed files with 43 additions and 55 deletions

View file

@ -31,13 +31,21 @@ use ipl\Web\Widget\StateBall;
use ipl\Web\Widget\TimeAgo;
/** @implements ItemRenderer<History> */
class HistoryRenderer implements ItemRenderer
class EventRenderer implements ItemRenderer
{
use Translation;
use TicketLinks;
use HostLink;
use ServiceLink;
/** @var NotificationRenderer To render NotificationHistory event */
protected $notificationRenderer;
public function __construct()
{
$this->notificationRenderer = new NotificationRenderer();
}
public function assembleAttributes($item, Attributes $attributes, string $layout): void
{
$attributes->get('class')->addValue('history');
@ -166,6 +174,16 @@ class HistoryRenderer implements ItemRenderer
public function assembleTitle($item, HtmlDocument $title, string $layout): void
{
if ($item->event_type === 'notification' && isset($item->notification->id)) {
$item->notification->history = $item;
$item->notification->host = $item->host;
$item->notification->service = $item->service;
$this->notificationRenderer->assembleTitle($item->notification, $title, $layout);
return;
}
switch ($item->event_type) {
case 'comment_add':
$subjectLabel = $this->translate('Comment added');
@ -237,13 +255,6 @@ class HistoryRenderer implements ItemRenderer
$subjectLabel = $this->translate('Acknowledgement cleared');
}
break;
case 'notification':
$subjectLabel = isset($item->notification->type) ? sprintf(
NotificationRenderer::phraseForType($item->notification->type),
ucfirst($item->object_type)
) : $item->event_type;
break;
case 'state_change':
$state = $item->state->state_type === 'hard'
@ -294,6 +305,14 @@ class HistoryRenderer implements ItemRenderer
public function assembleCaption($item, HtmlDocument $caption, string $layout): void
{
if ($item->event_type === 'notification') {
$item->notification->host = $item->host;
$item->notification->service = $item->service;
$this->notificationRenderer->assembleCaption($item->notification, $caption, $layout);
return;
}
switch ($item->event_type) {
case 'comment_add':
case 'comment_remove':
@ -367,37 +386,6 @@ class HistoryRenderer implements ItemRenderer
])->addFrom($markdownLine);
}
break;
case 'notification':
if (! empty($item->notification->author)) {
$caption->add([
new Icon(Icons::USER),
$item->notification->author,
': ',
$item->notification->text
]);
} else {
$commandName = $item->object_type === 'host'
? $item->host->checkcommand_name
: $item->service->checkcommand_name;
if (isset($commandName)) {
if (empty($item->notification->text)) {
$caption->addHtml(new EmptyState(t('Output unavailable.')));
} else {
$caption->addHtml(
new PluginOutputContainer(
(new PluginOutput($item->notification->text))
->setCommandName($commandName)
)
);
}
} else {
$caption->addHtml(
new EmptyState($this->translate('Waiting for Icinga DB to synchronize the config.'))
);
}
}
break;
case 'state_change':
$commandName = $item->object_type === 'host'

View file

@ -104,11 +104,11 @@ class NotificationRenderer implements ItemRenderer
$title->addHtml(HtmlElement::create(
'span',
['class' => 'subject'],
sprintf(self::phraseForType($item->type), ucfirst($item->object_type))
sprintf($this->phraseForType($item->type), ucfirst($item->object_type))
));
} else {
$title->addHtml(new Link(
sprintf(self::phraseForType($item->type), ucfirst($item->object_type)),
sprintf($this->phraseForType($item->type), ucfirst($item->object_type)),
Links::event($item->history),
['class' => 'subject']
));
@ -172,27 +172,27 @@ class NotificationRenderer implements ItemRenderer
*
* @return string
*/
public static function phraseForType(string $type): string
protected function phraseForType(string $type): string
{
switch ($type) {
case 'acknowledgement':
return t('Problem acknowledged');
return $this->translate('Problem acknowledged');
case 'custom':
return t('Custom Notification triggered');
return $this->translate('Custom Notification triggered');
case 'downtime_end':
return t('Downtime ended');
return $this->translate('Downtime ended');
case 'downtime_removed':
return t('Downtime removed');
return $this->translate('Downtime removed');
case 'downtime_start':
return t('Downtime started');
return $this->translate('Downtime started');
case 'flapping_end':
return t('Flapping stopped');
return $this->translate('Flapping stopped');
case 'flapping_start':
return t('Flapping started');
return $this->translate('Flapping started');
case 'problem':
return t('%s ran into a problem');
return $this->translate('%s ran into a problem');
case 'recovery':
return t('%s recovered');
return $this->translate('%s recovered');
default:
throw new InvalidArgumentException(sprintf('Type %s is not a valid notification type', $type));
}

View file

@ -17,7 +17,7 @@ use Icinga\Module\Icingadb\Model\User;
use Icinga\Module\Icingadb\Model\Usergroup;
use Icinga\Module\Icingadb\View\CommentRenderer;
use Icinga\Module\Icingadb\View\DowntimeRenderer;
use Icinga\Module\Icingadb\View\HistoryRenderer;
use Icinga\Module\Icingadb\View\EventRenderer;
use Icinga\Module\Icingadb\View\HostgroupRenderer;
use Icinga\Module\Icingadb\View\HostRenderer;
use Icinga\Module\Icingadb\View\RedundancyGroupRenderer;
@ -73,7 +73,7 @@ class ObjectHeader extends BaseHtmlElement
break;
case $this->object instanceof History:
$renderer = new HistoryRenderer();
$renderer = new EventRenderer();
break;
case $this->object instanceof Hostgroupsummary:

View file

@ -8,7 +8,7 @@ use Icinga\Exception\NotImplementedError;
use Icinga\Module\Icingadb\Common\LoadMore;
use Icinga\Module\Icingadb\Model\History;
use Icinga\Module\Icingadb\Model\NotificationHistory;
use Icinga\Module\Icingadb\View\HistoryRenderer;
use Icinga\Module\Icingadb\View\EventRenderer;
use Icinga\Module\Icingadb\View\NotificationRenderer;
use ipl\Orm\Model;
use ipl\Web\Widget\ItemList;
@ -30,7 +30,7 @@ class LoadMoreObjectList extends ObjectList
if ($item instanceof NotificationHistory) {
return new NotificationRenderer();
} elseif ($item instanceof History) {
return new HistoryRenderer();
return new EventRenderer();
}
throw new NotImplementedError('Not implemented');