new: [dashboard] added a way to auto reload widgets

- has to be defined in the code of the widget
pull/5635/head
iglocska 2020-03-04 14:46:01 +01:00
parent 14e2e68db8
commit 218ea0333c
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
9 changed files with 79 additions and 12 deletions

View File

@ -139,16 +139,25 @@ class DashboardsController extends AppController
$org_scope = $this->_isSiteAdmin() ? 0 : $this->Auth->user('org_id');
$lookup_hash = hash('sha256', $value['widget'] . $value['config']);
$data = $redis->get('misp:dashboard:' . $org_scope . ':' . $lookup_hash);
if (empty($data)) {
$cacheLifetime = isset($dashboardWidget->cacheLifetime) ? $dashboardWidget->cacheLifetime : 300;
if (!isset($dashboardWidget->cacheLifetime)) {
$dashboardWidget->cacheLifetime = false;
}
if (empty($dashboardWidget->cacheLifetime) || empty($data)) {
$data = $dashboardWidget->handler($this->Auth->user(), json_decode($value['config'], true));
$redis->set('misp:dashboard:' . $org_scope . ':' . $lookup_hash, json_encode(array('data' => $data)));
$redis->expire('misp:dashboard:' . $org_scope . ':' . $lookup_hash, $cacheLifetime);
if (!empty($dashboardWidget->cacheLifetime)) {
$redis->set('misp:dashboard:' . $org_scope . ':' . $lookup_hash, json_encode(array('data' => $data)));
$redis->expire('misp:dashboard:' . $org_scope . ':' . $lookup_hash, $dashboardWidget->cacheLifetime);
}
} else {
$data = json_decode($data, true)['data'];
}
$config = array(
'render' => $dashboardWidget->render,
'autoRefreshDelay' => empty($dashboardWidget->autoRefreshDelay) ? false : $dashboardWidget->autoRefreshDelay
);
$this->set('data', $data);
$this->render('/Dashboards/Widgets/' . $dashboardWidget->render);
$this->set('config', $config);
$this->render('widget_loader');
} else {
throw new MethodNotAllowedException(__('This endpoint can only be reached via POST requests.'));
}

View File

@ -8,7 +8,8 @@ class MispAdminResourceWidget
public $height = 2;
public $params = array();
public $description = 'Basic widget showing some server statistics in regards to MISP.';
public $cacheLifetime = 5;
public $cacheLifetime = false;
public $autoRefreshDelay = 3;
public function handler($user, $options = array())

View File

@ -8,7 +8,8 @@ class MispAdminWorkerWidget
public $height = 2;
public $params = array();
public $description = 'Basic widget showing some server statistics in regards to MISP.';
public $cacheLifetime = 5;
public $cacheLifetime = false;
public $autoRefreshDelay = 5;
public function handler($user, $options = array())
@ -23,11 +24,13 @@ class MispAdminWorkerWidget
}
$total = 0;
$alive = 0;
foreach ($queue['workers'] as $worker) {
if ($worker['alive']) {
$alive += 1;
if (!empty($queue['workers'])) {
foreach ($queue['workers'] as $worker) {
if ($worker['alive']) {
$alive += 1;
}
$total += 1;
}
$total += 1;
}
$colour = 'green';
if ($alive == 0) {

View File

@ -80,7 +80,8 @@ class Dashboard extends AppModel
'description' => empty($widgetClass->description) ? $widgetClass->title : $widgetClass->description,
'height' => empty($widgetClass->height) ? 1 : $widgetClass->height,
'width' => empty($widgetClass->width) ? 1 : $widgetClass->width,
'placeholder' => empty($widgetClass->placeholder) ? '' : $widgetClass->placeholder
'placeholder' => empty($widgetClass->placeholder) ? '' : $widgetClass->placeholder,
'autoRefreshDelay' => empty($widgetClass->autoRefreshDelay) ? false : $widgetClass->autoRefreshDelay,
);
return $widget;
}

View File

@ -0,0 +1,21 @@
<?php
$randomId = rand();
?>
<div id="widgetContentInner<?= $randomId ?>">
<?php
echo $this->element('/dashboard/Widgets/' . $config['render']);
?>
</div>
<script type="text/javascript">
$(document).ready(function() {
<?php
if ($config['autoRefreshDelay']) {
echo sprintf(
'setTimeout( function(){ updateDashboardWidget($("#widgetContentInner%s").parent().parent().parent())}, %s);',
$randomId,
$config['autoRefreshDelay'] . '000'
);
}
?>
});
</script>

View File

@ -0,0 +1,32 @@
<table style="border-spacing:0px;">
<?php
if (!empty($data['logarithmic'])) {
$max = max($data['logarithmic']);
} else {
$max = max($data['data']);
}
foreach ($data['data'] as $entry => $count) {
$value = $count;
if (!empty($data['logarithmic'])) {
$value = $data['logarithmic'][$entry];
}
echo sprintf(
'<tr><td style="%s">%s</td><td style="%s">%s</td></tr>',
'text-align:right;width:33%;',
h($entry),
'width:100%',
sprintf(
'<div title="%s" style="%s">%s</div>',
h($entry) . ': ' . h($count),
sprintf(
'background-color:%s; width:%s; color:white; text-align:center;',
(empty($data['colours'][$entry]) ? '#0088cc' : h($data['colours'][$entry])),
100 * h($value) / $max . '%;'
),
h($count)
),
'&nbsp;'
);
}
?>
</table>