diff --git a/test/interpreter_functional/plugins/kbn_tp_run_pipeline/kibana.json b/test/interpreter_functional/plugins/kbn_tp_run_pipeline/kibana.json index 7eafb185617c4..084cee2fddf08 100644 --- a/test/interpreter_functional/plugins/kbn_tp_run_pipeline/kibana.json +++ b/test/interpreter_functional/plugins/kbn_tp_run_pipeline/kibana.json @@ -8,7 +8,7 @@ "kibanaUtils", "expressions" ], - "server": false, + "server": true, "ui": true, "requiredBundles": [ "inspector" diff --git a/test/interpreter_functional/plugins/kbn_tp_run_pipeline/server/index.ts b/test/interpreter_functional/plugins/kbn_tp_run_pipeline/server/index.ts new file mode 100644 index 0000000000000..2e55966785909 --- /dev/null +++ b/test/interpreter_functional/plugins/kbn_tp_run_pipeline/server/index.ts @@ -0,0 +1,23 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { PluginInitializer } from '../../../../../src/core/server'; +import { TestPlugin, TestPluginSetup, TestPluginStart } from './plugin'; + +export const plugin: PluginInitializer = () => new TestPlugin(); diff --git a/test/interpreter_functional/plugins/kbn_tp_run_pipeline/server/plugin.ts b/test/interpreter_functional/plugins/kbn_tp_run_pipeline/server/plugin.ts new file mode 100644 index 0000000000000..dfac64ab8a444 --- /dev/null +++ b/test/interpreter_functional/plugins/kbn_tp_run_pipeline/server/plugin.ts @@ -0,0 +1,63 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { schema } from '@kbn/config-schema'; +import { CoreSetup, Plugin, HttpResponsePayload } from '../../../../../src/core/server'; +import { PluginStart as DataPluginStart } from '../../../../../src/plugins/data/server'; +import { ExpressionsServerStart } from '../../../../../src/plugins/expressions/server'; + +export interface TestStartDeps { + data: DataPluginStart; + expressions: ExpressionsServerStart; +} + +export class TestPlugin implements Plugin { + public setup(core: CoreSetup) { + const router = core.http.createRouter(); + + router.post( + { + path: '/api/interpreter_functional/run_expression', + validate: { + body: schema.object({ + input: schema.maybe(schema.nullable(schema.object({}, { unknowns: 'allow' }))), + expression: schema.string(), + }), + }, + }, + async (context, req, res) => { + const [, { expressions }] = await core.getStartServices(); + const output = await expressions.run( + req.body.expression, + req.body.input, + { + kibanaRequest: req, + } + ); + return res.ok({ body: output }); + } + ); + } + + public start() {} + public stop() {} +} + +export type TestPluginSetup = ReturnType; +export type TestPluginStart = ReturnType; diff --git a/test/interpreter_functional/plugins/kbn_tp_run_pipeline/tsconfig.json b/test/interpreter_functional/plugins/kbn_tp_run_pipeline/tsconfig.json index f77a5eaffc301..3d9d8ca9451d4 100644 --- a/test/interpreter_functional/plugins/kbn_tp_run_pipeline/tsconfig.json +++ b/test/interpreter_functional/plugins/kbn_tp_run_pipeline/tsconfig.json @@ -8,6 +8,7 @@ "index.ts", "public/**/*.ts", "public/**/*.tsx", + "server/**/*.ts", "../../../../typings/**/*", ], "exclude": [], diff --git a/test/interpreter_functional/test_suites/run_pipeline/esaggs.ts b/test/interpreter_functional/test_suites/run_pipeline/esaggs.ts index a7a7118da5222..d7908c7bb94d1 100644 --- a/test/interpreter_functional/test_suites/run_pipeline/esaggs.ts +++ b/test/interpreter_functional/test_suites/run_pipeline/esaggs.ts @@ -33,13 +33,27 @@ export default function ({ getService, updateBaselines, }: FtrProviderContext & { updateBaselines: boolean }) { + const supertest = getService('supertest'); let expectExpression: ExpectExpression; + + const expectClientToMatchServer = async (title: string, expression: string) => { + const clientResult = await expectExpression(title, expression).getResponse(); + await supertest + .post('/api/interpreter_functional/run_expression') + .set('kbn-xsrf', 'anything') + .send({ expression, input: undefined }) + .expect(200) + .expect(({ body }) => { + expect(body.rows).to.eql(clientResult.rows); + }); + }; + describe('esaggs pipeline expression tests', () => { before(() => { expectExpression = expectExpressionProvider({ getService, updateBaselines }); }); - describe('correctly renders tagcloud', () => { + describe('correctly filters based on context', () => { it('filters on index pattern primary date field by default', async () => { const timeRange = { from: '2006-09-21T00:00:00Z', @@ -88,5 +102,69 @@ export default function ({ expect(getCell(result, 0, 0)).to.be(7452); }); }); + + describe('correctly runs on the server', () => { + it('runs the provided agg on the server', async () => { + const expression = ` + esaggs index={indexPatternLoad id='logstash-*'} + aggs={aggAvg id="1" enabled=true schema="metric" field="bytes"} + `; + await supertest + .post('/api/interpreter_functional/run_expression') + .set('kbn-xsrf', 'anything') + .send({ expression, input: undefined }) + .expect(200) + .expect(({ body }) => { + expect(body.columns[0].meta.index).to.be('logstash-*'); + expect(body.columns[0].meta.source).to.be('esaggs'); + expect(body.columns[0].meta.sourceParams.type).to.be('avg'); + expect(getCell(body, 0, 0)).to.be(5727.3136246786635); + }); + }); + + it('works with timeRange filters', async () => { + const timeRange = { + from: '2006-09-21T00:00:00Z', + to: '2015-09-22T00:00:00Z', + }; + // we need to manually pass timeRange in the input until + // kibana_context is supported on the server + const kibanaContext = { + type: 'kibana_context', + timeRange, + }; + const expression = ` + esaggs index={indexPatternLoad id='logstash-*'} + aggs={aggCount id="1" enabled=true schema="metric"} + `; + await supertest + .post('/api/interpreter_functional/run_expression') + .set('kbn-xsrf', 'anything') + .send({ expression, input: kibanaContext }) + .expect(200) + .expect(({ body }) => { + expect(getCell(body, 0, 0)).to.be(9375); + }); + }); + + it('returns same results on client & server', async () => { + const a = ` + esaggs index={indexPatternLoad id='logstash-*'} + aggs={aggCount id="1" enabled=true schema="metric"} + aggs={aggTerms id="2" enabled=true schema="segment" field="response.raw"} + `; + await expectClientToMatchServer('multiple_aggs', a); + + // const b = ` + // esaggs index={indexPatternLoad id='logstash-*'} + // aggs={aggCount id="1" enabled=true schema="metric"} + // `; + + // const c = ` + // esaggs index={indexPatternLoad id='logstash-*'} + // aggs={aggCount id="1" enabled=true schema="metric"} + // `; + }); + }); }); }