Skip to content

Commit

Permalink
State summaries: Only select summary columns by default
Browse files Browse the repository at this point in the history
Previously, all columns, including the parent model columns,
were SELECTed, which would require adding all non-aggregated
columns to the GROUP BY clause when using PostgreSQL.
Now only the aggregated columns are SELECTed by default.
  • Loading branch information
lippserd committed Apr 13, 2022
1 parent ce10eb6 commit 4b80e06
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 115 deletions.
123 changes: 68 additions & 55 deletions library/Icingadb/Model/HoststateSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,80 @@

namespace Icinga\Module\Icingadb\Model;

use ipl\Sql\Connection;
use ipl\Sql\Expression;

class HoststateSummary extends Host
{
public function getSummaryColumns()
{
return [
'hosts_acknowledged' => new Expression(
'SUM(CASE WHEN host_state.is_acknowledged = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_active_checks_enabled' => new Expression(
'SUM(CASE WHEN host.active_checks_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_passive_checks_enabled' => new Expression(
'SUM(CASE WHEN host.passive_checks_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_down_handled' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 1'
. ' AND host_state.is_handled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_down_unhandled' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 1'
. ' AND host_state.is_handled = \'n\' THEN 1 ELSE 0 END)'
),
'hosts_event_handler_enabled' => new Expression(
'SUM(CASE WHEN host.event_handler_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_flapping_enabled' => new Expression(
'SUM(CASE WHEN host.flapping_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_notifications_enabled' => new Expression(
'SUM(CASE WHEN host.notifications_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_pending' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 99 THEN 1 ELSE 0 END)'
),
'hosts_problems_unacknowledged' => new Expression(
'SUM(CASE WHEN host_state.is_problem = \'y\''
. ' AND host_state.is_acknowledged = \'n\' THEN 1 ELSE 0 END)'
),
'hosts_total' => new Expression(
'SUM(CASE WHEN host.id IS NOT NULL THEN 1 ELSE 0 END)'
),
'hosts_unreachable' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 2 THEN 1 ELSE 0 END)'
),
'hosts_unreachable_handled' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 2'
. ' AND host_state.is_handled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_unreachable_unhandled' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 2'
. ' AND host_state.is_handled = \'n\' THEN 1 ELSE 0 END)'
),
'hosts_up' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 0 THEN 1 ELSE 0 END)'
)
];
}

public static function on(Connection $db)
{
$q = parent::on($db);
/** @var static $m */
$m = $q->getModel();
$q->columns($m->getSummaryColumns());

return $q;
}

public function getColumns()
{
return array_merge(
parent::getColumns(),
[
'hosts_acknowledged' => new Expression(
'SUM(CASE WHEN host_state.is_acknowledged = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_active_checks_enabled' => new Expression(
'SUM(CASE WHEN host.active_checks_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_passive_checks_enabled' => new Expression(
'SUM(CASE WHEN host.passive_checks_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_down_handled' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 1'
. ' AND host_state.is_handled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_down_unhandled' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 1'
. ' AND host_state.is_handled = \'n\' THEN 1 ELSE 0 END)'
),
'hosts_event_handler_enabled' => new Expression(
'SUM(CASE WHEN host.event_handler_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_flapping_enabled' => new Expression(
'SUM(CASE WHEN host.flapping_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_notifications_enabled' => new Expression(
'SUM(CASE WHEN host.notifications_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_pending' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 99 THEN 1 ELSE 0 END)'
),
'hosts_problems_unacknowledged' => new Expression(
'SUM(CASE WHEN host_state.is_problem = \'y\''
. ' AND host_state.is_acknowledged = \'n\' THEN 1 ELSE 0 END)'
),
'hosts_total' => new Expression(
'SUM(CASE WHEN host.id IS NOT NULL THEN 1 ELSE 0 END)'
),
'hosts_unreachable' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 2 THEN 1 ELSE 0 END)'
),
'hosts_unreachable_handled' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 2'
. ' AND host_state.is_handled = \'y\' THEN 1 ELSE 0 END)'
),
'hosts_unreachable_unhandled' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 2'
. ' AND host_state.is_handled = \'n\' THEN 1 ELSE 0 END)'
),
'hosts_up' => new Expression(
'SUM(CASE WHEN host_state.soft_state = 0 THEN 1 ELSE 0 END)'
)
]
);
return array_merge(parent::getColumns(), $this->getSummaryColumns());
}

public function getDefaultSort()
Expand Down
133 changes: 73 additions & 60 deletions library/Icingadb/Model/ServicestateSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,85 @@

namespace Icinga\Module\Icingadb\Model;

use ipl\Sql\Connection;
use ipl\Sql\Expression;

class ServicestateSummary extends Service
{
public function getSummaryColumns()
{
return [
'services_acknowledged' => new Expression(
'SUM(CASE WHEN service_state.is_acknowledged = \'y\' THEN 1 ELSE 0 END)'
),
'services_active_checks_enabled' => new Expression(
'SUM(CASE WHEN service.active_checks_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'services_passive_checks_enabled' => new Expression(
'SUM(CASE WHEN service.passive_checks_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'services_critical_handled' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 2'
. ' AND service_state.is_handled = \'y\' THEN 1 ELSE 0 END)'
),
'services_critical_unhandled' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 2'
. ' AND service_state.is_handled = \'n\' THEN 1 ELSE 0 END)'
),
'services_event_handler_enabled' => new Expression(
'SUM(CASE WHEN service.event_handler_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'services_flapping_enabled' => new Expression(
'SUM(CASE WHEN service.flapping_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'services_notifications_enabled' => new Expression(
'SUM(CASE WHEN service.notifications_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'services_ok' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 0 THEN 1 ELSE 0 END)'
),
'services_pending' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 99 THEN 1 ELSE 0 END)'
),
'services_problems_unacknowledged' => new Expression(
'SUM(CASE WHEN service_state.is_problem = \'y\''
. ' AND service_state.is_acknowledged = \'n\' THEN 1 ELSE 0 END)'
),
'services_total' => new Expression(
'SUM(CASE WHEN service.id IS NOT NULL THEN 1 ELSE 0 END)'
),
'services_unknown_handled' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 3'
. ' AND service_state.is_handled = \'y\' THEN 1 ELSE 0 END)'
),
'services_unknown_unhandled' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 3'
. ' AND service_state.is_handled = \'n\' THEN 1 ELSE 0 END)'
),
'services_warning_handled' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 1'
. ' AND service_state.is_handled = \'y\' THEN 1 ELSE 0 END)'
),
'services_warning_unhandled' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 1'
. ' AND service_state.is_handled = \'n\' THEN 1 ELSE 0 END)'
)
];
}

public static function on(Connection $db)
{
$q = parent::on($db);
/** @var static $m */
$m = $q->getModel();
$q->columns($m->getSummaryColumns());

return $q;
}

public function getColumns()
{
return array_merge(
parent::getColumns(),
[
'services_acknowledged' => new Expression(
'SUM(CASE WHEN service_state.is_acknowledged = \'y\' THEN 1 ELSE 0 END)'
),
'services_active_checks_enabled' => new Expression(
'SUM(CASE WHEN service.active_checks_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'services_passive_checks_enabled' => new Expression(
'SUM(CASE WHEN service.passive_checks_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'services_critical_handled' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 2'
. ' AND service_state.is_handled = \'y\' THEN 1 ELSE 0 END)'
),
'services_critical_unhandled' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 2'
. ' AND service_state.is_handled = \'n\' THEN 1 ELSE 0 END)'
),
'services_event_handler_enabled' => new Expression(
'SUM(CASE WHEN service.event_handler_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'services_flapping_enabled' => new Expression(
'SUM(CASE WHEN service.flapping_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'services_notifications_enabled' => new Expression(
'SUM(CASE WHEN service.notifications_enabled = \'y\' THEN 1 ELSE 0 END)'
),
'services_ok' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 0 THEN 1 ELSE 0 END)'
),
'services_pending' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 99 THEN 1 ELSE 0 END)'
),
'services_problems_unacknowledged' => new Expression(
'SUM(CASE WHEN service_state.is_problem = \'y\''
. ' AND service_state.is_acknowledged = \'n\' THEN 1 ELSE 0 END)'
),
'services_total' => new Expression(
'SUM(CASE WHEN service.id IS NOT NULL THEN 1 ELSE 0 END)'
),
'services_unknown_handled' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 3'
. ' AND service_state.is_handled = \'y\' THEN 1 ELSE 0 END)'
),
'services_unknown_unhandled' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 3'
. ' AND service_state.is_handled = \'n\' THEN 1 ELSE 0 END)'
),
'services_warning_handled' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 1'
. ' AND service_state.is_handled = \'y\' THEN 1 ELSE 0 END)'
),
'services_warning_unhandled' => new Expression(
'SUM(CASE WHEN service_state.soft_state = 1'
. ' AND service_state.is_handled = \'n\' THEN 1 ELSE 0 END)'
)
]
);
return array_merge(parent::getColumns(), $this->getSummaryColumns());
}

public function getDefaultSort()
Expand Down

0 comments on commit 4b80e06

Please sign in to comment.