Skip to content

Commit

Permalink
[TSVB] Disable runtime fields showing up in TSVB (#90163)
Browse files Browse the repository at this point in the history
* [TSVB] Disable runtime fields showing up in TSVB

* add tests

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
alexwizp and kibanamachine authored Feb 4, 2021
1 parent df4eb0f commit b413d9d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
*/

import { from } from 'rxjs';
import { AbstractSearchStrategy, ReqFacade } from './abstract_search_strategy';
import {
AbstractSearchStrategy,
ReqFacade,
toSanitizedFieldType,
} from './abstract_search_strategy';
import type { VisPayload } from '../../../../common/types';
import type { IFieldType } from '../../../../../data/common';
import type { FieldSpec, RuntimeField } from '../../../../../data/common';

class FooSearchStrategy extends AbstractSearchStrategy {}

Expand Down Expand Up @@ -91,4 +96,66 @@ describe('AbstractSearchStrategy', () => {
}
);
});

describe('toSanitizedFieldType', () => {
const mockedField = {
lang: 'lang',
conflictDescriptions: {},
aggregatable: true,
name: 'name',
type: 'type',
esTypes: ['long', 'geo'],
} as FieldSpec;

test('should sanitize fields ', async () => {
const fields = [mockedField] as FieldSpec[];

expect(toSanitizedFieldType(fields)).toMatchInlineSnapshot(`
Array [
Object {
"label": "name",
"name": "name",
"type": "type",
},
]
`);
});

test('should filter runtime fields', async () => {
const fields: FieldSpec[] = [
{
...mockedField,
runtimeField: {} as RuntimeField,
},
];

expect(toSanitizedFieldType(fields)).toMatchInlineSnapshot(`Array []`);
});

test('should filter non-aggregatable fields', async () => {
const fields: FieldSpec[] = [
{
...mockedField,
aggregatable: false,
},
];

expect(toSanitizedFieldType(fields)).toMatchInlineSnapshot(`Array []`);
});

test('should filter nested fields', async () => {
const fields: FieldSpec[] = [
{
...mockedField,
subType: {
nested: {
path: 'path',
},
},
},
];

expect(toSanitizedFieldType(fields)).toMatchInlineSnapshot(`Array []`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

import type { FakeRequest, IUiSettingsClient, SavedObjectsClientContract } from 'kibana/server';

import { indexPatterns } from '../../../../../data/server';
import { indexPatterns, IndexPatternsFetcher } from '../../../../../data/server';

import type { Framework } from '../../../plugin';
import type { IndexPatternsFetcher, IFieldType } from '../../../../../data/server';
import type { VisPayload } from '../../../../common/types';
import type { IndexPatternsService } from '../../../../../data/common';
import type { SanitizedFieldType } from '../../../../common/types';
import type { FieldSpec, IndexPatternsService } from '../../../../../data/common';
import type { VisPayload, SanitizedFieldType } from '../../../../common/types';
import type { VisTypeTimeseriesRequestHandlerContext } from '../../../types';

/**
Expand All @@ -36,11 +34,15 @@ export interface ReqFacade<T = unknown> extends FakeRequest {
getIndexPatternsService: () => Promise<IndexPatternsService>;
}

const toSanitizedFieldType = (fields: IFieldType[]) => {
export const toSanitizedFieldType = (fields: FieldSpec[]) => {
return fields
.filter((field) => field.aggregatable && !indexPatterns.isNestedField(field))
.filter(
(field) =>
// Make sure to only include mapped fields, e.g. no index pattern runtime fields
!field.runtimeField && field.aggregatable && !indexPatterns.isNestedField(field)
)
.map(
(field: IFieldType) =>
(field) =>
({
name: field.name,
label: field.customLabel ?? field.name,
Expand Down Expand Up @@ -95,7 +97,7 @@ export abstract class AbstractSearchStrategy {

return toSanitizedFieldType(
kibanaIndexPattern
? kibanaIndexPattern.fields.getAll()
? kibanaIndexPattern.getNonScriptedFields()
: await indexPatternsFetcher!.getFieldsForWildcard({
pattern: indexPattern,
fieldCapsOptions: { allow_no_indices: true },
Expand Down

0 comments on commit b413d9d

Please sign in to comment.