Skip to content

Commit

Permalink
Implement to get event object information from respective sources
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Mar 25, 2024
1 parent 2a6cb74 commit 6081322
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 28 deletions.
7 changes: 6 additions & 1 deletion library/Notifications/Common/BaseItemList.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ abstract class BaseItemList extends BaseHtmlElement
{
use BaseFilter;

public const ON_ITEM_ADD = 'item-added';

public const BEFORE_ITEM_ADD = 'before-item-added';

protected $baseAttributes = [
'class' => 'item-list',
'data-base-target' => '_next',
Expand Down Expand Up @@ -64,8 +68,9 @@ protected function assemble()
foreach ($this->data as $data) {
/** @var BaseListItem $item */
$item = new $itemClass($data, $this);

$this->emit(self::BEFORE_ITEM_ADD, [$item, $data]);
$this->add($item);
$this->emit(self::ON_ITEM_ADD, [$item, $data]);
}

if ($this->isEmpty()) {
Expand Down
88 changes: 88 additions & 0 deletions library/Notifications/Hook/EventsObjectsInfoHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/* Icinga Notifications Web | (c) 2024 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Notifications\Hook;

use Exception;
use Icinga\Application\Hook;
use Icinga\Application\Logger;
use ipl\Html\ValidHtml;

abstract class EventsObjectsInfoHook
{
/**
* Prepare object display names for the objects using the object ID tags
*
* @param array<string, array<string, string>> $objectIdTags
*
* @return array<string, ValidHtml>
*/
abstract public function renderObjectDisplayNames(array $objectIdTags): array;

/**
* Get the source of the objects
*
* @return string
*/
abstract public function getSource(): string;

/**
* Get the object list item widget for the given object ID tag
*
* @param array<string, string> $objectIdTag
*
* @return ValidHtml
*/
abstract public function getHtmlForObject(array $objectIdTag): ValidHtml;

/**
* Get object display names for the objects using the object ID tags
*
* @param array<string, array<string, array<string, string>>> $objectIdTags
*
* @return array<string, ValidHtml>
*/
final public static function getObjectsDisplayNames(array $objectIdTags): array
{
$objectDisplayNames = [];
foreach (Hook::all('Notifications\\EventsObjectsInfo') as $hook) {
/** @var self $hook */
try {
$objectDisplayNames = array_merge(
$objectDisplayNames,
$hook->renderObjectDisplayNames($objectIdTags[$hook->getSource()])
);
} catch (Exception $e) {
Logger::error('Failed to load hook %s:', get_class($hook), $e);
}
}

return $objectDisplayNames;
}

/**
* Get the object list item widget for the object using the object ID tag
*
* @param string $source
* @param array<string, string> $objectIdTag
*
* @return ?ValidHtml
*/
final public static function getObjectListItemWidget(string $source, array $objectIdTag): ?ValidHtml
{
$objectListItemWidget = null;
foreach (Hook::all('Notifications\\EventsObjectsInfo') as $hook) {
/** @var self $hook */
try {
if ($source === $hook->getSource()) {
$objectListItemWidget = $hook->getHtmlForObject($objectIdTag);
}
} catch (Exception $e) {
Logger::error('Failed to load hook %s:', get_class($hook), $e);
}
}

return $objectListItemWidget;
}
}
10 changes: 10 additions & 0 deletions library/Notifications/Model/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Icinga\Module\Notifications\Model;

use Icinga\Module\Notifications\Common\Database;
use DateTime;
use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behavior\MillisecondTimestamp;
use ipl\Orm\Behaviors;
Expand All @@ -14,6 +15,15 @@
use ipl\Sql\Connection;
use ipl\Sql\Select;

/**
* @property int $id
* @property DateTime $time
* @property string $object_id
* @property string $type
* @property string $severity
* @property string $message
* @property string $username
*/
class Event extends Model
{
public function getTableName()
Expand Down
4 changes: 4 additions & 0 deletions library/Notifications/Model/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
use ipl\Orm\Relations;

/**
* @property string $id
* @property int $source_id
* @property string $name
* @property string $url
* @property array<string, string> $id_tags
*/
class Objects extends Model
Expand Down
5 changes: 5 additions & 0 deletions library/Notifications/Model/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
use ipl\Web\Widget\IcingaIcon;
use ipl\Web\Widget\Icon;

/**
* @property int $id
* @property string $type
* @property string $name
*/
class Source extends Model
{
public function getTableName()
Expand Down
40 changes: 15 additions & 25 deletions library/Notifications/Widget/Detail/EventDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
namespace Icinga\Module\Notifications\Widget\Detail;

use Icinga\Date\DateFormatter;
use Icinga\Module\Notifications\Hook\EventsObjectsInfoHook;
use Icinga\Module\Notifications\Model\Event;
use Icinga\Module\Notifications\Model\Objects;
use Icinga\Module\Notifications\Model\Source;
use Icinga\Module\Notifications\Widget\EventSourceBadge;
use Icinga\Module\Notifications\Widget\ItemList\IncidentList;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Html\ValidHtml;
use ipl\Web\Url;
use ipl\Web\Widget\HorizontalKeyValue;
use ipl\Web\Widget\Link;
use ipl\Web\Widget\StateBall;
Expand Down Expand Up @@ -70,37 +73,24 @@ protected function createMessage(): array
/** @return ValidHtml[] */
protected function createRelatedObject(): array
{
//TODO(sd): This is just placeholder. Add hook implementation instead
$relatedObj = Html::tag('ul', ['class' => ['item-list', 'action-list'], 'data-base-target' => '_next']);

/** @var Objects $obj */
$obj = $this->event->object;

/** @var string $objUrl */
$objUrl = $obj->url;
$relatedObj->add(
Html::tag(
'li',
['class' => 'list-item', 'data-action-item' => true],
[ //TODO(sd): fix stateball
Html::tag('div', ['class' => 'visual'], new StateBall('down', StateBall::SIZE_LARGE)),
Html::tag(
'div',
['class' => 'main'],
Html::tag('header')
->add(Html::tag(
'div',
['class' => 'title'],
new Link($obj->getName(), $objUrl, ['class' => 'subject'])
))
)
]
)
);
/** @var Source $source */
$source = $obj->source;

$objWidget = EventsObjectsInfoHook::getObjectListItemWidget($source->name, $obj->id_tags);
$objUrl = Url::fromPath($obj->url);

return [
Html::tag('h2', t('Related Object')),
$relatedObj
$objWidget ?? (new Link(
$obj->getName(),
$objUrl->isExternal() ? $objUrl->getAbsoluteUrl() : $objUrl->getRelativeUrl(),
[
'class' => 'subject'
]
))->setBaseTarget('_next')
];
}

Expand Down
37 changes: 37 additions & 0 deletions library/Notifications/Widget/ItemList/EventList.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Icinga\Module\Notifications\Common\LoadMore;
use Icinga\Module\Notifications\Common\BaseItemList;
use Icinga\Module\Notifications\Common\NoSubjectLink;
use Icinga\Module\Notifications\Hook\EventsObjectsInfoHook;
use Icinga\Module\Notifications\Model\Event;
use Icinga\Module\Notifications\Model\Objects;
use Icinga\Module\Notifications\Model\Source;
use ipl\Html\ValidHtml;
use ipl\Orm\ResultSet;

class EventList extends BaseItemList
Expand All @@ -19,6 +24,12 @@ class EventList extends BaseItemList
/** @var ResultSet */
protected $data;

/** @var array<string, array<string, array<string, string>>> Object ID tags for each source */
public $objectIdTagsCache = [];

/** @var array<string, ValidHtml> Object display names obtained from the corresponding web modules of the sources */
public $renderedObjectDisplayNames = [];

public function __construct(ResultSet $data)
{
parent::__construct($data);
Expand All @@ -27,10 +38,36 @@ public function __construct(ResultSet $data)
protected function init(): void
{
$this->data = $this->getIterator($this->data);

$this->on(self::ON_ITEM_ADD, function (EventListItem $item, Event $data) {
/** @var Objects $obj */
$obj = $data->object;
/** @var Source $src */
$src = $obj->source;
$this->objectIdTagsCache[$src->type][$obj->id] = $obj->id_tags;
});

$this->on(self::ON_ASSEMBLED, function () {
$this->renderedObjectDisplayNames = EventsObjectsInfoHook::getObjectsDisplayNames(
$this->objectIdTagsCache
);
});
}

protected function getItemClass(): string
{
return EventListItem::class;
}

/**
* Get the rendered object display name for the given object ID
*
* @param string $objectID
*
* @return ?ValidHtml
*/
public function getRenderedObjectDisplayName(string $objectID): ?ValidHtml
{
return $this->renderedObjectDisplayNames[$objectID] ?? null;
}
}
6 changes: 4 additions & 2 deletions library/Notifications/Widget/ItemList/EventListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Icinga\Module\Notifications\Model\Objects;
use Icinga\Module\Notifications\Widget\SourceIcon;
use ipl\Html\BaseHtmlElement;
use ipl\Html\DeferredText;
use ipl\Html\Html;
use ipl\Web\Widget\Icon;
use ipl\Web\Widget\Link;
Expand Down Expand Up @@ -82,14 +83,15 @@ protected function assembleTitle(BaseHtmlElement $title): void

/** @var Objects $obj */
$obj = $this->item->object;
$name = $obj->getName();
$name = (new DeferredText(function () use ($obj) {
return $this->list->getRenderedObjectDisplayName($obj->id) ?? $obj->getName();
}))->setEscaped();
if (! $this->list->getNoSubjectLink()) {
$content = new Link($name, Links::event($this->item->id), ['class' => 'subject']);
} else {
$content = Html::tag('span', ['class' => 'subject'], $name);
}

$msg = null;
if ($this->item->severity === null) {
$msg = t('acknowledged');
} elseif ($this->item->severity === 'ok') {
Expand Down
14 changes: 14 additions & 0 deletions public/css/detail/event-detail.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.event-detail {
.subject {
color: @default-text-color;
}

a {
font-weight: bold;

&:hover {
color: @list-item-title-hover-color;
text-decoration: none;
}
}
}

0 comments on commit 6081322

Please sign in to comment.