Skip to content

Commit

Permalink
WIP2
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Aug 27, 2024
1 parent 2d1b93e commit 0173ead
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 48 deletions.
27 changes: 5 additions & 22 deletions library/Icingadb/Hook/CustomVarEnricherHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ abstract class CustomVarEnricherHook
abstract public function getGroups(): array;

/**
* Return a group name for the given variable name
* Return enriched vars in the following format
* [label => enriched custom var]
*
* @param array $vars
*
* @return array
*/
abstract public function enrichCustomVars(array $vars, Model $object): array;
abstract public function enrichCustomVars(array &$vars, Model $object): array;

public static function prepareEnrichedCustomVars(array $vars, Model $object): array
{
$enrichedVars = [];

$groups = [];

foreach (Hook::all('Icingadb/CustomVarEnricher') as $hook) {
/** @var self $hook */
try {
Expand All @@ -45,26 +46,8 @@ public static function prepareEnrichedCustomVars(array $vars, Model $object): ar
}
}

$enrichedVars = array_merge([], ...$enrichedVars);
$vars = array_merge($vars, ...$enrichedVars);
$groups = array_merge([], ...$groups);
foreach ($vars as $key => $var) {
if (array_key_exists($key, $enrichedVars)) {
$label = key($enrichedVars[$key]);
$vars[$label] = $enrichedVars[$key][$label];

unset($vars[$key]);

$key = $label;
}

foreach ($groups as $group) {
if (array_key_exists($key, $group)) {
unset($vars[$key]);

break;
}
}
}

return [$vars, $groups];
}
Expand Down
43 changes: 17 additions & 26 deletions library/Icingadb/ProvidedHook/Icingadb/CustomVarEnricher.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,11 @@ class CustomVarEnricher extends CustomVarEnricherHook
{
protected $fieldConfig;

protected $datalistMaps;
protected $datalistMaps = [];

protected $groups = [];

public function prefetchForObject(Model $object): bool
{
return false;
}

public function renderCustomVarKey(string $key)
{
return $key;
}

public function renderCustomVarValue(string $key, $value)
{
return $value;
}

public function identifyCustomVarGroup(string $key): ?string
{
return null;
}

public function enrichCustomVars(array $vars, Model $object): array
public function enrichCustomVars(array &$vars, Model $object): array
{
$directorObject = null;
$connection = Db::fromResourceName(Config::module('director')->get('db', 'resource'));
Expand All @@ -62,12 +42,21 @@ public function enrichCustomVars(array $vars, Model $object): array
$this->fieldConfig = (new IcingaObjectFieldLoader($directorObject))->getFields();

$this->buildDataListMap($connection);

if ($directorObject) {
foreach ($vars as $varName => $customVar) {
$newVars[$varName] = $this->resolveCustomVarMapping($varName, $customVar, $connection);
$varsToReplace = json_decode(json_encode($directorObject->getVars()), true)
+ json_decode(json_encode($directorObject->getInheritedVars()), true);


foreach ($varsToReplace as $varName => $customVar) {
if (isset($vars[$varName])) {
$newVars[] = $this->resolveCustomVarMapping($varName, $customVar, $connection);

unset($vars[$varName]);
}
}
} else {
$newVars = $vars;

$newVars = array_merge([], ...$newVars);
}

return $newVars;
Expand Down Expand Up @@ -128,6 +117,8 @@ protected function resolveCustomVarMapping(string $name, $val, DbConnection $con
} else {
$this->groups[$field->getCategoryName()][$name] = $val;
}

return [];
}
} elseif (is_array($val)) {
$newValue = [];
Expand Down

0 comments on commit 0173ead

Please sign in to comment.