diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php index 183c2ab6ae..5dc24eaee4 100644 --- a/pandora_console/include/functions.php +++ b/pandora_console/include/functions.php @@ -3908,7 +3908,14 @@ function series_type_graph_array($data, $show_elements_graph) $type_graph = $show_elements_graph['type_graph']; } - $color_series = color_graph_array(); + if (isset($show_elements_graph['array_colors']) === true + && empty($show_elements_graph['array_colors']) === false + && is_array($show_elements_graph['array_colors']) === true + ) { + $color_series = $show_elements_graph['array_colors']; + } else { + $color_series = color_graph_array(); + } if ($show_elements_graph['id_widget_dashboard']) { $opcion = unserialize( diff --git a/pandora_console/include/functions_graph.php b/pandora_console/include/functions_graph.php index fdf08bbcce..026ef1c889 100644 --- a/pandora_console/include/functions_graph.php +++ b/pandora_console/include/functions_graph.php @@ -833,6 +833,10 @@ function grafico_modulo_sparse($params) $params['basic_chart'] = false; } + if (isset($params['array_colors']) === false) { + $params['array_colors'] = false; + } + // If is metaconsole set 10pt size value. if (is_metaconsole()) { $font_size = '10'; diff --git a/pandora_console/include/lib/Dashboard/Widget.php b/pandora_console/include/lib/Dashboard/Widget.php index ec962bb36d..245c9f0481 100644 --- a/pandora_console/include/lib/Dashboard/Widget.php +++ b/pandora_console/include/lib/Dashboard/Widget.php @@ -423,6 +423,7 @@ class Widget case 'EventCardboard': case 'ModulesByStatus': case 'AvgSumMaxMinModule': + case 'BasicChart': $className .= '\\'.$name; break; diff --git a/pandora_console/include/lib/Dashboard/Widgets/BasicChart.php b/pandora_console/include/lib/Dashboard/Widgets/BasicChart.php new file mode 100644 index 0000000000..8041cf77d9 --- /dev/null +++ b/pandora_console/include/lib/Dashboard/Widgets/BasicChart.php @@ -0,0 +1,681 @@ +width = $width; + + // Height. + $this->height = $height; + + // Grid Width. + $this->gridWidth = $gridWidth; + + // Cell Id. + $this->cellId = $cellId; + + // Options. + $this->values = $this->decoders($this->getOptionsWidget()); + + // Positions. + $this->position = $this->getPositionWidget(); + + // Page. + $this->page = basename(__FILE__); + + // ClassName. + $class = new \ReflectionClass($this); + $this->className = $class->getShortName(); + + // Title. + $this->title = __('Basic chart'); + + // Name. + if (empty($this->name) === true) { + $this->name = 'BasicChart'; + } + + // This forces at least a first configuration. + $this->configurationRequired = false; + if (empty($this->values['moduleId']) === true) { + $this->configurationRequired = true; + } else { + try { + if (is_metaconsole() === true + && $this->values['metaconsoleId'] > 0 + ) { + $node = new Node($this->values['metaconsoleId']); + $node->connect(); + } + + $check_exist = db_get_sql( + sprintf( + 'SELECT id_agente_modulo + FROM tagente_modulo + WHERE id_agente_modulo = %s + AND delete_pending = 0', + $this->values['moduleId'] + ) + ); + } catch (\Exception $e) { + // Unexistent agent. + if (is_metaconsole() === true + && $this->values['metaconsoleId'] > 0 + ) { + $node->disconnect(); + } + + $check_exist = false; + } finally { + if (is_metaconsole() === true + && $this->values['metaconsoleId'] > 0 + ) { + $node->disconnect(); + } + } + + if ($check_exist === false) { + $this->loadError = true; + } + } + + $this->overflow_scrollbars = false; + } + + + /** + * Decoders hack for retrocompability. + * + * @param array $decoder Values. + * + * @return array Returns the values with the correct key. + */ + public function decoders(array $decoder): array + { + $values = []; + // Retrieve global - common inputs. + $values = parent::decoders($decoder); + + if (isset($decoder['id_agent_'.$this->cellId]) === true) { + $values['agentId'] = $decoder['id_agent_'.$this->cellId]; + } + + if (isset($decoder['agentId']) === true) { + $values['agentId'] = $decoder['agentId']; + } + + if (isset($decoder['metaconsoleId']) === true) { + $values['metaconsoleId'] = $decoder['metaconsoleId']; + } + + if (isset($decoder['id_module_'.$this->cellId]) === true) { + $values['moduleId'] = $decoder['id_module_'.$this->cellId]; + } + + if (isset($decoder['moduleId']) === true) { + $values['moduleId'] = $decoder['moduleId']; + } + + if (isset($decoder['period']) === true) { + $values['period'] = $decoder['period']; + } + + if (isset($decoder['showLabel']) === true) { + $values['showLabel'] = $decoder['showLabel']; + } + + if (isset($decoder['showValue']) === true) { + $values['showValue'] = $decoder['showValue']; + } + + if (isset($decoder['sizeLabel']) === true) { + $values['sizeLabel'] = $decoder['sizeLabel']; + } + + if (isset($decoder['sizeValue']) === true) { + $values['sizeValue'] = $decoder['sizeValue']; + } + + if (isset($decoder['colorLabel']) === true) { + $values['colorLabel'] = $decoder['colorLabel']; + } + + if (isset($decoder['colorValue']) === true) { + $values['colorValue'] = $decoder['colorValue']; + } + + if (isset($decoder['colorChart']) === true) { + $values['colorChart'] = $decoder['colorChart']; + } + + if (isset($decoder['formatData']) === true) { + $values['formatData'] = $decoder['formatData']; + } + + if (isset($decoder['label']) === true) { + $values['label'] = $decoder['label']; + } + + return $values; + } + + + /** + * Generates inputs for form (specific). + * + * @return array Of inputs. + * + * @throws Exception On error. + */ + public function getFormInputs(): array + { + global $config; + $values = $this->values; + + // Default values. + if (isset($values['period']) === false) { + $values['period'] = SECONDS_1DAY; + } + + if (isset($values['colorChart']) === false) { + $values['colorChart'] = $config['graph_color1']; + } + + if (isset($values['showLabel']) === false) { + $values['showLabel'] = 1; + } + + if (isset($values['showValue']) === false) { + $values['showValue'] = 1; + } + + if (isset($values['sizeLabel']) === false) { + $values['sizeLabel'] = 20; + } + + if (isset($values['sizeValue']) === false) { + $values['sizeValue'] = 20; + } + + if (isset($values['colorLabel']) === false) { + $values['colorLabel'] = '#333'; + } + + if (isset($values['colorValue']) === false) { + $values['colorValue'] = '#333'; + } + + if (isset($values['formatData']) === false) { + $values['formatData'] = 1; + } + + if (isset($values['label']) === false) { + $values['label'] = 'module'; + } + + // Retrieve global - common inputs. + $inputs = parent::getFormInputs(); + + $blocks = [ + 'row1', + 'row2', + ]; + + $inputs['blocks'] = $blocks; + + foreach ($inputs as $kInput => $vInput) { + $inputs['inputs']['row1'][] = $vInput; + } + + // Autocomplete agents. + $inputs['inputs']['row2'][] = [ + 'label' => __('Agent'), + 'arguments' => [ + 'type' => 'autocomplete_agent', + 'name' => 'agentAlias', + 'id_agent_hidden' => $values['agentId'], + 'name_agent_hidden' => 'agentId', + 'server_id_hidden' => $values['metaconsoleId'], + 'name_server_hidden' => 'metaconsoleId', + 'return' => true, + 'module_input' => true, + 'module_name' => 'moduleId', + 'module_none' => false, + 'size' => 0, + ], + ]; + + // Autocomplete module. + $inputs['inputs']['row2'][] = [ + 'label' => __('Module'), + 'arguments' => [ + 'type' => 'autocomplete_module', + 'name' => 'moduleId', + 'selected' => $values['moduleId'], + 'return' => true, + 'sort' => false, + 'agent_id' => $values['agentId'], + 'metaconsole_id' => $values['metaconsoleId'], + 'style' => 'width: inherit;', + 'nothing' => __('None'), + 'nothing_value' => 0, + ], + ]; + + // Period. + $inputs['inputs']['row1'][] = [ + 'label' => __('Interval'), + 'arguments' => [ + 'name' => 'period', + 'type' => 'interval', + 'value' => $values['period'], + 'nothing' => __('None'), + 'nothing_value' => 0, + 'style_icon' => 'flex-grow: 0', + ], + ]; + + $inputs['inputs']['row2'][] = [ + 'label' => __('Color chart'), + 'arguments' => [ + 'wrapper' => 'div', + 'name' => 'colorChart', + 'type' => 'color', + 'value' => $values['colorChart'], + 'return' => true, + ], + ]; + + $inputs['inputs']['row1'][] = [ + 'label' => __('Show label'), + 'arguments' => [ + 'name' => 'showLabel', + 'id' => 'showLabel', + 'type' => 'switch', + 'value' => $values['showLabel'], + ], + ]; + + $fields = [ + 'module' => __('Module'), + 'agent' => __('Agent'), + 'agent_module' => __('Agent / module'), + ]; + + $inputs['inputs']['row1'][] = [ + 'label' => __('Label'), + 'arguments' => [ + 'type' => 'select', + 'fields' => $fields, + 'name' => 'label', + 'selected' => $values['label'], + 'return' => true, + ], + ]; + + $inputs['inputs']['row1'][] = [ + 'label' => __('Label size in px'), + 'arguments' => [ + 'name' => 'sizeLabel', + 'type' => 'number', + 'value' => $values['sizeLabel'], + 'return' => true, + 'min' => 5, + 'max' => 50, + ], + ]; + + $inputs['inputs']['row1'][] = [ + 'label' => __('Color label'), + 'arguments' => [ + 'wrapper' => 'div', + 'name' => 'colorLabel', + 'type' => 'color', + 'value' => $values['colorLabel'], + 'return' => true, + ], + ]; + + $inputs['inputs']['row2'][] = [ + 'label' => __('Show Value'), + 'arguments' => [ + 'name' => 'showValue', + 'id' => 'showValue', + 'type' => 'switch', + 'value' => $values['showValue'], + ], + ]; + + $inputs['inputs']['row2'][] = [ + 'label' => __('Format'), + 'arguments' => [ + 'name' => 'formatData', + 'id' => 'formatData', + 'type' => 'switch', + 'value' => $values['formatData'], + ], + ]; + + $inputs['inputs']['row2'][] = [ + 'label' => __('Value size in px'), + 'arguments' => [ + 'name' => 'sizeValue', + 'type' => 'number', + 'value' => $values['sizeValue'], + 'return' => true, + 'min' => 5, + 'max' => 50, + ], + ]; + + $inputs['inputs']['row2'][] = [ + 'label' => __('Color value'), + 'arguments' => [ + 'wrapper' => 'div', + 'name' => 'colorValue', + 'type' => 'color', + 'value' => $values['colorValue'], + 'return' => true, + ], + ]; + + return $inputs; + } + + + /** + * Get Post for widget. + * + * @return array + */ + public function getPost():array + { + // Retrieve global - common inputs. + $values = parent::getPost(); + $values['agentId'] = \get_parameter('agentId', 0); + $values['metaconsoleId'] = \get_parameter('metaconsoleId', 0); + $values['moduleId'] = \get_parameter('moduleId', 0); + $values['period'] = \get_parameter('period', 0); + $values['showLabel'] = \get_parameter_switch('showLabel'); + $values['showValue'] = \get_parameter_switch('showValue'); + $values['sizeLabel'] = \get_parameter('sizeLabel', 0); + $values['sizeValue'] = \get_parameter('sizeValue', 0); + $values['colorLabel'] = \get_parameter('colorLabel'); + $values['colorValue'] = \get_parameter('colorValue'); + $values['colorChart'] = \get_parameter('colorChart'); + $values['formatData'] = \get_parameter_switch('formatData'); + $values['label'] = \get_parameter('label'); + + return $values; + } + + + /** + * Draw widget. + * + * @return string; + */ + public function load() + { + global $config; + + $size = parent::getSize(); + hd($size, true); + + include_once $config['homedir'].'/include/functions_graph.php'; + include_once $config['homedir'].'/include/functions_agents.php'; + include_once $config['homedir'].'/include/functions_modules.php'; + + $module_name = \modules_get_agentmodule_name($this->values['moduleId']); + $alias = \modules_get_agentmodule_agent_alias($this->values['moduleId']); + + $title = $module_name; + if ($this->values['label'] === 'agent') { + $title = $alias; + } else if ($this->values['label'] === 'agent_module') { + $title = $alias.'/'.$module_name; + } + + $units_name = \modules_get_unit($this->values['moduleId']); + $value = \modules_get_last_value($this->values['moduleId']); + if (isset($this->values['formatData']) === true + && (bool) $this->values['formatData'] === true + ) { + $value = \format_for_graph( + $value, + $config['graph_precision'] + ); + } else { + $value = \sla_truncate( + $value, + $config['graph_precision'] + ); + } + + $color_status = \modules_get_color_status(modules_get_agentmodule_last_status($this->values['moduleId'])); + if ($color_status === COL_NORMAL) { + $color_status = $this->values['colorValue']; + } + + $params = [ + 'agent_module_id' => $this->values['moduleId'], + 'period' => $this->values['period'], + 'show_events' => false, + 'width' => '100%', + 'height' => $size['height'], + 'title' => $module_name, + 'unit' => $units_name, + 'only_image' => false, + 'menu' => false, + 'vconsole' => true, + 'return_img_base_64' => false, + 'show_legend' => false, + 'show_title' => false, + 'dashboard' => true, + 'backgroundColor' => 'transparent', + // 'server_id' => $metaconsoleId, + 'basic_chart' => true, + 'array_colors' => [ + [ + 'border' => '#000000', + 'color' => (isset($this->values['colorChart']) === true) ? $this->values['colorChart'] : $config['graph_color1'], + 'alpha' => CHART_DEFAULT_ALPHA, + ], + ], + ]; + + $output = '
'; + return $output; + } + + + /** + * Get description. + * + * @return string. + */ + public static function getDescription() + { + return __('Basic chart'); + } + + + /** + * Get Name. + * + * @return string. + */ + public static function getName() + { + return 'BasicChart'; + } + + + /** + * Get size Modal Configuration. + * + * @return array + */ + public function getSizeModalConfiguration(): array + { + $size = [ + 'width' => 850, + 'height' => 430, + ]; + + return $size; + } + + +} diff --git a/pandora_console/include/styles/dashboards.css b/pandora_console/include/styles/dashboards.css index 360ba2190f..08f43f9d38 100644 --- a/pandora_console/include/styles/dashboards.css +++ b/pandora_console/include/styles/dashboards.css @@ -859,10 +859,6 @@ div.widget-mrgn-0px > div.parent_graph { margin: 0px; } -div.widget-mrgn-0px > div.parent_graph > div[id^="graph_"] { - width: calc(100% + 14px) !important; -} - div.content-widget > div.container-center > div.centered.w90p @@ -870,3 +866,19 @@ div.content-widget > canvas { height: 310px; } + +div.basic-chart-title { + position: absolute; + width: calc(100% - 20px); + margin: 10px; + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; + align-content: center; +} + +div.basic-chart-title span { + font-size: 20px; + font-weight: bold; +}