diff --git a/x-pack/plugins/ingest_manager/server/services/agents/actions.test.ts b/x-pack/plugins/ingest_manager/server/services/agents/actions.test.ts index c7390079523893..60d14f8f663728 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/actions.test.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/actions.test.ts @@ -8,6 +8,7 @@ import { createAgentAction } from './actions'; import { SavedObject } from 'kibana/server'; import { AgentAction } from '../../../common/types/models'; import { savedObjectsClientMock } from 'src/core/server/mocks'; +import { esKuery } from '../../../../../../src/plugins/data/server'; describe('test agent actions services', () => { it('should create a new action', async () => { @@ -35,3 +36,71 @@ describe('test agent actions services', () => { expect(createdAction?.sent_at).toEqual(newAgentAction.sent_at); }); }); + +class Benchmark { + private static NS_PER_SEC = 1e9; + #startTime?: [number, number]; + #elapsed?: [number, number]; + + start() { + if (this.#startTime) { + throw new Error(`Start?? We're started, we can't start again!`); + } + + this.#startTime = process.hrtime(); + } + stop() { + if (this.#startTime == null) { + throw new Error(`Stop?? We haven't even started yet!`); + } + + this.#elapsed = process.hrtime(this.#startTime); + } + + describe() { + if (this.#elapsed == null) { + return `Benchmark is still running`; + } + + return `Benchmark took ${ + this.#elapsed[0] * Benchmark.NS_PER_SEC + this.#elapsed[1] + } nanoseconds`; + } +} + +describe('micro-benchmark', () => { + test('parsing KQL expression', () => { + const b = new Benchmark(); + b.start(); + esKuery.fromKueryExpression( + 'not fleet-agent-actions.attributes.sent_at: * and fleet-agent-actions.attributes.agent_id:1234567' + ); + b.stop(); + console.log(b.describe()); + }); + + test('manually building KueryNode', () => { + const b = new Benchmark(); + b.start(); + esKuery.nodeTypes.function.buildNode('and', [ + esKuery.nodeTypes.function.buildNode( + 'not', + esKuery.nodeTypes.function.buildNode('is', 'fleet-agent-actions.attributes.sent_at', '*') + ), + esKuery.nodeTypes.function.buildNode( + 'is', + 'fleet-agent-actions.attributes.agent_id', + '1234567' + ), + ]); + b.stop(); + console.log(b.describe()); + }); + + test('doing nothing', () => { + const b = new Benchmark(); + b.start(); + b.stop(); + console.log(b.describe()); + }); +}); diff --git a/x-pack/plugins/ingest_manager/server/services/agents/actions.ts b/x-pack/plugins/ingest_manager/server/services/agents/actions.ts index cd0dd921312306..c8bfe223af6370 100644 --- a/x-pack/plugins/ingest_manager/server/services/agents/actions.ts +++ b/x-pack/plugins/ingest_manager/server/services/agents/actions.ts @@ -9,7 +9,7 @@ import { Agent, AgentAction, AgentActionSOAttributes } from '../../../common/typ import { AGENT_ACTION_SAVED_OBJECT_TYPE } from '../../../common/constants'; import { savedObjectToAgentAction } from './saved_objects'; import { appContextService } from '../app_context'; -import { nodeTypes } from '../../../../../../src/plugins/data/common'; +import { esKuery } from '../../../../../../src/plugins/data/server'; export async function createAgentAction( soClient: SavedObjectsClientContract, @@ -30,16 +30,16 @@ export async function getAgentActionsForCheckin( soClient: SavedObjectsClientContract, agentId: string ): Promise { - const filter = nodeTypes.function.buildNode('and', [ - nodeTypes.function.buildNode( + const filter = esKuery.nodeTypes.function.buildNode('and', [ + esKuery.nodeTypes.function.buildNode( 'not', - nodeTypes.function.buildNode( + esKuery.nodeTypes.function.buildNode( 'is', `${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.sent_at`, '*' ) ), - nodeTypes.function.buildNode( + esKuery.nodeTypes.function.buildNode( 'is', `${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.agent_id`, agentId @@ -94,16 +94,16 @@ export async function getAgentActionByIds( } export async function getNewActionsSince(soClient: SavedObjectsClientContract, timestamp: string) { - const filter = nodeTypes.function.buildNode('and', [ - nodeTypes.function.buildNode( + const filter = esKuery.nodeTypes.function.buildNode('and', [ + esKuery.nodeTypes.function.buildNode( 'not', - nodeTypes.function.buildNode( + esKuery.nodeTypes.function.buildNode( 'is', `${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.sent_at`, '*' ) ), - nodeTypes.function.buildNode( + esKuery.nodeTypes.function.buildNode( 'range', `${AGENT_ACTION_SAVED_OBJECT_TYPE}.attributes.created_at`, {