Skip to content

Commit

Permalink
IndexPattern class - no longer use getConfig or uiSettingsValues (#…
Browse files Browse the repository at this point in the history
…75717)

* remove getConfig and uiSettingsValues from IndexPattern class
  • Loading branch information
mattkime authored Aug 25, 2020
1 parent 446c523 commit 7fa23a4
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Constructs a new instance of the `IndexPattern` class
<b>Signature:</b>

```typescript
constructor(id: string | undefined, { getConfig, savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError, uiSettingsValues, }: IndexPatternDeps);
constructor(id: string | undefined, { savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError, shortDotsEnable, metaFields, }: IndexPatternDeps);
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| id | <code>string &#124; undefined</code> | |
| { getConfig, savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError, uiSettingsValues, } | <code>IndexPatternDeps</code> | |
| { savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError, shortDotsEnable, metaFields, } | <code>IndexPatternDeps</code> | |

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export declare class IndexPattern implements IIndexPattern
| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)(id, { getConfig, savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError, uiSettingsValues, })](./kibana-plugin-plugins-data-public.indexpattern._constructor_.md) | | Constructs a new instance of the <code>IndexPattern</code> class |
| [(constructor)(id, { savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError, shortDotsEnable, metaFields, })](./kibana-plugin-plugins-data-public.indexpattern._constructor_.md) | | Constructs a new instance of the <code>IndexPattern</code> class |
## Properties
Expand Down
27 changes: 27 additions & 0 deletions src/plugins/data/common/field_formats/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.
*/

export class FieldFormatNotFoundError extends Error {
public readonly formatId: string;
constructor(message: string, formatId: string) {
super(message);
this.name = 'FieldFormatNotFoundError';
this.formatId = formatId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { FieldFormat } from './field_format';
import { SerializedFieldFormat } from '../../../expressions/common/types';
import { ES_FIELD_TYPES, KBN_FIELD_TYPES } from '../kbn_field_types/types';
import { UI_SETTINGS } from '../constants';
import { FieldFormatNotFoundError } from '../field_formats';

export class FieldFormatsRegistry {
protected fieldFormats: Map<FieldFormatId, FieldFormatInstanceType> = new Map();
Expand Down Expand Up @@ -161,7 +162,7 @@ export class FieldFormatsRegistry {
const ConcreteFieldFormat = this.getType(formatId);

if (!ConcreteFieldFormat) {
throw new Error(`Field Format '${formatId}' not found!`);
throw new FieldFormatNotFoundError(`Field Format '${formatId}' not found!`, formatId);
}

return new ConcreteFieldFormat(params, this.getConfig);
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/common/field_formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ export {
IFieldFormat,
FieldFormatsStartCommon,
} from './types';

export * from './errors';
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { GetFieldsOptions, IIndexPatternsApiClient } from '../types';
export const createFieldsFetcher = (
indexPattern: IndexPattern,
apiClient: IIndexPatternsApiClient,
metaFields: string
metaFields: string[] = []
) => {
const fieldFetcher = {
fetch: (options: GetFieldsOptions) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { fieldFormatsMock } from '../../field_formats/mocks';

class MockFieldFormatter {}

fieldFormatsMock.getType = jest.fn().mockImplementation(() => MockFieldFormatter);
fieldFormatsMock.getInstance = jest.fn().mockImplementation(() => new MockFieldFormatter()) as any;

jest.mock('../../field_mapping', () => {
const originalModule = jest.requireActual('../../field_mapping');
Expand Down Expand Up @@ -89,10 +89,6 @@ const patternCache = {
clearAll: jest.fn(),
};

const config = {
get: jest.fn(),
};

const apiClient = {
_getUrl: jest.fn(),
getFieldsForTimePattern: jest.fn(),
Expand All @@ -102,14 +98,14 @@ const apiClient = {
// helper function to create index patterns
function create(id: string, payload?: any): Promise<IndexPattern> {
const indexPattern = new IndexPattern(id, {
getConfig: (cfg: any) => config.get(cfg),
savedObjectsClient: savedObjectsClient as any,
apiClient,
patternCache,
fieldFormats: fieldFormatsMock,
onNotification: () => {},
onError: () => {},
uiSettingsValues: { shortDotsEnable: false, metaFields: [] },
shortDotsEnable: false,
metaFields: [],
});

setDocsourcePayload(id, payload);
Expand Down Expand Up @@ -391,29 +387,29 @@ describe('IndexPattern', () => {
});
// Create a normal index pattern
const pattern = new IndexPattern('foo', {
getConfig: (cfg: any) => config.get(cfg),
savedObjectsClient: savedObjectsClient as any,
apiClient,
patternCache,
fieldFormats: fieldFormatsMock,
onNotification: () => {},
onError: () => {},
uiSettingsValues: { shortDotsEnable: false, metaFields: [] },
shortDotsEnable: false,
metaFields: [],
});
await pattern.init();

expect(get(pattern, 'version')).toBe('fooa');

// Create the same one - we're going to handle concurrency
const samePattern = new IndexPattern('foo', {
getConfig: (cfg: any) => config.get(cfg),
savedObjectsClient: savedObjectsClient as any,
apiClient,
patternCache,
fieldFormats: fieldFormatsMock,
onNotification: () => {},
onError: () => {},
uiSettingsValues: { shortDotsEnable: false, metaFields: [] },
shortDotsEnable: false,
metaFields: [],
});
await samePattern.init();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@ import { i18n } from '@kbn/i18n';
import { SavedObjectsClientCommon } from '../..';
import { DuplicateField, SavedObjectNotFound } from '../../../../kibana_utils/common';

import { ES_FIELD_TYPES, KBN_FIELD_TYPES, IIndexPattern } from '../../../common';
import {
ES_FIELD_TYPES,
KBN_FIELD_TYPES,
IIndexPattern,
FieldFormatNotFoundError,
} from '../../../common';
import { findByTitle } from '../utils';
import { IndexPatternMissingIndices } from '../lib';
import { IndexPatternField, IIndexPatternFieldList, FieldList } from '../fields';
import { createFieldsFetcher } from './_fields_fetcher';
import { formatHitProvider } from './format_hit';
import { flattenHitWrapper } from './flatten_hit';
import {
OnNotification,
OnError,
UiSettingsCommon,
IIndexPatternsApiClient,
IndexPatternAttributes,
} from '../types';
import { OnNotification, OnError, IIndexPatternsApiClient, IndexPatternAttributes } from '../types';
import { FieldFormatsStartCommon, FieldFormat } from '../../field_formats';
import { PatternCache } from './_pattern_cache';
import { expandShorthand, FieldMappingSpec, MappingObject } from '../../field_mapping';
Expand All @@ -44,21 +43,16 @@ import { SerializedFieldFormat } from '../../../../expressions/common';

const MAX_ATTEMPTS_TO_RESOLVE_CONFLICTS = 3;
const savedObjectType = 'index-pattern';
interface IUiSettingsValues {
[key: string]: any;
shortDotsEnable: any;
metaFields: any;
}

interface IndexPatternDeps {
getConfig: UiSettingsCommon['get'];
savedObjectsClient: SavedObjectsClientCommon;
apiClient: IIndexPatternsApiClient;
patternCache: PatternCache;
fieldFormats: FieldFormatsStartCommon;
onNotification: OnNotification;
onError: OnError;
uiSettingsValues: IUiSettingsValues;
shortDotsEnable: boolean;
metaFields: string[];
}

export class IndexPattern implements IIndexPattern {
Expand All @@ -78,7 +72,6 @@ export class IndexPattern implements IIndexPattern {
private version: string | undefined;
private savedObjectsClient: SavedObjectsClientCommon;
private patternCache: PatternCache;
private getConfig: UiSettingsCommon['get'];
private sourceFilters?: SourceFilter[];
private originalBody: { [key: string]: any } = {};
public fieldsFetcher: any; // probably want to factor out any direct usage and change to private
Expand All @@ -87,7 +80,6 @@ export class IndexPattern implements IIndexPattern {
private onNotification: OnNotification;
private onError: OnError;
private apiClient: IIndexPatternsApiClient;
private uiSettingsValues: IUiSettingsValues;

private mapping: MappingObject = expandShorthand({
title: ES_FIELD_TYPES.TEXT,
Expand All @@ -114,35 +106,31 @@ export class IndexPattern implements IIndexPattern {
constructor(
id: string | undefined,
{
getConfig,
savedObjectsClient,
apiClient,
patternCache,
fieldFormats,
onNotification,
onError,
uiSettingsValues,
shortDotsEnable = false,
metaFields = [],
}: IndexPatternDeps
) {
this.id = id;
this.savedObjectsClient = savedObjectsClient;
this.patternCache = patternCache;
// instead of storing config we rather store the getter only as np uiSettingsClient has circular references
// which cause problems when being consumed from angular
this.getConfig = getConfig;
this.fieldFormats = fieldFormats;
this.onNotification = onNotification;
this.onError = onError;
this.uiSettingsValues = uiSettingsValues;

this.shortDotsEnable = uiSettingsValues.shortDotsEnable;
this.metaFields = uiSettingsValues.metaFields;
this.shortDotsEnable = shortDotsEnable;
this.metaFields = metaFields;

this.fields = new FieldList(this, [], this.shortDotsEnable, this.onUnknownType);

this.apiClient = apiClient;
this.fieldsFetcher = createFieldsFetcher(this, apiClient, uiSettingsValues.metaFields);
this.flattenHit = flattenHitWrapper(this, uiSettingsValues.metaFields);
this.fieldsFetcher = createFieldsFetcher(this, apiClient, metaFields);
this.flattenHit = flattenHitWrapper(this, metaFields);
this.formatHit = formatHitProvider(
this,
fieldFormats.getDefaultInstance(KBN_FIELD_TYPES.STRING)
Expand All @@ -157,15 +145,15 @@ export class IndexPattern implements IIndexPattern {
}

private deserializeFieldFormatMap(mapping: any) {
const FieldFormatter = this.fieldFormats.getType(mapping.id);

return (
FieldFormatter &&
new FieldFormatter(
mapping.params,
(key: string) => this.uiSettingsValues[key]?.userValue || this.uiSettingsValues[key]?.value
)
);
try {
return this.fieldFormats.getInstance(mapping.id, mapping.params);
} catch (err) {
if (err instanceof FieldFormatNotFoundError) {
return undefined;
} else {
throw err;
}
}
}

private isFieldRefreshRequired(specs?: FieldSpec[]): boolean {
Expand Down Expand Up @@ -513,17 +501,14 @@ export class IndexPattern implements IIndexPattern {
saveAttempts++ < MAX_ATTEMPTS_TO_RESOLVE_CONFLICTS
) {
const samePattern = new IndexPattern(this.id, {
getConfig: this.getConfig,
savedObjectsClient: this.savedObjectsClient,
apiClient: this.apiClient,
patternCache: this.patternCache,
fieldFormats: this.fieldFormats,
onNotification: this.onNotification,
onError: this.onError,
uiSettingsValues: {
shortDotsEnable: this.shortDotsEnable,
metaFields: this.metaFields,
},
shortDotsEnable: this.shortDotsEnable,
metaFields: this.metaFields,
});

return samePattern.init().then(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,16 @@ export class IndexPatternsService {
async specToIndexPattern(spec: IndexPatternSpec) {
const shortDotsEnable = await this.config.get(UI_SETTINGS.SHORT_DOTS_ENABLE);
const metaFields = await this.config.get(UI_SETTINGS.META_FIELDS);
const uiSettingsValues = await this.config.getAll();

const indexPattern = new IndexPattern(spec.id, {
getConfig: (cfg: any) => this.config.get(cfg),
savedObjectsClient: this.savedObjectsClient,
apiClient: this.apiClient,
patternCache: indexPatternCache,
fieldFormats: this.fieldFormats,
onNotification: this.onNotification,
onError: this.onError,
uiSettingsValues: { ...uiSettingsValues, shortDotsEnable, metaFields },
shortDotsEnable,
metaFields,
});

indexPattern.initFromSpec(spec);
Expand All @@ -205,17 +204,16 @@ export class IndexPatternsService {
async make(id?: string): Promise<IndexPattern> {
const shortDotsEnable = await this.config.get(UI_SETTINGS.SHORT_DOTS_ENABLE);
const metaFields = await this.config.get(UI_SETTINGS.META_FIELDS);
const uiSettingsValues = await this.config.getAll();

const indexPattern = new IndexPattern(id, {
getConfig: (cfg: any) => this.config.get(cfg),
savedObjectsClient: this.savedObjectsClient,
apiClient: this.apiClient,
patternCache: indexPatternCache,
fieldFormats: this.fieldFormats,
onNotification: this.onNotification,
onError: this.onError,
uiSettingsValues: { ...uiSettingsValues, shortDotsEnable, metaFields },
shortDotsEnable,
metaFields,
});

return indexPattern.init();
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/index_patterns/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export interface GetFieldsOptions {
type?: string;
params?: any;
lookBack?: boolean;
metaFields?: string;
metaFields?: string[];
}

export interface IIndexPatternsApiClient {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class DataPublicPlugin
onNotification: (toastInputFields) => {
notifications.toasts.add(toastInputFields);
},
onError: notifications.toasts.addError,
onError: notifications.toasts.addError.bind(notifications.toasts),
onRedirectNoIndexPattern: onRedirectNoIndexPattern(
application.capabilities,
application.navigateToApp,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ export type IMetricAggType = MetricAggType;
// @public (undocumented)
export class IndexPattern implements IIndexPattern {
// Warning: (ae-forgotten-export) The symbol "IndexPatternDeps" needs to be exported by the entry point index.d.ts
constructor(id: string | undefined, { getConfig, savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError, uiSettingsValues, }: IndexPatternDeps);
constructor(id: string | undefined, { savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError, shortDotsEnable, metaFields, }: IndexPatternDeps);
// (undocumented)
[key: string]: any;
// (undocumented)
Expand Down

0 comments on commit 7fa23a4

Please sign in to comment.