Files
threshold-ui/addons/api/addon/handlers/client-daemon-handler.js
T
221ab1f7bd 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>
2024-01-16 10:17:45 -05:00

89 lines
2.7 KiB
JavaScript

import { pluralize } from 'ember-inflector';
import { generateMQLExpression } from '../utils/mqlQuery';
/**
* Not all types are yet supported by the client daemon so we'll
* just whitelist the ones we need to start with. Keys are the resource
* types and the values are the fields that are searchable by the client.
* @type {{session: string[], target: string[]}}
*/
const supportedTypes = {
target: ['id', 'name', 'description', 'address', 'scope_id'],
session: ['id', 'type', 'status', 'endpoint', 'scope_id', 'target_id'],
};
/**
* Handler to sit in front of the API request layer
* so we can request from the daemon first
*/
const ClientDaemonHandler = {
async request(context, next) {
switch (context.request.op) {
case 'query': {
const { store, data } = context.request;
const { type, query } = data;
const isClientDaemonRunning = await this.ipc.invoke(
'isClientDaemonRunning',
);
if (
!Object.keys(supportedTypes).includes(type) ||
!isClientDaemonRunning
) {
return next(context.request);
}
// TODO: Remove usages of recursive from callers and scope_id since
// all calls to daemon are recursive, scope_id can be part of search query instead
// eslint-disable-next-line no-unused-vars
let { recursive, scope_id, ...remainingQuery } = query;
let searchQuery = '';
if (remainingQuery.query) {
const { search, filters } = remainingQuery.query;
searchQuery = generateMQLExpression({
search: {
text: search,
fields: supportedTypes[type],
},
filters,
});
}
const auth_token_id = this.session.data?.authenticated?.id;
remainingQuery = {
...remainingQuery,
query: searchQuery,
auth_token_id,
resource: pluralize(type),
};
// Currently returns with a singular top level field with resource name
// e.g. { targets: [...] } or { sessions: [..] }
// So this just unwraps to the array, or undefined
const [results] = Object.values(
await this.ipc.invoke('searchClientDaemon', remainingQuery),
);
const payload = { items: results ?? [] };
const schema = store.modelFor(type);
const serializer = store.serializerFor(type);
const normalizedPayload = serializer.normalizeResponse(
store,
schema,
payload,
null,
'query',
);
// TODO: Add some pagination
return store.push(normalizedPayload);
}
default:
return next(context.request);
}
},
};
export default ClientDaemonHandler;