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..6a798d47 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 @@ -22,7 +23,7 @@ use ipl\Sql\Select; * @property int $nodes_warning_handled * @property int $nodes_warning_unhandled */ -class RedundancyGroupSummary extends RedundancyGroup +class RedundancyGroupSummary extends DependencyNode { public function getSummaryColumns(): array { @@ -35,9 +36,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 +48,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 +64,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 +80,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 +91,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 +103,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 +115,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 +127,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 +140,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 +152,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 +170,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 []; + } }