Skip to content

Commit

Permalink
fix: resolve HasProblematicBehavior comments
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Oct 23, 2024
1 parent af494c8 commit 2cd6c4a
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions library/Icingadb/Model/Behavior/HasProblematicParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,43 +35,51 @@ public function rewriteColumn($column, ?string $relation = null): ?AliasedExpres
return null;
}

$path = 'from.';
$subQueryRelation = $relation !== null ? $relation . $path : $path;
$subQuery = $this->query->createSubQuery(new DependencyEdge(), $subQueryRelation, null, false)
$subQueryModel = new DependencyEdge();
$subQuery = (new Query())
->setDb($this->query->getDb())
->setModel($subQueryModel)
->columns([new Expression('1')])
->limit(1)
->columns([new Expression('1')]);

$subQueryAlias = $subQuery->getResolver()->getAlias($subQuery->getModel());
->with([
'from',
'to',
'dependency'
])->filter(Filter::any(
Filter::equal('to.redundancy_group.state.failed', 'y'),
Filter::equal('dependency.state.failed', 'y')
));

$resolver = $this->query->getResolver();
$sourceKey = $resolver->qualifyColumn(
'id',
$relation !== null
? str_replace('.', '_', $relation)
: $resolver->getAlias($this->query->getModel())
);

$subQueryResolver = $subQuery->getResolver();
$subQueryTarget = $subQueryResolver->resolveRelation($subQueryModel->getTableName() . '.from')->getTarget();
$targetForeignKey = $subQueryResolver->qualifyColumn(
'service_id',
$subQueryResolver->getAlias($subQueryTarget)
);

$subQuery->getSelectBase()
->join(
['to_dependency_node' => 'dependency_node'],
["to_dependency_node.id = $subQueryAlias.to_node_id"]
)->joinLeft(
['root_dependency' => 'dependency'],
[ "$subQueryAlias.dependency_id = root_dependency.id"]
)->joinLeft(
['root_dependency_state' => 'dependency_state'],
['root_dependency.id = root_dependency_state.dependency_id']
)->joinLeft(
['root_group' => 'redundancy_group'],
['root_group.id = to_dependency_node.redundancy_group_id']
)->joinLeft(
['root_group_state' => 'redundancy_group_state'],
['root_group_state.redundancy_group_id = root_group.id']
)->where(
new Expression("root_dependency_state.failed = 'y' OR root_group_state.failed = 'y'")
)->where($subQueryAlias . '_from.service_id = service.id');
->where("$targetForeignKey = $sourceKey");

$column = $relation !== null ? str_replace('.', '_', $relation) . "_$column" : $column;

$alias = $this->query->getDb()->quoteIdentifier([$column]);

list($select, $values) = $this->query->getDb()
[$select, $values] = $this->query->getDb()
->getQueryBuilder()
->assembleSelect($subQuery->assembleSelect());

return new AliasedExpression($alias, "($select)", null, ...$values);
return new AliasedExpression(
$this->query->getDb()->quoteIdentifier([$column]),
"($select)",
null,
...$values
);
}

public function isSelectableColumn(string $name): bool
Expand Down

0 comments on commit 2cd6c4a

Please sign in to comment.