From 399d7ae06796087612933df93ce209e4845252e9 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 12 Jan 2024 11:14:19 +0100 Subject: [PATCH] event/events: Fetch id tags already with the base query --- application/controllers/EventController.php | 1 + application/controllers/EventsController.php | 3 ++- library/Notifications/Model/Event.php | 17 +++++++++++++++++ library/Notifications/Model/Objects.php | 15 ++++++--------- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/application/controllers/EventController.php b/application/controllers/EventController.php index fa9c70b7e..55a873e0d 100644 --- a/application/controllers/EventController.php +++ b/application/controllers/EventController.php @@ -26,6 +26,7 @@ public function indexAction(): void $query = Event::on(Database::get()) ->with(['object', 'object.source', 'incident', 'incident.object']) + ->withColumns(['object.id_tags', 'incident.object.id_tags']) ->filter(Filter::equal('event.id', $id)); // ipl-orm doesn't detect dependent joins yet diff --git a/application/controllers/EventsController.php b/application/controllers/EventsController.php index 8b7bf794d..470facfa6 100644 --- a/application/controllers/EventsController.php +++ b/application/controllers/EventsController.php @@ -32,7 +32,8 @@ public function indexAction(): void $compact = $this->view->compact; $events = Event::on(Database::get()) - ->with(['object', 'object.source', 'incident']); + ->with(['object', 'object.source', 'incident']) + ->withColumns('object.id_tags'); $limitControl = $this->createLimitControl(); $sortControl = $this->createSortControl( diff --git a/library/Notifications/Model/Event.php b/library/Notifications/Model/Event.php index 09f58b3d2..827845276 100644 --- a/library/Notifications/Model/Event.php +++ b/library/Notifications/Model/Event.php @@ -4,11 +4,15 @@ namespace Icinga\Module\Notifications\Model; +use Icinga\Module\Notifications\Common\Database; use ipl\Orm\Behavior\Binary; use ipl\Orm\Behavior\MillisecondTimestamp; use ipl\Orm\Behaviors; use ipl\Orm\Model; +use ipl\Orm\Query; use ipl\Orm\Relations; +use ipl\Sql\Connection; +use ipl\Sql\Select; class Event extends Model { @@ -56,6 +60,19 @@ public function getDefaultSort() return 'event.time'; } + public static function on(Connection $db) + { + $query = parent::on($db); + + $query->on(Query::ON_SELECT_ASSEMBLED, function (Select $select) use ($query) { + if (isset($query->getUtilize()['event.object.object_id_tag'])) { + Database::registerGroupBy($query, $select); + } + }); + + return $query; + } + public function createBehaviors(Behaviors $behaviors) { $behaviors->add(new MillisecondTimestamp(['time'])); diff --git a/library/Notifications/Model/Objects.php b/library/Notifications/Model/Objects.php index 1eda2e201..61ff187ed 100644 --- a/library/Notifications/Model/Objects.php +++ b/library/Notifications/Model/Objects.php @@ -4,14 +4,17 @@ namespace Icinga\Module\Notifications\Model; +use Icinga\Module\Notifications\Model\Behavior\IdTagAggregator; use ipl\Html\HtmlString; use ipl\Html\ValidHtml; use ipl\Orm\Behavior\Binary; use ipl\Orm\Behaviors; use ipl\Orm\Model; -use ipl\Orm\Query; use ipl\Orm\Relations; +/** + * @property array $id_tags + */ class Objects extends Model { public function getTableName() @@ -52,6 +55,7 @@ public function getDefaultSort() public function createBehaviors(Behaviors $behaviors) { $behaviors->add(new Binary(['id'])); + $behaviors->add(new IdTagAggregator()); } public function createRelations(Relations $relations) @@ -73,15 +77,8 @@ public function getName(): ValidHtml { //TODO: Once hooks are available, they should render the tags accordingly $objectTags = []; - /** @var Query $objectIdTagQuery */ - $objectIdTagQuery = $this->object_id_tag; - /** @var ObjectIdTag $id_tag */ - foreach ($objectIdTagQuery as $id_tag) { - /** @var string $tag */ - $tag = $id_tag->tag; - /** @var string $value */ - $value = $id_tag->value; + foreach ($this->id_tags as $tag => $value) { $objectTags[] = sprintf('%s=%s', $tag, $value); }