RedundancyGroupSummary: Use DepenedecyNode as base to apply restrictions correctly

* RedundancyGroupSummary:
  - Columns are same as in DependencyNodeSummary. Column `redundancy_group.state.failed` excluded because not required here.
  - Don't sort by default. Sorting the summary is not necessary and even fails for PostgreSQL as it would require adding the column to the GROUP BY clause.
  - Introduce new method for(), to easily get the summary query for given group id. Adjust filter. The summary is always for parent (member).
This commit is contained in:
Sukhwinder Dhillon 2025-06-16 10:34:34 +02:00 committed by Johannes Meyer
parent 823a575616
commit eb7179ffd8
2 changed files with 68 additions and 56 deletions

View file

@ -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);

View file

@ -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 [];
}
}