From 5b13df994550e750be0d2810b827618cadd51556 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 25 Jun 2024 11:16:48 +0200 Subject: [PATCH 1/2] Support new event types --- library/Notifications/Common/Icons.php | 30 +++++ library/Notifications/Model/Event.php | 110 ++++++++++++++++++ .../Widget/Detail/EventDetail.php | 4 +- .../Widget/ItemList/EventListItem.php | 66 +---------- 4 files changed, 147 insertions(+), 63 deletions(-) diff --git a/library/Notifications/Common/Icons.php b/library/Notifications/Common/Icons.php index d63fecad..22dd0ab3 100644 --- a/library/Notifications/Common/Icons.php +++ b/library/Notifications/Common/Icons.php @@ -41,4 +41,34 @@ private function __construct() public const RULE_MATCHED = 'filter'; public const UNDEFINED = 'notdef'; + + public const ACKNOWLEDGED = 'check'; + + public const UNACKNOWLEDGED = 'xmark'; + + public const DOWNTIME = 'plug'; + + public const FLAPPING = 'bolt'; + + public const INCIDENT_AGE = 'hourglass-end'; + + public const CUSTOM = 'message'; + + public const SEVERITY_OK = 'heart'; + + public const SEVERITY_CRIT = 'circle-exclamation'; + + public const SEVERITY_WARN = 'exclamation-triangle'; + + public const SEVERITY_ERR = 'circle-xmark'; + + public const SEVERITY_DEBUG = 'bug-slash'; + + public const SEVERITY_INFO = 'info'; + + public const SEVERITY_ALERT = 'bell'; + + public const SEVERITY_EMERG = 'tower-broadcast'; + + public const SEVERITY_NOTICE = 'envelope'; } diff --git a/library/Notifications/Model/Event.php b/library/Notifications/Model/Event.php index 11cc5ab1..758aac9c 100644 --- a/library/Notifications/Model/Event.php +++ b/library/Notifications/Model/Event.php @@ -6,6 +6,7 @@ use DateTime; use Icinga\Module\Notifications\Common\Database; +use Icinga\Module\Notifications\Common\Icons; use ipl\Orm\Behavior\Binary; use ipl\Orm\Behavior\MillisecondTimestamp; use ipl\Orm\Behaviors; @@ -14,6 +15,7 @@ use ipl\Orm\Relations; use ipl\Sql\Connection; use ipl\Sql\Select; +use ipl\Web\Widget\Icon; /** * Event model @@ -153,4 +155,112 @@ public static function mapSeverity(?string $severity): ?string return $label; } + + /** + * Get the type text + * + * @return string + */ + public function getTypeText(): string + { + if ($this->type === 'state') { + if ($this->severity === 'ok') { + return t('recovered', 'notifications.type'); + } + + return t('ran into a problem', 'notifications.type'); + } + + switch ($this->type) { + case 'acknowledgement-set': + return t('has been acknowledged', 'notifications.type'); + case 'acknowledgement-cleared': + return t('was unacknowledged', 'notifications.type'); + case 'downtime-start': + return t('entered a downtime period', 'notifications.type'); + case 'downtime-end': + return t('left a downtime period', 'notifications.type'); + case 'downtime-removed': + return t('prematurely left a downtime period', 'notifications.type'); + case 'flapping-start': + return t('entered a flapping period', 'notifications.type'); + case 'flapping-end': + return t('left a flapping period', 'notifications.type'); + case 'incident-age': + return t('exceeded a time constraint', 'notifications.type'); + default: // custom + return ''; + } + } + + /** + * Get the icon for the event + * + * @return ?Icon + */ + public function getIcon(): ?Icon + { + $icon = null; + + if ($this->type === 'state') { + $severity = $this->severity; + $class = 'severity-' . $severity; + switch ($severity) { + case 'ok': + $icon = (new Icon(Icons::SEVERITY_OK, ['class' => $class]))->setStyle('fa-regular'); + break; + case 'crit': + $icon = new Icon(Icons::SEVERITY_CRIT, ['class' => $class]); + break; + case 'warning': + $icon = new Icon(Icons::SEVERITY_WARN, ['class' => $class]); + break; + case 'err': + $icon = (new Icon(Icons::SEVERITY_ERR, ['class' => $class]))->setStyle('fa-regular'); + break; + case 'debug': + $icon = new Icon(Icons::SEVERITY_DEBUG); + break; + case 'info': + $icon = new Icon(Icons::SEVERITY_INFO); + break; + case 'alert': + $icon = new Icon(Icons::SEVERITY_ALERT); + break; + case 'emerg': + $icon = new Icon(Icons::SEVERITY_EMERG); + break; + case 'notice': + $icon = new Icon(Icons::SEVERITY_NOTICE); + break; + } + + return $icon; + } + + switch ($this->type) { + case 'acknowledgement-set': + $icon = new Icon(Icons::ACKNOWLEDGED); + break; + case 'acknowledgement-cleared': + $icon = new Icon(Icons::UNACKNOWLEDGED); + break; + case 'downtime-start': + case 'downtime-end': + case 'downtime-removed': + $icon = new Icon(Icons::DOWNTIME); + break; + case 'flapping-start': + case 'flapping-end': + $icon = new Icon(Icons::FLAPPING); + break; + case 'incident-age': + $icon = new Icon(Icons::INCIDENT_AGE); + break; + case 'custom': + $icon = new Icon(Icons::CUSTOM); + } + + return $icon; + } } diff --git a/library/Notifications/Widget/Detail/EventDetail.php b/library/Notifications/Widget/Detail/EventDetail.php index c3827e81..5f1024b7 100644 --- a/library/Notifications/Widget/Detail/EventDetail.php +++ b/library/Notifications/Widget/Detail/EventDetail.php @@ -109,8 +109,8 @@ protected function createIncident(): ?array protected function createSource(): array { $elements = []; - if ($this->event->type === 'internal') { - // return no source elements for internal events + if ($this->event->type !== 'state') { + // return no source elements for non state events return $elements; } diff --git a/library/Notifications/Widget/ItemList/EventListItem.php b/library/Notifications/Widget/ItemList/EventListItem.php index 2da23be7..e56b7b2e 100644 --- a/library/Notifications/Widget/ItemList/EventListItem.php +++ b/library/Notifications/Widget/ItemList/EventListItem.php @@ -51,50 +51,9 @@ protected function init(): void protected function assembleVisual(BaseHtmlElement $visual): void { - $content = null; - $severity = $this->item->severity; - $class = 'severity-' . $severity; - - if ($this->item->type === 'internal') { - /* - * TODO(nc): Add proper handling of internal events once - * https://github.com/Icinga/icinga-notifications/issues/162 gets sorted out - */ - $content = new IconBall('square-up-right', 'fa-solid'); - } else { - switch ($severity) { - case 'ok': - $content = (new Icon('heart', ['class' => $class]))->setStyle('fa-regular'); - break; - case 'crit': - $content = new Icon('circle-exclamation', ['class' => $class]); - break; - case 'warning': - $content = new Icon('exclamation-triangle', ['class' => $class]); - break; - case 'err': - $content = (new Icon('circle-xmark', ['class' => $class]))->setStyle('fa-regular'); - break; - case 'debug': - $content = new Icon('bug-slash'); - break; - case 'info': - $content = new Icon('info'); - break; - case 'alert': - $content = new Icon('bell'); - break; - case 'emerg': - $content = new Icon('tower-broadcast'); - break; - case 'notice': - $content = new Icon('envelope'); - break; - } - } - - if ($content) { - $visual->addHtml($content); + $icon = $this->item->getIcon(); + if ($icon) { + $visual->addHtml($icon); } } @@ -117,23 +76,8 @@ protected function assembleTitle(BaseHtmlElement $title): void $content->addAttributes($name->getAttributes()); $content->addFrom($name); - if ($this->item->severity === null) { - $description = strtolower(trim($this->item->message ?? '')); - if (Str::startsWith($description, 'incident reached age')) { - $msg = t('exceeded time constraint'); - } elseif (Str::startsWith($description, 'incident reevaluation')) { - $msg = t('was reevaluated at daemon startup'); - } else { - $msg = t('was acknowledged'); - } - } elseif ($this->item->severity === 'ok') { - $msg = t('recovered'); - } else { - $msg = t('ran into a problem'); - } - $title->addHtml($content); - $title->addHtml(Html::tag('span', ['class' => 'state'], $msg)); + $title->addHtml(HtmlElement::create('span', ['class' => 'state'], $this->item->getTypeText())); } protected function assembleCaption(BaseHtmlElement $caption): void @@ -144,7 +88,7 @@ protected function assembleCaption(BaseHtmlElement $caption): void protected function assembleHeader(BaseHtmlElement $header): void { $content = []; - if ($this->item->type !== 'internal') { + if ($this->item->type === 'state') { /** @var Objects $object */ $object = $this->item->object; /** @var Source $source */ From d302c39c3c442a13e259389be9bbad032092f7a7 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Wed, 26 Jun 2024 09:47:26 +0200 Subject: [PATCH 2/2] Icons: Update icons `exclamation-triangle` (fa-v5) -> `triangle-exclamation` (fa-v6) --- library/Notifications/Common/Icons.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/Notifications/Common/Icons.php b/library/Notifications/Common/Icons.php index 22dd0ab3..9d9fea63 100644 --- a/library/Notifications/Common/Icons.php +++ b/library/Notifications/Common/Icons.php @@ -10,7 +10,7 @@ private function __construct() { } - public const WARNING = 'exclamation-triangle'; + public const WARNING = 'triangle-exclamation'; public const OK = 'circle-check'; @@ -58,17 +58,17 @@ private function __construct() public const SEVERITY_CRIT = 'circle-exclamation'; - public const SEVERITY_WARN = 'exclamation-triangle'; + public const SEVERITY_WARN = 'triangle-exclamation'; public const SEVERITY_ERR = 'circle-xmark'; public const SEVERITY_DEBUG = 'bug-slash'; - public const SEVERITY_INFO = 'info'; + public const SEVERITY_INFO = 'circle-info'; public const SEVERITY_ALERT = 'bell'; - public const SEVERITY_EMERG = 'tower-broadcast'; + public const SEVERITY_EMERG = 'bullhorn'; public const SEVERITY_NOTICE = 'envelope'; }