Add support for scripted fields and (#53948)

default index pattern

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
Chris Davies 2020-01-06 14:55:51 -05:00 committed by GitHub
parent dbe0bfdf79
commit f08fc201c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 7 deletions

View file

@ -115,8 +115,12 @@ export function getIndexPatternDatasource({
const indexPatternDatasource: Datasource<IndexPatternPrivateState, IndexPatternPersistedState> = {
id: 'indexpattern',
initialize(state?: IndexPatternPersistedState) {
return loadInitialState({ state, savedObjectsClient });
async initialize(state?: IndexPatternPersistedState) {
return loadInitialState({
state,
savedObjectsClient,
defaultIndexPatternId: core.uiSettings.get('defaultIndex'),
});
},
getPersistableState({ currentIndexPatternId, layers }: IndexPatternPrivateState) {

View file

@ -114,8 +114,9 @@ const sampleIndexPatterns = {
{
name: 'source',
type: 'string',
aggregatable: true,
searchable: true,
aggregatable: false,
searchable: false,
scripted: true,
aggregationRestrictions: {
terms: {
agg: 'terms',
@ -196,7 +197,7 @@ describe('loader', () => {
expect(cache).toMatchObject(sampleIndexPatterns);
});
it('should not allow full text fields', async () => {
it('should allow scripted, but not full text fields', async () => {
const cache = await loadIndexPatterns({
cache: {},
patterns: ['a', 'b'],
@ -286,6 +287,26 @@ describe('loader', () => {
});
});
it('should use the default index pattern id, if provided', async () => {
const state = await loadInitialState({
defaultIndexPatternId: 'b',
savedObjectsClient: mockClient(),
});
expect(state).toMatchObject({
currentIndexPatternId: 'b',
indexPatternRefs: [
{ id: 'a', title: sampleIndexPatterns.a.title },
{ id: 'b', title: sampleIndexPatterns.b.title },
],
indexPatterns: {
b: sampleIndexPatterns.b,
},
layers: {},
showEmptyFields: false,
});
});
it('should initialize from saved state', async () => {
const savedState: IndexPatternPersistedState = {
currentIndexPatternId: 'b',

View file

@ -84,9 +84,11 @@ export async function loadIndexPatterns({
export async function loadInitialState({
state,
savedObjectsClient,
defaultIndexPatternId,
}: {
state?: IndexPatternPersistedState;
savedObjectsClient: SavedObjectsClient;
defaultIndexPatternId?: string;
}): Promise<IndexPatternPrivateState> {
const indexPatternRefs = await loadIndexPatternRefs(savedObjectsClient);
const requiredPatterns = _.unique(
@ -94,7 +96,7 @@ export async function loadInitialState({
? Object.values(state.layers)
.map(l => l.indexPatternId)
.concat(state.currentIndexPatternId)
: [indexPatternRefs[0].id]
: [defaultIndexPatternId || indexPatternRefs[0].id]
);
const currentIndexPatternId = requiredPatterns[0];
@ -280,7 +282,7 @@ function fromSavedObject(
type,
title: attributes.title,
fields: (JSON.parse(attributes.fields) as IndexPatternField[])
.filter(({ aggregatable }) => !!aggregatable)
.filter(({ aggregatable, scripted }) => !!aggregatable || !!scripted)
.concat(documentField),
typeMeta: attributes.typeMeta
? (JSON.parse(attributes.typeMeta) as SavedRestrictionsInfo)

View file

@ -39,6 +39,7 @@ export interface IndexPatternField {
type: string;
esTypes?: string[];
aggregatable: boolean;
scripted?: boolean;
searchable: boolean;
aggregationRestrictions?: AggregationRestrictions;
}