From f5a9016dea6206e0ec1a8adddfbbfae4f5db973d Mon Sep 17 00:00:00 2001
From: Eric Lippmann <eric.lippmann@icinga.com>
Date: Thu, 27 Jul 2017 11:38:00 +0200
Subject: [PATCH] Optimize queries for creating the host and service problem
 badges in the menu

We now use two fast count queries w/ an appropriate filter instead of fetching a whole bunch of unnecessary host and service counters.
---
 modules/monitoring/configuration.php          |  4 +-
 .../Ido/Query/UnhandledhostproblemsQuery.php  | 48 +++++++++++++++++++
 .../Query/UnhandledserviceproblemsQuery.php   | 48 +++++++++++++++++++
 .../DataView/Unhandledhostproblems.php        | 28 +++++++++++
 .../DataView/Unhandledserviceproblems.php     | 28 +++++++++++
 5 files changed, 154 insertions(+), 2 deletions(-)
 create mode 100644 modules/monitoring/library/Monitoring/Backend/Ido/Query/UnhandledhostproblemsQuery.php
 create mode 100644 modules/monitoring/library/Monitoring/Backend/Ido/Query/UnhandledserviceproblemsQuery.php
 create mode 100644 modules/monitoring/library/Monitoring/DataView/Unhandledhostproblems.php
 create mode 100644 modules/monitoring/library/Monitoring/DataView/Unhandledserviceproblems.php

diff --git a/modules/monitoring/configuration.php b/modules/monitoring/configuration.php
index 1b3433992..a096e9e9d 100644
--- a/modules/monitoring/configuration.php
+++ b/modules/monitoring/configuration.php
@@ -136,7 +136,7 @@ $section->add(N_('Host Problems'), array(
             'hosts_down_unhandled' => $this->translate('%d unhandled hosts down')
         ),
         'state'    => 'critical',
-        'dataView' => 'statussummary'
+        'dataView' => 'unhandledhostproblems'
     ),
     'url'       => 'monitoring/list/hosts?host_problem=1&sort=host_severity',
     'priority'  => 50
@@ -148,7 +148,7 @@ $section->add(N_('Service Problems'), array(
             'services_critical_unhandled' => $this->translate('%d unhandled services critical')
         ),
         'state'    => 'critical',
-        'dataView' => 'statussummary'
+        'dataView' => 'unhandledserviceproblems'
     ),
     'url'       => 'monitoring/list/services?service_problem=1&sort=service_severity&dir=desc',
     'priority'  => 60
diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/UnhandledhostproblemsQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/UnhandledhostproblemsQuery.php
new file mode 100644
index 000000000..f4c4e07e8
--- /dev/null
+++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/UnhandledhostproblemsQuery.php
@@ -0,0 +1,48 @@
+<?php
+/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Monitoring\Backend\Ido\Query;
+
+use Icinga\Data\Filter\Filter;
+
+/**
+ * Query for unhandled host problems
+ */
+class UnhandledhostproblemsQuery extends IdoQuery
+{
+    protected $allowCustomVars = true;
+
+    protected $columnMap = array(
+        'problems' => array(
+            'hosts_down_unhandled' => 'COUNT(*)',
+        )
+    );
+
+    /**
+     * The service status sub select
+     *
+     * @var HoststatusQuery
+     */
+    protected $subSelect;
+
+    public function addFilter(Filter $filter)
+    {
+        $this->subSelect->applyFilter(clone $filter);
+        return $this;
+    }
+
+    protected function joinBaseTables()
+    {
+        $this->subSelect = $this->createSubQuery(
+            'Hoststatus',
+            array('host_name')
+        );
+        $this->subSelect->where('host_handled', 0);
+        $this->subSelect->where('host_state', 1);
+        $this->select->from(
+            array('problems' => $this->subSelect->setIsSubQuery(true)),
+            array()
+        );
+        $this->joinedVirtualTables['problems'] = true;
+    }
+}
diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/UnhandledserviceproblemsQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/UnhandledserviceproblemsQuery.php
new file mode 100644
index 000000000..a218caf83
--- /dev/null
+++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/UnhandledserviceproblemsQuery.php
@@ -0,0 +1,48 @@
+<?php
+/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Monitoring\Backend\Ido\Query;
+
+use Icinga\Data\Filter\Filter;
+
+/**
+ * Query for unhandled service problems
+ */
+class UnhandledserviceproblemsQuery extends IdoQuery
+{
+    protected $allowCustomVars = true;
+
+    protected $columnMap = array(
+        'problems' => array(
+            'services_critical_unhandled' => 'COUNT(*)',
+        )
+    );
+
+    /**
+     * The service status sub select
+     *
+     * @var ServicestatusQuery
+     */
+    protected $subSelect;
+
+    public function addFilter(Filter $filter)
+    {
+        $this->subSelect->applyFilter(clone $filter);
+        return $this;
+    }
+
+    protected function joinBaseTables()
+    {
+        $this->subSelect = $this->createSubQuery(
+            'Servicestatus',
+            array('service_description')
+        );
+        $this->subSelect->where('service_handled', 0);
+        $this->subSelect->where('service_state', 2);
+        $this->select->from(
+            array('problems' => $this->subSelect->setIsSubQuery(true)),
+            array()
+        );
+        $this->joinedVirtualTables['problems'] = true;
+    }
+}
diff --git a/modules/monitoring/library/Monitoring/DataView/Unhandledhostproblems.php b/modules/monitoring/library/Monitoring/DataView/Unhandledhostproblems.php
new file mode 100644
index 000000000..4f5f392e3
--- /dev/null
+++ b/modules/monitoring/library/Monitoring/DataView/Unhandledhostproblems.php
@@ -0,0 +1,28 @@
+<?php
+/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Monitoring\DataView;
+
+/**
+ * Data view for unhandled host problems
+ */
+class Unhandledhostproblems extends DataView
+{
+    public function getColumns()
+    {
+        return array(
+            'hosts_down_unhandled'
+        );
+    }
+
+    public function getStaticFilterColumns()
+    {
+        return array(
+            'instance_name',
+            'host', 'host_alias', 'host_display_name', 'host_name',
+            'hostgroup', 'hostgroup_alias', 'hostgroup_name',
+            'service', 'service_description', 'service_display_name',
+            'servicegroup', 'servicegroup_alias', 'servicegroup_name'
+        );
+    }
+}
diff --git a/modules/monitoring/library/Monitoring/DataView/Unhandledserviceproblems.php b/modules/monitoring/library/Monitoring/DataView/Unhandledserviceproblems.php
new file mode 100644
index 000000000..3af4502db
--- /dev/null
+++ b/modules/monitoring/library/Monitoring/DataView/Unhandledserviceproblems.php
@@ -0,0 +1,28 @@
+<?php
+/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Monitoring\DataView;
+
+/**
+ * Data view for unhandled service problems
+ */
+class Unhandledserviceproblems extends DataView
+{
+    public function getColumns()
+    {
+        return array(
+            'services_critical_unhandled'
+        );
+    }
+
+    public function getStaticFilterColumns()
+    {
+        return array(
+            'instance_name',
+            'host', 'host_alias', 'host_display_name', 'host_name',
+            'hostgroup', 'hostgroup_alias', 'hostgroup_name',
+            'service', 'service_description', 'service_display_name',
+            'servicegroup', 'servicegroup_alias', 'servicegroup_name'
+        );
+    }
+}