Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion library/Director/Objects/IcingaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,13 @@ public function countDirectUses()
array('n' => 'icinga_notification'),
array('cnt' => 'COUNT(*)')
)->where('n.command_id = ?', $id);
$qc = $db->select()->from(
['c' => 'icinga_command_inheritance'],
['cnt' => 'COUNT(*)']
)->where('c.parent_command_id = ?', $id);

$query = $db->select()->union(
[$qh, $qs, $qn],
[$qh, $qs, $qn, $qc],
DbSelect::SQL_UNION_ALL
);

Expand Down
22 changes: 19 additions & 3 deletions library/Director/Resolver/CommandUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function getLinks()
'host' => ['check_command', 'event_command'],
'service' => ['check_command', 'event_command'],
'notification' => ['command'],
'command' => ['command'],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'command' => ['command'],
'command' => [],

This could be an empty array, as this is basically used to build the command id column for the table to filter the objects using the corresponding command.

];
$types = [
'host' => [
Expand All @@ -60,6 +61,10 @@ public function getLinks()
'template' => $this->translate('%d Notification Template(s)'),
'apply' => $this->translate('%d Notification Apply Rule(s)'),
],
'command' => [
'object' => $this->translate('%d Commands(s)'),
'template' => $this->translate('%d Command Templates(s)'),
],
];

$urlSuffix = [
Expand Down Expand Up @@ -93,10 +98,21 @@ protected function fetchFor($table, $rels, $objectTypes)
foreach ($objectTypes as $type) {
$columns[$type] = "COALESCE(SUM(CASE WHEN object_type = '$type' THEN 1 ELSE 0 END), 0)";
}
$query = $this->db->select()->from("icinga_$table", $columns);

foreach ($rels as $rel) {
$query->orWhere("{$rel}_id = ?", $id);
if ($table === "command") {
$query = $this->db->select()->from(array('c' => 'icinga_' . $table), $columns)->join(array('ici' => 'icinga_command_inheritance'),
'ici.command_id = c.id',
array());

foreach ($rels as $rel) {
$query->orWhere("parent_{$rel}_id = ?", $id);
}
Comment on lines +103 to +109
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$query = $this->db->select()->from(array('c' => 'icinga_' . $table), $columns)->join(array('ici' => 'icinga_command_inheritance'),
'ici.command_id = c.id',
array());
foreach ($rels as $rel) {
$query->orWhere("parent_{$rel}_id = ?", $id);
}
$query = $this->db->select()->from(['c' => 'icinga_' . $table], $columns)->join(
['ici' => 'icinga_command_inheritance'],
'ici.command_id = c.id',
[]
)->where('ici.parent_command_id = ?', $id);

IMO, you could rewrite the where as suggested, just makes it a bit easier to understand the code

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems not to be resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would render the $rels var useless, and might break something else

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The $rels variable comes from the $map variable. You could give an empty array there, as the relation in the map is basically used to build the column {rel}_id to filter the objects. And since for command you filter its inheritance table. You could directly filter it in the query.

Or you could do the below, where you add the join and the filter to the query only if the table is 'command', and everything remains same. This solution also results in minimal changes to the existing code. You should still give empty array for command ('command' => []) in $map variable. Also, looking at it again I find this solution better.

        $query = $this->db->select()->from(['c' => "icinga_$table"], $columns);
        if ($table === "command") {
            $query->join(
                ['ici' => 'icinga_command_inheritance'],
                'ici.command_id = c.id',
                []
            )->where('ici.parent_command_id = ?', $id);
        }

        foreach ($rels as $rel) {
            $query->orWhere("{$rel}_id = ?", $id);
        }

} else {
$query = $this->db->select()->from("icinga_$table", $columns);

foreach ($rels as $rel) {
$query->orWhere("{$rel}_id = ?", $id);
}
}

return $this->db->fetchRow($query);
Expand Down
12 changes: 12 additions & 0 deletions library/Director/Web/Controller/ObjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,18 @@ protected function eventuallyFilterCommand(ZfQueryBasedTable $table)
$command->getAutoincId()
);
break;
case 'command':
$table->getQuery()
->join(
['ici' => 'icinga_command_inheritance'],
'ici.command_id = o.id',
[]
)->where(
'ici.parent_command_id = ?',
$command->getAutoincId()
);

break;
}
}

Expand Down
Loading