[icinga-checkins] icinga.org: icingaweb2-module-director/master: suggestions: first test implementation to play with

git at icinga.org git at icinga.org
Fri Nov 18 09:39:52 CET 2016


Module: icingaweb2-module-director
Branch: master
Commit: 040a6584772b60c7e906180cdb0bc1549dc94025
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=040a6584772b60c7e906180cdb0bc1549dc94025

Author: Thomas Gelf <thomas at gelf.net>
Date:   Thu Nov 17 20:36:59 2016 +0100

suggestions: first test implementation to play with

---

 application/controllers/SuggestController.php |  122 +++++++++++++++++++++++++
 application/views/scripts/suggest/index.phtml |    3 +
 2 files changed, 125 insertions(+)

diff --git a/application/controllers/SuggestController.php b/application/controllers/SuggestController.php
new file mode 100644
index 0000000..cf264d0
--- /dev/null
+++ b/application/controllers/SuggestController.php
@@ -0,0 +1,122 @@
+<?php
+
+namespace Icinga\Module\Director\Controllers;
+
+use Icinga\Module\Director\Objects\IcingaHost;
+use Icinga\Module\Director\Objects\IcingaService;
+use Icinga\Module\Director\Web\Controller\ActionController;
+use Icinga\Data\Filter\Filter;
+
+class SuggestController extends ActionController
+{
+    /*
+    // TODO: Allow any once applying restrictions here
+    protected function checkDirectorPermissions()
+    {
+    }
+    */
+
+    public function indexAction()
+    {
+        // TODO: Using some temporarily hardcoded methods, should use DataViews later on
+        $context = $this->getRequest()->getPost('context');
+        $func = 'suggest' . ucfirst($context);
+        if (method_exists($this, $func)) {
+            $all = $this->$func();
+        } else {
+            $all = array();
+        }
+
+        $search = $this->getRequest()->getPost('value');
+        $begins = array();
+        $matches = array();
+        $begin = Filter::expression('value', '=', $search . '*');
+        $middle = Filter::expression('value', '=', '*' . $search . '*');
+        $prefixes = array();
+        foreach ($all as $str) {
+            if (false !== ($pos = strrpos($str, '.'))) {
+                $prefix = substr($str, 0, $pos) . '.';
+                $prefixes[$prefix] = $prefix;
+            }
+            if (strlen($search)) {
+                $row = (object) array('value' => $str);
+                if ($begin->matches($row)) {
+                    $begins[] = $this->highlight($str, $search);
+                } elseif ($middle->matches($row)) {
+                    $matches[] = $this->highlight($str, $search);
+                }
+            }
+        }
+
+        $containing = array_slice(array_merge($begins, $matches), 0, 30);
+        $suggestions = $containing;
+
+        if ($func === 'suggestHostFilterColumns' || $func === 'suggestHostaddresses') {
+            ksort($prefixes);
+
+            if (count($suggestions) < 5) {
+                $suggestions = array_merge($suggestions, array_keys($prefixes));
+            }
+        }
+        $this->view->suggestions = $suggestions;
+    }
+
+    protected function suggestHostnames()
+    {
+        $db = $this->db()->getDbAdapter();
+        $query = $db->select()->from('icinga_host', 'object_name')->order('object_name');
+        return $db->fetchCol($query);
+    }
+
+    protected function suggestCheckcommandnames()
+    {
+        $db = $this->db()->getDbAdapter();
+        $query = $db->select()->from('icinga_command', 'object_name')->order('object_name');
+        return $db->fetchCol($query);
+    }
+
+    protected function suggestHostgroupnames()
+    {
+        $db = $this->db()->getDbAdapter();
+        $query = $db->select()->from('icinga_hostgroup', 'object_name')->order('object_name');
+        return $db->fetchCol($query);
+    }
+
+    protected function suggestHostaddresses()
+    {
+        $db = $this->db()->getDbAdapter();
+        $query = $db->select()->from('icinga_host', 'address')->order('address');
+        return $db->fetchCol($query);
+    }
+
+    protected function suggestHostFilterColumns()
+    {
+        $all = IcingaHost::enumProperties($this->db(), 'host.');
+        $all = array_merge(
+            array_keys($all[$this->translate('Host properties')]),
+            array_keys($all[$this->translate('Custom variables')])
+        );
+        natsort($all);
+        return $all;
+    }
+
+    protected function suggestServiceFilterColumns()
+    {
+        $all = IcingaService::enumProperties($this->db(), 'service.');
+        $all = array_merge(
+            array_keys($all[$this->translate('Service properties')]),
+            array_keys($all[$this->translate('Host properties')]),
+            array_keys($all[$this->translate('Host Custom variables')]),
+            array_keys($all[$this->translate('Custom variables')])
+        );
+        // natsort($all);
+        return $all;
+    }
+
+    protected function highlight($val, $search)
+    {
+        $search = $this->view->escape($search);
+        $val = $this->view->escape($val);
+        return str_replace($search, '<strong>' . $search . '</strong>', $val);
+    }
+}
diff --git a/application/views/scripts/suggest/index.phtml b/application/views/scripts/suggest/index.phtml
new file mode 100644
index 0000000..5f804e1
--- /dev/null
+++ b/application/views/scripts/suggest/index.phtml
@@ -0,0 +1,3 @@
+<?php foreach ($suggestions as $suggest): ?>
+<li><?= $suggest ?></li>
+<?php endforeach ?>



More information about the icinga-checkins mailing list