Skip to content

Commit

Permalink
using esql params
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Oct 23, 2024
1 parent 6b0b0ad commit a03061e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
import { type ObservabilityElasticsearchClient } from '@kbn/observability-utils/es/client/create_observability_es_client';
import { kqlQuery } from '@kbn/observability-utils/es/queries/kql_query';
import { esqlResultToPlainObjects } from '@kbn/observability-utils/es/utils/esql_result_to_plain_objects';
import { ENTITY_LAST_SEEN } from '@kbn/observability-shared-plugin/common';
import { ENTITY_LAST_SEEN, ENTITY_TYPE } from '@kbn/observability-shared-plugin/common';
import type { ScalarValue } from '@elastic/elasticsearch/lib/api/types';
import {
ENTITIES_LATEST_ALIAS,
MAX_NUMBER_OF_ENTITIES,
type Entity,
type EntityColumnIds,
} from '../../../common/entities';
import {
getBuiltinEntityDefinitionIdESQLWhereClause,
getEntityTypesWhereClause,
} from './query_helper';
import { getBuiltinEntityDefinitionIdESQLWhereClause } from './query_helper';

export async function getLatestEntities({
inventoryEsClient,
Expand All @@ -38,26 +36,27 @@ export async function getLatestEntities({

const from = `FROM ${ENTITIES_LATEST_ALIAS}`;
const where: string[] = [getBuiltinEntityDefinitionIdESQLWhereClause()];
const params: ScalarValue[] = [];

if (entityTypes) {
where.push(getEntityTypesWhereClause(entityTypes));
where.push(`WHERE ${ENTITY_TYPE} IN (${entityTypes.map(() => '?').join()})`);
params.push(...entityTypes.map((entityType) => entityType));
}

const sort = `SORT ${entitiesSortField} ${sortDirection}`;
const limit = `LIMIT ${MAX_NUMBER_OF_ENTITIES}`;

const query = [from, ...where, sort, limit].join(' | ');

const request = {
const latestEntitiesEsqlResponse = await inventoryEsClient.esql('get_latest_entities', {
query,
filter: {
bool: {
filter: [...kqlQuery(kuery)],
},
},
};

const latestEntitiesEsqlResponse = await inventoryEsClient.esql('get_latest_entities', request);
params,
});

return esqlResultToPlainObjects<Entity>(latestEntitiesEsqlResponse);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
* 2.0.
*/

import { ENTITY_DEFINITION_ID, ENTITY_TYPE } from '@kbn/observability-shared-plugin/common';

export const getEntityTypesWhereClause = (entityTypes: string[]) =>
`WHERE ${ENTITY_TYPE} IN (${entityTypes.map((entityType) => `"${entityType}"`).join()})`;
import { ENTITY_DEFINITION_ID } from '@kbn/observability-shared-plugin/common';

export const getBuiltinEntityDefinitionIdESQLWhereClause = () =>
`WHERE ${ENTITY_DEFINITION_ID} LIKE "builtin_*"`;

0 comments on commit a03061e

Please sign in to comment.