Skip to content

Commit

Permalink
RedundancyGroupParentStateSummary: Group by all non-aggregrate column…
Browse files Browse the repository at this point in the history
…s for postgres
  • Loading branch information
raviks789 committed Oct 8, 2024
1 parent 3914d94 commit 4033b39
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions library/Icingadb/Model/RedundancyGroupParentStateSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Icinga\Module\Icingadb\Model;

use ipl\Orm\Query;
use ipl\Sql\Adapter\Pgsql;
use ipl\Sql\Connection;
use ipl\Sql\Expression;
use ipl\Sql\Select;

/**
* Redundancy group's parent nodes summary
Expand All @@ -31,7 +33,7 @@ public function getSummaryColumns(): array
'SUM(CASE WHEN redundancy_group_from_to_host_state.soft_state = 1'
. ' AND (redundancy_group_from_to_host_state.is_handled = \'y\''
. ' OR redundancy_group_from_to_host_state.is_reachable = \'n\') THEN 1 ELSE 0 END'
. '+ CASE WHEN redundancy_group_from_to_service_state.soft_state = 2'
. ' + CASE WHEN redundancy_group_from_to_service_state.soft_state = 2'
. ' AND (redundancy_group_from_to_service_state.is_handled = \'y\''
. ' OR redundancy_group_from_to_service_state.is_reachable = \'n\') THEN 1 ELSE 0 END)'
),
Expand All @@ -45,12 +47,12 @@ public function getSummaryColumns(): array
),
'objects_pending' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_host_state.soft_state = 99 THEN 1 ELSE 0 END'
. '+ CASE WHEN redundancy_group_from_to_service_state.soft_state = 99 THEN 1 ELSE 0 END)'
. ' + CASE WHEN redundancy_group_from_to_service_state.soft_state = 99 THEN 1 ELSE 0 END)'
),
'objects_problems_unacknowledged' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_host_state.is_problem = \'y\''
. ' AND redundancy_group_from_to_host_state.is_acknowledged = \'n\' THEN 1 ELSE 0 END'
. '+ CASE WHEN redundancy_group_from_to_service_state.is_problem = \'y\''
. ' + CASE WHEN redundancy_group_from_to_service_state.is_problem = \'y\''
. ' AND redundancy_group_from_to_service_state.is_acknowledged = \'n\' THEN 1 ELSE 0 END)'
),
'objects_total' => new Expression(
Expand All @@ -59,7 +61,7 @@ public function getSummaryColumns(): array
),
'objects_ok' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_host_state.soft_state = 0 THEN 1 ELSE 0 END'
. '+ CASE WHEN redundancy_group_from_to_service_state.soft_state = 0 THEN 1 ELSE 0 END)'
. ' + CASE WHEN redundancy_group_from_to_service_state.soft_state = 0 THEN 1 ELSE 0 END)'
),
'objects_unknown_handled' => new Expression(
'SUM(CASE WHEN redundancy_group_from_to_service_state.soft_state = 3'
Expand Down Expand Up @@ -98,6 +100,26 @@ public static function on(Connection $db): Query
$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);

// For PostgreSQL, ALL non-aggregate SELECT columns must appear in the GROUP BY clause:
if ($q->getDb()->getAdapter() instanceof Pgsql) {
/**
* Ignore Expressions, i.e. aggregate functions {@see getColumns()},
* which do not need to be added to the GROUP BY.
*/
$candidates = array_filter($select->getColumns(), 'is_string');
// Remove already considered columns for the GROUP BY, i.e. the primary key.
$candidates = array_diff_assoc($candidates, $groupBy);
$groupBy = array_merge($groupBy, $candidates);
}

$select->groupBy($groupBy);
});

return $q;
}

Expand Down

0 comments on commit 4033b39

Please sign in to comment.