Skip to content

Commit

Permalink
Using msearch for tree api endpoint (elastic#73813)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Aug 4, 2020
1 parent 435ca3d commit e88c12a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,28 @@ import { TypeOf } from '@kbn/config-schema';
import { eventsIndexPattern, alertsIndexPattern } from '../../../../common/endpoint/constants';
import { validateTree } from '../../../../common/endpoint/schema/resolver';
import { Fetcher } from './utils/fetch';
import { Tree } from './utils/tree';
import { EndpointAppContext } from '../../types';

export function handleTree(
log: Logger,
endpointAppContext: EndpointAppContext
): RequestHandler<TypeOf<typeof validateTree.params>, TypeOf<typeof validateTree.query>> {
return async (context, req, res) => {
const {
params: { id },
query: {
children,
ancestors,
events,
alerts,
afterAlert,
afterEvent,
afterChild,
legacyEndpointID: endpointID,
},
} = req;
try {
const client = context.core.elasticsearch.legacy.client;

const fetcher = new Fetcher(client, id, eventsIndexPattern, alertsIndexPattern, endpointID);
const fetcher = new Fetcher(
client,
req.params.id,
eventsIndexPattern,
alertsIndexPattern,
req.query.legacyEndpointID
);

const [childrenNodes, ancestry, relatedEvents, relatedAlerts] = await Promise.all([
fetcher.children(children, afterChild),
fetcher.ancestors(ancestors),
fetcher.events(events, afterEvent),
fetcher.alerts(alerts, afterAlert),
]);

const tree = new Tree(id, {
ancestry,
children: childrenNodes,
relatedEvents,
relatedAlerts,
});

const enrichedTree = await fetcher.stats(tree);
const tree = await fetcher.tree(req.query);

return res.ok({
body: enrichedTree.render(),
body: tree.render(),
});
} catch (err) {
log.warn(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ export class ChildrenLifecycleQueryHandler implements SingleQueryHandler<Resolve
}

this.handleResponse(await this.query.search(client, this.childrenHelper.getEntityIDs()));
return this.getResults() || createChildren();
return this.getResults() ?? createChildren();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class Fetcher {
);

// now that we have all the start events get the full lifecycle nodes
childrenLifecycleHandler.search(this.client);
await childrenLifecycleHandler.search(this.client);

const tree = new Tree(this.id, {
ancestry: ancestryHandler.getResults(),
Expand Down
4 changes: 1 addition & 3 deletions x-pack/test/security_solution_endpoint_api_int/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export default function endpointAPIIntegrationTests(providerContext: FtrProvider
before(async () => {
await ingestManager.setup();
});
loadTestFile(require.resolve('./resolver/entity_id'));
loadTestFile(require.resolve('./resolver/tree'));
loadTestFile(require.resolve('./resolver/children'));
loadTestFile(require.resolve('./resolver/index'));
loadTestFile(require.resolve('./metadata'));
loadTestFile(require.resolve('./policy'));
loadTestFile(require.resolve('./artifacts'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '../../../../plugins/security_solution/common/endpoint/generate_data';
import { InsertedEvents } from '../../services/resolver';

export default function resolverAPIIntegrationTests({ getService }: FtrProviderContext) {
export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const resolver = getService('resolverGenerator');
const generator = new EndpointDocGenerator('resolver');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { FtrProviderContext } from '../../ftr_provider_context';

export default function (providerContext: FtrProviderContext) {
const { loadTestFile } = providerContext;

describe('Resolver tests', () => {
loadTestFile(require.resolve('./entity_id'));
loadTestFile(require.resolve('./children'));
loadTestFile(require.resolve('./tree'));
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ const verifyLifecycleStats = (
}
};

export default function resolverAPIIntegrationTests({ getService }: FtrProviderContext) {
export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');
const resolver = getService('resolverGenerator');
Expand Down

0 comments on commit e88c12a

Please sign in to comment.