[icinga-checkins] icinga.org: icingaweb2-module-elasticsearch/master: Add and integrate AutoRefresher widget

git at icinga.org git at icinga.org
Thu Jun 30 17:08:10 CEST 2016


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

Author: Markus Frosch <lazyfrosch at icinga.org>
Date:   Fri Jun  3 15:31:54 2016 +0200

Add and integrate AutoRefresher widget

refs #11884

---

 application/controllers/EventController.php        |   16 +--
 .../forms/Widget/AutoRefresherControlForm.php      |  115 ++++++++++++++++++++
 application/forms/Widget/FieldSelectorForm.php     |    2 +-
 application/views/scripts/event/search.phtml       |    1 +
 library/Elasticsearch/Controller.php               |   28 +++++
 library/Elasticsearch/Web/Widget/AutoRefresher.php |   55 ++++++++++
 public/css/module.less                             |   26 +++++
 7 files changed, 228 insertions(+), 15 deletions(-)

diff --git a/application/controllers/EventController.php b/application/controllers/EventController.php
index 5deb62c..2c8e8e0 100644
--- a/application/controllers/EventController.php
+++ b/application/controllers/EventController.php
@@ -36,24 +36,18 @@ class EventController extends Controller
         foreach ($query->getColumns() as $value) {
             $sort_columns[$value] = $value;
         }
-        $this->setupFilterControl($query, null, null, array('fields'));
+        $this->setupFilterControl($query, null, null, array('fields', 'refresh'));
         $this->setupLimitControl(100);
         $this->setupSortControl($sort_columns, $query, array('@timestamp' => 'desc'));
         $this->setupPaginationControl($query, 100);
         $this->setupFieldSelectorControl($query);
+        $this->setupAutoRefresherControl();
 
         $this->getTabs()->add('search', array(
             'title' => $this->translate('Events'),
             'url'   => $this->view->url()
         ))->activate(('search'));;
 
-        $this->view->live = $this->params->shift('live');
-        if ($this->view->live) {
-            $this->setAutorefreshInterval(1);
-        } else {
-            $this->setAutorefreshInterval(15);
-        }
-
         /* TODO: adapt to FilterEditor
         $this->view->show_ack = $this->_getParam('show_ack', 0);
 
@@ -68,15 +62,9 @@ class EventController extends Controller
         */
 
         // TODO: reimplement
-        //if (isset($fields))
-        //    $search->setFields($fields);
-
-
-        // TODO: reimplement
         //if (! $this->view->show_ack)
         //    $search->setWithoutAck(true);
 
-
         $this->view->events = $query->fetchAll();
 
         // TODO: reimplement
diff --git a/application/forms/Widget/AutoRefresherControlForm.php b/application/forms/Widget/AutoRefresherControlForm.php
new file mode 100644
index 0000000..0855261
--- /dev/null
+++ b/application/forms/Widget/AutoRefresherControlForm.php
@@ -0,0 +1,115 @@
+<?php
+/* Elasticsearch for Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Elasticsearch\Forms\Widget;
+
+use Icinga\Web\Form;
+
+/**
+ * AutoRefresher control form
+ */
+class AutoRefresherControlForm extends Form
+{
+    /**
+     * CSS class for the auto refresh control
+     *
+     * @var string
+     */
+    const CSS_CLASS_AUTOREFRESH = 'auto-refresh-control';
+
+    /**
+     * Default interval
+     *
+     * @var int
+     */
+    const DEFAULT_INTERVAL = 15;
+
+    /**
+     * Selectable intervals
+     *
+     * @var int[]
+     */
+    public static $intervals = array(
+        15  => '15s',
+        30  => '30s',
+        60  => '1m',
+        120 => '2m',
+        300 => '5m'
+    );
+
+    /**
+     * Default interval for this instance
+     *
+     * @var int|null
+     */
+    protected $defaultRefresh;
+
+    /**
+     * {@inheritdoc}
+     */
+    public function init()
+    {
+        $this->setAttrib('class', static::CSS_CLASS_AUTOREFRESH);
+    }
+
+    /**
+     * Get the default inteval
+     *
+     * @return int
+     */
+    public function getDefaultRefresh()
+    {
+        return $this->defaultRefresh !== null ? $this->defaultRefresh : static::DEFAULT_INTERVAL;
+    }
+
+    /**
+     * Set the default interval
+     *
+     * @param   int $defaultRefresh
+     *
+     * @return  $this
+     */
+    public function setDefaultRefresh($defaultRefresh)
+    {
+        $this->defaultRefresh = (int) $defaultRefresh;
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getRedirectUrl()
+    {
+        return $this->getRequest()->getUrl()
+            ->setParam('refresh', $this->getElement('refresh')->getValue());
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function createElements(array $formData)
+    {
+        $this->addElement(
+            'select',
+            'refresh',
+            array(
+                'autosubmit'    => true,
+                'escape'        => false,
+                'label'         => $this->getView()->icon('cw'),
+                'multiOptions'  => static::$intervals,
+                'value'         => $this->getRequest()->getUrl()->getParam('refresh', $this->getDefaultRefresh()),
+            )
+        );
+        $this->getElement('refresh')->getDecorator('label')->setOption('escape', false);
+    }
+
+    /**
+     * Auto refresh control is always successful
+     *
+     * @return bool
+     */
+    public function onSuccess()
+    {
+        return true;
+    }
+}
diff --git a/application/forms/Widget/FieldSelectorForm.php b/application/forms/Widget/FieldSelectorForm.php
index e9bd23c..cac1595 100644
--- a/application/forms/Widget/FieldSelectorForm.php
+++ b/application/forms/Widget/FieldSelectorForm.php
@@ -1,5 +1,5 @@
 <?php
-/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */
+/* Elasticsearch for Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
 
 namespace Icinga\Module\Elasticsearch\Forms\Widget;
 
diff --git a/application/views/scripts/event/search.phtml b/application/views/scripts/event/search.phtml
index 05701d4..e6782c4 100644
--- a/application/views/scripts/event/search.phtml
+++ b/application/views/scripts/event/search.phtml
@@ -4,6 +4,7 @@
     <?= $this->paginator; ?>
     <?= $this->fieldSelector ?>
     <div class="sort-controls-container">
+        <?= $this->autorefresher ?>
         <?= $this->limiter ?>
         <?= $this->sortBox ?>
     </div>
diff --git a/library/Elasticsearch/Controller.php b/library/Elasticsearch/Controller.php
index a039f18..77dc416 100644
--- a/library/Elasticsearch/Controller.php
+++ b/library/Elasticsearch/Controller.php
@@ -1,7 +1,9 @@
 <?php
+/* Elasticsearch for Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
 
 namespace Icinga\Module\Elasticsearch;
 
+use Icinga\Module\Elasticsearch\Web\Widget\AutoRefresher;
 use Icinga\Module\Elasticsearch\Web\Widget\FieldSelector;
 use Icinga\Repository\RepositoryQuery;
 use Icinga\Web\Controller as IcingaWebController;
@@ -23,4 +25,30 @@ class Controller extends IcingaWebController
         }
         return $this;
     }
+
+    /**
+     * Sets up the AutoRefresher Widget for the current view
+     *
+     * @param    int    $defaultRefresh   Default auto-refresh interval
+     *
+     * @return   $this
+     */
+    public function setupAutoRefresherControl($defaultRefresh=null)
+    {
+        $widget = new AutoRefresher();
+        if ($defaultRefresh !== null) {
+            $widget->setDefaultRefresh($defaultRefresh);
+        }
+
+        if (! $this->view->compact) {
+            $this->view->autorefresher = $widget;
+        }
+
+        $interval = (int) $this->getRequest()->getParam('refresh', $widget->getDefaultRefresh());
+        if ($interval !== null && $interval > 0) {
+            $this->setAutorefreshInterval($interval);
+        }
+
+        return $this;
+    }
 }
diff --git a/library/Elasticsearch/Web/Widget/AutoRefresher.php b/library/Elasticsearch/Web/Widget/AutoRefresher.php
new file mode 100644
index 0000000..f4faea6
--- /dev/null
+++ b/library/Elasticsearch/Web/Widget/AutoRefresher.php
@@ -0,0 +1,55 @@
+<?php
+/* Elasticsearch for Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */
+
+namespace Icinga\Module\Elasticsearch\Web\Widget;
+
+use Icinga\Web\Widget\AbstractWidget;
+use Icinga\Module\Elasticsearch\Forms\Widget\AutoRefresherControlForm;
+
+/**
+ * AutoRefresher control widget
+ */
+class AutoRefresher extends AbstractWidget
+{
+    /**
+     * Default auto refresh interval for this instance
+     *
+     * @var int|null
+     */
+    protected $defaultRefresh;
+
+    /**
+     * Get the default interval
+     *
+     * @return int|null
+     */
+    public function getDefaultRefresh()
+    {
+        return $this->defaultRefresh;
+    }
+
+    /**
+     * Set the default interval
+     *
+     * @param   int $defaultRefresh
+     *
+     * @return  $this
+     */
+    public function setDefaultRefresh($defaultRefresh)
+    {
+        $this->defaultRefresh = (int) $defaultRefresh;
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function render()
+    {
+        $control = new AutoRefresherControlForm();
+        $control
+            ->setDefaultRefresh($this->defaultRefresh)
+            ->handleRequest();
+        return (string)$control;
+    }
+}
diff --git a/public/css/module.less b/public/css/module.less
index 5a433e0..710f211 100644
--- a/public/css/module.less
+++ b/public/css/module.less
@@ -85,3 +85,29 @@ form.fieldSelector {
   }
 }
 /* FieldSelector END */
+
+/* AutoRefresher START */
+.auto-refresh-control {
+  display: inline-block;
+  margin: 0 1em;
+
+  > .control-group {
+    padding: 0;
+    // Note that the sort-control form does not have padding as it's utilizing different decorators
+
+    > .control-label-group {
+      text-align: left;
+      padding: 0;
+      width: 1.2em;
+    }
+
+    > select {
+      width: 4.5em;
+    }
+
+    > i {
+      .sr-only();
+    }
+  }
+}
+/* AutoRefresher END */



More information about the icinga-checkins mailing list