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 Apr 9, 2024
1 parent 1fc71dd commit d9da5ee
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 30 deletions.
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;
}
}
1 change: 1 addition & 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 Down
3 changes: 0 additions & 3 deletions library/Notifications/Model/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
use ipl\Orm\Model;
use ipl\Orm\Relations;

/**
* @property array<string, string> $id_tags
*/
class Objects extends Model
{
public function getTableName()
Expand Down
8 changes: 8 additions & 0 deletions library/Notifications/Model/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
use ipl\Orm\Relations;
use ipl\Web\Widget\IcingaIcon;
use ipl\Web\Widget\Icon;
use ipl\Orm\Query;

/**
* @property int $id
* @property string $type
* @property string $name
*
* @property Model<Objects> | Query<Objects> $object
*/
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,15 +5,18 @@
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\Incident;
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 InvalidArgumentException;
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 @@ -80,37 +83,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);

Check failure on line 92 in library/Notifications/Widget/Detail/EventDetail.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #2 $objectIdTag of static method Icinga\Module\Notifications\Hook\EventsObjectsInfoHook::getObjectListItemWidget() expects array<string, string>, mixed given.

Check failure on line 92 in library/Notifications/Widget/Detail/EventDetail.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #2 $objectIdTag of static method Icinga\Module\Notifications\Hook\EventsObjectsInfoHook::getObjectListItemWidget() expects array<string, string>, mixed given.

Check failure on line 92 in library/Notifications/Widget/Detail/EventDetail.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #2 $objectIdTag of static method Icinga\Module\Notifications\Hook\EventsObjectsInfoHook::getObjectListItemWidget() expects array<string, string>, mixed given.

Check failure on line 92 in library/Notifications/Widget/Detail/EventDetail.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #2 $objectIdTag of static method Icinga\Module\Notifications\Hook\EventsObjectsInfoHook::getObjectListItemWidget() expects array<string, string>, mixed given.

Check failure on line 92 in library/Notifications/Widget/Detail/EventDetail.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #2 $objectIdTag of static method Icinga\Module\Notifications\Hook\EventsObjectsInfoHook::getObjectListItemWidget() expects array<string, string>, mixed given.

Check failure on line 92 in library/Notifications/Widget/Detail/EventDetail.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #2 $objectIdTag of static method Icinga\Module\Notifications\Hook\EventsObjectsInfoHook::getObjectListItemWidget() expects array<string, string>, mixed given.
$objUrl = Url::fromPath($obj->url);

Check failure on line 93 in library/Notifications/Widget/Detail/EventDetail.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #1 $url of static method Icinga\Web\Url::fromPath() expects string, mixed given.

Check failure on line 93 in library/Notifications/Widget/Detail/EventDetail.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #1 $url of static method Icinga\Web\Url::fromPath() expects string, mixed given.

Check failure on line 93 in library/Notifications/Widget/Detail/EventDetail.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #1 $url of static method Icinga\Web\Url::fromPath() expects string, mixed given.

Check failure on line 93 in library/Notifications/Widget/Detail/EventDetail.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #1 $url of static method Icinga\Web\Url::fromPath() expects string, mixed given.

Check failure on line 93 in library/Notifications/Widget/Detail/EventDetail.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #1 $url of static method Icinga\Web\Url::fromPath() expects string, mixed given.

Check failure on line 93 in library/Notifications/Widget/Detail/EventDetail.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #1 $url of static method Icinga\Web\Url::fromPath() expects string, mixed given.

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 @@ -6,6 +6,11 @@

use Icinga\Module\Notifications\Common\LoadMore;
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;
use ipl\Web\Common\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) {

Check failure on line 42 in library/Notifications/Widget/ItemList/EventList.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Access to undefined constant Icinga\Module\Notifications\Widget\ItemList\EventList::ON_ITEM_ADD.

Check failure on line 42 in library/Notifications/Widget/ItemList/EventList.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Access to undefined constant Icinga\Module\Notifications\Widget\ItemList\EventList::ON_ITEM_ADD.

Check failure on line 42 in library/Notifications/Widget/ItemList/EventList.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Access to undefined constant Icinga\Module\Notifications\Widget\ItemList\EventList::ON_ITEM_ADD.

Check failure on line 42 in library/Notifications/Widget/ItemList/EventList.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Access to undefined constant Icinga\Module\Notifications\Widget\ItemList\EventList::ON_ITEM_ADD.

Check failure on line 42 in library/Notifications/Widget/ItemList/EventList.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Access to undefined constant Icinga\Module\Notifications\Widget\ItemList\EventList::ON_ITEM_ADD.

Check failure on line 42 in library/Notifications/Widget/ItemList/EventList.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Access to undefined constant Icinga\Module\Notifications\Widget\ItemList\EventList::ON_ITEM_ADD.
/** @var Objects $obj */
$obj = $data->object;
/** @var Source $src */
$src = $obj->source;
$this->objectIdTagsCache[$src->type][$obj->id] = $obj->id_tags;

Check failure on line 47 in library/Notifications/Widget/ItemList/EventList.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Property Icinga\Module\Notifications\Widget\ItemList\EventList::$objectIdTagsCache (array<string, array<string, array<string, string>>>) does not accept array<string, array<int|string, mixed>>.

Check failure on line 47 in library/Notifications/Widget/ItemList/EventList.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Property Icinga\Module\Notifications\Widget\ItemList\EventList::$objectIdTagsCache (array<string, array<string, array<string, string>>>) does not accept array<string, array<int|string, mixed>>.

Check failure on line 47 in library/Notifications/Widget/ItemList/EventList.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Property Icinga\Module\Notifications\Widget\ItemList\EventList::$objectIdTagsCache (array<string, array<string, array<string, string>>>) does not accept array<string, array<int|string, mixed>>.

Check failure on line 47 in library/Notifications/Widget/ItemList/EventList.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Property Icinga\Module\Notifications\Widget\ItemList\EventList::$objectIdTagsCache (array<string, array<string, array<string, string>>>) does not accept array<string, array<int|string, mixed>>.

Check failure on line 47 in library/Notifications/Widget/ItemList/EventList.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Property Icinga\Module\Notifications\Widget\ItemList\EventList::$objectIdTagsCache (array<string, array<string, array<string, string>>>) does not accept array<string, array<int|string, mixed>>.

Check failure on line 47 in library/Notifications/Widget/ItemList/EventList.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Property Icinga\Module\Notifications\Widget\ItemList\EventList::$objectIdTagsCache (array<string, array<string, array<string, string>>>) does not accept array<string, array<int|string, mixed>>.
});

$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 @@ -11,6 +11,7 @@
use Icinga\Module\Notifications\Widget\SourceIcon;
use InvalidArgumentException;
use ipl\Html\BaseHtmlElement;
use ipl\Html\DeferredText;
use ipl\Html\Html;
use ipl\Web\Common\BaseListItem;
use ipl\Web\Widget\Icon;
Expand Down Expand Up @@ -93,14 +94,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 d9da5ee

Please sign in to comment.