From b439ed684ee04ea181a751aca841dfe864e4ae2b Mon Sep 17 00:00:00 2001 From: Pablo Aragon Date: Fri, 10 Feb 2023 10:31:41 +0100 Subject: [PATCH] 9859-Widget AvgSumMaxMinModule --- .../images/widgets/AvgSumMaxMinModule.png | Bin 0 -> 5984 bytes .../include/lib/Dashboard/Widget.php | 1 + .../Dashboard/Widgets/AvgSumMaxMinModule.php | 701 ++++++++++++++++++ 3 files changed, 702 insertions(+) create mode 100644 pandora_console/images/widgets/AvgSumMaxMinModule.png create mode 100644 pandora_console/include/lib/Dashboard/Widgets/AvgSumMaxMinModule.php diff --git a/pandora_console/images/widgets/AvgSumMaxMinModule.png b/pandora_console/images/widgets/AvgSumMaxMinModule.png new file mode 100644 index 0000000000000000000000000000000000000000..19e980f38dda95f64625f15bcbda86269fb91cf0 GIT binary patch literal 5984 zcmV-m7oX^fP)guYlu6{FpRP`{R5z^LYHU^Hb=W>G!Z0!KJtSeTP|0isO3c1!^*62EBsb^vQ9q2EvWRKbqA_&u+J=_HscXEL z-nnI43}$R@u-C#wk9&JD3HP>!KQ*ce=g*ykzaGJ;h(rNZQBYQzS*!A`aY5*#TU|I zB?rqu(22nnkffLxN(jT)D#62&uUPVRpN{ zaO6MV6^fZ<8+L@37L?uSV*(zP9G@kWl7CB=E(K?3OQEYN%q!&zm5{=sjLi0Krz?a~ z(~3z8eqWTPM(lSg7mD19;w&Qp{Oj)SR<6spkSskpx zD5sTpHzV}ekjHzA3^+1Y&*YX5SeMfYd$#WsdQsF94ps~}SSL+*PDnrd@@yei{u3)n z2kXl8PvBw^4~1oNbi)DL@9uKW@@nNd*(m1*J}f{NuUrGK%wC~|FTlj+Mbsd@zws*p zIFSt0y?ggUr$#cGpc=5aQ<61StS``(SZ5ODJe`8*nJ)Wi++T|25 z`?ZlfjvYHzgXR~eZu9D` zW(G!yOL4_thMCq$E1FHSj#}BNi7~kXBPA1jK;!e)46#xUfWxQy^>Fo4jytYjQe3?T zO-d*XW0huif4e%XTf$HQNBY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$4`J_^`)&8Of9s}05EVxkQhAHnD8OH}0klJpcj zG`M`0i(k8}%)u|pvFdE(ONSt@mFeqX*b{?fWH^q4mtUF)FHK(r&kr96GL%PwY=AymSQU`SXA+vTxIQ5`$M+=WY3IKO890x6Ft$Q0AET@HiTed87oinu#-Gt7TebxAcXuGFz#rqZ4y#Lw09d$V*=n7d-YHuY7_ z@&o(#!N?I$0$w8_4Z$@+gby9u-mE(HYz(6VJqK_B?t+`4t^TKX7D zWmTk4%E5~oGBY!Kkq%<0=e<-=ymA0XHNAR(rZM(cz)8S0vRiQE$PwXND0mYF%d(o^ z8}_w5X6hq<(kFaGod!pJmC>i)Ki;4bRx{$``1p7ZFMJ7fpi0J=rtuMW#~pVFzm%p; zn+j?ASfe-;UphGG0HLuz+Kzp2AL$G16SKFAc;f4qDj* z&wig2@2Sz1sqEs#i*9nY5`%YnMAWYP@uN>ZH{Tb7LaA(iQI3>gpwswWsy}`|`J&d9 zGg`dydsU5^wwMv9t7x=%`U^(xA0ft{RnR<&TyEsAKB$y*P%q4)kG^g1W^CfUL6xDU zU#(x#{nW98FBBISH7D%})Z@_|5)yi9!SYyt~-WgalnB_i0xewidth = $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 = __('Avg|Sum|Max|Min Module Data'); + + // Name. + if (empty($this->name) === true) { + $this->name = 'AvgSumMaxMinModule'; + } + + // 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['label_'.$this->cellId]) === true) { + $values['label'] = $decoder['label_'.$this->cellId]; + } + + if (isset($decoder['label']) === true) { + $values['label'] = $decoder['label']; + } + + if (isset($decoder['id_agent_'.$this->cellId]) === true) { + $values['agentId'] = $decoder['id_agent_'.$this->cellId]; + } + + if (isset($decoder['type_'.$this->cellId]) === true) { + $values['type'] = $decoder['type_'.$this->cellId]; + } + + if (isset($decoder['type']) === true) { + $values['type'] = $decoder['type']; + } + + if (isset($decoder['period_'.$this->cellId]) === true) { + $values['period'] = $decoder['period_'.$this->cellId]; + } + + if (isset($decoder['period']) === true) { + $values['period'] = $decoder['period']; + } + + 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['size_value_'.$this->cellId]) === true) { + $values['sizeValue'] = $decoder['size_value_'.$this->cellId]; + } + + if (isset($decoder['sizeValue']) === true) { + $values['sizeValue'] = $decoder['sizeValue']; + } + + if (isset($decoder['size_label_'.$this->cellId]) === true) { + $values['sizeLabel'] = $decoder['size_label_'.$this->cellId]; + } + + if (isset($decoder['sizeLabel']) === true) { + $values['sizeLabel'] = $decoder['sizeLabel']; + } + + if (isset($decoder['text_color_'.$this->cellId]) === true) { + $values['text_color'] = $decoder['text_color_'.$this->cellId]; + } + + if (isset($decoder['text_color']) === true) { + $values['text_color'] = $decoder['text_color']; + } + + if (isset($decoder['unit_'.$this->cellId]) === true) { + $values['unit'] = $decoder['unit_'.$this->cellId]; + } + + if (isset($decoder['unit']) === true) { + $values['unit'] = $decoder['unit']; + } + + 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['sizeLabel']) === false) { + $values['sizeLabel'] = 20; + } + + if (isset($values['sizeValue']) === false) { + $values['sizeValue'] = 20; + } + + // Retrieve global - common inputs. + $inputs = parent::getFormInputs(); + + // Label. + $inputs[] = [ + 'label' => __('Label'), + 'arguments' => [ + 'name' => 'label', + 'type' => 'text', + 'value' => $values['label'], + 'return' => true, + 'size' => 0, + ], + ]; + + // Type. + $type_fields = []; + $type_fields[0] = 'AVG'; + $type_fields[1] = 'SUM'; + $type_fields[2] = 'MAX'; + $type_fields[3] = 'MIN'; + $type_selected = explode(',', $values['type']); + + (isset($values['type']) === false) ? $type_selected = 0 : ''; + + $inputs[] = [ + 'label' => __('Type'), + 'arguments' => [ + 'name' => 'type', + 'type' => 'select', + 'fields' => $type_fields, + 'selected' => $type_selected, + 'return' => true, + 'required' => true, + 'select2_enable' => false, + 'sort' => false, + ], + ]; + + // Period. + $period_fields = []; + $period_fields[0] = __('Last 30 days'); + $period_fields[1] = __('This month'); + $period_fields[2] = __('Last 7 days'); + $period_fields[3] = __('This week'); + $period_fields[4] = __('Last 24 hrs'); + $period_fields[5] = __('Today'); + $period_selected = explode(',', $values['period']); + + (isset($values['period']) === false) ? $period_selected = 0 : ''; + + $inputs[] = [ + 'label' => __('Time period'), + 'arguments' => [ + 'name' => 'period', + 'type' => 'select', + 'fields' => $period_fields, + 'selected' => $period_selected, + 'return' => true, + 'required' => true, + 'select2_enable' => false, + 'sort' => false, + ], + ]; + + // Autocomplete agents. + $inputs[] = [ + '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[] = [ + '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;', + 'filter_modules' => (users_access_to_agent($values['agentId']) === false) ? [$values['moduleId']] : [], + 'nothing' => __('None'), + 'nothing_value' => 0, + ], + ]; + + // Text size of value in px. + $inputs[] = [ + 'label' => __('Text size of value in px'), + 'arguments' => [ + 'name' => 'sizeValue', + 'type' => 'number', + 'value' => $values['sizeValue'], + 'return' => true, + 'min' => 0, + ], + ]; + + // Text size of label in px. + $inputs[] = [ + 'label' => __('Text size of label in px'), + 'arguments' => [ + 'name' => 'sizeLabel', + 'type' => 'number', + 'value' => $values['sizeLabel'], + 'return' => true, + 'min' => 0, + ], + ]; + + // Text color. + if (empty($values['text_color']) === true) { + $values['text_color'] = '#000000'; + + if ($config['style'] === 'pandora_black' + && is_metaconsole() === false + ) { + $values['text_color'] = '#eeeeee'; + } + } + + $inputs[] = [ + 'label' => __('Text color'), + 'arguments' => [ + 'wrapper' => 'div', + 'name' => 'text_color', + 'type' => 'color', + 'value' => $values['text_color'], + 'return' => true, + ], + ]; + + // Unit. + $inputs[] = [ + 'label' => __('Unit'), + 'arguments' => [ + 'wrapper' => 'div', + 'name' => 'unit', + 'type' => 'switch', + 'value' => $values['unit'], + 'return' => true, + ], + ]; + + return $inputs; + } + + + /** + * Get Post for widget. + * + * @return array + */ + public function getPost():array + { + // Retrieve global - common inputs. + $values = parent::getPost(); + + $values['label'] = \get_parameter('label', ''); + $values['type'] = \get_parameter('type', 0); + $values['period'] = \get_parameter('period', 0); + $values['agentId'] = \get_parameter('agentId', 0); + $values['metaconsoleId'] = \get_parameter('metaconsoleId', 0); + $values['moduleId'] = \get_parameter('moduleId', 0); + $values['sizeValue'] = \get_parameter('sizeValue', 0); + $values['sizeLabel'] = \get_parameter_switch('sizeLabel'); + $values['text_color'] = \get_parameter('text_color', 0); + $values['unit'] = \get_parameter_switch('unit'); + + return $values; + } + + + /** + * Draw widget. + * + * @return string; + */ + public function load() + { + global $config; + + $output = ''; + $text_color = 'color:'.$this->values['text_color'].' !important;'; + + $id_module = $this->values['moduleId']; + + $to = 0; + $now = time(); + + switch ((int) $this->values['period']) { + case 0: + $to = strtotime('-30 days'); + break; + + case 1: + $to = strtotime(date('Y-m-01 00:00:00')); + break; + + case 2: + $to = strtotime('-7 days'); + break; + + case 3: + $to = strtotime('last Monday'); + break; + + case 4: + $to = strtotime('-1 days'); + break; + + case 5: + $to = strtotime(date('Y-m-d 00:00:00')); + break; + + default: + $to = 0; + break; + } + + $data = 0; + switch ((int) $this->values['type']) { + case 0: + $rows = modules_get_raw_data($id_module, $to, $now); + $count = (int) count($rows); + $sum = 0; + foreach ($rows as $row) { + $sum += (int) $row['datos']; + } + + $data = ($sum / $count); + break; + + case 1: + $rows = modules_get_raw_data($id_module, $to, $now); + $sum = 0; + foreach ($rows as $row) { + $sum += (int) $row['datos']; + } + + $data = $sum; + break; + + case 2: + $rows = modules_get_min_max_data($id_module, $to); + + $data = $rows[0]['max']; + break; + + case 3: + $rows = modules_get_min_max_data($id_module, $to); + + $data = $rows[0]['min']; + break; + + default: + $data = 0; + break; + } + + $label = $this->values['label']; + $sizeLabel = (isset($this->values['sizeLabel']) === true) ? $this->values['sizeLabel'] : 40; + $sizeValue = (isset($this->values['sizeValue']) === true) ? $this->values['sizeValue'] : 40; + + $output .= '
'; + // General div. + $output .= '
'; + // Div value. + $output .= '
'; + + if (is_numeric($data) === true) { + $dataDatos = remove_right_zeros( + number_format( + $data, + $config['graph_precision'], + $config['decimal_separator'], + $config['thousand_separator'] + ) + ); + } else { + $dataDatos = trim($data); + } + + $unit = ''; + if (empty($this->values['unit']) === false) { + $unit = modules_get_unit($id_module); + } + + $output .= $dataDatos.' '.$unit; + + $output .= '
'; + + if (empty($label) === false) { + // Div Label. + $output .= '
'.$label.'
'; + } + + $output .= '
'; + $output .= '
'; + return $output; + } + + + /** + * Get description. + * + * @return string. + */ + public static function getDescription() + { + return __('Avg|Sum|Max|Min Module Data'); + } + + + /** + * Get Name. + * + * @return string. + */ + public static function getName() + { + return 'AvgSumMaxMinModule'; + } + + + /** + * Get size Modal Configuration. + * + * @return array + */ + public function getSizeModalConfiguration(): array + { + $size = [ + 'width' => 550, + 'height' => 710, + ]; + + return $size; + } + + +}