Skip to content

Commit

Permalink
event/events: Fetch id tags already with the base query
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg authored and raviks789 committed Mar 21, 2024
1 parent ef5d063 commit 399d7ae
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions application/controllers/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion application/controllers/EventsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
17 changes: 17 additions & 0 deletions library/Notifications/Model/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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']));
Expand Down
15 changes: 6 additions & 9 deletions library/Notifications/Model/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> $id_tags
*/
class Objects extends Model
{
public function getTableName()
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}

Expand Down

0 comments on commit 399d7ae

Please sign in to comment.