Merge branch 'ent-9641-mejorar-heatmaps' into 'develop'

Ent 9641 mejorar heatmaps

See merge request artica/pandorafms!5196
This commit is contained in:
Diego Muñoz-Reja 2022-10-21 09:29:40 +00:00
commit 423d4b8763
3 changed files with 176 additions and 24 deletions

View File

@ -78,6 +78,7 @@ if (is_ajax() === true) {
0 => __('Group agents'),
1 => __('Group modules by tag'),
2 => __('Group modules by module group'),
3 => __('Group modules by agents'),
],
'type',
$type,
@ -203,6 +204,10 @@ if (is_ajax() === true) {
'5'
);
break;
case 3:
// Empty.
break;
}
echo '</div>';
@ -212,6 +217,7 @@ if (is_ajax() === true) {
enterprise_include_once('include/functions_agents.php');
$id = get_parameter('id', 0);
switch ($type) {
case 3:
case 2:
$data = db_get_row('tagente_modulo', 'id_agente_modulo', $id);

View File

@ -139,7 +139,7 @@ class Heatmap
ui_require_css_file('heatmap');
$settings = [
'type' => 'POST',
'type' => 'GET',
'dataType' => 'html',
'url' => ui_get_full_url(
'ajax.php',
@ -169,11 +169,41 @@ class Heatmap
setting['data']['height'] = $(`#div_${randomId}`).height() + 10;
setting['data']['width'] = $(`#div_${randomId}`).width();
var totalModules = 0;
// Initial charge.
ajaxRequest(
`div_${randomId}`,
setting
);
$.ajax({
type: setting.type,
dataType: setting.dataType,
url: setting.url,
data: setting.data,
success: function(data) {
$(`#div_${randomId}`).append(data);
totalModules = $('rect').length;
let cont = 0;
while (cont < Math.ceil(totalModules / 10)) {
oneSquare(getRandomInteger(1, 10), getRandomInteger(100, 900));
cont ++;
}
}
});
function getRandomInteger(min, max) {
return Math.floor(Math.random() * max) + min;
}
function oneSquare(solid, time) {
var randomPoint = getRandomInteger(1, totalModules);
let target = $(`#${randomId}_${randomPoint}`);
setTimeout(function() {
let class_name = target.attr('class');
class_name = class_name.split(' ')[0];
const newClassName = class_name.split('_')[0];
target.removeClass(`${class_name} hover`);
target.addClass(`${newClassName}_${solid} hover`);
oneSquare(getRandomInteger(1, 10), getRandomInteger(100, 900));
}, time);
}
// Refresh.
setInterval(
@ -206,23 +236,29 @@ class Heatmap
// randomly sort.
lista = lista.sort(function() {return Math.random() - 0.5});
const countPerSecond = total / refresh;
let countPerSecond = total / refresh;
if (countPerSecond < 1) {
countPerSecond = 1;
}
let cont = 0;
let limit = countPerSecond - 1;
const timer = setInterval(
function() {
while (cont <= limit) {
$(`#${randomId}_${lista[cont]['id']}`).removeClass();
$(`#${randomId}_${lista[cont]['id']}`).addClass(`${lista[cont]['status']} hover`);
cont++;
function() {
while (cont <= limit) {
if (typeof lista[cont] !== 'undefined') {
const rect = document.getElementsByName(`${lista[cont]['id']}`);
$(`#${rect[0].id}`).removeClass();
$(`#${rect[0].id}`).addClass(`${lista[cont]['status']} hover`);
}
limit = limit + countPerSecond;
},
1000
);
cont++;
}
limit = limit + countPerSecond;
},
1000
);
setTimeout(
function(){
@ -314,8 +350,10 @@ class Heatmap
// All agents.
$sql = sprintf(
'SELECT DISTINCT id_agente as id,alias,id_grupo,normal_count,warning_count,critical_count, unknown_count,notinit_count,total_count,fired_count,
(SELECT last_status_change FROM tagente_estado WHERE id_agente = tagente.id_agente ORDER BY last_status_change DESC LIMIT 1) AS last_status_change
'SELECT DISTINCT id_agente as id,alias,id_grupo,normal_count,warning_count,critical_count,
unknown_count,notinit_count,total_count,fired_count,
(SELECT last_status_change FROM tagente_estado WHERE id_agente = tagente.id_agente
ORDER BY last_status_change DESC LIMIT 1) AS last_status_change
FROM tagente WHERE `disabled` = 0 %s %s ORDER BY id_grupo,id_agente ASC',
$alias,
$id_grupo
@ -391,7 +429,8 @@ class Heatmap
// All modules.
$sql = sprintf(
'SELECT am.id_agente_modulo AS id, ae.known_status AS `status`, am.id_module_group AS id_grupo, ae.last_status_change FROM tagente_modulo am
'SELECT am.id_agente_modulo AS id, ae.estado AS `status`, am.id_module_group AS id_grupo,
ae.last_status_change FROM tagente_modulo am
INNER JOIN tagente_estado ae ON am.id_agente_modulo = ae.id_agente_modulo
WHERE am.disabled = 0 %s %s GROUP BY am.id_module_group, am.id_agente_modulo',
$filter_group,
@ -489,7 +528,8 @@ class Heatmap
// All modules.
$sql = sprintf(
'SELECT ae.id_agente_modulo AS id, ae.known_status AS `status`, tm.id_tag AS id_grupo, ae.last_status_change FROM tagente_estado ae
'SELECT ae.id_agente_modulo AS id, ae.estado AS `status`, tm.id_tag AS id_grupo,
ae.last_status_change FROM tagente_estado ae
INNER JOIN ttag_module tm ON tm.id_agente_modulo = ae.id_agente_modulo
WHERE 1=1 %s %s GROUP BY tm.id_tag, ae.id_agente_modulo',
$filter_tag,
@ -567,6 +607,98 @@ class Heatmap
}
/**
* Get all modules group by agents
*
* @return array
*/
protected function getAllModulesByAgents()
{
$filter_name = '';
if (empty($this->search) === false) {
$filter_name = 'AND nombre LIKE "%'.$this->search.'%"';
}
// All modules.
$sql = sprintf(
'SELECT am.id_agente_modulo AS id, ae.estado AS `status`, am.id_agente AS id_grupo,
ae.last_status_change FROM tagente_modulo am
INNER JOIN tagente_estado ae ON am.id_agente_modulo = ae.id_agente_modulo
WHERE am.disabled = 0 %s GROUP BY ae.id_agente_modulo ORDER BY id_grupo',
$filter_name
);
$result = db_get_all_rows_sql($sql);
// Module status.
foreach ($result as $key => $module) {
$status = '';
switch ($module['status']) {
case AGENT_MODULE_STATUS_CRITICAL_BAD:
case AGENT_MODULE_STATUS_CRITICAL_ALERT:
case 1:
case 100:
$status = 'critical';
break;
case AGENT_MODULE_STATUS_NORMAL:
case AGENT_MODULE_STATUS_NORMAL_ALERT:
case 0:
case 300:
$status = 'normal';
break;
case AGENT_MODULE_STATUS_WARNING:
case AGENT_MODULE_STATUS_WARNING_ALERT:
case 2:
case 200:
$status = 'warning';
break;
default:
case AGENT_MODULE_STATUS_UNKNOWN:
case 3:
$status = 'unknown';
break;
case AGENT_MODULE_STATUS_NOT_INIT:
case 5:
$status = 'notinit';
break;
}
if ($module['last_status_change'] != 0) {
$seconds = (time() - $module['last_status_change']);
if ($seconds >= SECONDS_1DAY) {
$status .= '_10';
} else if ($seconds >= 77760) {
$status .= '_9';
} else if ($seconds >= 69120) {
$status .= '_8';
} else if ($seconds >= 60480) {
$status .= '_7';
} else if ($seconds >= 51840) {
$status .= '_6';
} else if ($seconds >= 43200) {
$status .= '_5';
} else if ($seconds >= 34560) {
$status .= '_4';
} else if ($seconds >= 25920) {
$status .= '_3';
} else if ($seconds >= 17280) {
$status .= '_2';
} else if ($seconds >= 8640) {
$status .= '_1';
}
}
$result[$key]['status'] = $status;
}
return $result;
}
/**
* GetData
*
@ -575,6 +707,10 @@ class Heatmap
public function getData()
{
switch ($this->type) {
case 3:
$data = $this->getAllModulesByAgents();
break;
case 2:
$data = $this->getAllModulesByGroup();
break;
@ -709,9 +845,10 @@ class Heatmap
$groups = [];
$contX = 0;
$contY = 0;
$cont = 1;
foreach ($result as $value) {
echo '<rect id="'.$this->randomId.'_'.$value['id'].'" class="'.$value['status'].' hover"
width="1" height="1" x ="'.$contX.' "y="'.$contY.'" />';
echo '<rect id="'.$this->randomId.'_'.$cont.'" class="'.$value['status'].' hover"
width="1" height="1" x ="'.$contX.' "y="'.$contY.'" name="'.$value['id'].'" />';
$contX++;
if ($contX >= $Xaxis) {
@ -724,14 +861,15 @@ class Heatmap
} else {
$groups[$value['id_grupo']] += 1;
}
$cont++;
}
?>
<script type="text/javascript">
$('rect').click(function() {
const type = <?php echo $this->type; ?>;
const hash = '<?php echo $this->randomId; ?>';
const id = this.id.replace(`${hash}_`, '');
const id = $(`#${this.id}`).attr("name");
$("#info_dialog").dialog({
resizable: true,
@ -778,6 +916,10 @@ class Heatmap
foreach ($groups as $key => $group) {
$name = '';
switch ($this->type) {
case 3:
$name = agents_get_alias($key);
break;
case 2:
$name = modules_get_modulegroup_name($key);
break;

View File

@ -91,6 +91,10 @@ if ($is_ajax === false && $pure === false) {
$header_name = __('Heatmap view');
switch ($type) {
case 3:
$header_name .= ' - '.__('Agents');
break;
case 2:
if (current($filter) == 0) {
$header_name .= ' - '.__('Module group').': '.__('Not assigned');