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 6 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
10 changes: 9 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;
}

interface TimelionPluginStartDeps {
data: PluginStart;
}

/**
* Represents Timelion Plugin instance that will be managed by the Kibana plugin system.
*/
Expand Down Expand Up @@ -83,9 +88,12 @@ export class Plugin {
};

functionsRoute(router, deps);
runRoute(router, deps);
validateEsRoute(router);

core.getStartServices().then(([_, depsStart]) => {
VladLasitsa marked this conversation as resolved.
Show resolved Hide resolved
runRoute(router, { ...deps, data: (depsStart as TimelionPluginStartDeps).data });
});

core.uiSettings.register({
'timelion:es.timefield': {
name: i18n.translate('timelion.uiSettings.timeFieldLabel', {
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/vis_type_timelion/server/routes/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { PluginStart } from '../../../../../src/plugins/data/server';
import { TimelionFunctionInterface } from '../types';
import { ConfigManager } from '../lib/config_manager';

Expand All @@ -37,10 +38,12 @@ export function runRoute(
logger,
getFunction,
configManager,
data,
}: {
logger: Logger;
getFunction: (name: string) => TimelionFunctionInterface;
configManager: ConfigManager;
data: PluginStart;
}
) {
router.post(
Expand Down Expand Up @@ -81,13 +84,14 @@ export function runRoute(
const uiSettings = await context.core.uiSettings.client.getAll();

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: data.search.search,
});
const chainRunner = chainRunnerFn(tlConfig);
const sheet = await Bluebird.all(chainRunner.processRequest(request.body));
Expand Down
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
14 changes: 10 additions & 4 deletions src/plugins/vis_type_timelion/server/series_functions/es/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,15 @@ 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,
{
VladLasitsa marked this conversation as resolved.
Show resolved Hide resolved
params: body,
},
{ strategy: 'es' }
VladLasitsa marked this conversation as resolved.
Show resolved Hide resolved
);

if (!resp.rawResponse._shards.total) {
throw new Error(
i18n.translate('timelion.serverSideErrors.esFunction.indexNotFoundErrorMessage', {
defaultMessage: 'Elasticsearch index not found: {index}',
Expand All @@ -143,7 +149,7 @@ export default new Datasource('es', {
}
return {
type: 'seriesList',
list: toSeriesList(resp.aggregations, config),
list: toSeriesList(resp.rawResponse.aggregations, config),
};
},
});