From 014441a2daa8a7f39deedfbaac9cc3a0723c07ec Mon Sep 17 00:00:00 2001 From: Daniel Barbero Date: Thu, 6 Oct 2022 17:13:45 +0200 Subject: [PATCH] new widget grouped meter graphs pandora_enterprise#8620 --- .../Dashboard/Widgets/GroupedMeterGraphs.php | 224 ++++++++++++++---- 1 file changed, 176 insertions(+), 48 deletions(-) diff --git a/pandora_console/include/lib/Dashboard/Widgets/GroupedMeterGraphs.php b/pandora_console/include/lib/Dashboard/Widgets/GroupedMeterGraphs.php index 9ba51eeefb..b88414d623 100644 --- a/pandora_console/include/lib/Dashboard/Widgets/GroupedMeterGraphs.php +++ b/pandora_console/include/lib/Dashboard/Widgets/GroupedMeterGraphs.php @@ -42,6 +42,7 @@ class GroupedMeterGraphs extends Widget private const STATUS_WARNING = 'warning'; private const RATIO_WITH_BOX = 20.1518; private const MAX_MODULES = 20; + private const MAX_INCREASE = 0.10; /** * Name widget. @@ -257,6 +258,16 @@ class GroupedMeterGraphs extends Widget $values['label'] = $decoder['label']; } + $values['min_value'] = null; + if (isset($decoder['min_value']) === true) { + $values['min_value'] = $decoder['min_value']; + } + + $values['max_value'] = null; + if (isset($decoder['max_value']) === true) { + $values['max_value'] = $decoder['max_value']; + } + $values['min_critical'] = null; if (isset($decoder['min_critical']) === true) { $values['min_critical'] = $decoder['min_critical']; @@ -336,6 +347,36 @@ class GroupedMeterGraphs extends Widget ], ]; + $inputs['inputs']['row1'][] = [ + 'class' => 'dashboard-input-threshold', + 'direct' => 1, + 'block_content' => [ + [ + 'label' => __('Values'), + 'arguments' => [], + ], + [ + 'label' => __('Min'), + 'arguments' => [ + 'name' => 'min_value', + 'id' => 'min_value', + 'type' => 'number', + 'value' => $values['min_value'], + ], + ], + [ + 'label' => __('Max'), + 'arguments' => [ + 'name' => 'max_value', + 'id' => 'max_value', + 'type' => 'number', + 'value' => $values['max_value'], + ], + ], + ], + + ]; + $inputs['inputs']['row1'][] = [ 'class' => 'dashboard-input-threshold', 'direct' => 1, @@ -478,6 +519,9 @@ class GroupedMeterGraphs extends Widget $values['label'] = \get_parameter('label', 'module'); + $values['min_value'] = \get_parameter('min_value', null); + $values['max_value'] = \get_parameter('max_value', null); + $values['min_critical'] = \get_parameter('min_critical', null); $values['max_critical'] = \get_parameter('max_critical', null); $values['min_warning'] = \get_parameter('min_warning', null); @@ -553,41 +597,75 @@ class GroupedMeterGraphs extends Widget return $output; } - $moduleData = array_map( - function ($module) { - return ($module['data'] ?? 0); - }, - $modules - ); + $max = null; + $min = null; + // Dinamic treshold. + if ($this->values['min_critical'] !== null + || $this->values['max_critical'] !== null + || $this->values['min_warning'] !== null + || $this->values['max_warning'] !== null + ) { + if ($this->values['max_value'] === null || $this->values['min_value'] === null) { + $tresholdData = [ + ($this->values['min_critical'] ?? 0), + ($this->values['max_critical'] ?? 0), + ($this->values['min_warning'] ?? 0), + ($this->values['max_warning'] ?? 0), + ]; - $tresholdData = [ - ($this->values['min_critical'] ?? 0), - ($this->values['max_critical'] ?? 0), - ($this->values['min_warning'] ?? 0), - ($this->values['max_warning'] ?? 0), - ]; + $moduleData = array_map( + function ($module) { + return ($module['data'] ?? 0); + }, + $modules + ); + } - $max = max( - array_merge( - $moduleData, - $tresholdData - ) - ); + if ($this->values['max_value'] === null) { + $max = max( + array_merge( + $moduleData, + $tresholdData + ) + ); + } else { + $max = $this->values['max_value']; + } - $min = min( - array_merge( - $moduleData, - $tresholdData - ) - ); + // Increases max. + if ($this->values['max_critical'] === null && $this->values['max_value'] === null) { + $max_increase = ($max * self::MAX_INCREASE); + $max = ($max + $max_increase); + } - $this->thresholds = $this->calculateThreshold($max, $min); + if ($this->values['min_value'] === null) { + $min = min( + array_merge( + $moduleData, + $tresholdData + ) + ); + } else { + $min = $this->values['min_value']; + } - $output .= '
'; + $tresholds_array = [ + 'min_critical' => $this->values['min_critical'], + 'max_critical' => $this->values['max_critical'], + 'min_warning' => $this->values['min_warning'], + 'max_warning' => $this->values['max_warning'], + ]; + + $this->thresholds = $this->calculateThreshold($max, $min, $tresholds_array); + } + + $style = 'color:'.$this->values['fontColor'].';'; + $output .= '
'; foreach ($modules as $module) { $output .= $this->drawRowModule( $module, - $max + $max, + $min ); } @@ -658,18 +736,63 @@ class GroupedMeterGraphs extends Widget /** * Draw info module. * - * @param array $data Data module. - * @param float $max Value max. + * @param array $data Data module. + * @param null|float $max Value max. + * @param null|float $min Value min. * * @return string */ private function drawRowModule( array $data, - float $max + ?float $max, + ?float $min ):string { global $config; - $module_data = $this->getBoxPercentageMaths($max, $data['data']); + // Dinamic. + if ($max === null && $min === null) { + $all_values_module = [ + ($data['w_min'] ?? 0), + ($data['w_max'] ?? 0), + ($data['c_min'] ?? 0), + ($data['c_max'] ?? 0), + ($data['data'] ?? 0), + ]; + + if ($this->values['max_value'] === null) { + $max = max($all_values_module); + } else { + $max = $this->values['max_value']; + } + + // Increases max. + if (empty($data['c_max']) === true + && $this->values['max_value'] === null + ) { + $max_increase = ($max * self::MAX_INCREASE); + $max = ($max + $max_increase); + } + + $min = 0; + if ($this->values['min_value'] !== null) { + $min = $this->values['min_value']; + } + + $thresholds_array = [ + 'min_critical' => (empty($data['c_min']) === true) ? null : $data['c_min'], + 'max_critical' => (empty($data['c_max']) === true) ? null : $data['c_max'], + 'min_warning' => (empty($data['w_min']) === true) ? null : $data['w_min'], + 'max_warning' => (empty($data['w_max']) === true) ? null : $data['w_max'], + ]; + + $this->thresholds = $this->calculateThreshold( + $max, + $min, + $thresholds_array + ); + } + + $module_data = $this->getBoxPercentageMaths($max, $min, $data['data']); $output = ''; $output .= '
'; @@ -765,41 +888,42 @@ class GroupedMeterGraphs extends Widget /** * Get tresholds. * - * @param float $max Value max. - * @param float $min Value min. + * @param float $max Value max. + * @param float $min Value min. + * @param array $thresholds_array Array thresholds. * * @return array Array threshold. */ - private function calculateThreshold(float $max, float $min) + private function calculateThreshold(float $max, float $min, array $thresholds_array) { $nMax = null; - if ($this->values['min_warning'] !== null) { - $nMax = $this->getBoxPercentageMaths($max, $this->values['min_warning']); + if ($thresholds_array['min_warning'] !== null) { + $nMax = $this->getBoxPercentageMaths($max, $min, $thresholds_array['min_warning']); } $wMin = null; - if ($this->values['min_warning'] !== null) { - $wMin = $this->getBoxPercentageMaths($max, $this->values['min_warning']); + if ($thresholds_array['min_warning'] !== null) { + $wMin = $this->getBoxPercentageMaths($max, $min, $thresholds_array['min_warning']); } $wMax = null; - if ($this->values['max_warning'] !== null) { - $wMax = $this->getBoxPercentageMaths($max, $this->values['max_warning']); + if ($thresholds_array['max_warning'] !== null) { + $wMax = $this->getBoxPercentageMaths($max, $min, $thresholds_array['max_warning']); } $cMin = null; - if ($this->values['min_critical'] !== null) { - $cMin = $this->getBoxPercentageMaths($max, $this->values['min_critical']); + if ($thresholds_array['min_critical'] !== null) { + $cMin = $this->getBoxPercentageMaths($max, $min, $thresholds_array['min_critical']); } $cMax = null; - if ($this->values['max_critical'] !== null) { - $cMax = $this->getBoxPercentageMaths($max, $this->values['max_critical']); + if ($thresholds_array['max_critical'] !== null) { + $cMax = $this->getBoxPercentageMaths($max, $min, $thresholds_array['max_critical']); } $thresholds = [ 'normal' => [ - 'min' => $min, + 'min' => $this->getBoxPercentageMaths($max, $min, $min), 'max' => $nMax, ], 'warning' => [ @@ -820,13 +944,17 @@ class GroupedMeterGraphs extends Widget * Get porcentage. * * @param float $max Maximum. + * @param float $min Minimum. * @param float $value Value. * * @return float */ - private function getBoxPercentageMaths(float $max, float $value):float - { - return (($value / $max) * $this->boxNumber); + private function getBoxPercentageMaths( + float $max, + float $min, + float $value + ):float { + return (((($value - $min) / ($max - $min))) * $this->boxNumber); }