Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index pattern public api => common #68289

Merged
merged 33 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a2da8d3
partial progress
mattkime Jun 4, 2020
3a40208
front end code should be working again
mattkime Jun 5, 2020
cb9e775
restore unknown field toast
mattkime Jun 5, 2020
5de1b21
fix jest test mock
mattkime Jun 5, 2020
2683b7a
Revert "fix jest test mock"
mattkime Jun 5, 2020
2913835
fix jest test mock
mattkime Jun 5, 2020
5cfaee5
add onError callback, remove comments
mattkime Jun 5, 2020
f963496
Merge branch 'master' into index_pattern_server_api_2
mattkime Jun 5, 2020
ec2f0c6
better define fieldFormats common start service
mattkime Jun 5, 2020
ebc56d8
dependency cleanup
mattkime Jun 5, 2020
c994a1f
Revert "dependency cleanup"
mattkime Jun 5, 2020
f097b31
update api docs
mattkime Jun 6, 2020
6998499
remove commented lines
mattkime Jun 6, 2020
01e6e2f
typefix
mattkime Jun 6, 2020
f75873f
refactor core usage
mattkime Jun 6, 2020
49a06d7
Merge branch 'master' into index_pattern_server_api_2
elasticmachine Jun 8, 2020
ba4d131
Merge branch 'master' into index_pattern_server_api_2
mattkime Jun 8, 2020
30bf1e0
more specific dependencies
mattkime Jun 8, 2020
8e0f3b8
Merge branch 'master' into index_pattern_server_api_2
elasticmachine Jun 8, 2020
bce0d2e
Revert "more specific dependencies"
mattkime Jun 8, 2020
90a8695
more specific core usage
mattkime Jun 8, 2020
240371a
Merge branch 'index_pattern_server_api_2' of github.com:mattkime/kiba…
mattkime Jun 8, 2020
d8b1999
more specific core usage
mattkime Jun 9, 2020
6c30c28
move field_mapping code from kibana_utils to data
mattkime Jun 9, 2020
f96d3c3
fix jest test mock
mattkime Jun 9, 2020
5beadc0
Merge branch 'master' into index_pattern_server_api_2
elasticmachine Jun 9, 2020
7aeb8f2
remove fields index file, was only re-exporting from common
mattkime Jun 9, 2020
ace29ae
Merge branch 'index_pattern_server_api_2' of github.com:mattkime/kiba…
mattkime Jun 9, 2020
e3a16b3
remove index pattern lib dir which just re-exports from common
mattkime Jun 9, 2020
0b7f84f
remove re-export
mattkime Jun 10, 2020
885c8ce
remove re-export
mattkime Jun 10, 2020
03e849d
Revert "remove re-export"
mattkime Jun 10, 2020
985e90a
Revert "remove re-export"
mattkime Jun 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
getIndexPatternFieldListCreator: ({ fieldFormats, toastNotifications, }: FieldListDependencies) => CreateIndexPatternFieldList
getIndexPatternFieldListCreator: ({ fieldFormats, onNotification, }: FieldListDependencies) => CreateIndexPatternFieldList
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Constructs a new instance of the `IndexPattern` class
<b>Signature:</b>

```typescript
constructor(id: string | undefined, getConfig: any, savedObjectsClient: SavedObjectsClientContract, apiClient: IIndexPatternsApiClient, patternCache: PatternCache);
constructor(id: string | undefined, getConfig: any, savedObjectsClient: SavedObjectsClientContract, apiClient: IIndexPatternsApiClient, patternCache: PatternCache, fieldFormats: FieldFormatsStartCommon, onNotification: OnNotification, onError: OnError);
```

## Parameters
Expand All @@ -21,4 +21,7 @@ constructor(id: string | undefined, getConfig: any, savedObjectsClient: SavedObj
| savedObjectsClient | <code>SavedObjectsClientContract</code> | |
| apiClient | <code>IIndexPatternsApiClient</code> | |
| patternCache | <code>PatternCache</code> | |
| fieldFormats | <code>FieldFormatsStartCommon</code> | |
| onNotification | <code>OnNotification</code> | |
| onError | <code>OnError</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)](./kibana-plugin-plugins-data-public.indexpattern._constructor_.md) | | Constructs a new instance of the <code>IndexPattern</code> class |
| [(constructor)(id, getConfig, savedObjectsClient, apiClient, patternCache, fieldFormats, onNotification, onError)](./kibana-plugin-plugins-data-public.indexpattern._constructor_.md) | | Constructs a new instance of the <code>IndexPattern</code> class |
## Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Constructs a new instance of the `Field` class
<b>Signature:</b>

```typescript
constructor(indexPattern: IndexPattern, spec: FieldSpec | Field, shortDotsEnable: boolean, { fieldFormats, toastNotifications }: FieldDependencies);
constructor(indexPattern: IIndexPattern, spec: FieldSpec | Field, shortDotsEnable: boolean, { fieldFormats, onNotification }: FieldDependencies);
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| indexPattern | <code>IndexPattern</code> | |
| indexPattern | <code>IIndexPattern</code> | |
| spec | <code>FieldSpec &#124; Field</code> | |
| shortDotsEnable | <code>boolean</code> | |
| { fieldFormats, toastNotifications } | <code>FieldDependencies</code> | |
| { fieldFormats, onNotification } | <code>FieldDependencies</code> | |

Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
indexPattern?: IndexPattern;
indexPattern?: IIndexPattern;
```
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export declare class Field implements IFieldType
| Constructor | Modifiers | Description |
| --- | --- | --- |
| [(constructor)(indexPattern, spec, shortDotsEnable, { fieldFormats, toastNotifications })](./kibana-plugin-plugins-data-public.indexpatternfield._constructor_.md) | | Constructs a new instance of the <code>Field</code> class |
| [(constructor)(indexPattern, spec, shortDotsEnable, { fieldFormats, onNotification })](./kibana-plugin-plugins-data-public.indexpatternfield._constructor_.md) | | Constructs a new instance of the <code>Field</code> class |
## Properties
Expand All @@ -28,7 +28,7 @@ export declare class Field implements IFieldType
| [esTypes](./kibana-plugin-plugins-data-public.indexpatternfield.estypes.md) | | <code>string[]</code> | |
| [filterable](./kibana-plugin-plugins-data-public.indexpatternfield.filterable.md) | | <code>boolean</code> | |
| [format](./kibana-plugin-plugins-data-public.indexpatternfield.format.md) | | <code>any</code> | |
| [indexPattern](./kibana-plugin-plugins-data-public.indexpatternfield.indexpattern.md) | | <code>IndexPattern</code> | |
| [indexPattern](./kibana-plugin-plugins-data-public.indexpatternfield.indexpattern.md) | | <code>IIndexPattern</code> | |
| [lang](./kibana-plugin-plugins-data-public.indexpatternfield.lang.md) | | <code>string</code> | |
| [name](./kibana-plugin-plugins-data-public.indexpatternfield.name.md) | | <code>string</code> | |
| [readFromDocValues](./kibana-plugin-plugins-data-public.indexpatternfield.readfromdocvalues.md) | | <code>boolean</code> | |
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/field_formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ export {
// Used in data plugin only
FieldFormatInstanceType,
IFieldFormat,
FieldFormatsStartCommon,
} from './types';
3 changes: 3 additions & 0 deletions src/plugins/data/common/field_formats/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/
import { FieldFormat } from './field_format';
import { FieldFormatsRegistry } from './field_formats_registry';

/** @public **/
export type FieldFormatsContentType = 'html' | 'text';
Expand Down Expand Up @@ -99,3 +100,5 @@ export interface IFieldFormatMetaParams {
basePath?: string;
};
}

export type FieldFormatsStartCommon = Omit<FieldFormatsRegistry, 'init' & 'register'>;
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
*/

import { Field } from './field';
import { IndexPattern } from '..';
import { notificationServiceMock } from '../../../../../core/public/mocks';
import { FieldFormatsStart } from '../../field_formats';
import { IndexPattern } from '../index_patterns';
import { FieldFormatsStartCommon } from '../..';
import { KBN_FIELD_TYPES } from '../../../common';

describe('Field', function () {
Expand All @@ -34,8 +33,8 @@ describe('Field', function () {
{ ...fieldValues, ...values },
false,
{
fieldFormats: {} as FieldFormatsStart,
toastNotifications: notificationServiceMock.createStartContract().toasts,
fieldFormats: {} as FieldFormatsStartCommon,
onNotification: () => {},
}
);
}
Expand Down Expand Up @@ -215,8 +214,8 @@ describe('Field', function () {

it('exports the property to JSON', () => {
const field = new Field({ fieldFormatMap: { name: {} } } as IndexPattern, fieldValues, false, {
fieldFormats: {} as FieldFormatsStart,
toastNotifications: notificationServiceMock.createStartContract().toasts,
fieldFormats: {} as FieldFormatsStartCommon,
onNotification: () => {},
});
expect(flatten(field)).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@
*/

import { i18n } from '@kbn/i18n';
import { ToastsStart } from 'kibana/public';
// @ts-ignore
import { ObjDefine } from './obj_define';
import { IndexPattern } from '../index_patterns';
import { IIndexPattern } from '../../types';
import {
IFieldType,
getKbnFieldType,
IFieldSubType,
FieldFormat,
shortenDottedString,
} from '../../../common';
import { FieldFormatsStart } from '../../field_formats';
import { OnNotification } from '../types';
import { FieldFormatsStartCommon } from '../../field_formats';

export type FieldSpec = Record<string, any>;

interface FieldDependencies {
fieldFormats: FieldFormatsStart;
toastNotifications: ToastsStart;
fieldFormats: FieldFormatsStartCommon;
onNotification: OnNotification;
}

export class Field implements IFieldType {
Expand All @@ -55,17 +55,17 @@ export class Field implements IFieldType {
scripted?: boolean;
subType?: IFieldSubType;
displayName?: string;
indexPattern?: IndexPattern;
indexPattern?: IIndexPattern;
readFromDocValues?: boolean;
format: any;
$$spec: FieldSpec;
conflictDescriptions?: Record<string, string[]>;

constructor(
indexPattern: IndexPattern,
indexPattern: IIndexPattern,
spec: FieldSpec | Field,
shortDotsEnable: boolean,
{ fieldFormats, toastNotifications }: FieldDependencies
{ fieldFormats, onNotification }: FieldDependencies
) {
// unwrap old instances of Field
if (spec instanceof Field) spec = spec.$$spec;
Expand All @@ -90,11 +90,7 @@ export class Field implements IFieldType {
values: { name: spec.name, title: indexPattern.title },
defaultMessage: 'Field {name} in indexPattern {title} is using an unknown field type.',
});

toastNotifications.addDanger({
title,
text,
});
onNotification({ title, text, color: 'danger', iconType: 'alert' });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we throw here and let the caller handle the exception. instead of passing onNotification in they can wrap code in try/cache

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good idea but I'd prefer to address it in another ticket as this PR aims to be a minimal set of changes required to provide a common api. I think I'd need to examine all places where index patterns are used to ensure errors are being caught. I've added a ticket to #67920

}

if (!type) type = getKbnFieldType('unknown');
Expand All @@ -103,7 +99,7 @@ export class Field implements IFieldType {

if (!FieldFormat.isInstanceOfFieldFormat(format)) {
format =
indexPattern.fieldFormatMap[spec.name] ||
(indexPattern.fieldFormatMap && indexPattern.fieldFormatMap[spec.name]) ||
fieldFormats.getDefaultInstance(spec.type, spec.esTypes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
*/

import { findIndex } from 'lodash';
import { ToastsStart } from 'kibana/public';
import { IndexPattern } from '../index_patterns';
import { IIndexPattern } from '../../types';
import { IFieldType } from '../../../common';
import { Field, FieldSpec } from './field';
import { FieldFormatsStart } from '../../field_formats';
import { OnNotification } from '../types';
import { FieldFormatsStartCommon } from '../../field_formats';

type FieldMap = Map<Field['name'], Field>;

interface FieldListDependencies {
fieldFormats: FieldFormatsStart;
toastNotifications: ToastsStart;
fieldFormats: FieldFormatsStartCommon;
onNotification: OnNotification;
}

export interface IIndexPatternFieldList extends Array<Field> {
Expand All @@ -40,19 +40,19 @@ export interface IIndexPatternFieldList extends Array<Field> {
}

export type CreateIndexPatternFieldList = (
indexPattern: IndexPattern,
indexPattern: IIndexPattern,
specs?: FieldSpec[],
shortDotsEnable?: boolean
) => IIndexPatternFieldList;

export const getIndexPatternFieldListCreator = ({
fieldFormats,
toastNotifications,
onNotification,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on this level we can then remove onNotification and leave catching the exception to the caller.

}: FieldListDependencies): CreateIndexPatternFieldList => (...fieldListParams) => {
class FieldList extends Array<Field> implements IIndexPatternFieldList {
private byName: FieldMap = new Map();
private groups: Map<Field['type'], FieldMap> = new Map();
private indexPattern: IndexPattern;
private indexPattern: IIndexPattern;
private shortDotsEnable: boolean;
private setByName = (field: Field) => this.byName.set(field.name, field);
private setByGroup = (field: Field) => {
Expand All @@ -63,7 +63,7 @@ export const getIndexPatternFieldListCreator = ({
};
private removeByGroup = (field: IFieldType) => this.groups.get(field.type)!.delete(field.name);

constructor(indexPattern: IndexPattern, specs: FieldSpec[] = [], shortDotsEnable = false) {
constructor(indexPattern: IIndexPattern, specs: FieldSpec[] = [], shortDotsEnable = false) {
super();
this.indexPattern = indexPattern;
this.shortDotsEnable = shortDotsEnable;
Expand All @@ -76,7 +76,7 @@ export const getIndexPatternFieldListCreator = ({
add = (field: FieldSpec) => {
const newField = new Field(this.indexPattern, field, this.shortDotsEnable, {
fieldFormats,
toastNotifications,
onNotification,
});
this.push(newField);
this.setByName(newField);
Expand All @@ -94,7 +94,7 @@ export const getIndexPatternFieldListCreator = ({
update = (field: FieldSpec) => {
const newField = new Field(this.indexPattern, field, this.shortDotsEnable, {
fieldFormats,
toastNotifications,
onNotification,
});
const index = this.findIndex((f) => f.name === newField.name);
this.splice(index, 1, newField);
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/common/index_patterns/fields/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

export * from './types';
export { isFilterable, isNestedField } from './utils';
export * from './field_list';
export * from './field';
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
* under the License.
*/

import { IndexPattern } from './index_pattern';
import { GetFieldsOptions, IIndexPatternsApiClient } from './index_patterns_api_client';
import { GetFieldsOptions, IIndexPatternsApiClient, IndexPattern } from '.';

/** @internal */
export const createFieldsFetcher = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.
*/

import { contains } from 'lodash';
import { CoreStart } from 'kibana/public';
import { IndexPatternsContract } from './index_patterns';

export type EnsureDefaultIndexPattern = () => Promise<unknown> | undefined;

export const createEnsureDefaultIndexPattern = (
uiSettings: CoreStart['uiSettings'],
onRedirectNoIndexPattern: () => Promise<unknown> | void
) => {
/**
* Checks whether a default index pattern is set and exists and defines
* one otherwise.
*/
return async function ensureDefaultIndexPattern(this: IndexPatternsContract) {
const patterns = await this.getIds();
let defaultId = uiSettings.get('defaultIndex');
let defined = !!defaultId;
const exists = contains(patterns, defaultId);

if (defined && !exists) {
uiSettings.remove('defaultIndex');
defaultId = defined = false;
}

if (defined) {
return;
}

// If there is any index pattern created, set the first as default
if (patterns.length >= 1) {
defaultId = patterns[0];
uiSettings.set('defaultIndex', defaultId);
} else {
return onRedirectNoIndexPattern();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets have this method always return and the caller decide what to do (no need to pass in callbacks)

}
};
};
26 changes: 26 additions & 0 deletions src/plugins/data/common/index_patterns/index_patterns/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 * from './index_patterns_api_client';
export * from './types';
export * from './_pattern_cache';
export * from './flatten_hit';
export * from './format_hit';
export * from './index_pattern';
export * from './index_patterns';
Loading