-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
HasRootProblem
behavior for service
- Loading branch information
Showing
4 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2 */ | ||
|
||
namespace Icinga\Module\Icingadb\Model\Behavior; | ||
|
||
use Icinga\Module\Icingadb\Model\DependencyEdge; | ||
use ipl\Orm\AliasedExpression; | ||
use ipl\Orm\ColumnDefinition; | ||
use ipl\Orm\Query; | ||
use ipl\Sql\Expression; | ||
use ipl\Sql\Filter\Exists; | ||
use ipl\Sql\Filter\NotExists; | ||
use ipl\Stdlib\Filter; | ||
use ipl\Orm\Contract\RewriteColumnBehavior; | ||
use ipl\Orm\Contract\QueryAwareBehavior; | ||
|
||
class HasRootProblem implements RewriteColumnBehavior, QueryAwareBehavior | ||
{ | ||
/** @var Query */ | ||
protected $query; | ||
|
||
public function setQuery(Query $query) | ||
{ | ||
$this->query = $query; | ||
|
||
return $this; | ||
} | ||
|
||
public function rewriteColumn($column, ?string $relation = null) | ||
{ | ||
if ($this->isSelectableColumn($column)) { | ||
$subQuery = $this->createBehaviorSubquery($relation); | ||
|
||
$column = $relation !== null ? str_replace('.', '_', $relation) . "_$column" : $column; | ||
|
||
$alias = $this->query->getDb()->quoteIdentifier([$column]); | ||
|
||
list($select, $values) = $this->query->getDb() | ||
->getQueryBuilder() | ||
->assembleSelect($subQuery->assembleSelect()); | ||
|
||
return new AliasedExpression($alias, "($select)", null, ...$values); | ||
} | ||
} | ||
|
||
public function isSelectableColumn(string $name): bool | ||
{ | ||
return $name === 'has_root_problem'; | ||
} | ||
|
||
public function rewriteColumnDefinition(ColumnDefinition $def, string $relation): void | ||
{ | ||
$name = $def->getName(); | ||
|
||
if ($this->isSelectableColumn($name)) { | ||
$def->setLabel(t('Has Root Problem')); | ||
} | ||
} | ||
|
||
/** | ||
* Create the sub-query used by the behavior | ||
* | ||
* @param ?string $relation | ||
* | ||
* @return Query | ||
*/ | ||
protected function createBehaviorSubquery(?string $relation = null): Query | ||
{ | ||
$path = 'from.dependency_node'; | ||
$subQueryRelation = $relation !== null ? $relation . $path : $path; | ||
$subQuery = $this->query->createSubQuery(new DependencyEdge(), $subQueryRelation) | ||
->limit(1) | ||
->columns([new Expression('1')]); | ||
|
||
$subQuery->getSelectBase()->join( | ||
['root_dependency' => 'dependency'], | ||
[$subQuery->getResolver()->getAlias($subQuery->getModel()) . '.dependency_id = root_dependency.id'] | ||
)->join( | ||
['root_dependency_state' => 'dependency_state'], | ||
['root_dependency.id = root_dependency_state.dependency_id'] | ||
)->where(new Expression("root_dependency_state.failed = 'y'")); | ||
|
||
return $subQuery; | ||
} | ||
|
||
public function rewriteCondition(Filter\Condition $condition, $relation = null) | ||
{ | ||
$column = substr($condition->getColumn(), strlen($relation)); | ||
|
||
if ($this->isSelectableColumn($column)) { | ||
$subQuery = $this->createBehaviorSubquery($relation); | ||
if ($condition->getValue()) { | ||
if ($condition instanceof Filter\Unequal) { | ||
return new NotExists($subQuery->assembleSelect()); | ||
} else { | ||
return new Exists($subQuery->assembleSelect()); | ||
} | ||
} elseif ($condition instanceof Filter\Unequal) { | ||
return new Exists($subQuery->assembleSelect()); | ||
} else { | ||
return new NotExists($subQuery->assembleSelect()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters