Skip to content

Commit 5c98b11

Browse files
authored
expressions indexPattern function (#70315)
1 parent 22a41c5 commit 5c98b11

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
export * from './load_index_pattern';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { indexPatternLoad } from './load_index_pattern';
21+
22+
jest.mock('../../services', () => ({
23+
getIndexPatterns: () => ({
24+
get: (id: string) => ({
25+
toSpec: () => ({
26+
title: 'value',
27+
}),
28+
}),
29+
}),
30+
}));
31+
32+
describe('indexPattern expression function', () => {
33+
test('returns serialized index pattern', async () => {
34+
const indexPatternDefinition = indexPatternLoad();
35+
const result = await indexPatternDefinition.fn(null, { id: '1' }, {} as any);
36+
expect(result.type).toEqual('index_pattern');
37+
expect(result.value.title).toEqual('value');
38+
});
39+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { i18n } from '@kbn/i18n';
21+
import { ExpressionFunctionDefinition } from '../../../../../plugins/expressions/public';
22+
import { getIndexPatterns } from '../../services';
23+
import { IndexPatternSpec } from '../../../common/index_patterns';
24+
25+
const name = 'indexPatternLoad';
26+
27+
type Input = null;
28+
type Output = Promise<{ type: 'index_pattern'; value: IndexPatternSpec }>;
29+
30+
interface Arguments {
31+
id: string;
32+
}
33+
34+
export const indexPatternLoad = (): ExpressionFunctionDefinition<
35+
typeof name,
36+
Input,
37+
Arguments,
38+
Output
39+
> => ({
40+
name,
41+
type: 'index_pattern',
42+
inputTypes: ['null'],
43+
help: i18n.translate('data.functions.indexPatternLoad.help', {
44+
defaultMessage: 'Loads an index pattern',
45+
}),
46+
args: {
47+
id: {
48+
types: ['string'],
49+
required: true,
50+
help: i18n.translate('data.functions.indexPatternLoad.id.help', {
51+
defaultMessage: 'index pattern id to load',
52+
}),
53+
},
54+
},
55+
async fn(input, args) {
56+
const indexPatterns = getIndexPatterns();
57+
58+
const indexPattern = await indexPatterns.get(args.id);
59+
60+
return { type: 'index_pattern', value: indexPattern.toSpec() };
61+
},
62+
});

src/plugins/data/public/plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import {
8282
ValueClickActionContext,
8383
} from './actions/value_click_action';
8484
import { SavedObjectsClientPublicToCommon } from './index_patterns';
85+
import { indexPatternLoad } from './index_patterns/expressions/load_index_pattern';
8586

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

128129
expressions.registerFunction(esaggs);
130+
expressions.registerFunction(indexPatternLoad);
129131

130132
const queryService = this.queryService.setup({
131133
uiSettings: core.uiSettings,

0 commit comments

Comments
 (0)