Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Search API in Timelion (sync) #75115

Merged
merged 18 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/plugins/vis_type_timelion/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { first } from 'rxjs/operators';
import { TypeOf, schema } from '@kbn/config-schema';
import { RecursiveReadonly } from '@kbn/utility-types';

import { PluginStart } from '../../../../src/plugins/data/server';
import { CoreSetup, PluginInitializerContext } from '../../../../src/core/server';
import { deepFreeze } from '../../../../src/core/server';
import { configSchema } from '../config';
Expand All @@ -42,6 +43,10 @@ export interface PluginSetupContract {
uiEnabled: boolean;
}

export interface TimelionPluginStartDeps {
data: PluginStart;
}

/**
* Represents Timelion Plugin instance that will be managed by the Kibana plugin system.
*/
Expand Down Expand Up @@ -80,11 +85,12 @@ export class Plugin {
functions,
getFunction,
logger,
core,
};

functionsRoute(router, deps);
runRoute(router, deps);
validateEsRoute(router);
runRoute(router, deps);
VladLasitsa marked this conversation as resolved.
Show resolved Hide resolved

core.uiSettings.register({
'timelion:es.timefield': {
Expand Down
9 changes: 7 additions & 2 deletions src/plugins/vis_type_timelion/server/routes/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { IRouter, Logger } from 'kibana/server';
import { IRouter, Logger, CoreSetup } from 'kibana/server';
import { schema } from '@kbn/config-schema';
import Bluebird from 'bluebird';
import _ from 'lodash';
Expand All @@ -26,6 +26,7 @@ import chainRunnerFn from '../handlers/chain_runner.js';
import getNamespacesSettings from '../lib/get_namespaced_settings';
// @ts-ignore
import getTlConfig from '../handlers/lib/tl_config';
import { TimelionPluginStartDeps } from '../plugin';
import { TimelionFunctionInterface } from '../types';
import { ConfigManager } from '../lib/config_manager';

Expand All @@ -37,10 +38,12 @@ export function runRoute(
logger,
getFunction,
configManager,
core,
}: {
logger: Logger;
getFunction: (name: string) => TimelionFunctionInterface;
configManager: ConfigManager;
core: CoreSetup;
}
) {
router.post(
Expand Down Expand Up @@ -79,15 +82,17 @@ export function runRoute(
router.handleLegacyErrors(async (context, request, response) => {
try {
const uiSettings = await context.core.uiSettings.client.getAll();
const deps = (await core.getStartServices())[1] as TimelionPluginStartDeps;

const tlConfig = getTlConfig({
context,
request,
settings: _.defaults(uiSettings, timelionDefaults), // Just in case they delete some setting.
getFunction,
allowedGraphiteUrls: configManager.getGraphiteUrls(),
esShardTimeout: configManager.getEsShardTimeout(),
savedObjectsClient: context.core.savedObjects.client,
esDataClient: () => context.core.elasticsearch.legacy.client,
esDataClient: deps.data.search.search,
});
const chainRunner = chainRunnerFn(tlConfig);
const sheet = await Bluebird.all(chainRunner.processRequest(request.body));
Expand Down
45 changes: 21 additions & 24 deletions src/plugins/vis_type_timelion/server/series_functions/es/es.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import buildRequest from './lib/build_request';
import createDateAgg from './lib/create_date_agg';
import esResponse from '../fixtures/es_response';

import Bluebird from 'bluebird';
import _ from 'lodash';
import { expect } from 'chai';
import sinon from 'sinon';
Expand All @@ -34,14 +33,10 @@ import { UI_SETTINGS } from '../../../../data/server';

function stubRequestAndServer(response, indexPatternSavedObjects = []) {
return {
esDataClient: sinon.stub().returns({
callAsCurrentUser: function () {
return Bluebird.resolve(response);
},
}),
esDataClient: sinon.stub().returns(Promise.resolve(response)),
savedObjectsClient: {
find: function () {
return Bluebird.resolve({
return Promise.resolve({
alexwizp marked this conversation as resolved.
Show resolved Hide resolved
saved_objects: indexPatternSavedObjects,
});
},
Expand All @@ -55,7 +50,9 @@ describe('es', () => {
describe('seriesList processor', () => {
it('throws an error then the index is missing', () => {
tlConfig = stubRequestAndServer({
_shards: { total: 0 },
rawResponse: {
_shards: { total: 0 },
},
});
return invoke(es, [5], tlConfig)
.then(expect.fail)
Expand All @@ -65,7 +62,7 @@ describe('es', () => {
});

it('returns a seriesList', () => {
tlConfig = stubRequestAndServer(esResponse);
tlConfig = stubRequestAndServer({ rawResponse: esResponse });
return invoke(es, [5], tlConfig).then((r) => {
expect(r.output.type).to.eql('seriesList');
});
Expand Down Expand Up @@ -168,22 +165,22 @@ describe('es', () => {
config.index = 'beer';
const request = fn(config, tlConfig, emptyScriptedFields);

expect(request.index).to.equal('beer');
expect(request.params.index).to.equal('beer');
});

it('always sets body.size to 0', () => {
const request = fn(config, tlConfig, emptyScriptedFields);

expect(request.body.size).to.equal(0);
expect(request.params.body.size).to.equal(0);
});

it('creates a filters agg that contains each of the queries passed', () => {
config.q = ['foo', 'bar'];
const request = fn(config, tlConfig, emptyScriptedFields);

expect(request.body.aggs.q.meta.type).to.equal('split');
expect(request.params.body.aggs.q.meta.type).to.equal('split');

const filters = request.body.aggs.q.filters.filters;
const filters = request.params.body.aggs.q.filters.filters;
expect(filters.foo.query_string.query).to.eql('foo');
expect(filters.bar.query_string.query).to.eql('bar');
});
Expand All @@ -193,14 +190,14 @@ describe('es', () => {
config.index = 'beer';
const request = fn(config, tlConfig, emptyScriptedFields, 30000);

expect(request.timeout).to.equal('30000ms');
expect(request.params.timeout).to.equal('30000ms');
});

it('sets no timeout if elasticsearch.shardTimeout is set to 0', () => {
config.index = 'beer';
const request = fn(config, tlConfig, emptyScriptedFields, 0);

expect(request).to.not.have.property('timeout');
expect(request.params).to.not.have.property('timeout');
});
});

Expand All @@ -220,15 +217,15 @@ describe('es', () => {
tlConfig.settings[UI_SETTINGS.SEARCH_INCLUDE_FROZEN] = false;
const request = fn(config, tlConfig, emptyScriptedFields);

expect(request.ignore_throttled).to.equal(true);
expect(request.params.ignore_throttled).to.equal(true);
});

it('sets no timeout if elasticsearch.shardTimeout is set to 0', () => {
tlConfig.settings[UI_SETTINGS.SEARCH_INCLUDE_FROZEN] = true;
config.index = 'beer';
const request = fn(config, tlConfig, emptyScriptedFields);

expect(request.ignore_throttled).to.equal(false);
expect(request.params.ignore_throttled).to.equal(false);
});
});

Expand Down Expand Up @@ -262,21 +259,21 @@ describe('es', () => {
it('adds the contents of body.extended.es.filter to a filter clause of the bool', () => {
config.kibana = true;
const request = fn(config, tlConfig, emptyScriptedFields);
const filter = request.body.query.bool.filter.bool;
const filter = request.params.body.query.bool.filter.bool;
expect(filter.must.length).to.eql(1);
expect(filter.must_not.length).to.eql(2);
});

it('does not include filters if config.kibana = false', () => {
config.kibana = false;
const request = fn(config, tlConfig, emptyScriptedFields);
expect(request.body.query.bool.filter).to.eql(undefined);
expect(request.params.body.query.bool.filter).to.eql(undefined);
});

it('adds a time filter to the bool querys must clause', () => {
let request = fn(config, tlConfig, emptyScriptedFields);
expect(request.body.query.bool.must.length).to.eql(1);
expect(request.body.query.bool.must[0]).to.eql({
expect(request.params.body.query.bool.must.length).to.eql(1);
expect(request.params.body.query.bool.must[0]).to.eql({
range: {
'@timestamp': {
format: 'strict_date_optional_time',
Expand All @@ -288,7 +285,7 @@ describe('es', () => {

config.kibana = true;
request = fn(config, tlConfig, emptyScriptedFields);
expect(request.body.query.bool.must.length).to.eql(1);
expect(request.params.body.query.bool.must.length).to.eql(1);
});
});

Expand All @@ -297,7 +294,7 @@ describe('es', () => {
config.split = ['beer:5', 'wine:10'];
const request = fn(config, tlConfig, emptyScriptedFields);

const aggs = request.body.aggs.q.aggs;
const aggs = request.params.body.aggs.q.aggs;

expect(aggs.beer.meta.type).to.eql('split');
expect(aggs.beer.terms.field).to.eql('beer');
Expand All @@ -324,7 +321,7 @@ describe('es', () => {
];
const request = fn(config, tlConfig, scriptedFields);

const aggs = request.body.aggs.q.aggs;
const aggs = request.params.body.aggs.q.aggs;

expect(aggs.scriptedBeer.meta.type).to.eql('split');
expect(aggs.scriptedBeer.terms.script).to.eql({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { i18n } from '@kbn/i18n';
import _ from 'lodash';
import { ES_SEARCH_STRATEGY } from '../../../../data/server';
import Datasource from '../../lib/classes/datasource';
import buildRequest from './lib/build_request';
import toSeriesList from './lib/agg_response_to_series_list';
Expand Down Expand Up @@ -129,9 +130,11 @@ export default new Datasource('es', {

const body = buildRequest(config, tlConfig, scriptedFields, esShardTimeout);

const { callAsCurrentUser: callWithRequest } = tlConfig.esDataClient();
const resp = await callWithRequest('search', body);
if (!resp._shards.total) {
const resp = await tlConfig.esDataClient(tlConfig.context, body, {
strategy: ES_SEARCH_STRATEGY,
});

if (!resp.rawResponse._shards.total) {
throw new Error(
i18n.translate('timelion.serverSideErrors.esFunction.indexNotFoundErrorMessage', {
defaultMessage: 'Elasticsearch index not found: {index}',
Expand All @@ -143,7 +146,7 @@ export default new Datasource('es', {
}
return {
type: 'seriesList',
list: toSeriesList(resp.aggregations, config),
list: toSeriesList(resp.rawResponse.aggregations, config),
};
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@ export default function buildRequest(config, tlConfig, scriptedFields, timeout)
request.timeout = `${timeout}ms`;
}

return request;
return {
params: request,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ export default function () {
if (!functions[name]) throw new Error('No such function: ' + name);
return functions[name];
},
esDataClient: sinon.stub().returns({
callAsCurrentUser: function () {
return Promise.resolve(esResponse);
},
}),
esDataClient: sinon.stub().returns(Promise.resolve({ rawResponse: esResponse })),
VladLasitsa marked this conversation as resolved.
Show resolved Hide resolved
esShardTimeout: moment.duration(30000),
allowedGraphiteUrls: ['https://www.hostedgraphite.com/UID/ACCESS_KEY/graphite'],
});
Expand Down