From bdaa5a66eaad4a7a028d6ae6c366b41916563f6e Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 6 Oct 2023 11:36:47 +0200 Subject: [PATCH] WIP --- library/Notifications/Model/ObjectIdTag.php | 42 +++++++++++++++++++ library/Notifications/Model/Objects.php | 32 ++++++++------ .../Widget/Detail/EventDetail.php | 11 ++--- .../Widget/Detail/IncidentDetail.php | 11 ++--- .../Widget/ItemList/EventListItem.php | 18 ++++---- .../ItemList/IncidentHistoryListItem.php | 11 ++--- .../Widget/ItemList/IncidentListItem.php | 10 +++-- 7 files changed, 94 insertions(+), 41 deletions(-) create mode 100644 library/Notifications/Model/ObjectIdTag.php diff --git a/library/Notifications/Model/ObjectIdTag.php b/library/Notifications/Model/ObjectIdTag.php new file mode 100644 index 000000000..3f53b9f07 --- /dev/null +++ b/library/Notifications/Model/ObjectIdTag.php @@ -0,0 +1,42 @@ +add(new Binary(['object_id'])); + } + + public function createRelations(Relations $relations): void + { + $relations->belongsTo('object', Objects::class); + } +} diff --git a/library/Notifications/Model/Objects.php b/library/Notifications/Model/Objects.php index ec06e199f..be18c287f 100644 --- a/library/Notifications/Model/Objects.php +++ b/library/Notifications/Model/Objects.php @@ -25,29 +25,19 @@ public function getColumns() { return [ 'source_id', - 'host', - 'service', 'name', 'url' ]; } - public function getColumnDefinitions() - { - return [ - 'host' => t('Host Name'), - 'service' => t('Service Name') - ]; - } - public function getSearchColumns() { - return ['host', 'service']; + return ['tag', 'value']; } public function getDefaultSort() { - return 'object.host'; + return 'object.value'; } public function createBehaviors(Behaviors $behaviors) @@ -60,7 +50,25 @@ public function createRelations(Relations $relations) $relations->hasMany('event', Event::class); $relations->hasMany('incident', Incident::class); $relations->belongsTo('source', Source::class); + $relations->hasMany('object_id_tag', ObjectIdTag::class); $relations->hasMany('object_extra_tag', ObjectExtraTag::class) ->setJoinType('LEFT'); } + + //TODO: Just a temporary solution to avoid changing code everywhere. + // This must be done with hooks, remove once hooks are available + public function getHostAndService(): array + { + $hostName = null; + $serviceName = null; + foreach ($this->object_id_tag as $id_tag) { + if ($id_tag->tag === 'host') { + $hostName = sprintf('host=%s', $id_tag->value); + } else { + $serviceName = sprintf('service=%s', $id_tag->value); + } + } + + return [$hostName, $serviceName]; + } } diff --git a/library/Notifications/Widget/Detail/EventDetail.php b/library/Notifications/Widget/Detail/EventDetail.php index 0b1e2f794..56737f670 100644 --- a/library/Notifications/Widget/Detail/EventDetail.php +++ b/library/Notifications/Widget/Detail/EventDetail.php @@ -71,13 +71,14 @@ 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']); - $obj = new Link($this->event->object->host, $this->event->object->url, ['class' => 'subject']); + [$hostName, $serviceName] = $this->event->object->getHostAndService(); + $obj = new Link($hostName, $this->event->object->url, ['class' => 'subject']); - if ($this->event->object->service) { + if ($serviceName) { $obj = Html::sprintf( - t('%s on %s', ' on '), - $obj->setContent($this->event->object->service), - Html::tag('span', ['class' => 'subject'], $this->event->object->host) + t('%s : %s'), + $obj->setContent($serviceName), + Html::tag('span', ['class' => 'subject'], $hostName) ); } diff --git a/library/Notifications/Widget/Detail/IncidentDetail.php b/library/Notifications/Widget/Detail/IncidentDetail.php index 1a4fbe986..790cd6e44 100644 --- a/library/Notifications/Widget/Detail/IncidentDetail.php +++ b/library/Notifications/Widget/Detail/IncidentDetail.php @@ -56,15 +56,16 @@ protected function createRelatedObject() { //TODO(sd): Add hook implementation $list = Html::tag('ul', ['class' => ['item-list', 'minimal', 'action-list'], 'data-base-target' => '_next']); + [$hostName, $serviceName] = $this->incident->object->getHostAndService(); - if ($this->incident->object->service) { + if ($serviceName) { $objectLink = Html::sprintf( - t('%s on %s', ' on '), - new Link($this->incident->object->service, $this->incident->object->url, ['class' => 'subject']), - Html::tag('span', ['class' => 'subject'], $this->incident->object->host) + t('%s : %s'), + new Link($serviceName, $this->incident->object->url, ['class' => 'subject']), + Html::tag('span', ['class' => 'subject'], $hostName) ); } else { - $objectLink = new Link($this->incident->object->host, $this->incident->object->url, ['class' => 'subject']); + $objectLink = new Link($hostName, $this->incident->object->url, ['class' => 'subject']); } $list->add(Html::tag( diff --git a/library/Notifications/Widget/ItemList/EventListItem.php b/library/Notifications/Widget/ItemList/EventListItem.php index 9d12546c8..f0c81af7c 100644 --- a/library/Notifications/Widget/ItemList/EventListItem.php +++ b/library/Notifications/Widget/ItemList/EventListItem.php @@ -79,21 +79,19 @@ protected function assembleTitle(BaseHtmlElement $title): void $title->addHtml(Html::tag('span', [], sprintf('#%d:', $this->item->incident->id))); } + [$hostName, $serviceName] = $this->item->object->getHostAndService(); + if (! $this->list->getNoSubjectLink()) { - $content = new Link( - $this->item->object->host, - Links::event($this->item->id), - ['class' => 'subject'] - ); + $content = new Link($hostName, Links::event($this->item->id), ['class' => 'subject']); } else { - $content = Html::tag('span', ['class' => 'subject'], $this->item->object->host); + $content = Html::tag('span', ['class' => 'subject'], $hostName); } - if ($this->item->object->service) { + if ($serviceName) { $content = Html::sprintf( - t('%s on %s', ' on '), - $content->setContent($this->item->object->service), - Html::tag('span', ['class' => 'subject'], $this->item->object->host) + t('%s:%s'), + $content->setContent($serviceName), + Html::tag('span', ['class' => 'subject'], $hostName) ); } diff --git a/library/Notifications/Widget/ItemList/IncidentHistoryListItem.php b/library/Notifications/Widget/ItemList/IncidentHistoryListItem.php index 177231f89..81c3a5ba8 100644 --- a/library/Notifications/Widget/ItemList/IncidentHistoryListItem.php +++ b/library/Notifications/Widget/ItemList/IncidentHistoryListItem.php @@ -53,19 +53,20 @@ protected function assembleTitle(BaseHtmlElement $title): void $this->getAttributes() ->set('data-action-item', true); - if ($event->object->service) { + [$hostName, $serviceName] = $event->object->getHostAndService(); + if ($serviceName) { $content = Html::sprintf( - t('%s on %s', ' on '), + t('%s : %s'), new Link( - $event->object->service, + $serviceName, Links::event($this->item->event_id), ['class' => 'subject'] ), - Html::tag('span', ['class' => 'subject'], $event->object->host) + Html::tag('span', ['class' => 'subject'], $hostName) ); } else { $content = new Link( - $event->object->host, + $hostName, Links::event($this->item->event_id), ['class' => 'subject'] ); diff --git a/library/Notifications/Widget/ItemList/IncidentListItem.php b/library/Notifications/Widget/ItemList/IncidentListItem.php index de3a1afbf..08b3f442c 100644 --- a/library/Notifications/Widget/ItemList/IncidentListItem.php +++ b/library/Notifications/Widget/ItemList/IncidentListItem.php @@ -48,7 +48,9 @@ protected function assembleVisual(BaseHtmlElement $visual): void protected function assembleTitle(BaseHtmlElement $title): void { $title->addHtml(Html::tag('span', [], sprintf('#%d:', $this->item->id))); - $subject = $this->item->object->service ?? $this->item->object->host; + [$hostName, $serviceName] = $this->item->object->getHostAndService(); + + $subject = $serviceName ?? $hostName; if (! $this->list->getNoSubjectLink()) { $content = new Link( $subject, @@ -63,11 +65,11 @@ protected function assembleTitle(BaseHtmlElement $title): void ); } - if ($this->item->object->service) { + if ($serviceName) { $content = Html::sprintf( - t('%s on %s', ' on '), + t('%s : %s'), $content, - Html::tag('span', ['class' => 'subject'], $this->item->object->host) + Html::tag('span', ['class' => 'subject'], $hostName) ); }