Icu 11748 add free search for targets (#1995)
* refactor: 💡 fix how we access socket_address from status call ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11748 * feat: 🎸 add free search capability to targets list ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11748 * test: 💍 update test asserts to look for hds appl state title ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11748 * refactor: 💡 remove filtering for yes, no, & narrow-results ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11748 * refactor: 💡 add aria-label to dropdown search input ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11748 * refactor: 💡 add to handleInput signature comment ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11748 * refactor: 💡 move buildQuery to client daemon handler ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11748 * refactor: 💡 Add new parser for MQL expressions ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11748 * refactor: 💡 use queryParams for searching ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11748 * fix: 🐛 Fix some MQL expressions not being properly filtered ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11748 * style: 💄 update handleSearchInput comment signature ✅ Closes: https://hashicorp.atlassian.net/browse/ICU-11748 * Update addons/api/addon/utils/mqlQuery.js Co-authored-by: Lisbet Alvarez <107949262+lisbet-alvarez@users.noreply.github.com> --------- Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com> Co-authored-by: Lisbet Alvarez <107949262+lisbet-alvarez@users.noreply.github.com>
This commit is contained in:
1 parent
4bf1a88764
commit
221ab1f7bd
14 files changed
+419
-288
No files matched your search
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* Copyright (c) HashiCorp, Inc.
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Takes a POJO representing a filter query and converts it to an
|
||||
* MQL expression string which is understood by the client daemon.
|
||||
*
|
||||
* @example
|
||||
* const filter = generateMQLFilterExpression({
|
||||
* name: { contains: 'search text' },
|
||||
* scope: [{ equals: 'scope1'}, { equals: 'scope2' }],
|
||||
* });
|
||||
*
|
||||
* // (name % "search text") and (scope = "scope1" or scope = "scope2")
|
||||
*/
|
||||
export function generateMQLFilterExpression(filterObj) {
|
||||
if (!filterObj) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const expressions = Object.entries(filterObj)
|
||||
.map(([key, filterObjValue]) => {
|
||||
const filterObjValueArray = Array.isArray(filterObjValue)
|
||||
? filterObjValue
|
||||
: [filterObjValue];
|
||||
|
||||
const orClauses = or(
|
||||
filterObjValueArray.map((filterObjValue) => {
|
||||
if (!filterObjValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const [operation, value] = Object.entries(filterObjValue)[0];
|
||||
|
||||
switch (operation) {
|
||||
case 'contains':
|
||||
return contains(key, value);
|
||||
case 'gt':
|
||||
return gt(key, value);
|
||||
case 'gte':
|
||||
return gte(key, value);
|
||||
case 'lt':
|
||||
return lt(key, value);
|
||||
case 'lte':
|
||||
return lte(key, value);
|
||||
default:
|
||||
return equals(key, value);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
return parenthetical(orClauses);
|
||||
})
|
||||
.filter((item) => item);
|
||||
return and(expressions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a POJO representing a search query and converts it to an
|
||||
* MQL expression string which is understood by the client daemon.
|
||||
*
|
||||
* @example
|
||||
* const search = generateMQLSearchExpression({
|
||||
* text: 'search',
|
||||
* fields: ['id', 'name', 'description']
|
||||
* });
|
||||
*
|
||||
* // (id % 'search' or name % 'search' or description % 'search')
|
||||
*/
|
||||
export function generateMQLSearchExpression(searchObj) {
|
||||
if (!searchObj) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { text, fields } = searchObj;
|
||||
|
||||
return parenthetical(
|
||||
or(fields.map((field) => contains(field, text)).filter((item) => item)),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the MQL expression when a user passes in both search and filters.
|
||||
*/
|
||||
export function generateMQLExpression(obj) {
|
||||
const { search, filters } = obj;
|
||||
|
||||
const expressions = [
|
||||
generateMQLSearchExpression(search),
|
||||
generateMQLFilterExpression(filters),
|
||||
].filter((item) => item);
|
||||
|
||||
return and(expressions);
|
||||
}
|
||||
|
||||
const isEmpty = (input) =>
|
||||
input === null || input === undefined || input === '';
|
||||
|
||||
// Escape any double quotes or backslashes
|
||||
const sanitize = (input) => input?.replace(/(["\\])/g, '\\$1');
|
||||
|
||||
// Comparison Operators
|
||||
const equals = (key, value) =>
|
||||
!isEmpty(value) ? `${key} = "${sanitize(value)}"` : null;
|
||||
|
||||
const contains = (key, value) =>
|
||||
!isEmpty(value) ? `${key} % "${sanitize(value)}"` : null;
|
||||
|
||||
const gt = (key, value) =>
|
||||
!isEmpty(value) ? `${key} > "${sanitize(value)}"` : null;
|
||||
|
||||
const gte = (key, value) =>
|
||||
!isEmpty(value) ? `${key} >= "${sanitize(value)}"` : null;
|
||||
|
||||
const lt = (key, value) =>
|
||||
!isEmpty(value) ? `${key} < "${sanitize(value)}"` : null;
|
||||
|
||||
const lte = (key, value) =>
|
||||
!isEmpty(value) ? `${key} <= "${sanitize(value)}"` : null;
|
||||
|
||||
// Logical Operators
|
||||
const and = (clauses) => clauses.join(' and ');
|
||||
|
||||
const or = (clauses) => clauses.join(' or ');
|
||||
|
||||
const parenthetical = (expression) => (expression ? `(${expression})` : null);
|
||||
Reference in new issue
Block a user