Skip to content

Commit

Permalink
expressions indexPattern function (#70315)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored Jul 1, 2020
1 parent 22a41c5 commit 5c98b11
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/plugins/data/public/index_patterns/expressions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

export * from './load_index_pattern';
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 { indexPatternLoad } from './load_index_pattern';

jest.mock('../../services', () => ({
getIndexPatterns: () => ({
get: (id: string) => ({
toSpec: () => ({
title: 'value',
}),
}),
}),
}));

describe('indexPattern expression function', () => {
test('returns serialized index pattern', async () => {
const indexPatternDefinition = indexPatternLoad();
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
@@ -0,0 +1,62 @@
/*
* 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 { i18n } from '@kbn/i18n';
import { ExpressionFunctionDefinition } from '../../../../../plugins/expressions/public';
import { getIndexPatterns } from '../../services';
import { IndexPatternSpec } from '../../../common/index_patterns';

const name = 'indexPatternLoad';

type Input = null;
type Output = Promise<{ type: 'index_pattern'; value: IndexPatternSpec }>;

interface Arguments {
id: string;
}

export const indexPatternLoad = (): ExpressionFunctionDefinition<
typeof name,
Input,
Arguments,
Output
> => ({
name,
type: 'index_pattern',
inputTypes: ['null'],
help: i18n.translate('data.functions.indexPatternLoad.help', {
defaultMessage: 'Loads an index pattern',
}),
args: {
id: {
types: ['string'],
required: true,
help: i18n.translate('data.functions.indexPatternLoad.id.help', {
defaultMessage: 'index pattern id to load',
}),
},
},
async fn(input, args) {
const indexPatterns = getIndexPatterns();

const indexPattern = await indexPatterns.get(args.id);

return { type: 'index_pattern', value: indexPattern.toSpec() };
},
});
2 changes: 2 additions & 0 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
ValueClickActionContext,
} from './actions/value_click_action';
import { SavedObjectsClientPublicToCommon } from './index_patterns';
import { indexPatternLoad } from './index_patterns/expressions/load_index_pattern';

declare module '../../ui_actions/public' {
export interface ActionContextMapping {
Expand Down Expand Up @@ -126,6 +127,7 @@ export class DataPublicPlugin implements Plugin<DataPublicPluginSetup, DataPubli
};

expressions.registerFunction(esaggs);
expressions.registerFunction(indexPatternLoad);

const queryService = this.queryService.setup({
uiSettings: core.uiSettings,
Expand Down

0 comments on commit 5c98b11

Please sign in to comment.