diff --git a/application/controllers/RedundancygroupController.php b/application/controllers/RedundancygroupController.php index 50a91b8a..e81ff874 100644 --- a/application/controllers/RedundancygroupController.php +++ b/application/controllers/RedundancygroupController.php @@ -53,13 +53,13 @@ class RedundancygroupController extends Controller */ protected function loadGroup(): void { - $query = RedundancyGroup::on($this->getDb()) - ->with(['state']) - ->filter(Filter::equal('id', $this->groupId)); + $query = DependencyNode::on($this->getDb()) + ->with(['redundancy_group', 'redundancy_group.state']) + ->filter(Filter::equal('redundancy_group_id', $this->groupId)); $this->applyRestrictions($query); - $this->group = $query->first(); + $this->group = $query->first()?->redundancy_group; if ($this->group === null) { $this->httpNotFound($this->translate('Redundancy Group not found')); diff --git a/library/Icingadb/Model/RedundancyGroup.php b/library/Icingadb/Model/RedundancyGroup.php index 885e288f..b7110696 100644 --- a/library/Icingadb/Model/RedundancyGroup.php +++ b/library/Icingadb/Model/RedundancyGroup.php @@ -13,7 +13,6 @@ use ipl\Orm\Defaults; use ipl\Orm\Model; use ipl\Orm\Query; use ipl\Orm\Relations; -use ipl\Stdlib\Filter; /** * Redundancy group model. @@ -86,11 +85,10 @@ class RedundancyGroup extends Model ->through(DependencyNode::class); } - public function createDefaults(Defaults $defaults) + public function createDefaults(Defaults $defaults): void { $defaults->add('summary', function (RedundancyGroup $group) { - $summary = RedundancyGroupSummary::on(Backend::getDb()) - ->filter(Filter::equal('id', $group->id)); + $summary = RedundancyGroupSummary::for($group->id, Backend::getDb()); $this->applyRestrictions($summary); diff --git a/library/Icingadb/Model/RedundancyGroupSummary.php b/library/Icingadb/Model/RedundancyGroupSummary.php index 71a790d1..9de6a2b6 100644 --- a/library/Icingadb/Model/RedundancyGroupSummary.php +++ b/library/Icingadb/Model/RedundancyGroupSummary.php @@ -4,10 +4,11 @@ namespace Icinga\Module\Icingadb\Model; +use InvalidArgumentException; use ipl\Orm\Query; use ipl\Sql\Connection; use ipl\Sql\Expression; -use ipl\Sql\Select; +use ipl\Stdlib\Filter; /** * Redundancy group's summary @@ -21,8 +22,10 @@ use ipl\Sql\Select; * @property int $nodes_unknown_unhandled * @property int $nodes_warning_handled * @property int $nodes_warning_unhandled + * @property int $nodes_acknowledged + * @property int $nodes_problems_unacknowledged */ -class RedundancyGroupSummary extends RedundancyGroup +class RedundancyGroupSummary extends DependencyNode { public function getSummaryColumns(): array { @@ -35,9 +38,9 @@ class RedundancyGroupSummary extends RedundancyGroup . ' ELSE 0' . ' END)', [ - 'from.to.service_id', - 'from.to.service.state.soft_state', - 'from.to.host.state.soft_state', + 'service_id', + 'service.state.soft_state', + 'host.state.soft_state', ] ), 'nodes_problem_handled' => new Expression( @@ -47,13 +50,13 @@ class RedundancyGroupSummary extends RedundancyGroup . ' ELSE 0' . ' END)', [ - 'from.to.service_id', - 'from.to.service.state.soft_state', - 'from.to.service.state.is_handled', - 'from.to.service.state.is_reachable', - 'from.to.host.state.soft_state', - 'from.to.host.state.is_handled', - 'from.to.host.state.is_reachable', + 'service_id', + 'service.state.soft_state', + 'service.state.is_handled', + 'service.state.is_reachable', + 'host.state.soft_state', + 'host.state.is_handled', + 'host.state.is_reachable', ] ), 'nodes_problem_unhandled' => new Expression( @@ -63,13 +66,13 @@ class RedundancyGroupSummary extends RedundancyGroup . ' ELSE 0' . ' END)', [ - 'from.to.service_id', - 'from.to.service.state.soft_state', - 'from.to.service.state.is_handled', - 'from.to.service.state.is_reachable', - 'from.to.host.state.soft_state', - 'from.to.host.state.is_handled', - 'from.to.host.state.is_reachable', + 'service_id', + 'service.state.soft_state', + 'service.state.is_handled', + 'service.state.is_reachable', + 'host.state.soft_state', + 'host.state.is_handled', + 'host.state.is_reachable', ] ), 'nodes_pending' => new Expression( @@ -79,9 +82,9 @@ class RedundancyGroupSummary extends RedundancyGroup . ' ELSE 0' . ' END)', [ - 'from.to.service_id', - 'from.to.service.state.soft_state', - 'from.to.host.state.soft_state', + 'service_id', + 'service.state.soft_state', + 'host.state.soft_state', ] ), 'nodes_unknown_handled' => new Expression( @@ -90,10 +93,10 @@ class RedundancyGroupSummary extends RedundancyGroup . ' ELSE 0' . ' END)', [ - 'from.to.service_id', - 'from.to.service.state.soft_state', - 'from.to.service.state.is_handled', - 'from.to.service.state.is_reachable' + 'service_id', + 'service.state.soft_state', + 'service.state.is_handled', + 'service.state.is_reachable' ] ), 'nodes_unknown_unhandled' => new Expression( @@ -102,10 +105,10 @@ class RedundancyGroupSummary extends RedundancyGroup . ' ELSE 0' . ' END)', [ - 'from.to.service_id', - 'from.to.service.state.soft_state', - 'from.to.service.state.is_handled', - 'from.to.service.state.is_reachable' + 'service_id', + 'service.state.soft_state', + 'service.state.is_handled', + 'service.state.is_reachable' ] ), 'nodes_warning_handled' => new Expression( @@ -114,10 +117,10 @@ class RedundancyGroupSummary extends RedundancyGroup . ' ELSE 0' . ' END)', [ - 'from.to.service_id', - 'from.to.service.state.soft_state', - 'from.to.service.state.is_handled', - 'from.to.service.state.is_reachable' + 'service_id', + 'service.state.soft_state', + 'service.state.is_handled', + 'service.state.is_reachable' ] ), 'nodes_warning_unhandled' => new Expression( @@ -126,10 +129,10 @@ class RedundancyGroupSummary extends RedundancyGroup . ' ELSE 0' . ' END)', [ - 'from.to.service_id', - 'from.to.service.state.soft_state', - 'from.to.service.state.is_handled', - 'from.to.service.state.is_reachable' + 'service_id', + 'service.state.soft_state', + 'service.state.is_handled', + 'service.state.is_reachable' ] ), 'nodes_acknowledged' => new Expression( @@ -139,9 +142,9 @@ class RedundancyGroupSummary extends RedundancyGroup . ' ELSE 0' . ' END)', [ - 'from.to.service_id', - 'from.to.service.state.is_acknowledged', - 'from.to.host.state.is_acknowledged', + 'service_id', + 'service.state.is_acknowledged', + 'host.state.is_acknowledged', ] ), 'nodes_problems_unacknowledged' => new Expression( @@ -151,11 +154,11 @@ class RedundancyGroupSummary extends RedundancyGroup . ' ELSE 0' . ' END)', [ - 'from.to.service_id', - 'from.to.service.state.is_problem', - 'from.to.service.state.is_acknowledged', - 'from.to.host.state.is_problem', - 'from.to.host.state.is_acknowledged', + 'service_id', + 'service.state.is_problem', + 'service.state.is_acknowledged', + 'host.state.is_problem', + 'host.state.is_acknowledged', ] ) ]; @@ -169,17 +172,30 @@ class RedundancyGroupSummary extends RedundancyGroup $m = $q->getModel(); $q->columns($m->getSummaryColumns()); - $q->on($q::ON_SELECT_ASSEMBLED, function (Select $select) use ($q) { - $model = $q->getModel(); - $groupBy = $q->getResolver()->qualifyColumnsAndAliases((array) $model->getKeyName(), $model, false); - $select->groupBy($groupBy); - }); - return $q; } + /** + * Get the summary query for the given redundancy group id + * + * @param string $groupId The redundancy group id for summary + * @param Connection $db Db connection to use + * + * @return Query + */ + public static function for(string $groupId, Connection $db): Query + { + return self::on($db) + ->filter(Filter::equal('child.redundancy_group.id', $groupId)); + } + public function getColumns(): array { return array_merge(parent::getColumns(), $this->getSummaryColumns()); } + + public function getDefaultSort(): array + { + return []; + } }