Dashboards in metaconsole

This commit is contained in:
fbsanchez 2021-02-18 12:47:42 +01:00
parent 1b3eb9a19e
commit ef16ef4281
9 changed files with 241 additions and 57 deletions

View File

@ -712,6 +712,7 @@ function events_update_status($id_evento, $status, $filter=null, $history=false)
* @param boolean $validatedEvents If true, evaluate validated events.
* @param boolean $recursiveGroups If true, filtered groups and their children
* will be search.
* @param boolean $nodeConnected Already connected to node (uses tevento).
*
* @return array Events.
* @throws Exception On error.
@ -727,7 +728,8 @@ function events_get_all(
$return_sql=false,
$having='',
$validatedEvents=false,
$recursiveGroups=true
$recursiveGroups=true,
$nodeConnected=false
) {
global $config;
@ -1017,7 +1019,10 @@ function events_get_all(
);
}
$table = events_get_events_table(is_metaconsole(), $history);
$table = events_get_events_table(
(is_metaconsole() && $nodeConnected === false),
$history
);
$tevento = sprintf(
' %s te',
$table
@ -1028,7 +1033,7 @@ function events_get_all(
$tagente_table = 'tagente';
$tagente_field = 'id_agente';
$conditionMetaconsole = '';
if (is_metaconsole()) {
if (is_metaconsole() && $nodeConnected === false) {
$tagente_table = 'tmetaconsole_agent';
$tagente_field = 'id_tagente';
$conditionMetaconsole = ' AND ta.id_tmetaconsole_setup = te.server_id ';
@ -1075,7 +1080,7 @@ function events_get_all(
);
}
if (is_metaconsole()) {
if (is_metaconsole() && $nodeConnected === false) {
// Id source event.
if (!empty($filter['id_source_event'])) {
$sql_filters[] = sprintf(
@ -1239,7 +1244,7 @@ function events_get_all(
// Query_table.
'',
// Meta.
is_metaconsole(),
is_metaconsole() && $nodeConnected === false,
// Childrens_ids.
[],
// Force_group_and_tag.
@ -1265,7 +1270,7 @@ function events_get_all(
// Query_table.
'',
// Meta.
is_metaconsole(),
is_metaconsole() && $nodeConnected === false,
// Childrens_ids.
[],
// Force_group_and_tag.
@ -1291,7 +1296,7 @@ function events_get_all(
// Query_table.
'',
// Meta.
is_metaconsole(),
is_metaconsole() && $nodeConnected === false,
// Childrens_ids.
[],
// Force_group_and_tag.
@ -1312,7 +1317,7 @@ function events_get_all(
// Module search.
$agentmodule_join = 'LEFT JOIN tagente_modulo am ON te.id_agentmodule = am.id_agente_modulo';
if (is_metaconsole()) {
if (is_metaconsole() && $nodeConnected === false) {
$agentmodule_join = '';
} else if (!empty($filter['module_search'])) {
$agentmodule_join = 'INNER JOIN tagente_modulo am ON te.id_agentmodule = am.id_agente_modulo';
@ -1339,7 +1344,7 @@ function events_get_all(
}
$extra = '';
if (is_metaconsole()) {
if (is_metaconsole() && $nodeConnected === false) {
$extra = ', server_id';
}
@ -1405,7 +1410,7 @@ function events_get_all(
}
$server_join = '';
if (is_metaconsole()) {
if (is_metaconsole() && $nodeConnected === false) {
$server_join = ' LEFT JOIN tmetaconsole_setup ts
ON ts.id = te.server_id';
if (!empty($filter['server_id'])) {

View File

@ -4318,6 +4318,7 @@ function html_print_input($data, $wrapper='div', $input_only=false)
return '';
}
enterprise_include_once('include/functions_metaconsole.php');
$output = '';
if ($data['label'] && $input_only === false) {

View File

@ -216,8 +216,6 @@ class Cell
*/
public static function getCells(int $dashboardId):array
{
global $config;
$cells = db_get_all_rows_filter(
'twidget_dashboard',
[

View File

@ -178,6 +178,7 @@ class Manager
'saveWidgetIntoCell',
'imageIconDashboardAjax',
'formSlides',
'callWidgetMethod',
];
@ -1501,4 +1502,25 @@ class Manager
}
/**
* Call widget method (ajax only).
*
* @param string $method Method to be invoked.
*
* @return boolean Executed or not.
*/
public function callWidgetMethod(string $method):bool
{
$widget = $this->instanceWidget();
if (method_exists($widget, $method) === true) {
$widget->$method();
return true;
}
return false;
}
}

View File

@ -14,7 +14,7 @@ class Widget
*
* @var integer
*/
private $dashboardId;
protected $dashboardId;
/**
* Cell ID.
@ -28,7 +28,7 @@ class Widget
*
* @var integer
*/
private $widgetId;
protected $widgetId;
/**
* Values widget.
@ -42,7 +42,14 @@ class Widget
*
* @var integer
*/
private $nodeId;
protected $nodeId;
/**
* Should we show select node in metaconsole environments?
*
* @var boolean
*/
private $showSelectNodeMeta;
/**
@ -63,6 +70,7 @@ class Widget
$this->cellId = $cellId;
$this->dashboardId = $dashboardId;
$this->fields = $this->get();
$this->className = $this->fields['class_name'];
$cellClass = new Cell($this->cellId, $this->dashboardId);
$this->dataCell = $cellClass->get();
@ -427,7 +435,7 @@ class Widget
if ((bool) \is_metaconsole() === true) {
\enterprise_include_once('include/functions_metaconsole.php');
if ($this->nodeId !== null) {
if ($this->nodeId > 0) {
if (\metaconsole_connect(null, $this->nodeId) !== NOERR) {
$output .= '<div class="container-center">';
$output .= \ui_print_info_message(
@ -438,6 +446,8 @@ class Widget
$output .= '</div>';
return $output;
}
$config['metaconsole'] = false;
}
}
@ -463,8 +473,9 @@ class Widget
}
if ((bool) \is_metaconsole() === true) {
if ($this->nodeId !== null) {
if ($this->nodeId > 0) {
\metaconsole_restore_db();
$config['metaconsole'] = true;
}
}
@ -538,7 +549,9 @@ class Widget
],
];
if ((bool) \is_metaconsole()) {
if ((bool) \is_metaconsole() === true
&& $this->shouldSelectNode() === true
) {
\enterprise_include_once('include/functions_metaconsole.php');
$servers = \metaconsole_get_servers();
if (is_array($servers) === true) {
@ -561,6 +574,8 @@ class Widget
'type' => 'select',
'fields' => $servers,
'selected' => $values['node'],
'nothing' => __('This metaconsole'),
'nothing_value' => -1,
'return' => true,
],
];
@ -581,7 +596,11 @@ class Widget
$values['title'] = \get_parameter('title', '');
$values['background'] = \get_parameter('background', '#ffffff');
if ((bool) \is_metaconsole() === true) {
if ($this->shouldSelectNode() === true) {
$values['node'] = \get_parameter('node', null);
} else {
$values['node'] = \get_parameter('metaconsoleId', null);
}
}
return $values;
@ -654,6 +673,32 @@ class Widget
}
/**
* Should select for nodes been shown while in metaconsole environment?
*
* @return boolean
*/
protected function shouldSelectNode():bool
{
if ($this->showSelectNodeMeta !== null) {
return (bool) $this->showSelectNodeMeta;
}
switch ($this->className) {
case 'EventsListWidget':
case 'ReportsWidget':
$this->showSelectNodeMeta = true;
break;
default:
$this->showSelectNodeMeta = false;
break;
}
return (bool) $this->showSelectNodeMeta;
}
/**
* Get description should be implemented for each child.
*

View File

@ -603,7 +603,10 @@ class EventsListWidget extends Widget
'ta.alias as agent_name',
'tg.nombre as group_name',
];
if ((bool) \is_metaconsole() === false) {
if ((bool) \is_metaconsole() === false
|| $this->nodeId > 0
) {
$fields[] = 'am.nombre as module_name';
$fields[] = 'am.id_agente_modulo as id_agentmodule';
$fields[] = 'am.custom_id as module_custom_id';
@ -636,7 +639,9 @@ class EventsListWidget extends Widget
// ValidatedEvents.
false,
// Recursive Groups.
(bool) $this->values['groupRecursion']
(bool) $this->values['groupRecursion'],
// Already connected.
($this->nodeId > 0)
);
if ($events === false) {

View File

@ -203,6 +203,90 @@ class ReportsWidget extends Widget
}
/**
* Return report list.
*
* @return array
*/
protected function getReports(): array
{
$return_all_group = false;
if ((bool) users_can_manage_group_all('RM') === true) {
$return_all_group = true;
}
// Reports.
$reports = \reports_get_reports(
false,
[
'id_report',
'name',
],
$return_all_group
);
// If currently selected report is not included in fields array
// (it belongs to a group over which user has no permissions), then add
// it to fields array.
// This is aimed to avoid overriding this value when a user with
// narrower permissions edits widget configuration.
if ($this->values['reportId'] !== null
&& in_array(
$this->values['reportId'],
array_column(
$reports,
'id_report'
)
) === false
) {
$selected_report = db_get_row(
'treport',
'id_report',
$this->values['reportId']
);
$reports[] = $selected_report;
}
$fields = array_reduce(
$reports,
function ($carry, $item) {
$carry[$item['id_report']] = \io_safe_output($item['name']);
return $carry;
},
[]
);
return $fields;
}
/**
* Dumps report list in json to fullfill select for report.
*
* @return void
*/
public function getReportList(): void
{
$node_id = \get_parameter('nodeId', $this->nodeId);
if (\is_metaconsole() === true && $node_id > 0) {
if (\metaconsole_connect(null, $node_id) !== NOERR) {
echo json_encode(
['error' => __('Failed to connect to node %d', $node_id) ]
);
}
}
echo json_encode(
$this->getReports(),
1
);
if (\is_metaconsole() === true && $node_id > 0) {
\metaconsole_restore_db();
}
}
/**
* Generates inputs for form (specific).
*
@ -217,31 +301,7 @@ class ReportsWidget extends Widget
// Retrieve global - common inputs.
$inputs = parent::getFormInputs();
$return_all_group = false;
if (users_can_manage_group_all('RM')) {
$return_all_group = true;
}
// Reports.
$reports = \reports_get_reports(false, ['id_report', 'name'], $return_all_group);
// If currently selected report is not included in fields array (it belongs to a group over which user has no permissions), then add it to fields array.
// This is aimed to avoid overriding this value when a user with narrower permissions edits widget configuration.
if ($values['reportId'] !== null && !in_array($values['reportId'], array_column($reports, 'id_report'))) {
$selected_report = db_get_row('treport', 'id_report', $values['reportId']);
$reports[] = $selected_report;
}
$fields = array_reduce(
$reports,
function ($carry, $item) {
$carry[$item['id_report']] = $item['name'];
return $carry;
},
[]
);
$fields = $this->getReports();
$inputs[] = [
'label' => __('Report'),
@ -282,10 +342,6 @@ class ReportsWidget extends Widget
*/
public function load()
{
global $config;
$size = parent::getSize();
$output = '';
ob_start();
if ($this->values['reportId'] !== 0) {
@ -397,4 +453,50 @@ class ReportsWidget extends Widget
}
/**
* Return aux javascript code for forms.
*
* @return string
*/
public function getFormJS()
{
ob_start();
?>
$('#node').on('change', function() {
$.ajax({
method: "POST",
url: '<?php echo \ui_get_full_url('ajax.php'); ?>',
data: {
page: 'operation/dashboard/dashboard',
dashboardId: '<?php echo $this->dashboardId; ?>',
widgetId: '<?php echo $this->widgetId; ?>',
cellId: '<?php echo $this->cellId; ?>',
class: '<?php echo __CLASS__; ?>',
method: 'getReportList',
nodeId: $('#node').val()
},
dataType: 'JSON',
success: function(data) {
$('#reportId').empty();
Object.entries(data).forEach(e => {
key = e[0];
value = e[1];
$('#reportId').append($('<option>').val(key).text(value))
});
if (Object.entries(data).length == 0) {
$('#reportId').append(
$('<option>')
.val(-1)
.text("<?php echo __('None'); ?>")
);
}
}
})
});
<?php
$js = ob_get_clean();
return $js;
}
}

View File

@ -0,0 +1,4 @@
div#page {
width: 90% !important;
margin: 0 auto;
}

View File

@ -65,8 +65,10 @@ if (is_ajax() === true) {
$cs->error('Unavailable method.');
}
} else {
if ($cs->callWidgetMethod($method) === false) {
$cs->error('Method not found. ['.$method.']');
}
}
} else {
// Run.
$cs->run();