Skip to content

Commit

Permalink
Add support for scripted fields and (#53948) (#54048)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdavies authored Jan 10, 2020
1 parent d811e4d commit 4636f10
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ const sampleIndexPatterns = {
{
name: 'source',
type: 'string',
aggregatable: true,
searchable: true,
aggregatable: false,
searchable: false,
scripted: true,
aggregationRestrictions: {
terms: {
agg: 'terms',
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,19 @@ 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(
state
? Object.values(state.layers)
.map(l => l.indexPatternId)
.concat(state.currentIndexPatternId)
: [indexPatternRefs[0].id]
: [defaultIndexPatternId || indexPatternRefs[0].id]
);

const currentIndexPatternId = requiredPatterns[0];
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface IndexPatternField {
type: string;
esTypes?: string[];
aggregatable: boolean;
scripted?: boolean;
searchable: boolean;
aggregationRestrictions?: AggregationRestrictions;
}
Expand Down

0 comments on commit 4636f10

Please sign in to comment.