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 = __('Block histogram'); // Name. if (empty($this->name) === true) { $this->name = 'single_graph'; } // This forces at least a first configuration. $this->configurationRequired = false; if (empty($this->values['moduleBlockHistogram']) === true) { $this->configurationRequired = 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); $values['agentsBlockHistogram'] = []; if (isset($decoder['agentsBlockHistogram']) === true) { if (isset($decoder['agentsBlockHistogram'][0]) === true && empty($decoder['agentsBlockHistogram']) === false ) { $values['agentsBlockHistogram'] = explode( ',', $decoder['agentsBlockHistogram'][0] ); } } if (isset($decoder['selectionBlockHistogram']) === true) { $values['selectionBlockHistogram'] = $decoder['selectionBlockHistogram']; } $values['moduleBlockHistogram'] = []; if (isset($decoder['moduleBlockHistogram']) === true) { if (empty($decoder['moduleBlockHistogram']) === false) { $values['moduleBlockHistogram'] = $decoder['moduleBlockHistogram']; } } if (isset($decoder['period']) === true) { $values['period'] = $decoder['period']; } if (isset($decoder['fontColor']) === true) { $values['fontColor'] = $decoder['fontColor']; } $values['label'] = 'module'; 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 { $values = $this->values; // Retrieve global - common inputs. $inputs = parent::getFormInputs(); // Default values. if (isset($values['period']) === false) { $values['period'] = SECONDS_1DAY; } if (empty($values['fontColor']) === true) { $values['fontColor'] = '#2c3e50'; } $inputs[] = [ 'label' => __('Font color'), 'arguments' => [ 'wrapper' => 'div', 'name' => 'fontColor', 'type' => 'color', 'value' => $values['fontColor'], 'return' => true, ], ]; // Type Label. $fields = [ 'module' => __('Module'), 'agent' => __('Agent'), 'agent_module' => __('Agent / module'), ]; $inputs[] = [ 'label' => __('Label'), 'arguments' => [ 'type' => 'select', 'fields' => $fields, 'name' => 'label', 'selected' => $values['label'], 'return' => true, ], ]; // Periodicity. $inputs[] = [ 'label' => __('Interval'), 'arguments' => [ 'name' => 'period', 'type' => 'interval', 'value' => $values['period'], 'nothing' => __('None'), 'nothing_value' => 0, 'style_icon' => 'flex-grow: 0', 'script' => 'check_period_warning(this, \''.__('Warning').'\', \''.__('Displaying items with extended historical data can have an impact on system performance. We do not recommend that you use intervals longer than 30 days, especially if you combine several of them in a report, dashboard or visual console.').'\')', 'script_input' => 'check_period_warning_manual(\'period\', \''.__('Warning').'\', \''.__('Displaying items with extended historical data can have an impact on system performance. We do not recommend that you use intervals longer than 30 days, especially if you combine several of them in a report, dashboard or visual console.').'\')', ], ]; $inputs[] = [ 'arguments' => [ 'type' => 'select_multiple_modules_filtered_select2', 'agent_values' => agents_get_agents_selected(0), 'agent_name' => 'agentsBlockHistogram[]', 'agent_ids' => $values['agentsBlockHistogram'], 'selectionModules' => $values['selectionBlockHistogram'], 'selectionModulesNameId' => 'selectionBlockHistogram', 'modules_ids' => $values['moduleBlockHistogram'], 'modules_name' => 'moduleBlockHistogram[]', ], ]; return $inputs; } /** * Get Post for widget. * * @return array */ public function getPost():array { // Retrieve global - common inputs. $values = parent::getPost(); $values['agentsBlockHistogram'] = \get_parameter( 'agentsBlockHistogram', [] ); $values['selectionBlockHistogram'] = \get_parameter( 'selectionBlockHistogram', 0 ); $values['moduleBlockHistogram'] = \get_parameter( 'moduleBlockHistogram' ); $agColor = []; if (isset($values['agentsBlockHistogram'][0]) === true && empty($values['agentsBlockHistogram'][0]) === false ) { $agColor = explode(',', $values['agentsBlockHistogram'][0]); } $agModule = []; if (isset($values['moduleBlockHistogram'][0]) === true && empty($values['moduleBlockHistogram'][0]) === false ) { $agModule = explode(',', $values['moduleBlockHistogram'][0]); } $values['moduleBlockHistogram'] = get_same_modules_all( $agColor, $agModule ); $values['period'] = \get_parameter('period', 0); $values['fontColor'] = \get_parameter('fontColor', '#2c3e50'); $values['label'] = \get_parameter('label', 'agent'); return $values; } /** * Draw widget. * * @return string; */ public function load() { global $config; $size = parent::getSize(); $output = ''; if (is_metaconsole() === true) { $modules_nodes = array_reduce( $this->values['moduleBlockHistogram'], function ($carry, $item) { $explode = explode('|', $item); $carry[$explode[0]][] = $explode[1]; return $carry; }, [] ); $modules = []; foreach ($modules_nodes as $n => $mod) { try { $node = new Node((int) $n); $node->connect(); $node_mods = $this->getInfoModules($mod); if (empty($node_mods) === false) { foreach ($node_mods as $value) { $value['id_node'] = $n; $value['server_name'] = $node->toArray()['server_name']; $modules[] = $value; } } $node->disconnect(); } catch (\Exception $e) { // Unexistent agent. $node->disconnect(); } } } else { $modules = $this->getInfoModules( $this->values['moduleBlockHistogram'] ); } if ($modules !== false && empty($modules) === false) { $total_modules = count($modules); $output .= '
'; $output .= ''; foreach ($modules as $key => $module) { $last = false; if (($total_modules - 1) === $key) { $last = true; } if (is_metaconsole() === true) { try { $node = new Node((int) $module['id_node']); $node->connect(); $output .= $this->drawHistograms($module, $last); $node->disconnect(); } catch (\Exception $e) { // Unexistent agent. $node->disconnect(); } } else { $output .= $this->drawHistograms($module, $last); } } $output .= '
'; $output .= '
'; } else { $output .= '
'; $output .= \ui_print_info_message( __('Not found modules'), '', true ); $output .= '
'; } return $output; } /** * Get info modules. * * @param array $modules Modules. * * @return array Data. */ private function getInfoModules(array $modules): array { $where = sprintf( 'tagente_modulo.id_agente_modulo IN (%s) AND tagente_modulo.delete_pending = 0', implode(',', $modules) ); $sql = sprintf( 'SELECT tagente_modulo.id_agente_modulo AS `id`, tagente_modulo.nombre AS `name`, tagente_modulo.unit AS `unit`, tagente.alias AS `agent_alias`, tagente.id_agente AS `agent_id` FROM tagente_modulo INNER JOIN tagente ON tagente_modulo.id_agente = tagente.id_agente WHERE %s', $where ); $modules = db_get_all_rows_sql($sql); if ($modules === false) { $modules = []; } return $modules; } /** * Draw histogram module. * * @param array $data Info module. * @param boolean $last Last histogram. * * @return string */ private function drawHistograms(array $data, bool $last):string { global $config; $size = parent::getSize(); // Desactive scroll bars only this item. $id_agent = $data['agent_id']; $id_module = $data['id']; $period = $this->values['period']; switch ($this->values['label']) { case 'module': $label = ui_print_truncate_text( $data['name'], 25, false, true, true, '[…]', '' ); break; case 'agent_module': $label = ui_print_truncate_text( $data['agent_alias'].' / '.$data['name'], 25, false, true, true ); break; default: case 'agent': $label = ui_print_truncate_text( $data['agent_alias'], 25, false, true, true, '[…]', '' ); break; } $size_label = 10; $id_group = \agents_get_agent_group($id_agent); $height_graph = 30; if ($last === true) { if ($period > 86500) { $height_graph = 60; } else { $height_graph = 50; } } $content = [ 'id_agent_module' => $id_module, 'period' => $period, 'time_from' => '00:00:00', 'time_to' => '00:00:00', 'id_group' => $id_group, 'sizeForTicks' => ($size['width'] - 200), 'showLabelTicks' => ($last === true) ? true : false, 'height_graph' => $height_graph, [ ['id_agent_module' => $id_module], ] ]; $graph = \reporting_module_histogram_graph( ['datetime' => time()], $content ); $style = 'min-width:200px;'; if ($last === false) { if ($period > 86500) { $style .= 'width:calc(100% - 24px); margin-left: 12px;'; } else { $style .= 'width:calc(100% - 16px); margin-left: 8px;'; } } else { $style .= 'height:60px;'; } $st = 'font-size:'.$size_label.'px;'; if (is_metaconsole() === false) { $st .= 'height: 28px;'; } $output = ''; $output .= ''; $output .= '
'; $output .= $label; $output .= '
'; $output .= ''; $output .= ''; $output .= '
'; $output .= $graph['chart']; $output .= '
'; $output .= ''; $output .= ''; return $output; } /** * Get description. * * @return string. */ public static function getDescription() { return __('Block histogram'); } /** * Get Name. * * @return string. */ public static function getName() { return 'BlockHistogram'; } /** * Get size Modal Configuration. * * @return array */ public function getSizeModalConfiguration(): array { $size = [ 'width' => (is_metaconsole() === true) ? 700 : 500, 'height' => 670, ]; return $size; } }