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

[Vega] Filter bar in Vega is not usable with non default index pattern. #84090

Merged
merged 27 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a16fec4
[Vega] Filtering is not working
alexwizp Nov 23, 2020
39dd4b7
Merge remote-tracking branch 'upstream/master' into 81738
alexwizp Nov 24, 2020
9cf57e7
fix CI
alexwizp Nov 24, 2020
6952981
some work
alexwizp Nov 24, 2020
58a1d93
Merge remote-tracking branch 'upstream/master' into 81738
alexwizp Nov 24, 2020
d0c3f53
some work
alexwizp Nov 24, 2020
ef4e702
add tests for extract_index_pattern
alexwizp Nov 24, 2020
410cc4c
Merge remote-tracking branch 'upstream/master' into 81738
alexwizp Nov 24, 2020
e66c610
simplify extract_index_pattern
alexwizp Nov 24, 2020
7fd0ad2
Merge remote-tracking branch 'upstream/master' into 81738
alexwizp Nov 24, 2020
0eb9680
fix type error
alexwizp Nov 25, 2020
e068c90
cleanup
alexwizp Nov 25, 2020
6c846f0
Merge branch 'master' into 81738
kibanamachine Nov 25, 2020
0010868
Update vega_base_view.js
alexwizp Nov 26, 2020
629398d
Update extract_index_pattern.test.ts
alexwizp Nov 26, 2020
1491d37
Merge branch 'master' into 81738
kibanamachine Nov 26, 2020
606e67d
fix PR comments
alexwizp Nov 26, 2020
7b25522
Merge branch 'master' into 81738
kibanamachine Nov 27, 2020
37044aa
fix some comments
alexwizp Nov 27, 2020
4cfe514
Merge branch 'master' into 81738
kibanamachine Nov 30, 2020
90c87c0
Merge branch 'master' into 81738
kibanamachine Nov 30, 2020
1548365
findByTitle -> getByTitle
alexwizp Nov 30, 2020
5c9643a
Merge remote-tracking branch 'upstream/master' into 81738
alexwizp Nov 30, 2020
d8022ef
remove getByTitle
alexwizp Nov 30, 2020
cea9694
fix vega_base_view
alexwizp Nov 30, 2020
7a0a9b7
fix jest
alexwizp Nov 30, 2020
b538cab
allow to set multiple indexes from top_nav
alexwizp Dec 1, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [IndexPatternsService](./kibana-plugin-plugins-data-public.indexpatternsservice.md) &gt; [findByTitle](./kibana-plugin-plugins-data-public.indexpatternsservice.findbytitle.md)

## IndexPatternsService.findByTitle property

Returns an object matching a given title

<b>Signature:</b>

```typescript
findByTitle: (title: string, refresh?: boolean) => Promise<IndexPattern | undefined>;
```
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export declare class IndexPatternsService
| [clearCache](./kibana-plugin-plugins-data-public.indexpatternsservice.clearcache.md) | | <code>(id?: string &#124; undefined) =&gt; void</code> | Clear index pattern list cache |
| [ensureDefaultIndexPattern](./kibana-plugin-plugins-data-public.indexpatternsservice.ensuredefaultindexpattern.md) | | <code>EnsureDefaultIndexPattern</code> | |
| [fieldArrayToMap](./kibana-plugin-plugins-data-public.indexpatternsservice.fieldarraytomap.md) | | <code>(fields: FieldSpec[], fieldAttrs?: FieldAttrs &#124; undefined) =&gt; Record&lt;string, FieldSpec&gt;</code> | Converts field array to map |
| [findByTitle](./kibana-plugin-plugins-data-public.indexpatternsservice.findbytitle.md) | | <code>(title: string, refresh?: boolean) =&gt; Promise&lt;IndexPattern &#124; undefined&gt;</code> | Returns an object matching a given title |
| [get](./kibana-plugin-plugins-data-public.indexpatternsservice.get.md) | | <code>(id: string) =&gt; Promise&lt;IndexPattern&gt;</code> | Get an index pattern by id. Cache optimized |
| [getCache](./kibana-plugin-plugins-data-public.indexpatternsservice.getcache.md) | | <code>() =&gt; Promise&lt;SavedObject&lt;IndexPatternSavedObjectAttrs&gt;[] &#124; null &#124; undefined&gt;</code> | |
| [getDefault](./kibana-plugin-plugins-data-public.indexpatternsservice.getdefault.md) | | <code>() =&gt; Promise&lt;IndexPattern &#124; null&gt;</code> | Get default index pattern |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ export class IndexPatternsService {
}));
};

/**
* Returns an object matching a given title
*
* @param title {string}
* @returns {Promise<SavedObject|undefined>}
*/
findByTitle = async (title: string, refresh: boolean = false) => {
if (!this.savedObjectsCache || refresh) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could rely on getIdsWithTitle, filter based on the result, and then get

Lets call this getByTitle

await this.refreshSavedObjectsCache();
}
if (this.savedObjectsCache) {
const so = this.savedObjectsCache.find((obj) => obj.attributes.title === title);

if (so?.id) {
return this.get(so.id);
}
}
};

/**
* Clear index pattern list cache
* @param id optionally clear a single id
Expand Down
34 changes: 13 additions & 21 deletions src/plugins/data/common/index_patterns/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

import { find } from 'lodash';
import { SavedObjectsClientCommon, SavedObject } from '..';
import type { IndexPatternSavedObjectAttrs } from './index_patterns';
import type { SavedObjectsClientCommon } from '../types';

/**
* Returns an object matching a given title
Expand All @@ -27,24 +27,16 @@ import { SavedObjectsClientCommon, SavedObject } from '..';
* @param title {string}
* @returns {Promise<SavedObject|undefined>}
*/
export async function findByTitle(
client: SavedObjectsClientCommon,
title: string
): Promise<SavedObject<any> | void> {
if (!title) {
return Promise.resolve();
}

const savedObjects = await client.find({
type: 'index-pattern',
perPage: 10,
search: `"${title}"`,
searchFields: ['title'],
fields: ['title'],
});
export async function findByTitle(client: SavedObjectsClientCommon, title: string) {
if (title) {
const savedObjects = await client.find<IndexPatternSavedObjectAttrs>({
type: 'index-pattern',
perPage: 10,
search: `"${title}"`,
searchFields: ['title'],
fields: ['title'],
});

return find(
savedObjects,
(obj: SavedObject<any>) => obj.attributes.title.toLowerCase() === title.toLowerCase()
);
return savedObjects.find((obj) => obj.attributes.title.toLowerCase() === title.toLowerCase());
}
}
1 change: 1 addition & 0 deletions src/plugins/data/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const createStartContract = (): Start => {
SearchBar: jest.fn().mockReturnValue(null),
},
indexPatterns: ({
findByTitle: jest.fn((title) => ({ title, id: title })),
createField: jest.fn(() => {}),
createFieldList: jest.fn(() => []),
ensureDefaultIndexPattern: jest.fn(),
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,7 @@ export class IndexPatternsService {
// (undocumented)
ensureDefaultIndexPattern: EnsureDefaultIndexPattern;
fieldArrayToMap: (fields: FieldSpec[], fieldAttrs?: FieldAttrs | undefined) => Record<string, FieldSpec>;
findByTitle: (title: string, refresh?: boolean) => Promise<IndexPattern | undefined>;
get: (id: string) => Promise<IndexPattern>;
// Warning: (ae-forgotten-export) The symbol "IndexPatternSavedObjectAttrs" needs to be exported by the entry point index.d.ts
//
Expand Down
13 changes: 6 additions & 7 deletions src/plugins/vis_type_timeseries/public/metrics_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { toExpressionAst } from './to_ast';
import { VIS_EVENT_TO_TRIGGER, VisGroups, VisParams } from '../../visualizations/public';
import { getDataStart } from './services';
import { INDEXES_SEPARATOR } from '../common/constants';
import { IndexPattern } from '../../data/public';

export const metricsVisDefinition = {
name: 'metrics',
Expand Down Expand Up @@ -87,13 +88,11 @@ export const metricsVisDefinition = {
const indexes: string = params.index_pattern;

if (indexes) {
const cachedIndexes = await indexPatterns.getIdsWithTitle();
const ids = indexes
.split(INDEXES_SEPARATOR)
.map((title) => cachedIndexes.find((i) => i.title === title)?.id)
.filter((id) => id);

return Promise.all(ids.map((id) => indexPatterns.get(id!)));
return (
await Promise.all(
indexes.split(INDEXES_SEPARATOR).map((title) => indexPatterns.findByTitle(title))
alexwizp marked this conversation as resolved.
Show resolved Hide resolved
)
).filter((index) => Boolean(index)) as IndexPattern[];
}

return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ export class EsQueryParser {
const requestObject = requests.find((item) => getRequestName(item, index) === data.name);

if (requestObject) {
requestObject.dataObject.url = requestObject.url;
requestObject.dataObject.values = data.rawResponse;
}
});
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/vis_type_vega/public/data_model/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ interface Projection {
name: string;
}

interface RequestDataObject {
interface RequestDataObject<TUrlData = UrlObject> {
name?: string;
url?: TUrlData;
values: SearchResponse<unknown>;
}

Expand Down Expand Up @@ -186,7 +187,7 @@ export interface CacheBounds {
max: number;
}

interface Requests<TUrlData = UrlObject, TRequestDataObject = RequestDataObject> {
interface Requests<TUrlData = UrlObject, TRequestDataObject = RequestDataObject<TUrlData>> {
url: TUrlData;
name: string;
dataObject: TRequestDataObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,21 @@ describe('VegaParser._resolveEsQueries', () => {
'es',
check(
{ data: { name: 'requestId', url: { index: 'a' }, x: 1 } },
{ data: { name: 'requestId', values: [42], x: 1 } }
{ data: { name: 'requestId', url: { index: 'a', body: {} }, values: [42], x: 1 } }
)
);
test(
'es 2',
check(
{ data: { name: 'requestId', url: { '%type%': 'elasticsearch', index: 'a' } } },
{ data: { name: 'requestId', values: [42] } }
{ data: { name: 'requestId', url: { index: 'a', body: {} }, values: [42] } }
)
);
test(
'es arr',
check(
{ arr: [{ data: { name: 'requestId', url: { index: 'a' }, x: 1 } }] },
{ arr: [{ data: { name: 'requestId', values: [42], x: 1 } }] }
{ arr: [{ data: { name: 'requestId', url: { index: 'a', body: {} }, values: [42], x: 1 } }] }
)
);
test(
Expand Down
125 changes: 125 additions & 0 deletions src/plugins/vis_type_vega/public/lib/extract_index_pattern.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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 { dataPluginMock } from '../../../data/public/mocks';
import { extractIndexPatternsFromSpec } from './extract_index_pattern';
import { setData } from '../services';

import type { VegaSpec } from '../data_model/types';

const getMockedSpec = (mockedObj: any) => (mockedObj as unknown) as VegaSpec;

describe('extractIndexPatternsFromSpec', () => {
const dataStart = dataPluginMock.createStartContract();

beforeAll(() => {
setData(dataStart);
});

test('should not throw errors if no index is specified', async () => {
const spec = getMockedSpec({
data: {},
});

const indexes = await extractIndexPatternsFromSpec(spec);

expect(indexes).toMatchInlineSnapshot(`Array []`);
});

test('should extract single index pattern', async () => {
const spec = getMockedSpec({
data: {
url: {
index: 'test',
},
},
});

const indexes = await extractIndexPatternsFromSpec(spec);

expect(indexes).toMatchInlineSnapshot(`
Array [
Object {
"id": "test",
"title": "test",
},
]
`);
});

test('should extract multiple index patterns', async () => {
const spec = getMockedSpec({
data: [
{
url: {
index: 'test1',
},
},
{
url: {
index: 'test2',
},
},
],
});

const indexes = await extractIndexPatternsFromSpec(spec);

expect(indexes).toMatchInlineSnapshot(`
Array [
Object {
"id": "test1",
"title": "test1",
},
Object {
"id": "test2",
"title": "test2",
},
]
`);
});

test('should filter empty values', async () => {
const spec = getMockedSpec({
data: [
{
url: {
wrong: 'wrong',
},
},
{
url: {
index: 'ok',
},
},
],
});

const indexes = await extractIndexPatternsFromSpec(spec);

expect(indexes).toMatchInlineSnapshot(`
Array [
Object {
"id": "ok",
"title": "ok",
},
]
`);
});
});
46 changes: 46 additions & 0 deletions src/plugins/vis_type_vega/public/lib/extract_index_pattern.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 { getData } from '../services';

import type { Data, VegaSpec } from '../data_model/types';
import type { IndexPattern } from '../../../data/public';

export const extractIndexPatternsFromSpec = async (spec: VegaSpec) => {
const { indexPatterns } = getData();
let data: Data[] = [];

if (Array.isArray(spec.data)) {
data = spec.data;
} else if (spec.data) {
data = [spec.data];
}

return (
await Promise.all(
data.reduce<Data[]>((accumulator, currentValue) => {
if (currentValue.url?.index) {
accumulator.push(indexPatterns.findByTitle(currentValue.url.index));
}

return accumulator;
}, [])
)
).filter((index) => Boolean(index)) as IndexPattern[];
};
2 changes: 0 additions & 2 deletions src/plugins/vis_type_vega/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { Setup as InspectorSetup } from '../../inspector/public';
import {
setNotifications,
setData,
setSavedObjects,
setInjectedVars,
setUISettings,
setMapsLegacyConfig,
Expand Down Expand Up @@ -100,7 +99,6 @@ export class VegaPlugin implements Plugin<Promise<void>, void> {

public start(core: CoreStart, { data }: VegaPluginStartDependencies) {
setNotifications(core.notifications);
setSavedObjects(core.savedObjects);
setData(data);
setInjectedMetadata(core.injectedMetadata);
}
Expand Down
Loading