Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show root problem list for objects with problem and are part of dependency #1057

Open
wants to merge 2 commits into
base: dependencies
Choose a base branch
from
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
88 changes: 57 additions & 31 deletions library/Icingadb/Common/StateBadges.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ public function __construct($item)
$this->url = $this->getBaseUrl();
}

/**
* Get the badge base URL
*
* @return Url
*/
abstract protected function getBaseUrl(): Url;

/**
* Get the type of the items
*
Expand All @@ -67,14 +60,27 @@ abstract protected function getType(): string;
*/
abstract protected function getPrefix(): string;

/**
* Get the badge base URL
*
* @return ?Url
*/
protected function getBaseUrl(): ?Url
{
return null;
}

/**
* Get the integer of the given state text
*
* @param string $state
*
* @return int
*/
abstract protected function getStateInt(string $state): int;
protected function getStateInt(string $state): int
{
return 0;
Copy link
Member

Choose a reason for hiding this comment

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

Throw InvalidArgumentException instead

}

/**
* Get the badge URL
Expand Down Expand Up @@ -132,18 +138,25 @@ public function createLink($content, Filter\Rule $filter = null): Link
* Create a state bade
*
* @param string $state
* @param bool $createLink Create link for the badge if true
*
* @return ?BaseHtmlElement
*/
protected function createBadge(string $state)
protected function createBadge(string $state, bool $createLink = true)
Copy link
Member

Choose a reason for hiding this comment

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

Drop $createLink and replace it with $this->url !== null

{
$key = $this->prefix . "_{$state}";

if (isset($this->item->$key) && $this->item->$key) {
return Html::tag('li', $this->createLink(
new StateBadge($this->item->$key, $state),
Filter::equal($this->type . '.state.soft_state', $this->getStateInt($state))
));
$stateBadge = new StateBadge($this->item->$key, $state);

if ($createLink) {
$this->createLink(
$stateBadge,
Filter::equal($this->type . '.state.soft_state', $this->getStateInt($state))
);
}

return Html::tag('li', $stateBadge);
}

return null;
Expand All @@ -153,37 +166,50 @@ protected function createBadge(string $state)
* Create a state group
*
* @param string $state
* @param bool $createLink Create link for the badge if true
*
* @return ?BaseHtmlElement
*/
protected function createGroup(string $state)
protected function createGroup(string $state, bool $createLink = true)
Copy link
Member

Choose a reason for hiding this comment

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

Drop $createLink and replace it with $this->url !== null

{
$content = [];
$handledKey = $this->prefix . "_{$state}_handled";
$unhandledKey = $this->prefix . "_{$state}_unhandled";

if (isset($this->item->$unhandledKey) && $this->item->$unhandledKey) {
$content[] = Html::tag('li', $this->createLink(
new StateBadge($this->item->$unhandledKey, $state),
Filter::all(
Filter::equal($this->type . '.state.soft_state', $this->getStateInt($state)),
Filter::equal($this->type . '.state.is_handled', 'n'),
Filter::equal($this->type . '.state.is_reachable', 'y')
)
));
$unhandledStateBadge = new StateBadge($this->item->$unhandledKey, $state);

if ($createLink) {
$unhandledStateBadge = $this->createLink(
$unhandledStateBadge,
Filter::all(
Filter::equal($this->type . '.state.soft_state', $this->getStateInt($state)),
Filter::equal($this->type . '.state.is_handled', 'n'),
Filter::equal($this->type . '.state.is_reachable', 'y')
)
);
}

$content[] = Html::tag('li', $unhandledStateBadge);
}

if (isset($this->item->$handledKey) && $this->item->$handledKey) {
$content[] = Html::tag('li', $this->createLink(
new StateBadge($this->item->$handledKey, $state, true),
Filter::all(
Filter::equal($this->type . '.state.soft_state', $this->getStateInt($state)),
Filter::any(
Filter::equal($this->type . '.state.is_handled', 'y'),
Filter::equal($this->type . '.state.is_reachable', 'n')
$handledStateBadge = new StateBadge($this->item->$handledKey, $state, true);

if ($createLink) {
$handledStateBadge = $this->createLink(
$handledStateBadge,
Filter::all(
Filter::equal($this->type . '.state.soft_state', $this->getStateInt($state)),
Filter::any(
Filter::equal($this->type . '.state.is_handled', 'y'),
Filter::equal($this->type . '.state.is_reachable', 'n')
)
)
)
));
);
}

$content[] = Html::tag('li', $handledStateBadge);
}

if (empty($content)) {
Expand Down
14 changes: 10 additions & 4 deletions library/Icingadb/Model/DependencyEdge.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* @property string $from_node_id
* @property ?string $dependency_id
*
* @property DependencyNode|Query $from
* @property DependencyNode|Query $to
* @property DependencyNode|Query $child
* @property DependencyNode|Query $parent
* @property (?Dependency)|Query $dependency
*/
class DependencyEdge extends Model
Expand Down Expand Up @@ -53,11 +53,17 @@ public function createBehaviors(Behaviors $behaviors): void

public function createRelations(Relations $relations): void
{
$relations->belongsTo('from', DependencyNode::class)
$relations->belongsTo('child', DependencyNode::class)
->setCandidateKey('from_node_id');
$relations->belongsTo('to', DependencyNode::class)
$relations->belongsTo('parent', DependencyNode::class)
->setCandidateKey('to_node_id');
$relations->belongsTo('dependency', Dependency::class)
->setJoinType('LEFT');

// "from" and "to" are only necessary for sub-query filters.
$relations->belongsTo('from', DependencyNode::class)
->setCandidateKey('from_node_id');
$relations->belongsTo('to', DependencyNode::class)
->setCandidateKey('to_node_id');
}
}
25 changes: 13 additions & 12 deletions library/Icingadb/Model/DependencyNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Icinga\Module\Icingadb\Model;

use Icinga\Module\Icingadb\Model\Behavior\ReRoute;
use ipl\Orm\Behavior\Binary;
use ipl\Orm\Behaviors;
use ipl\Orm\Model;
Expand All @@ -23,8 +24,6 @@
* @property (?RedundancyGroup)|Query $redundancy_group
* @property (?DependencyEdge)|Query $from
* @property (?DependencyEdge)|Query $to
* @property (?DependencyNode)|Query $child
* @property (?DependencyNode)|Query $parent
*/
class DependencyNode extends Model
{
Expand Down Expand Up @@ -56,6 +55,10 @@ public function createBehaviors(Behaviors $behaviors): void
'service_id',
'redundancy_group_id'
]));
$behaviors->add(new ReRoute([
'child' => 'to.from',
'parent' => 'from.to'
]));
}

public function createRelations(Relations $relations): void
Expand All @@ -74,15 +77,13 @@ public function createRelations(Relations $relations): void
->setForeignKey('to_node_id')
->setJoinType('LEFT');

$relations->belongsToMany('child', self::class)
->through(DependencyEdge::class)
->setForeignKey('to_node_id')
->setTargetForeignKey('from_node_id')
->setJoinType('LEFT');
$relations->belongsToMany('parent', self::class)
->through(DependencyEdge::class)
->setForeignKey('from_node_id')
->setTargetForeignKey('to_node_id')
->setJoinType('LEFT');
// TODO: This self join is only a work-around as when selecting nodes and filtering by child or parent,
// the ORM wants to join the base table as usual in case a sub-query is used. Though, in this case
// resolving e.g. child to "to.from" is reversed in a sub-query to "from.to" and the ORM does not
// detect that "to" is already the link to the base table.
// Given the path "dependency_node.to.from.host", the sub-query uses "host.from.to.dependency_node".
// "to.dependency_node" is the crucial part, as "dependency_node" is said self-join.
$relations->hasOne('dependency_node', self::class)
->setForeignKey('id');
}
}
15 changes: 11 additions & 4 deletions library/Icingadb/Model/Host.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public function createBehaviors(Behaviors $behaviors)
]));

$behaviors->add(new ReRoute([
'child' => 'dependency_node.child',
'parent' => 'dependency_node.parent',
'child' => 'to.from',
'parent' => 'from.to',
'servicegroup' => 'service.servicegroup',
'user' => 'notification.user',
'usergroup' => 'notification.usergroup'
Expand Down Expand Up @@ -237,8 +237,6 @@ public function createDefaults(Defaults $defaults)

public function createRelations(Relations $relations)
{
$relations->belongsTo('dependency_node', DependencyNode::class)
->setJoinType('LEFT');
$relations->belongsTo('environment', Environment::class);
$relations->belongsTo('eventcommand', Eventcommand::class);
$relations->belongsTo('checkcommand', Checkcommand::class);
Expand Down Expand Up @@ -274,5 +272,14 @@ public function createRelations(Relations $relations)
$relations->hasMany('notification', Notification::class)->setJoinType('LEFT');
$relations->hasMany('notification_history', NotificationHistory::class);
$relations->hasMany('service', Service::class)->setJoinType('LEFT');

$relations->belongsToMany('from', DependencyEdge::class)
->setTargetCandidateKey('from_node_id')
->setTargetForeignKey('id')
->through(DependencyNode::class);
$relations->belongsToMany('to', DependencyEdge::class)
->setTargetCandidateKey('to_node_id')
->setTargetForeignKey('id')
->through(DependencyNode::class);
}
}
19 changes: 13 additions & 6 deletions library/Icingadb/Model/RedundancyGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*
* @property (?RedundancyGroupState)|Query $state
* @property Dependency|Query $dependency
* @property DependencyEdge|Query $from
* @property DependencyEdge|Query $to
*/
class RedundancyGroup extends Model
{
Expand Down Expand Up @@ -48,20 +50,25 @@ public function createBehaviors(Behaviors $behaviors): void
'id'
]));
$behaviors->add(new ReRoute([
'child' => 'dependency_node.child',
'parent' => 'dependency_node.parent'
'child' => 'to.from',
'parent' => 'from.to'
]));
}

public function createRelations(Relations $relations): void
{
$relations->belongsTo('dependency_node', DependencyNode::class)
->setForeignKey('redundancy_group_id')
->setCandidateKey('id');

$relations->hasOne('state', RedundancyGroupState::class)
->setJoinType('LEFT');

$relations->hasMany('dependency', Dependency::class);

$relations->belongsToMany('from', DependencyEdge::class)
->setTargetCandidateKey('from_node_id')
->setTargetForeignKey('id')
->through(DependencyNode::class);
$relations->belongsToMany('to', DependencyEdge::class)
->setTargetCandidateKey('to_node_id')
->setTargetForeignKey('id')
->through(DependencyNode::class);
}
}
Loading