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

View File

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

View File

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

View File

@ -178,6 +178,7 @@ class Manager
'saveWidgetIntoCell', 'saveWidgetIntoCell',
'imageIconDashboardAjax', 'imageIconDashboardAjax',
'formSlides', '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 * @var integer
*/ */
private $dashboardId; protected $dashboardId;
/** /**
* Cell ID. * Cell ID.
@ -28,7 +28,7 @@ class Widget
* *
* @var integer * @var integer
*/ */
private $widgetId; protected $widgetId;
/** /**
* Values widget. * Values widget.
@ -42,7 +42,14 @@ class Widget
* *
* @var integer * @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->cellId = $cellId;
$this->dashboardId = $dashboardId; $this->dashboardId = $dashboardId;
$this->fields = $this->get(); $this->fields = $this->get();
$this->className = $this->fields['class_name'];
$cellClass = new Cell($this->cellId, $this->dashboardId); $cellClass = new Cell($this->cellId, $this->dashboardId);
$this->dataCell = $cellClass->get(); $this->dataCell = $cellClass->get();
@ -427,7 +435,7 @@ class Widget
if ((bool) \is_metaconsole() === true) { if ((bool) \is_metaconsole() === true) {
\enterprise_include_once('include/functions_metaconsole.php'); \enterprise_include_once('include/functions_metaconsole.php');
if ($this->nodeId !== null) { if ($this->nodeId > 0) {
if (\metaconsole_connect(null, $this->nodeId) !== NOERR) { if (\metaconsole_connect(null, $this->nodeId) !== NOERR) {
$output .= '<div class="container-center">'; $output .= '<div class="container-center">';
$output .= \ui_print_info_message( $output .= \ui_print_info_message(
@ -438,6 +446,8 @@ class Widget
$output .= '</div>'; $output .= '</div>';
return $output; return $output;
} }
$config['metaconsole'] = false;
} }
} }
@ -463,8 +473,9 @@ class Widget
} }
if ((bool) \is_metaconsole() === true) { if ((bool) \is_metaconsole() === true) {
if ($this->nodeId !== null) { if ($this->nodeId > 0) {
\metaconsole_restore_db(); \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'); \enterprise_include_once('include/functions_metaconsole.php');
$servers = \metaconsole_get_servers(); $servers = \metaconsole_get_servers();
if (is_array($servers) === true) { if (is_array($servers) === true) {
@ -556,12 +569,14 @@ class Widget
$inputs[] = [ $inputs[] = [
'label' => __('Node'), 'label' => __('Node'),
'arguments' => [ 'arguments' => [
'wrapper' => 'div', 'wrapper' => 'div',
'name' => 'node', 'name' => 'node',
'type' => 'select', 'type' => 'select',
'fields' => $servers, 'fields' => $servers,
'selected' => $values['node'], 'selected' => $values['node'],
'return' => true, 'nothing' => __('This metaconsole'),
'nothing_value' => -1,
'return' => true,
], ],
]; ];
} }
@ -581,7 +596,11 @@ class Widget
$values['title'] = \get_parameter('title', ''); $values['title'] = \get_parameter('title', '');
$values['background'] = \get_parameter('background', '#ffffff'); $values['background'] = \get_parameter('background', '#ffffff');
if ((bool) \is_metaconsole() === true) { if ((bool) \is_metaconsole() === true) {
$values['node'] = \get_parameter('node', null); if ($this->shouldSelectNode() === true) {
$values['node'] = \get_parameter('node', null);
} else {
$values['node'] = \get_parameter('metaconsoleId', null);
}
} }
return $values; 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. * Get description should be implemented for each child.
* *

View File

@ -603,7 +603,10 @@ class EventsListWidget extends Widget
'ta.alias as agent_name', 'ta.alias as agent_name',
'tg.nombre as group_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.nombre as module_name';
$fields[] = 'am.id_agente_modulo as id_agentmodule'; $fields[] = 'am.id_agente_modulo as id_agentmodule';
$fields[] = 'am.custom_id as module_custom_id'; $fields[] = 'am.custom_id as module_custom_id';
@ -636,7 +639,9 @@ class EventsListWidget extends Widget
// ValidatedEvents. // ValidatedEvents.
false, false,
// Recursive Groups. // Recursive Groups.
(bool) $this->values['groupRecursion'] (bool) $this->values['groupRecursion'],
// Already connected.
($this->nodeId > 0)
); );
if ($events === false) { 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). * Generates inputs for form (specific).
* *
@ -217,31 +301,7 @@ class ReportsWidget extends Widget
// Retrieve global - common inputs. // Retrieve global - common inputs.
$inputs = parent::getFormInputs(); $inputs = parent::getFormInputs();
$return_all_group = false; $fields = $this->getReports();
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;
},
[]
);
$inputs[] = [ $inputs[] = [
'label' => __('Report'), 'label' => __('Report'),
@ -282,10 +342,6 @@ class ReportsWidget extends Widget
*/ */
public function load() public function load()
{ {
global $config;
$size = parent::getSize();
$output = ''; $output = '';
ob_start(); ob_start();
if ($this->values['reportId'] !== 0) { 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,7 +65,9 @@ if (is_ajax() === true) {
$cs->error('Unavailable method.'); $cs->error('Unavailable method.');
} }
} else { } else {
$cs->error('Method not found. ['.$method.']'); if ($cs->callWidgetMethod($method) === false) {
$cs->error('Method not found. ['.$method.']');
}
} }
} else { } else {
// Run. // Run.