diff options
Diffstat (limited to 'ui/include/classes/widgets/fields/CWidgetFieldGraphDataSet.php')
-rw-r--r-- | ui/include/classes/widgets/fields/CWidgetFieldGraphDataSet.php | 160 |
1 files changed, 82 insertions, 78 deletions
diff --git a/ui/include/classes/widgets/fields/CWidgetFieldGraphDataSet.php b/ui/include/classes/widgets/fields/CWidgetFieldGraphDataSet.php index b65418b16f5..1ff879942f8 100644 --- a/ui/include/classes/widgets/fields/CWidgetFieldGraphDataSet.php +++ b/ui/include/classes/widgets/fields/CWidgetFieldGraphDataSet.php @@ -1,4 +1,4 @@ -<?php +<?php declare(strict_types = 0); /* ** Zabbix ** Copyright (C) 2001-2022 Zabbix SIA @@ -19,61 +19,65 @@ **/ +namespace Zabbix\Widgets\Fields; + +use API, + CApiInputValidator; + +use Zabbix\Widgets\CWidgetField; + /** * Class for data set widget field used in Graph widget configuration Data set tab. */ class CWidgetFieldGraphDataSet extends CWidgetField { + public const DEFAULT_VALUE = []; + + public const DATASET_TYPE_SINGLE_ITEM = 0; + public const DATASET_TYPE_PATTERN_ITEM = 1; + // Predefined colors for data-sets in JSON format. Each next data set takes next sequential value from palette. - const DEFAULT_COLOR_PALETTE = ["FF465C","B0AF07","0EC9AC","524BBC","ED1248","D1E754","2AB5FF","385CC7","EC1594","BAE37D","6AC8FF","EE2B29","3CA20D","6F4BBC","00A1FF","F3601B","1CAE59","45CFDB","894BBC","6D6D6D"]; + public const DEFAULT_COLOR_PALETTE = [ + 'FF465C', 'B0AF07', '0EC9AC', '524BBC', 'ED1248', 'D1E754', '2AB5FF', '385CC7', 'EC1594', 'BAE37D', + '6AC8FF', 'EE2B29', '3CA20D', '6F4BBC', '00A1FF', 'F3601B', '1CAE59', '45CFDB', '894BBC', '6D6D6D' + ]; // First color from the default color palette. - const DEFAULT_COLOR = 'FF465C'; - - /** - * Create widget field for Data set selection. - * - * @param string $name Field name in form. - * @param string $label Label for the field in form. - */ - public function __construct($name, $label) { + private const DEFAULT_COLOR = 'FF465C'; + + public function __construct(string $name, string $label = null) { parent::__construct($name, $label); - $this->setSaveType(ZBX_WIDGET_FIELD_TYPE_STR); - $this->setValidationRules(['type' => API_OBJECTS, 'fields' => [ - 'dataset_type' => ['type' => API_INT32, 'in' => implode(',', [CWidgetHelper::DATASET_TYPE_SINGLE_ITEM, CWidgetHelper::DATASET_TYPE_PATTERN_ITEM])], - 'hosts' => ['type' => API_STRINGS_UTF8, 'flags' => null], - 'items' => ['type' => API_STRINGS_UTF8, 'flags' => null], - 'itemids' => ['type' => API_IDS, 'flags' => null], - 'color' => ['type' => API_COLOR, 'flags' => API_REQUIRED | API_NOT_EMPTY], - 'type' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => implode(',', [SVG_GRAPH_TYPE_LINE, SVG_GRAPH_TYPE_POINTS, SVG_GRAPH_TYPE_STAIRCASE, SVG_GRAPH_TYPE_BAR])], - 'stacked' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => implode(',', [SVG_GRAPH_STACKED_OFF, SVG_GRAPH_STACKED_ON])], - 'width' => ['type' => API_INT32, 'in' => implode(',', range(0, 10))], - 'pointsize' => ['type' => API_INT32, 'in' => implode(',', range(1, 10))], - 'transparency' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => implode(',', range(0, 10))], - 'fill' => ['type' => API_INT32, 'in' => implode(',', range(0, 10))], - 'missingdatafunc' => ['type' => API_INT32, 'in' => implode(',', [SVG_GRAPH_MISSING_DATA_NONE, SVG_GRAPH_MISSING_DATA_CONNECTED, SVG_GRAPH_MISSING_DATA_TREAT_AS_ZERO, SVG_GRAPH_MISSING_DATA_LAST_KNOWN])], - 'axisy' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => implode(',', [GRAPH_YAXIS_SIDE_LEFT, GRAPH_YAXIS_SIDE_RIGHT])], - 'timeshift' => ['type' => API_TIME_UNIT, 'flags' => API_REQUIRED, 'in' => implode(':', [ZBX_MIN_TIMESHIFT, ZBX_MAX_TIMESHIFT])], - 'aggregate_function' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => implode(',', [AGGREGATE_NONE, AGGREGATE_MIN, AGGREGATE_MAX, AGGREGATE_AVG, AGGREGATE_COUNT, AGGREGATE_SUM, AGGREGATE_FIRST, AGGREGATE_LAST])], - 'aggregate_interval' => ['type' => API_MULTIPLE, 'rules' => [ - ['if' => ['field' => 'aggregate_function', 'in' => implode(',', [AGGREGATE_MIN, AGGREGATE_MAX, AGGREGATE_AVG, AGGREGATE_COUNT, AGGREGATE_SUM, AGGREGATE_FIRST, AGGREGATE_LAST])], - 'type' => API_TIME_UNIT, 'flags' => API_REQUIRED | API_NOT_EMPTY | API_TIME_UNIT_WITH_YEAR, 'in' => implode(':', [1, ZBX_MAX_TIMESHIFT])], - ['else' => true, 'type' => API_STRING_UTF8, 'in' => GRAPH_AGGREGATE_DEFAULT_INTERVAL] - ]], - 'aggregate_grouping' => ['type' => API_INT32, 'in' => implode(',', [GRAPH_AGGREGATE_BY_ITEM, GRAPH_AGGREGATE_BY_DATASET])], - 'approximation' => ['type' => API_INT32, 'in' => implode(',', [APPROXIMATION_MIN, APPROXIMATION_AVG, APPROXIMATION_MAX, APPROXIMATION_ALL])] - ]]); - - $this->setDefault([]); + $this + ->setDefault(self::DEFAULT_VALUE) + ->setSaveType(ZBX_WIDGET_FIELD_TYPE_STR) + ->setValidationRules(['type' => API_OBJECTS, 'fields' => [ + 'dataset_type' => ['type' => API_INT32, 'in' => implode(',', [self::DATASET_TYPE_SINGLE_ITEM, self::DATASET_TYPE_PATTERN_ITEM])], + 'hosts' => ['type' => API_STRINGS_UTF8, 'flags' => null], + 'items' => ['type' => API_STRINGS_UTF8, 'flags' => null], + 'itemids' => ['type' => API_IDS, 'flags' => null], + 'color' => ['type' => API_COLOR, 'flags' => API_REQUIRED | API_NOT_EMPTY], + 'type' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => implode(',', [SVG_GRAPH_TYPE_LINE, SVG_GRAPH_TYPE_POINTS, SVG_GRAPH_TYPE_STAIRCASE, SVG_GRAPH_TYPE_BAR])], + 'stacked' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => implode(',', [SVG_GRAPH_STACKED_OFF, SVG_GRAPH_STACKED_ON])], + 'width' => ['type' => API_INT32, 'in' => implode(',', range(0, 10))], + 'pointsize' => ['type' => API_INT32, 'in' => implode(',', range(1, 10))], + 'transparency' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => implode(',', range(0, 10))], + 'fill' => ['type' => API_INT32, 'in' => implode(',', range(0, 10))], + 'missingdatafunc' => ['type' => API_INT32, 'in' => implode(',', [SVG_GRAPH_MISSING_DATA_NONE, SVG_GRAPH_MISSING_DATA_CONNECTED, SVG_GRAPH_MISSING_DATA_TREAT_AS_ZERO, SVG_GRAPH_MISSING_DATA_LAST_KNOWN])], + 'axisy' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => implode(',', [GRAPH_YAXIS_SIDE_LEFT, GRAPH_YAXIS_SIDE_RIGHT])], + 'timeshift' => ['type' => API_TIME_UNIT, 'flags' => API_REQUIRED, 'in' => implode(':', [ZBX_MIN_TIMESHIFT, ZBX_MAX_TIMESHIFT])], + 'aggregate_function' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => implode(',', [AGGREGATE_NONE, AGGREGATE_MIN, AGGREGATE_MAX, AGGREGATE_AVG, AGGREGATE_COUNT, AGGREGATE_SUM, AGGREGATE_FIRST, AGGREGATE_LAST])], + 'aggregate_interval' => ['type' => API_MULTIPLE, 'rules' => [ + ['if' => ['field' => 'aggregate_function', 'in' => implode(',', [AGGREGATE_MIN, AGGREGATE_MAX, AGGREGATE_AVG, AGGREGATE_COUNT, AGGREGATE_SUM, AGGREGATE_FIRST, AGGREGATE_LAST])], + 'type' => API_TIME_UNIT, 'flags' => API_REQUIRED | API_NOT_EMPTY | API_TIME_UNIT_WITH_YEAR, 'in' => implode(':', [1, ZBX_MAX_TIMESHIFT])], + ['else' => true, 'type' => API_STRING_UTF8, 'in' => GRAPH_AGGREGATE_DEFAULT_INTERVAL] + ]], + 'aggregate_grouping' => ['type' => API_INT32, 'in' => implode(',', [GRAPH_AGGREGATE_BY_ITEM, GRAPH_AGGREGATE_BY_DATASET])], + 'approximation' => ['type' => API_INT32, 'in' => implode(',', [APPROXIMATION_MIN, APPROXIMATION_AVG, APPROXIMATION_MAX, APPROXIMATION_ALL])] + ]]); } - /** - * Set field values for the datasets. - * - * @return $this - */ - public function setValue($value) { + public function setValue($value): self { $data_sets = []; foreach ((array) $value as $data_set) { @@ -83,14 +87,7 @@ class CWidgetFieldGraphDataSet extends CWidgetField { return parent::setValue($data_sets); } - /** - * Set additional flags, which can be used in configuration form. - * - * @param int $flags - * - * @return $this - */ - public function setFlags($flags) { + public function setFlags($flags): self { parent::setFlags($flags); if ($flags & self::FLAG_NOT_EMPTY) { @@ -102,20 +99,15 @@ class CWidgetFieldGraphDataSet extends CWidgetField { $this->setStrictValidationRules($strict_validation_rules); } else { - $this->setStrictValidationRules(null); + $this->setStrictValidationRules(); } return $this; } - /** - * Default values filled in newly created data set or used as unspecified values. - * - * @return array - */ - public static function getDefaults() { + public static function getDefaults(): array { return [ - 'dataset_type' => CWidgetHelper::DATASET_TYPE_PATTERN_ITEM, + 'dataset_type' => self::DATASET_TYPE_PATTERN_ITEM, 'hosts' => [], 'items' => [], 'itemids' => [], @@ -136,11 +128,29 @@ class CWidgetFieldGraphDataSet extends CWidgetField { ]; } - /** - * @param bool $strict Widget form submit validation? - * - * @return array Errors. - */ + public static function getItemNames(array $itemids): array { + $names = []; + + $items = API::Item()->get([ + 'output' => ['itemid', 'hostid', 'name'], + 'selectHosts' => ['hostid', 'name'], + 'webitems' => true, + 'itemids' => $itemids, + 'preservekeys' => true + ]); + + if (!$items) { + return $names; + } + + foreach ($items as $item) { + $hosts = array_column($item['hosts'], 'name', 'hostid'); + $names[$item['itemid']] = $hosts[$item['hostid']].NAME_DELIMITER.$item['name']; + } + + return $names; + } + public function validate(bool $strict = false): array { $errors = []; @@ -148,13 +158,14 @@ class CWidgetFieldGraphDataSet extends CWidgetField { ? $this->strict_validation_rules : $this->validation_rules; $validation_rules += $this->ex_validation_rules; - $value = ($this->value === null) ? $this->default : $this->value; + + $value = $this->value ?? $this->default; if ($this->full_name !== null) { $label = $this->full_name; } else { - $label = ($this->label === null) ? $this->name : $this->label; + $label = $this->label ?? $this->name; } if ($strict) { @@ -170,7 +181,7 @@ class CWidgetFieldGraphDataSet extends CWidgetField { foreach ($value as $i => $data) { $validation_rules_by_type = $validation_rules; - if ($data['dataset_type'] == CWidgetHelper::DATASET_TYPE_SINGLE_ITEM) { + if ($data['dataset_type'] == self::DATASET_TYPE_SINGLE_ITEM) { $validation_rules_by_type['fields']['itemids']['flags'] |= API_REQUIRED; $validation_rules_by_type['fields']['color']['type'] = API_COLORS; @@ -200,14 +211,7 @@ class CWidgetFieldGraphDataSet extends CWidgetField { return $errors; } - /** - * Prepares array entry for widget field, ready to be passed to CDashboard API functions. - * Reference is needed here to avoid array merging in CWidgetForm::fieldsToApi method. With large number of widget - * fields it causes significant performance decrease. - * - * @param array $widget_fields Reference to array of widget fields. - */ - public function toApi(array &$widget_fields = []) { + public function toApi(array &$widget_fields = []): void { $value = $this->getValue(); $dataset_fields = [ @@ -251,8 +255,8 @@ class CWidgetFieldGraphDataSet extends CWidgetField { 'value' => $itemid ]; } - // Field "color" stored as array for dataset type CWidgetHelper::DATASET_TYPE_SINGLE_ITEM (0) - if ($val['dataset_type'] == CWidgetHelper::DATASET_TYPE_SINGLE_ITEM) { + // Field "color" stored as array for dataset type DATASET_TYPE_SINGLE_ITEM (0) + if ($val['dataset_type'] == self::DATASET_TYPE_SINGLE_ITEM) { foreach ($val['color'] as $num => $color) { $widget_fields[] = [ 'type' => ZBX_WIDGET_FIELD_TYPE_STR, |