1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
<?php declare(strict_types = 0);
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
namespace Widgets\PieChart\Includes;
use API,
CApiInputValidator;
use Zabbix\Widgets\CWidgetField;
/**
* Class for data set widget field used in Pie chart widget configuration Data set tab.
*/
class CWidgetFieldDataSet extends CWidgetField {
public const DEFAULT_VIEW = CWidgetFieldDataSetView::class;
public const DEFAULT_VALUE = [];
public const DATASET_TYPE_SINGLE_ITEM = 0;
public const DATASET_TYPE_PATTERN_ITEM = 1;
public const ITEM_TYPE_NORMAL = 0;
public const ITEM_TYPE_TOTAL = 1;
// Predefined colors for data-sets in JSON format. Each next data set takes next sequential value from palette.
public const DEFAULT_COLOR_PALETTE = [
'FF465C', 'FFD54F', '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.
private const DEFAULT_COLOR = 'FF465C';
public function __construct(string $name, ?string $label = null) {
parent::__construct($name, $label);
$this
->setDefault(self::DEFAULT_VALUE)
->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],
'items' => ['type' => API_STRINGS_UTF8],
'itemids' => ['type' => API_IDS],
'references' => ['type' => API_STRINGS_UTF8],
'color' => ['type' => API_COLOR, 'flags' => API_REQUIRED | API_NOT_EMPTY],
'aggregate_function' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => implode(',', [AGGREGATE_MIN, AGGREGATE_MAX, AGGREGATE_AVG, AGGREGATE_COUNT, AGGREGATE_SUM, AGGREGATE_FIRST, AGGREGATE_LAST])],
'dataset_aggregation' => ['type' => API_INT32, 'flags' => API_REQUIRED, 'in' => implode(',', [AGGREGATE_NONE, AGGREGATE_MIN, AGGREGATE_MAX, AGGREGATE_AVG, AGGREGATE_COUNT, AGGREGATE_SUM])],
'type' => ['type' => API_INTS32, 'flags' => null, 'in' => implode(',', [self::ITEM_TYPE_NORMAL, self::ITEM_TYPE_TOTAL])],
'data_set_label' => ['type' => API_STRING_UTF8, 'length' => 255]
]]);
}
public function setValue($value): self {
$data_sets = [];
foreach ((array) $value as $data_set) {
$data_sets[] = $data_set + self::getDefaults();
}
return parent::setValue($data_sets);
}
public static function getDefaults(): array {
return [
'dataset_type' => self::DATASET_TYPE_PATTERN_ITEM,
'hosts' => [],
'items' => [],
'itemids' => [],
'color' => self::DEFAULT_COLOR,
'aggregate_function' => AGGREGATE_LAST,
'dataset_aggregation' => AGGREGATE_NONE,
'type' => [],
'data_set_label' => ''
];
}
public static function getItemNames(array $itemids, bool $resolve_macros): array {
$names = [];
$items = API::Item()->get([
'output' => ['itemid', 'hostid', $resolve_macros ? 'name_resolved' : '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']] = $resolve_macros
? $hosts[$item['hostid']].NAME_DELIMITER.$item['name_resolved']
: $hosts[$item['hostid']].NAME_DELIMITER.$item['name'];
}
return $names;
}
public function validate(bool $strict = false): array {
if (!$strict) {
return [];
}
$errors = [];
$total_item_count = 0;
$validation_rules = $this->getValidationRules($strict);
$value = $this->getValue();
$label = $this->getErrorLabel();
if (!count($value)) {
if (!CApiInputValidator::validate($validation_rules, $value, $label, $error)) {
$errors[] = $error;
}
}
else {
$validation_rules['type'] = API_OBJECT;
}
foreach ($value as $index => &$data) {
$validation_rules_by_type = $validation_rules;
if ($data['dataset_type'] == self::DATASET_TYPE_SINGLE_ITEM) {
foreach($data['type'] as $item_type) {
if ($item_type == self::ITEM_TYPE_TOTAL) {
$total_item_count++;
}
}
$validation_rules_by_type['fields']['itemids']['flags'] |= API_REQUIRED;
$validation_rules_by_type['fields']['references']['flags'] |= API_REQUIRED;
$validation_rules_by_type['fields']['color']['type'] = API_COLORS;
$validation_rules_by_type['fields']['type']['flags'] |= API_REQUIRED;
unset($data['hosts'], $data['items']);
}
else {
if (!$this->isTemplateDashboard()) {
$validation_rules_by_type['fields']['hosts']['flags'] |= API_REQUIRED;
}
$validation_rules_by_type['fields']['items']['flags'] |= API_REQUIRED;
unset($data['itemids'], $data['type'], $data['references']);
}
if (!CApiInputValidator::validate($validation_rules_by_type, $data, $label.'/'.($index + 1), $error)) {
$errors[] = $error;
break;
}
if ($data['dataset_type'] == self::DATASET_TYPE_SINGLE_ITEM) {
foreach ($data['itemids'] as $i => &$item_spec) {
if ($item_spec == 0) {
$item_spec = [CWidgetField::FOREIGN_REFERENCE_KEY => $data['references'][$i]];
}
}
unset($item_spec);
unset($data['references']);
}
}
unset($data);
if ($total_item_count > 1) {
$errors[] = _('Cannot add more than one item with type "Total" to the chart.');
}
if ($total_item_count > 0) {
foreach ($value as $data) {
if ($data['dataset_aggregation'] !== AGGREGATE_NONE) {
$errors[] =
_('Cannot set "Data set aggregation" when item with type "Total" is added to the chart.');
break;
}
}
}
if (!$errors) {
$this->setValue($value);
}
return $errors;
}
public function toApi(array &$widget_fields = []): void {
$dataset_fields = [
'dataset_type' => ZBX_WIDGET_FIELD_TYPE_INT32,
'aggregate_function' => ZBX_WIDGET_FIELD_TYPE_INT32,
'dataset_aggregation' => ZBX_WIDGET_FIELD_TYPE_INT32,
'data_set_label' => ZBX_WIDGET_FIELD_TYPE_STR
];
$dataset_defaults = self::getDefaults();
foreach ($this->getValue() as $index => $value) {
foreach ($value['hosts'] as $host_index => $pattern_host) {
$widget_fields[] = [
'type' => ZBX_WIDGET_FIELD_TYPE_STR,
'name' => $this->name.'.'.$index.'.hosts.'.$host_index,
'value' => $pattern_host
];
}
foreach ($value['items'] as $item_index => $pattern_item) {
$widget_fields[] = [
'type' => ZBX_WIDGET_FIELD_TYPE_STR,
'name' => $this->name.'.'.$index.'.items.'.$item_index,
'value' => $pattern_item
];
}
foreach ($value['itemids'] as $item_index => $item_spec) {
if (is_array($item_spec)) {
$widget_fields[] = [
'type' => ZBX_WIDGET_FIELD_TYPE_STR,
'name' => $this->name.'.'.$index.'.itemids.'.$item_index.'.'.
CWidgetField::FOREIGN_REFERENCE_KEY,
'value' => $item_spec[CWidgetField::FOREIGN_REFERENCE_KEY]
];
}
else {
$widget_fields[] = [
'type' => ZBX_WIDGET_FIELD_TYPE_ITEM,
'name' => $this->name.'.'.$index.'.itemids.'.$item_index,
'value' => $item_spec
];
}
}
foreach ($value['type'] as $type_index => $type) {
$widget_fields[] = [
'type' => ZBX_WIDGET_FIELD_TYPE_INT32,
'name' => $this->name.'.'.$index.'.type.'.$type_index,
'value' => $type
];
}
// Field "color" stored as array for dataset type DATASET_TYPE_SINGLE_ITEM (0)
if ($value['dataset_type'] == self::DATASET_TYPE_SINGLE_ITEM) {
foreach ($value['color'] as $color_index => $color) {
$widget_fields[] = [
'type' => ZBX_WIDGET_FIELD_TYPE_STR,
'name' => $this->name.'.'.$index.'.color.'.$color_index,
'value' => $color
];
}
}
else {
$widget_fields[] = [
'type' => ZBX_WIDGET_FIELD_TYPE_STR,
'name' => $this->name.'.'.$index.'.color',
'value' => $value['color']
];
}
// Other dataset fields are stored if different from the defaults.
foreach ($dataset_fields as $name => $type) {
if ($value[$name] !== null && $value[$name] != $dataset_defaults[$name]) {
$widget_fields[] = [
'type' => $type,
'name' => $this->name.'.'.$index.'.'.$name,
'value' => $value[$name]
];
}
}
}
}
protected function getValidationRules(bool $strict = false): array {
$validation_rules = parent::getValidationRules($strict);
if (($this->getFlags() & self::FLAG_NOT_EMPTY) !== 0) {
self::setValidationRuleFlag($validation_rules, API_NOT_EMPTY);
if (!$this->isTemplateDashboard()) {
self::setValidationRuleFlag($validation_rules['fields']['hosts'], API_NOT_EMPTY);
}
self::setValidationRuleFlag($validation_rules['fields']['items'], API_NOT_EMPTY);
self::setValidationRuleFlag($validation_rules['fields']['itemids'], API_NOT_EMPTY);
self::setValidationRuleFlag($validation_rules['fields']['references'], API_NOT_EMPTY);
}
return $validation_rules;
}
}
|