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

[data.search][data.indexPatterns] Expose esaggs + indexPatternLoad on the server. #84590

Merged
merged 10 commits into from
Dec 3, 2020
Prev Previous commit
Next Next commit
Add comments to document how dependency injection works.
  • Loading branch information
lukeelmers committed Dec 2, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 8b95a6a5dfa20f91da5b14dc97b05744a82ae7b5
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
*/

import { IndexPatternLoadStartDependencies } from '../../../common/index_patterns/expressions';
import { createIndexPatternLoad } from './load_index_pattern';
import { getFunctionDefinition } from './load_index_pattern';

describe('indexPattern expression function', () => {
let getStartDependencies: () => Promise<IndexPatternLoadStartDependencies>;
@@ -36,7 +36,7 @@ describe('indexPattern expression function', () => {
});

test('returns serialized index pattern', async () => {
const indexPatternDefinition = createIndexPatternLoad({ getStartDependencies });
const indexPatternDefinition = getFunctionDefinition({ getStartDependencies });
const result = await indexPatternDefinition().fn(null, { id: '1' }, {} as any);
expect(result.type).toEqual('index_pattern');
expect(result.value.title).toEqual('value');
Original file line number Diff line number Diff line change
@@ -25,8 +25,19 @@ import {
} from '../../../common/index_patterns/expressions';
import { DataPublicPluginStart, DataStartDependencies } from '../../types';

/** @internal */
export function createIndexPatternLoad({
/**
* Returns the expression function definition. Any stateful dependencies are accessed
* at runtime via the `getStartDependencies` param, which provides the specific services
* needed for this function to run.
*
* This function is an implementation detail of this module, and is exported separately
* only for testing purposes.
*
* @param getStartDependencies - async function that resolves with IndexPatternLoadStartDependencies
*
* @internal
*/
export function getFunctionDefinition({
getStartDependencies,
}: {
getStartDependencies: () => Promise<IndexPatternLoadStartDependencies>;
@@ -43,13 +54,26 @@ export function createIndexPatternLoad({
});
}

/** @internal */
/**
* This is some glue code that takes in `core.getStartServices`, extracts the dependencies
* needed for this function, and wraps them behind a `getStartDependencies` function that
* is then called at runtime.
*
* We do this so that we can be explicit about exactly which dependencies the function
* requires, without cluttering up the top-level `plugin.ts` with this logic. It also
* makes testing the expression function a bit easier since `getStartDependencies` is
* the only thing you should need to mock.
*
* @param getStartServices - core's StartServicesAccessor for this plugin
*
* @internal
*/
export function getIndexPatternLoad({
lukeelmers marked this conversation as resolved.
Show resolved Hide resolved
getStartServices,
}: {
getStartServices: StartServicesAccessor<DataStartDependencies, DataPublicPluginStart>;
}) {
return createIndexPatternLoad({
return getFunctionDefinition({
getStartDependencies: async () => {
const [, , { indexPatterns }] = await getStartServices();
return { indexPatterns };
31 changes: 28 additions & 3 deletions src/plugins/data/public/search/expressions/esaggs.ts
Original file line number Diff line number Diff line change
@@ -28,7 +28,19 @@ import {
} from '../../../common/search/expressions';
import { DataPublicPluginStart, DataStartDependencies } from '../../types';

function createEsaggs({
/**
* Returns the expression function definition. Any stateful dependencies are accessed
* at runtime via the `getStartDependencies` param, which provides the specific services
* needed for this function to run.
*
* This function is an implementation detail of this module, and is exported separately
* only for testing purposes.
*
* @param getStartDependencies - async function that resolves with EsaggsStartDependencies
*
* @internal
*/
export function getFunctionDefinition({
getStartDependencies,
}: {
getStartDependencies: () => Promise<EsaggsStartDependencies>;
@@ -68,13 +80,26 @@ function createEsaggs({
});
}

/** @internal */
/**
* This is some glue code that takes in `core.getStartServices`, extracts the dependencies
* needed for this function, and wraps them behind a `getStartDependencies` function that
* is then called at runtime.
*
* We do this so that we can be explicit about exactly which dependencies the function
* requires, without cluttering up the top-level `plugin.ts` with this logic. It also
* makes testing the expression function a bit easier since `getStartDependencies` is
* the only thing you should need to mock.
*
* @param getStartServices - core's StartServicesAccessor for this plugin
*
* @internal
*/
export function getEsaggs({
getStartServices,
}: {
getStartServices: StartServicesAccessor<DataStartDependencies, DataPublicPluginStart>;
}) {
return createEsaggs({
return getFunctionDefinition({
getStartDependencies: async () => {
const [, , self] = await getStartServices();
const { fieldFormats, indexPatterns, query, search } = self;
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
*/

import { IndexPatternLoadStartDependencies } from '../../../common/index_patterns/expressions';
import { createIndexPatternLoad } from './load_index_pattern';
import { getFunctionDefinition } from './load_index_pattern';

describe('indexPattern expression function', () => {
let getStartDependencies: () => Promise<IndexPatternLoadStartDependencies>;
@@ -36,7 +36,7 @@ describe('indexPattern expression function', () => {
});

test('returns serialized index pattern', async () => {
const indexPatternDefinition = createIndexPatternLoad({ getStartDependencies });
const indexPatternDefinition = getFunctionDefinition({ getStartDependencies });
const result = await indexPatternDefinition().fn(null, { id: '1' }, {
kibanaRequest: {},
} as any);
Original file line number Diff line number Diff line change
@@ -27,8 +27,19 @@ import {
} from '../../../common/index_patterns/expressions';
import { DataPluginStartDependencies, DataPluginStart } from '../../plugin';

/** @internal */
export function createIndexPatternLoad({
/**
* Returns the expression function definition. Any stateful dependencies are accessed
* at runtime via the `getStartDependencies` param, which provides the specific services
* needed for this function to run.
*
* This function is an implementation detail of this module, and is exported separately
* only for testing purposes.
*
* @param getStartDependencies - async function that resolves with IndexPatternLoadStartDependencies
*
* @internal
*/
export function getFunctionDefinition({
getStartDependencies,
}: {
getStartDependencies: (req: KibanaRequest) => Promise<IndexPatternLoadStartDependencies>;
@@ -55,13 +66,26 @@ export function createIndexPatternLoad({
});
}

/** @internal */
/**
* This is some glue code that takes in `core.getStartServices`, extracts the dependencies
* needed for this function, and wraps them behind a `getStartDependencies` function that
* is then called at runtime.
*
* We do this so that we can be explicit about exactly which dependencies the function
* requires, without cluttering up the top-level `plugin.ts` with this logic. It also
* makes testing the expression function a bit easier since `getStartDependencies` is
* the only thing you should need to mock.
*
* @param getStartServices - core's StartServicesAccessor for this plugin
*
* @internal
*/
export function getIndexPatternLoad({
getStartServices,
}: {
getStartServices: StartServicesAccessor<DataPluginStartDependencies, DataPluginStart>;
}) {
return createIndexPatternLoad({
return getFunctionDefinition({
getStartDependencies: async (request: KibanaRequest) => {
const [{ elasticsearch, savedObjects }, , { indexPatterns }] = await getStartServices();
return {
38 changes: 31 additions & 7 deletions src/plugins/data/server/search/expressions/esaggs.ts
Original file line number Diff line number Diff line change
@@ -29,13 +29,24 @@ import {
} from '../../../common/search/expressions';
import { DataPluginStartDependencies, DataPluginStart } from '../../plugin';

/** @internal */
function createEsaggs({
/**
* Returns the expression function definition. Any stateful dependencies are accessed
* at runtime via the `getStartDependencies` param, which provides the specific services
* needed for this function to run.
*
* This function is an implementation detail of this module, and is exported separately
* only for testing purposes.
*
* @param getStartDependencies - async function that resolves with EsaggsStartDependencies
*
* @internal
*/
export function getFunctionDefinition({
getStartDependencies,
}: {
getStartDependencies: (req: KibanaRequest) => Promise<EsaggsStartDependencies>;
}) {
return (): EsaggsExpressionFunctionDefinition => ({
}): () => EsaggsExpressionFunctionDefinition {
return () => ({
...getEsaggsMeta(),
async fn(input, args, { inspectorAdapters, abortSignal, getSearchSessionId, kibanaRequest }) {
if (!kibanaRequest) {
@@ -78,13 +89,26 @@ function createEsaggs({
});
}

/** @internal */
/**
* This is some glue code that takes in `core.getStartServices`, extracts the dependencies
* needed for this function, and wraps them behind a `getStartDependencies` function that
* is then called at runtime.
*
* We do this so that we can be explicit about exactly which dependencies the function
* requires, without cluttering up the top-level `plugin.ts` with this logic. It also
* makes testing the expression function a bit easier since `getStartDependencies` is
* the only thing you should need to mock.
*
* @param getStartServices - core's StartServicesAccessor for this plugin
*
* @internal
*/
export function getEsaggs({
getStartServices,
}: {
getStartServices: StartServicesAccessor<DataPluginStartDependencies, DataPluginStart>;
}) {
return createEsaggs({
}): () => EsaggsExpressionFunctionDefinition {
return getFunctionDefinition({
getStartDependencies: async (request: KibanaRequest) => {
const [{ elasticsearch, savedObjects, uiSettings }, , self] = await getStartServices();
const { fieldFormats, indexPatterns, search } = self;