Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook to include custom variables by default when searching under Overview entries #1046

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions library/Icingadb/Hook/CustomVarsRetrieverHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Icinga\Module\Icingadb\Hook;

use Icinga\Application\Hook;
use Icinga\Application\Logger;
use ipl\Orm\Model;
use Throwable;

abstract class CustomVarsRetrieverHook
{
abstract public function retrieveCustomVars(Model $model): array;

final public static function getCustomVarColumns(Model $model): array
{
$columns = [];

$hooks = Hook::all('Icingadb/CustomVarsRetriever');
foreach ($hooks as $hook) {
try {
$customVars = $hook->retrieveCustomVars($model);
if (!empty($customVars)) {
$columns = [...$columns, ...$customVars];
}
} catch (Throwable $e) {
Logger::error(
'Error retrieving Custom Vars for %s: %s',
$object,
$e->getMessage()
);
}
}

return $columns;
}
}
5 changes: 4 additions & 1 deletion library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Generator;
use Icinga\Module\Icingadb\Common\Auth;
use Icinga\Module\Icingadb\Common\Database;
use Icinga\Module\Icingadb\Hook\CustomVarsRetrieverHook;
use Icinga\Module\Icingadb\Model\Behavior\ReRoute;
use Icinga\Module\Icingadb\Model\CustomvarFlat;
use Icinga\Module\Icingadb\Model\Host;
Expand Down Expand Up @@ -132,7 +133,9 @@ protected function createQuickSearchFilter($searchTerm)
$resolver = $model::on($this->getDb())->getResolver();

$quickFilter = Filter::any();
foreach ($model->getSearchColumns() as $column) {
$customVarColumns = CustomVarsRetrieverHook::getCustomVarColumns($model);
$columns = [...$model->getSearchColumns(), ...$customVarColumns];
foreach ($columns as $column) {
if (strpos($column, '.') === false) {
$column = $resolver->qualifyColumn($column, $model->getTableName());
}
Expand Down
5 changes: 4 additions & 1 deletion library/Icingadb/Web/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Icinga\Module\Icingadb\Common\SearchControls;
use Icinga\Module\Icingadb\Data\CsvResultSet;
use Icinga\Module\Icingadb\Data\JsonResultSet;
use Icinga\Module\Icingadb\Hook\CustomVarsRetrieverHook;
use Icinga\Module\Icingadb\Web\Control\GridViewModeSwitcher;
use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher;
use Icinga\Module\Icingadb\Widget\ItemTable\StateItemTable;
Expand Down Expand Up @@ -305,7 +306,9 @@ public function handleSearchRequest(Query $query, array $additionalColumns = [])
*/
protected function prepareSearchFilter(Query $query, string $search, Filter\Any $filter, array $additionalColumns)
{
$columns = array_merge($query->getModel()->getSearchColumns(), $additionalColumns);
$model = $query->getModel();
$customVarColumns = CustomVarsRetrieverHook::getCustomVarColumns($model);
$columns = array_merge($model->getSearchColumns(), $additionalColumns, $customVarColumns);
foreach ($columns as $column) {
if (strpos($column, '.') === false) {
$column = $query->getResolver()->qualifyColumn($column, $query->getModel()->getTableName());
Expand Down