Skip to content

Commit cc6c809

Browse files
committed
ObjectSuggestions: Prioritze Column suggestions and best suggestions
Show custom variable suggestions only if the column suggestions have not consumed all the slots (50).
1 parent af69318 commit cc6c809

File tree

1 file changed

+32
-42
lines changed

1 file changed

+32
-42
lines changed

library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,11 @@ protected function fetchColumnSuggestions($searchTerm)
221221
$model = $this->getModel();
222222
$query = $model::on($this->getDb());
223223

224-
// Collect custom variables first and check for exact matches
224+
// Collect custom variables
225225
$parsedArrayVars = [];
226226
$varSuggestions = [];
227-
$exactVarMatches = [];
228-
$halfLimit = (int) (static::DEFAULT_LIMIT / 2);
229-
230227
$exactSearchTerm = trim($searchTerm, ' *');
228+
$titleAdded = false;
231229
if ($exactSearchTerm !== '') {
232230
foreach ($this->getDb()->select($this->queryCustomvarConfig($searchTerm)) as $customVar) {
233231
$search = $name = $customVar->flatname;
@@ -245,68 +243,60 @@ protected function fetchColumnSuggestions($searchTerm)
245243
if (isset($customVar->$relation)) {
246244
$varRelation = $relation . '.vars.' . $search;
247245
$varLabel = sprintf($label, $name);
246+
// Suggest exact custom variable matches first
248247
if ($name === $exactSearchTerm) {
249-
$exactVarMatches[$varRelation] = $varLabel;
250-
} elseif ($this->matchSuggestion($varRelation, $varLabel, $searchTerm)) {
248+
if ($titleAdded === false) {
249+
$this->addHtml(HtmlElement::create(
250+
'li',
251+
['class' => static::SUGGESTION_TITLE_CLASS],
252+
t('Best Suggestions')
253+
));
254+
255+
$titleAdded = true;
256+
}
257+
258+
yield $varRelation => $varLabel;
259+
} else {
251260
$varSuggestions[$varRelation] = $varLabel;
252261
}
253262
}
254263
}
255264
}
256265
}
257266

258-
// Adjust the number of columns to be suggested based on custom variable count
259-
$varCount = count($exactVarMatches) + count($varSuggestions);
260-
$colLimit = $halfLimit;
261-
if ($varCount < $halfLimit) {
262-
$colLimit = $halfLimit + ($halfLimit - $varCount);
263-
}
264-
265-
// Exact custom variable matches first
266-
if (! empty($exactVarMatches)) {
267-
$this->addHtml(HtmlElement::create(
268-
'li',
269-
['class' => static::SUGGESTION_TITLE_CLASS],
270-
t('Best Suggestions')
271-
));
272-
273-
foreach ($exactVarMatches as $relation => $label) {
274-
yield $relation => $label;
275-
}
276-
}
277-
278267
// Ordinary columns comes after exact matches,
279268
// or if there ar no exact matches they come first
280-
$colCount = 0;
269+
$titleAdded = false;
281270
foreach (self::collectFilterColumns($model, $query->getResolver()) as $columnName => $columnMeta) {
282-
if ($colCount > $colLimit) {
283-
break;
284-
}
285-
286271
if ($this->matchSuggestion($columnName, $columnMeta, $searchTerm)) {
287-
if ($colCount === 0) {
272+
if ($titleAdded === false) {
288273
$this->addHtml(HtmlElement::create(
289274
'li',
290275
['class' => static::SUGGESTION_TITLE_CLASS],
291276
t('Columns')
292277
));
293-
}
294278

295-
$colCount++;
279+
$titleAdded = true;
280+
}
296281

297282
yield $columnName => $columnMeta;
298283
}
299284
}
300285

301286
// Finally, the other custom variable suggestions
302-
if (! empty($varSuggestions)) {
303-
$this->addHtml(HtmlElement::create(
304-
'li',
305-
['class' => static::SUGGESTION_TITLE_CLASS],
306-
t('Custom Variables')
307-
));
308-
309-
foreach ($varSuggestions as $relation => $label) {
287+
$titleAdded = false;
288+
foreach ($varSuggestions as $relation => $label) {
289+
if ($this->matchSuggestion($relation, $label, $searchTerm)) {
290+
if ($titleAdded === false) {
291+
$this->addHtml(HtmlElement::create(
292+
'li',
293+
['class' => static::SUGGESTION_TITLE_CLASS],
294+
t('Custom Variables')
295+
));
296+
297+
$titleAdded = true;
298+
}
299+
310300
yield $relation => $label;
311301
}
312302
}

0 commit comments

Comments
 (0)