Skip to content

Commit

Permalink
[Index patterns] Remove field cache (#82223)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkime authored Nov 23, 2020
1 parent 348f6b6 commit fd9f504
Show file tree
Hide file tree
Showing 23 changed files with 5,407 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

```typescript
getFieldAttrs: () => {
[x: string]: {
customLabel: string;
};
[x: string]: FieldAttrSet;
};
```
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export declare class IndexPattern implements IIndexPattern
| [flattenHit](./kibana-plugin-plugins-data-public.indexpattern.flattenhit.md) | | <code>(hit: Record&lt;string, any&gt;, deep?: boolean) =&gt; Record&lt;string, any&gt;</code> | |
| [formatField](./kibana-plugin-plugins-data-public.indexpattern.formatfield.md) | | <code>FormatFieldFn</code> | |
| [formatHit](./kibana-plugin-plugins-data-public.indexpattern.formathit.md) | | <code>{</code><br/><code> (hit: Record&lt;string, any&gt;, type?: string): any;</code><br/><code> formatField: FormatFieldFn;</code><br/><code> }</code> | |
| [getFieldAttrs](./kibana-plugin-plugins-data-public.indexpattern.getfieldattrs.md) | | <code>() =&gt; {</code><br/><code> [x: string]: {</code><br/><code> customLabel: string;</code><br/><code> };</code><br/><code> }</code> | |
| [getFieldAttrs](./kibana-plugin-plugins-data-public.indexpattern.getfieldattrs.md) | | <code>() =&gt; {</code><br/><code> [x: string]: FieldAttrSet;</code><br/><code> }</code> | |
| [getOriginalSavedObjectBody](./kibana-plugin-plugins-data-public.indexpattern.getoriginalsavedobjectbody.md) | | <code>() =&gt; {</code><br/><code> fieldAttrs?: string &#124; undefined;</code><br/><code> title?: string &#124; undefined;</code><br/><code> timeFieldName?: string &#124; undefined;</code><br/><code> intervalName?: string &#124; undefined;</code><br/><code> fields?: string &#124; undefined;</code><br/><code> sourceFilters?: string &#124; undefined;</code><br/><code> fieldFormatMap?: string &#124; undefined;</code><br/><code> typeMeta?: string &#124; undefined;</code><br/><code> type?: string &#124; undefined;</code><br/><code> }</code> | Get last saved saved object fields |
| [id](./kibana-plugin-plugins-data-public.indexpattern.id.md) | | <code>string</code> | |
| [intervalName](./kibana-plugin-plugins-data-public.indexpattern.intervalname.md) | | <code>string &#124; undefined</code> | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

```typescript
getFieldAttrs: () => {
[x: string]: {
customLabel: string;
};
[x: string]: FieldAttrSet;
};
```
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export declare class IndexPattern implements IIndexPattern
| [flattenHit](./kibana-plugin-plugins-data-server.indexpattern.flattenhit.md) | | <code>(hit: Record&lt;string, any&gt;, deep?: boolean) =&gt; Record&lt;string, any&gt;</code> | |
| [formatField](./kibana-plugin-plugins-data-server.indexpattern.formatfield.md) | | <code>FormatFieldFn</code> | |
| [formatHit](./kibana-plugin-plugins-data-server.indexpattern.formathit.md) | | <code>{</code><br/><code> (hit: Record&lt;string, any&gt;, type?: string): any;</code><br/><code> formatField: FormatFieldFn;</code><br/><code> }</code> | |
| [getFieldAttrs](./kibana-plugin-plugins-data-server.indexpattern.getfieldattrs.md) | | <code>() =&gt; {</code><br/><code> [x: string]: {</code><br/><code> customLabel: string;</code><br/><code> };</code><br/><code> }</code> | |
| [getFieldAttrs](./kibana-plugin-plugins-data-server.indexpattern.getfieldattrs.md) | | <code>() =&gt; {</code><br/><code> [x: string]: FieldAttrSet;</code><br/><code> }</code> | |
| [getOriginalSavedObjectBody](./kibana-plugin-plugins-data-server.indexpattern.getoriginalsavedobjectbody.md) | | <code>() =&gt; {</code><br/><code> fieldAttrs?: string &#124; undefined;</code><br/><code> title?: string &#124; undefined;</code><br/><code> timeFieldName?: string &#124; undefined;</code><br/><code> intervalName?: string &#124; undefined;</code><br/><code> fields?: string &#124; undefined;</code><br/><code> sourceFilters?: string &#124; undefined;</code><br/><code> fieldFormatMap?: string &#124; undefined;</code><br/><code> typeMeta?: string &#124; undefined;</code><br/><code> type?: string &#124; undefined;</code><br/><code> }</code> | Get last saved saved object fields |
| [id](./kibana-plugin-plugins-data-server.indexpattern.id.md) | | <code>string</code> | |
| [intervalName](./kibana-plugin-plugins-data-server.indexpattern.intervalname.md) | | <code>string &#124; undefined</code> | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import _, { each, reject } from 'lodash';
import { FieldAttrs } from '../..';
import { FieldAttrs, FieldAttrSet } from '../..';
import { DuplicateField } from '../../../../kibana_utils/common';

import { ES_FIELD_TYPES, KBN_FIELD_TYPES, IIndexPattern, IFieldType } from '../../../common';
Expand Down Expand Up @@ -135,8 +135,19 @@ export class IndexPattern implements IIndexPattern {
const newFieldAttrs = { ...this.fieldAttrs };

this.fields.forEach((field) => {
const attrs: FieldAttrSet = {};
let hasAttr = false;
if (field.customLabel) {
newFieldAttrs[field.name] = { customLabel: field.customLabel };
attrs.customLabel = field.customLabel;
hasAttr = true;
}
if (field.count) {
attrs.count = field.count;
hasAttr = true;
}

if (hasAttr) {
newFieldAttrs[field.name] = attrs;
} else {
delete newFieldAttrs[field.name];
}
Expand Down Expand Up @@ -298,7 +309,9 @@ export class IndexPattern implements IIndexPattern {
timeFieldName: this.timeFieldName,
intervalName: this.intervalName,
sourceFilters: this.sourceFilters ? JSON.stringify(this.sourceFilters) : undefined,
fields: this.fields ? JSON.stringify(this.fields) : undefined,
fields: this.fields
? JSON.stringify(this.fields.filter((field) => field.scripted))
: undefined,
fieldFormatMap,
type: this.type,
typeMeta: this.typeMeta ? JSON.stringify(this.typeMeta) : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,11 @@ describe('IndexPatterns', () => {

// Create a normal index patterns
const pattern = await indexPatterns.get('foo');

expect(pattern.version).toBe('fooa');
indexPatterns.clearCache();

// Create the same one - we're going to handle concurrency
const samePattern = await indexPatterns.get('foo');

expect(samePattern.version).toBe('fooaa');

// This will conflict because samePattern did a save (from refreshFields)
// but the resave should work fine
pattern.title = 'foo2';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,6 @@ export class IndexPatternsService {
}
};

private isFieldRefreshRequired(specs?: IndexPatternFieldMap): boolean {
if (!specs) {
return true;
}

return Object.values(specs).every((spec) => {
// See https://github.com/elastic/kibana/pull/8421
const hasFieldCaps = 'aggregatable' in spec && 'searchable' in spec;

// See https://github.com/elastic/kibana/pull/11969
const hasDocValuesFlag = 'readFromDocValues' in spec;

return !hasFieldCaps || !hasDocValuesFlag;
});
}

/**
* Get field list by providing { pattern }
* @param options
Expand Down Expand Up @@ -299,8 +283,8 @@ export class IndexPatternsService {
values: { id, title },
}),
});
throw err;
}
return fields;
};

/**
Expand All @@ -309,7 +293,11 @@ export class IndexPatternsService {
*/
fieldArrayToMap = (fields: FieldSpec[], fieldAttrs?: FieldAttrs) =>
fields.reduce<IndexPatternFieldMap>((collector, field) => {
collector[field.name] = { ...field, customLabel: fieldAttrs?.[field.name]?.customLabel };
collector[field.name] = {
...field,
customLabel: fieldAttrs?.[field.name]?.customLabel,
count: fieldAttrs?.[field.name]?.count,
};
return collector;
}, {});

Expand Down Expand Up @@ -372,25 +360,20 @@ export class IndexPatternsService {
? JSON.parse(savedObject.attributes.fieldAttrs)
: {};

const isFieldRefreshRequired = this.isFieldRefreshRequired(spec.fields);
let isSaveRequired = isFieldRefreshRequired;
try {
spec.fields = isFieldRefreshRequired
? await this.refreshFieldSpecMap(
spec.fields || {},
id,
spec.title as string,
{
pattern: title as string,
metaFields: await this.config.get(UI_SETTINGS.META_FIELDS),
type,
rollupIndex: typeMeta?.params?.rollupIndex,
},
spec.fieldAttrs
)
: spec.fields;
spec.fields = await this.refreshFieldSpecMap(
spec.fields || {},
id,
spec.title as string,
{
pattern: title as string,
metaFields: await this.config.get(UI_SETTINGS.META_FIELDS),
type,
rollupIndex: typeMeta?.params?.rollup_index,
},
spec.fieldAttrs
);
} catch (err) {
isSaveRequired = false;
if (err instanceof IndexPatternMissingIndices) {
this.onNotification({
title: (err as any).message,
Expand All @@ -412,23 +395,6 @@ export class IndexPatternsService {
: {};

const indexPattern = await this.create(spec, true);
if (isSaveRequired) {
try {
this.updateSavedObject(indexPattern);
} catch (err) {
this.onError(err, {
title: i18n.translate('data.indexPatterns.fetchFieldSaveErrorTitle', {
defaultMessage:
'Error saving after fetching fields for index pattern {title} (ID: {id})',
values: {
id: indexPattern.id,
title: indexPattern.title,
},
}),
});
}
}

indexPattern.resetOriginalSavedObjectBody();
return indexPattern;
};
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/data/common/index_patterns/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ export interface IndexPatternAttributes {
}

export interface FieldAttrs {
[key: string]: { customLabel: string };
[key: string]: FieldAttrSet;
}

export interface FieldAttrSet {
customLabel?: string;
count?: number;
}

export type OnNotification = (toastInputFields: ToastInputFields) => void;
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1151,9 +1151,7 @@ export class IndexPattern implements IIndexPattern {
};
// (undocumented)
getFieldAttrs: () => {
[x: string]: {
customLabel: string;
};
[x: string]: FieldAttrSet;
};
// (undocumented)
getFieldByName(name: string): IndexPatternField | undefined;
Expand Down Expand Up @@ -2366,6 +2364,7 @@ export const UI_SETTINGS: {
// src/plugins/data/common/es_query/filters/phrase_filter.ts:33:3 - (ae-forgotten-export) The symbol "PhraseFilterMeta" needs to be exported by the entry point index.d.ts
// src/plugins/data/common/es_query/filters/phrases_filter.ts:31:3 - (ae-forgotten-export) The symbol "PhrasesFilterMeta" needs to be exported by the entry point index.d.ts
// src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts:64:5 - (ae-forgotten-export) The symbol "FormatFieldFn" needs to be exported by the entry point index.d.ts
// src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts:135:7 - (ae-forgotten-export) The symbol "FieldAttrSet" needs to be exported by the entry point index.d.ts
// src/plugins/data/common/search/aggs/types.ts:113:51 - (ae-forgotten-export) The symbol "AggTypesRegistryStart" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/field_formats/field_formats_service.ts:67:3 - (ae-forgotten-export) The symbol "FormatFactory" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:66:23 - (ae-forgotten-export) The symbol "FILTERS" needs to be exported by the entry point index.d.ts
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/data/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,7 @@ export class IndexPattern implements IIndexPattern {
};
// (undocumented)
getFieldAttrs: () => {
[x: string]: {
customLabel: string;
};
[x: string]: FieldAttrSet;
};
// (undocumented)
getFieldByName(name: string): IndexPatternField | undefined;
Expand Down Expand Up @@ -1215,6 +1213,7 @@ export function usageProvider(core: CoreSetup_2): SearchUsage;
// src/plugins/data/common/es_query/filters/meta_filter.ts:54:3 - (ae-forgotten-export) The symbol "FilterMeta" needs to be exported by the entry point index.d.ts
// src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts:58:45 - (ae-forgotten-export) The symbol "IndexPatternFieldMap" needs to be exported by the entry point index.d.ts
// src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts:64:5 - (ae-forgotten-export) The symbol "FormatFieldFn" needs to be exported by the entry point index.d.ts
// src/plugins/data/common/index_patterns/index_patterns/index_pattern.ts:135:7 - (ae-forgotten-export) The symbol "FieldAttrSet" needs to be exported by the entry point index.d.ts
// src/plugins/data/server/index.ts:40:23 - (ae-forgotten-export) The symbol "buildCustomFilter" needs to be exported by the entry point index.d.ts
// src/plugins/data/server/index.ts:40:23 - (ae-forgotten-export) The symbol "buildFilter" needs to be exported by the entry point index.d.ts
// src/plugins/data/server/index.ts:71:21 - (ae-forgotten-export) The symbol "getEsQueryConfig" needs to be exported by the entry point index.d.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,6 @@ const mappingConflictHeader = i18n.translate(
}
);

const confirmMessage = i18n.translate('indexPatternManagement.editIndexPattern.refreshLabel', {
defaultMessage: 'This action resets the popularity counter of each field.',
});

const confirmModalOptionsRefresh = {
confirmButtonText: i18n.translate('indexPatternManagement.editIndexPattern.refreshButton', {
defaultMessage: 'Refresh',
}),
title: i18n.translate('indexPatternManagement.editIndexPattern.refreshHeader', {
defaultMessage: 'Refresh field list?',
}),
};

const confirmModalOptionsDelete = {
confirmButtonText: i18n.translate('indexPatternManagement.editIndexPattern.deleteButton', {
defaultMessage: 'Delete',
Expand Down Expand Up @@ -118,16 +105,6 @@ export const EditIndexPattern = withRouter(
setDefaultIndex(indexPattern.id || '');
}, [uiSettings, indexPattern.id]);

const refreshFields = () => {
overlays.openConfirm(confirmMessage, confirmModalOptionsRefresh).then(async (isConfirmed) => {
if (isConfirmed) {
await data.indexPatterns.refreshFields(indexPattern);
await data.indexPatterns.updateSavedObject(indexPattern);
setFields(indexPattern.getNonScriptedFields());
}
});
};

const removePattern = () => {
async function doRemove() {
if (indexPattern.id === defaultIndex) {
Expand Down Expand Up @@ -190,7 +167,6 @@ export const EditIndexPattern = withRouter(
<IndexHeader
indexPattern={indexPattern}
setDefault={setDefaultPattern}
refreshFields={refreshFields}
deleteIndexPatternClick={removePattern}
defaultIndex={defaultIndex}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ interface IndexHeaderProps {
indexPattern: IIndexPattern;
defaultIndex?: string;
setDefault?: () => void;
refreshFields?: () => void;
deleteIndexPatternClick?: () => void;
}

Expand All @@ -44,14 +43,6 @@ const setDefaultTooltip = i18n.translate(
}
);

const refreshAriaLabel = i18n.translate('indexPatternManagement.editIndexPattern.refreshAria', {
defaultMessage: 'Reload field list.',
});

const refreshTooltip = i18n.translate('indexPatternManagement.editIndexPattern.refreshTooltip', {
defaultMessage: 'Refresh field list.',
});

const removeAriaLabel = i18n.translate('indexPatternManagement.editIndexPattern.removeAria', {
defaultMessage: 'Remove index pattern.',
});
Expand All @@ -64,7 +55,6 @@ export function IndexHeader({
defaultIndex,
indexPattern,
setDefault,
refreshFields,
deleteIndexPatternClick,
}: IndexHeaderProps) {
return (
Expand All @@ -90,20 +80,6 @@ export function IndexHeader({
</EuiFlexItem>
)}

{refreshFields && (
<EuiFlexItem>
<EuiToolTip content={refreshTooltip}>
<EuiButtonIcon
color="text"
onClick={refreshFields}
iconType="refresh"
aria-label={refreshAriaLabel}
data-test-subj="refreshFieldsIndexPatternButton"
/>
</EuiToolTip>
</EuiFlexItem>
)}

{deleteIndexPatternClick && (
<EuiFlexItem>
<EuiToolTip content={removeTooltip}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default function ({ getService, getPageObjects }) {
// check that it is 0 (previous increase was cancelled
const popularity = await PageObjects.settings.getPopularity();
log.debug('popularity = ' + popularity);
expect(popularity).to.be('0');
expect(popularity).to.be('');
});

it('can be saved', async function () {
Expand Down
2 changes: 1 addition & 1 deletion test/functional/apps/timelion/_expression_typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default function ({ getPageObjects }) {
await PageObjects.timelion.updateExpression(',split');
await PageObjects.timelion.clickSuggestion();
const suggestions = await PageObjects.timelion.getSuggestionItemsText();
expect(suggestions.length).to.eql(52);
expect(suggestions.length).to.eql(51);
expect(suggestions[0].includes('@message.raw')).to.eql(true);
await PageObjects.timelion.clickSuggestion(10);
});
Expand Down
Loading

0 comments on commit fd9f504

Please sign in to comment.