[icinga-checkins] icinga.org: icingaweb2-module-logstash/master: Add Event detail view

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


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

Author: Markus Frosch <markus at lazyfrosch.de>
Date:   Fri Jun 19 11:40:07 2015 +0200

Add Event detail view

---

 application/controllers/EventController.php  |   39 ++++++---
 application/views/scripts/event/search.phtml |    9 +-
 application/views/scripts/event/show.phtml   |   49 +++++++++++
 library/Logstash/Event.php                   |  121 ++++++++++++++++++++++++++
 public/css/module.less                       |   11 +++
 5 files changed, 209 insertions(+), 20 deletions(-)

diff --git a/application/controllers/EventController.php b/application/controllers/EventController.php
index a4e124a..fdb79a7 100644
--- a/application/controllers/EventController.php
+++ b/application/controllers/EventController.php
@@ -1,30 +1,25 @@
 <?php
 
-use Icinga\Module\Logstash\Search;
 use Icinga\Module\Logstash\Controller;
+use Icinga\Module\Logstash\Search;
+use Icinga\Module\Logstash\Event;
 
 use Icinga\Web\Widget\Limiter;
 use Icinga\Web\Widget\Paginator;
 
 class Logstash_EventController extends Controller
 {
-    public function init()
-    {
-        parent::init();
-        $tabs = $this->getTabs();
-
-        $tabs->add('Events', array(
-            'title' => $this->translate('Events'),
-            'url'   => 'logstash/event/search'
-        ));
-    }
-
     public function indexAction() {
         $this->redirectNow('logstash/event/search');
     }
 
     public function searchAction()
     {
+        $this->getTabs()->add('search', array(
+            'title' => $this->translate('Events'),
+            'url'   => $this->view->url()
+        ))->activate(('search'));;
+
         $this->view->compact = $this->_getParam('view') === 'compact';
 
         $this->view->live = $this->params->shift('live');
@@ -67,4 +62,24 @@ class Logstash_EventController extends Controller
             $this->view->search = $search;
         }
     }
+
+    public function showAction() {
+
+        $index = $this->_getParam('index');
+        $type = $this->_getParam('type');
+        $id = $this->_getParam('id');
+
+        $this->getTabs()->add('show', array(
+            'title' => $this->translate('Event detail'),
+            'url'   => $this->view->url()
+        ))->activate(('show'));
+
+        $event = new Event($this->elasticsearch_url);
+
+        $event->setIndex($index);
+        $event->setType($type);
+        $event->setId($id);
+
+        $this->view->event = $event->fetch();
+    }
 }
diff --git a/application/views/scripts/event/search.phtml b/application/views/scripts/event/search.phtml
index 2732679..141254f 100644
--- a/application/views/scripts/event/search.phtml
+++ b/application/views/scripts/event/search.phtml
@@ -39,7 +39,7 @@
         <?php endif; ?>
         <?php foreach ($this->search->hits as $row):
             $num = 0;
-            $href = $this->url("logstash/show/".$row->_id);
+            $href = $this->url("logstash/event/show", array('index' => $row->_index, 'type' => $row->_type, 'id' => $row->_id));
         ?>
             <tr class="document" data-elastic-id="<?= $row->_id ?>"
                 href="<?= $href ?>">
@@ -55,13 +55,6 @@
                     <td class="rawjson"><pre><?= json_encode($row->_source, JSON_PRETTY_PRINT); ?></pre></td>
                 <?php endif ?>
             </tr>
-            <?php /* TODO: feature?
-            <tr class="document-detail">
-                <td colspan="<?= count($this->fieldlist) ?>">
-                    event
-                </td>
-            </tr>
-            */ ?>
         <?php endforeach ?>
         <?php endif ?>
     </table>
diff --git a/application/views/scripts/event/show.phtml b/application/views/scripts/event/show.phtml
new file mode 100644
index 0000000..32ae485
--- /dev/null
+++ b/application/views/scripts/event/show.phtml
@@ -0,0 +1,49 @@
+<?php if (! $this->compact): ?>
+<div class="controls">
+    <?= $this->tabs ?>
+</div>
+<?php endif ?>
+<div class="content">
+
+    <?php if (!$this->event->found()): ?>
+        <p>Document not found!</p>
+    <?php else: ?>
+
+        <table class="event_detail">
+            <tr>
+                <th>Index</th>
+                <td><?= $this->escape($this->event->getIndex()) ?></td>
+            </tr>
+            <tr>
+                <th>Type</th>
+                <td><?= $this->escape($this->event->getType()) ?></td>
+            </tr>
+            <tr>
+                <th>ID</th>
+                <td><?= $this->escape($this->event->getId()) ?></td>
+            </tr>
+            <tr>
+                <td colspan="2"> </td>
+            </tr>
+            <?php foreach($this->event->getSource() as $key => $value):
+                if (substr($key, 0, 1) == '@' or $key == 'type') continue;
+            ?>
+            <tr>
+                <th><?= $this->escape($key) ?></th>
+                <td><?php
+                    if (is_array($value)) {
+                        echo $this->escape(implode(', ', $value));
+                    }
+                    elseif (is_object($value)) {
+                        echo '<pre>'.$this->escape(json_encode($value, JSON_PRETTY_PRINT)).'</pre>';
+                    }
+                    else {
+                        echo $this->escape((String) $value);
+                    }
+                ?></td>
+            </tr>
+            <?php endforeach ?>
+        </table>
+
+    <?php endif ?>
+</div>
diff --git a/library/Logstash/Event.php b/library/Logstash/Event.php
new file mode 100644
index 0000000..62bd9db
--- /dev/null
+++ b/library/Logstash/Event.php
@@ -0,0 +1,121 @@
+<?php
+
+namespace Icinga\Module\Logstash;
+
+use Icinga\Module\Logstash\Curl;
+use Exception;
+use stdClass;
+
+class Event extends ElasticsearchBackend
+{
+    protected $index;
+    protected $type;
+    protected $id;
+
+    protected $found = false;
+    protected $version;
+    protected $source;
+
+    public function fetch()
+    {
+        if (!$this->getElasticsearch()) {
+            throw new Exception("Elasticsearch URL has not be configured!");
+        }
+
+        if (!$this->index or !$this->type or !$this->id)
+            throw new Exception("index, type and id must be set for fetching!");
+
+        $result = $this->curl->get_json(
+            sprintf('/%s/%s/%s',
+                $this->index,
+                $this->type,
+                $this->id
+            )
+        );
+
+        if ($result !== false or $result->found !== false) {
+            $this->found = true;
+            $this->version = $result->_version;
+            $this->source = $result->_source;
+        }
+        return $this;
+    }
+
+    /**
+     * @return String
+     */
+    public function getIndex()
+    {
+        return $this->index;
+    }
+
+    /**
+     * @param String $index
+     * @throws Exception
+     */
+    public function setIndex($index)
+    {
+        if (defined($this->index))
+            throw new Exception("Cannot change index!");
+        $this->index = $index;
+    }
+
+    /**
+     * @return String
+     */
+    public function getType()
+    {
+        return $this->type;
+    }
+
+    /**
+     * @param String $type
+     * @throws Exception
+     */
+    public function setType($type)
+    {
+        if (defined($this->type))
+            throw new Exception("Cannot change type!");
+        $this->type = $type;
+    }
+
+    /**
+     * @return String
+     */
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    /**
+     * @param String $id
+     * @throws Exception
+     */
+    public function setId($id)
+    {
+        if (defined($this->id))
+            throw new Exception("Cannot change id!");
+        $this->id = $id;
+    }
+
+    /**
+     * @return Integer
+     */
+    public function getVersion()
+    {
+        return $this->version;
+    }
+
+    /**
+     * @return stdClass
+     */
+    public function getSource()
+    {
+        return $this->source;
+    }
+
+    public function found() {
+        return (bool) $this->found;
+    }
+
+}
diff --git a/public/css/module.less b/public/css/module.less
index e647c66..5f17e36 100644
--- a/public/css/module.less
+++ b/public/css/module.less
@@ -63,3 +63,14 @@ table.documents {
     font-size: 0.8em;
   }
 }
+
+table.event_detail {
+  td, th {
+    padding: 5px;
+  }
+  th {
+    vertical-align: top;
+    text-align: left;
+  }
+
+}



More information about the icinga-checkins mailing list