[icinga-checkins] icinga.org: icingaweb2-module-logstash/master: Use proper Search interfaces and only fetch field list

git at icinga.org git at icinga.org
Fri Feb 5 14:05:47 CET 2016


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

Author: Markus Frosch <markus at lazyfrosch.de>
Date:   Fri Jun 19 12:28:17 2015 +0200

Use proper Search interfaces and only fetch field list

---

 application/controllers/EventController.php  |   12 +++--
 application/views/scripts/event/search.phtml |   17 +++----
 library/Logstash/Search.php                  |   65 +++++++++++++++++++++++---
 3 files changed, 76 insertions(+), 18 deletions(-)

diff --git a/application/controllers/EventController.php b/application/controllers/EventController.php
index 4231fe0..3ffca1c 100644
--- a/application/controllers/EventController.php
+++ b/application/controllers/EventController.php
@@ -32,16 +32,20 @@ class Logstash_EventController extends Controller
         $this->view->query = $this->_getParam('query');
         $this->view->filter = $this->_getParam('filter');
 
+        $fields = null;
         $this->view->fields = $this->_getParam('fields');
         $this->view->fieldlist = array();
         if ($this->view->fields) {
-            $this->view->fieldlist = preg_split('/\s*[,]\s*/', $this->view->fields);
+            $this->view->fieldlist = $fields = preg_split('/\s*[,]\s*/', $this->view->fields);
         }
 
         $search = new Search($this->elasticsearch_url."/".$this->index_pattern);
         if ($this->view->query) {
             $search->setQueryString($this->view->query);
 
+            if (isset($fields))
+                $search->setFields($fields);
+
             if ($this->view->filter)
                 $search->setFilterQueryString($this->view->filter);
 
@@ -55,9 +59,9 @@ class Logstash_EventController extends Controller
             $this->view->paginator = new Paginator();
             $this->view->paginator->setQuery($search);
 
-            $search->search();
-
-            $this->view->search = $search;
+            $this->view->hits = $search->fetchAll();
+            $this->view->count = $search->count();
+            $this->view->took = $search->getTook();
         }
     }
 
diff --git a/application/views/scripts/event/search.phtml b/application/views/scripts/event/search.phtml
index 141254f..9f9821a 100644
--- a/application/views/scripts/event/search.phtml
+++ b/application/views/scripts/event/search.phtml
@@ -22,14 +22,14 @@
 </div>
 <div class="content">
 
-    <?php if (!$this->search): ?>
+    <?php if ($this->count == 0): ?>
         <p>No results found!</p>
     <?php else: ?>
 
-    <p class="small">Found <?= $this->search->total ?> hits in <?= $this->search->took ?>ms</p>
+    <p class="small">Found <?= $this->count ?> hits in <?= $this->took ?>ms</p>
 
     <table class="documents" data-base-target="_next">
-    <?php if ($this->search->total > 0): ?>
+    <?php if ($this->count > 0): ?>
         <?php if (count($this->fieldlist) > 0): ?>
         <tr class="header">
             <?php foreach($this->fieldlist as $field): ?>
@@ -37,14 +37,15 @@
             <?php endforeach ?>
         </tr>
         <?php endif; ?>
-        <?php foreach ($this->search->hits as $row):
+        <?php foreach ($this->hits as $hit):
             $num = 0;
-            $href = $this->url("logstash/event/show", array('index' => $row->_index, 'type' => $row->_type, 'id' => $row->_id));
+            $href = $this->url("logstash/event/show", array('index' => $hit['_index'], 'type' => $hit['_type'], 'id' => $hit['_id']));
         ?>
-            <tr class="document" data-elastic-id="<?= $row->_id ?>"
+            <tr class="document" data-elastic-id="<?= $hit['_id'] ?>"
                 href="<?= $href ?>">
                 <?php if(count($this->fieldlist) > 0) foreach($this->fieldlist as $field): ?>
-                    <td><?php $value = (property_exists($row->_source, $field) ? $row->_source->$field : '');
+                    <td><?php $value = (array_key_exists($field, $hit) ? $hit[$field] : '');
+                        $value = $this->escape($value);
                         if ($num++ == 0): ?>
                         <a href="<?= $href ?>"><?= $value ?></a>
                         <?php else: echo $value; endif ?>
@@ -52,7 +53,7 @@
                 <?php endforeach ?>
 
                 <?php if (count($this->fieldlist) == 0): ?>
-                    <td class="rawjson"><pre><?= json_encode($row->_source, JSON_PRETTY_PRINT); ?></pre></td>
+                    <td class="rawjson"><pre><?= json_encode($hit, JSON_PRETTY_PRINT); ?></pre></td>
                 <?php endif ?>
             </tr>
         <?php endforeach ?>
diff --git a/library/Logstash/Search.php b/library/Logstash/Search.php
index 61e567a..9b2650f 100644
--- a/library/Logstash/Search.php
+++ b/library/Logstash/Search.php
@@ -17,18 +17,19 @@ class Search extends ElasticsearchBackend implements QueryInterface
     protected $query;
     protected $filter = array();
     protected $filter_query;
+    protected $fields = array();
 
     protected $sort_field;
     protected $sort_direction;
     protected $size;
     protected $from;
 
-    public $took;
-    public $hits;
-    public $total;
-    public $timed_out;
+    protected $took;
+    protected $hits;
+    protected $total;
+    protected $timed_out;
 
-    public function search()
+    protected function search()
     {
         if (!$this->getElasticsearch()) {
             throw new Exception("Elasticsearch URL has not be configured!");
@@ -51,6 +52,15 @@ class Search extends ElasticsearchBackend implements QueryInterface
             );
         }
 
+        if (count($this->fields) > 0) {
+            $post['fields'] = array_merge(
+                array(
+                    'test'
+                ),
+                $this->fields
+            );
+        }
+
         if ($this->size)
             $post['size'] = $this->size;
 
@@ -141,7 +151,26 @@ class Search extends ElasticsearchBackend implements QueryInterface
      */
     public function fetchAll()
     {
-        // TODO: Implement fetchAll() method.
+        $this->search();
+        $hits = array();
+        foreach ($this->hits as $hit) {
+            $h = array(
+                '_index' => $hit->_index,
+                '_type' => $hit->_type,
+                '_id' => $hit->_id,
+            );
+            if (property_exists($hit, 'fields'))
+                foreach ($hit->fields as $key => $value) {
+                    $h[$key] = $value[0];
+                }
+            else
+                foreach ($hit->_source as $key => $value) {
+                    $h[$key] = $value;
+                }
+
+            $hits[] = $h;
+        }
+        return $hits;
     }
 
     /**
@@ -318,4 +347,28 @@ class Search extends ElasticsearchBackend implements QueryInterface
     {
         // TODO: Implement addFilter() method.
     }
+
+    /**
+     * @return array
+     */
+    public function getFields()
+    {
+        return $this->fields;
+    }
+
+    /**
+     * @param array $fields
+     */
+    public function setFields($fields)
+    {
+        $this->fields = $fields;
+    }
+
+    /**
+     * @return Integer milliseconds
+     */
+    public function getTook()
+    {
+        return $this->took;
+    }
 }



More information about the icinga-checkins mailing list