Skip to content

Commit

Permalink
Index pattern field list - transition away from extending array - int…
Browse files Browse the repository at this point in the history
…roduce and use getAll() (elastic#74718) (elastic#74756)

- Introduce `indexPattern.fields.getAll()` and use where possible
- Rename `index_patterns/fields/fields.mocks.ts.ts => index_patterns/fields/fields.mocks.ts`
- FieldSpec - make `count` and `scripted` fields optional
- use `indexPattern.fields.getByName` instead of filter where possible
  • Loading branch information
mattkime committed Aug 11, 2020
1 parent b012dfc commit f4b44c7
Show file tree
Hide file tree
Showing 38 changed files with 143 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [FieldList](./kibana-plugin-plugins-data-public.fieldlist.md) &gt; [getAll](./kibana-plugin-plugins-data-public.fieldlist.getall.md)

## FieldList.getAll property

<b>Signature:</b>

```typescript
readonly getAll: () => IndexPatternField[];
```
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export declare class FieldList extends Array<IndexPatternField> implements IInde
| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [add](./kibana-plugin-plugins-data-public.fieldlist.add.md) | | <code>(field: FieldSpec) =&gt; void</code> | |
| [getAll](./kibana-plugin-plugins-data-public.fieldlist.getall.md) | | <code>() =&gt; IndexPatternField[]</code> | |
| [getByName](./kibana-plugin-plugins-data-public.fieldlist.getbyname.md) | | <code>(name: IndexPatternField['name']) =&gt; IndexPatternField &#124; undefined</code> | |
| [getByType](./kibana-plugin-plugins-data-public.fieldlist.getbytype.md) | | <code>(type: IndexPatternField['type']) =&gt; any[]</code> | |
| [remove](./kibana-plugin-plugins-data-public.fieldlist.remove.md) | | <code>(field: IFieldType) =&gt; void</code> | |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) &gt; [IIndexPatternFieldList](./kibana-plugin-plugins-data-public.iindexpatternfieldlist.md) &gt; [getAll](./kibana-plugin-plugins-data-public.iindexpatternfieldlist.getall.md)

## IIndexPatternFieldList.getAll() method

<b>Signature:</b>

```typescript
getAll(): IndexPatternField[];
```
<b>Returns:</b>

`IndexPatternField[]`

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface IIndexPatternFieldList extends Array<IndexPatternField>
| Method | Description |
| --- | --- |
| [add(field)](./kibana-plugin-plugins-data-public.iindexpatternfieldlist.add.md) | |
| [getAll()](./kibana-plugin-plugins-data-public.iindexpatternfieldlist.getall.md) | |
| [getByName(name)](./kibana-plugin-plugins-data-public.iindexpatternfieldlist.getbyname.md) | |
| [getByType(type)](./kibana-plugin-plugins-data-public.iindexpatternfieldlist.getbytype.md) | |
| [remove(field)](./kibana-plugin-plugins-data-public.iindexpatternfieldlist.remove.md) | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { buildExistsFilter, getExistsFilterField } from './exists_filter';
import { IIndexPattern } from '../../index_patterns';
import { fields } from '../../index_patterns/fields/fields.mocks.ts';
import { fields } from '../../index_patterns/fields/fields.mocks';

describe('exists filter', function () {
const indexPattern: IIndexPattern = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { buildPhraseFilter } from './phrase_filter';
import { buildQueryFilter } from './query_string_filter';
import { getFilterField } from './get_filter_field';
import { IIndexPattern } from '../../index_patterns';
import { fields } from '../../index_patterns/fields/fields.mocks.ts';
import { fields } from '../../index_patterns/fields/fields.mocks';

describe('getFilterField', function () {
const indexPattern: IIndexPattern = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { buildPhrasesFilter, getPhrasesFilterField } from './phrases_filter';
import { IIndexPattern } from '../../index_patterns';
import { fields } from '../../index_patterns/fields/fields.mocks.ts';
import { fields } from '../../index_patterns/fields/fields.mocks';

describe('phrases filter', function () {
const indexPattern: IIndexPattern = ({
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/common/index_patterns/fields/field_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type FieldMap = Map<IndexPatternField['name'], IndexPatternField>;

export interface IIndexPatternFieldList extends Array<IndexPatternField> {
add(field: FieldSpec): void;
getAll(): IndexPatternField[];
getByName(name: IndexPatternField['name']): IndexPatternField | undefined;
getByType(type: IndexPatternField['type']): IndexPatternField[];
remove(field: IFieldType): void;
Expand Down Expand Up @@ -72,6 +73,7 @@ export class FieldList extends Array<IndexPatternField> implements IIndexPattern
specs.map((field) => this.add(field));
}

public readonly getAll = () => [...this.byName.values()];
public readonly getByName = (name: IndexPatternField['name']) => this.byName.get(name);
public readonly getByType = (type: IndexPatternField['type']) => [
...(this.groups.get(type) || new Map()).values(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class IndexPatternField implements IFieldType {

// writable attrs
public get count() {
return this.spec.count;
return this.spec.count || 0;
}

public set count(count) {
Expand Down Expand Up @@ -107,7 +107,7 @@ export class IndexPatternField implements IFieldType {
}

public get scripted() {
return this.spec.scripted;
return !!this.spec.scripted;
}

public get searchable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,11 @@ export class IndexPattern implements IIndexPattern {
}

getNonScriptedFields() {
return [...this.fields.filter((field) => !field.scripted)];
return [...this.fields.getAll().filter((field) => !field.scripted)];
}

getScriptedFields() {
return [...this.fields.filter((field) => field.scripted)];
return [...this.fields.getAll().filter((field) => field.scripted)];
}

getIndex() {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data/common/index_patterns/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
* under the License.
*/

export * from './fields/fields.mocks.ts';
export * from './fields/fields.mocks';
4 changes: 2 additions & 2 deletions src/plugins/data/common/index_patterns/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export interface FieldSpecExportFmt {
}

export interface FieldSpec {
count: number;
count?: number;
script?: string;
lang?: string;
conflictDescriptions?: Record<string, string[]>;
Expand All @@ -169,7 +169,7 @@ export interface FieldSpec {
name: string;
type: string;
esTypes?: string[];
scripted: boolean;
scripted?: boolean;
searchable: boolean;
aggregatable: boolean;
readFromDocValues?: boolean;
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ export class FieldList extends Array<IndexPatternField> implements IIndexPattern
// (undocumented)
readonly add: (field: FieldSpec) => void;
// (undocumented)
readonly getAll: () => IndexPatternField[];
// (undocumented)
readonly getByName: (name: IndexPatternField['name']) => IndexPatternField | undefined;
// (undocumented)
readonly getByType: (type: IndexPatternField['type']) => any[];
Expand Down Expand Up @@ -881,6 +883,8 @@ export interface IIndexPatternFieldList extends Array<IndexPatternField> {
// (undocumented)
add(field: FieldSpec): void;
// (undocumented)
getAll(): IndexPatternField[];
// (undocumented)
getByName(name: IndexPatternField['name']): IndexPatternField | undefined;
// (undocumented)
getByType(type: IndexPatternField['type']): IndexPatternField[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import { difference, map } from 'lodash';
import { difference } from 'lodash';
import { IndexPattern, IndexPatternField } from 'src/plugins/data/public';

export function getIndexPatternFieldList(
Expand All @@ -26,7 +26,7 @@ export function getIndexPatternFieldList(
if (!indexPattern || !fieldCounts) return [];

const fieldNamesInDocs = Object.keys(fieldCounts);
const fieldNamesInIndexPattern = map(indexPattern.fields, 'name');
const fieldNamesInIndexPattern = indexPattern.fields.getAll().map((fld) => fld.name);
const unknownTypes: IndexPatternField[] = [];

difference(fieldNamesInDocs, fieldNamesInIndexPattern).forEach((unknownFieldName) => {
Expand All @@ -36,5 +36,5 @@ export function getIndexPatternFieldList(
} as IndexPatternField);
});

return [...indexPattern.fields, ...unknownTypes];
return [...indexPattern.fields.getAll(), ...unknownTypes];
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,47 @@ import { DocViewTable } from './table';
import { indexPatterns, IndexPattern } from '../../../../../data/public';

const indexPattern = {
fields: [
{
name: '_index',
type: 'string',
scripted: false,
filterable: true,
},
{
name: 'message',
type: 'string',
scripted: false,
filterable: false,
},
{
name: 'extension',
type: 'string',
scripted: false,
filterable: true,
},
{
name: 'bytes',
type: 'number',
scripted: false,
filterable: true,
},
{
name: 'scripted',
type: 'number',
scripted: true,
filterable: false,
},
],
fields: {
getAll: () => [
{
name: '_index',
type: 'string',
scripted: false,
filterable: true,
},
{
name: 'message',
type: 'string',
scripted: false,
filterable: false,
},
{
name: 'extension',
type: 'string',
scripted: false,
filterable: true,
},
{
name: 'bytes',
type: 'number',
scripted: false,
filterable: true,
},
{
name: 'scripted',
type: 'number',
scripted: true,
filterable: false,
},
],
},
metaFields: ['_index', '_score'],
flattenHit: undefined,
formatHit: jest.fn((hit) => hit._source),
} as IndexPattern;

indexPattern.fields.getByName = (name: string) => {
return indexPattern.fields.find((field) => field.name === name);
return indexPattern.fields.getAll().find((field) => field.name === name);
};

indexPattern.flattenHit = indexPatterns.flattenHitWrapper(indexPattern, indexPattern.metaFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,13 @@ export function DocViewTable({
// to the index pattern, but that has its own complications which you can read more about in the following
// issue: https://github.com/elastic/kibana/issues/54957
const isNestedField =
!indexPattern.fields.find((patternField) => patternField.name === field) &&
!!indexPattern.fields.find((patternField) => {
!indexPattern.fields.getByName(field) &&
!!indexPattern.fields.getAll().find((patternField) => {
// We only want to match a full path segment
const nestedRootRegex = new RegExp(escapeRegExp(field) + '(\\.|$)');
return nestedRootRegex.test(patternField.subType?.nested?.path ?? '');
});
const fieldType = isNestedField
? 'nested'
: indexPattern.fields.find((patternField) => patternField.name === field)?.type;
const fieldType = isNestedField ? 'nested' : indexPattern.fields.getByName(field)?.type;

return (
<DocViewTableRow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ export const EditIndexPattern = withRouter(
} = useKibana<IndexPatternManagmentContext>().services;
const [fields, setFields] = useState<IndexPatternField[]>(indexPattern.getNonScriptedFields());
const [conflictedFields, setConflictedFields] = useState<IndexPatternField[]>(
indexPattern.fields.filter((field) => field.type === 'conflict')
indexPattern.fields.getAll().filter((field) => field.type === 'conflict')
);
const [defaultIndex, setDefaultIndex] = useState<string>(uiSettings.get('defaultIndex'));
const [tags, setTags] = useState<any[]>([]);

useEffect(() => {
setFields(indexPattern.getNonScriptedFields());
setConflictedFields(indexPattern.fields.filter((field) => field.type === 'conflict'));
setConflictedFields(
indexPattern.fields.getAll().filter((field) => field.type === 'conflict')
);
}, [indexPattern]);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function Tabs({ indexPattern, fields, history, location }: TabsProps) {
const refreshFilters = useCallback(() => {
const tempIndexedFieldTypes: string[] = [];
const tempScriptedFieldLanguages: string[] = [];
indexPattern.fields.forEach((field) => {
indexPattern.fields.getAll().forEach((field) => {
if (field.scripted) {
if (field.lang) {
tempScriptedFieldLanguages.push(field.lang);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ export function getTabs(
fieldFilter: string,
indexPatternListProvider: IndexPatternManagementStart['list']
) {
const totalCount = getCounts(indexPattern.fields, indexPattern.getSourceFiltering());
const totalCount = getCounts(indexPattern.fields.getAll(), indexPattern.getSourceFiltering());
const filteredCount = getCounts(
indexPattern.fields,
indexPattern.fields.getAll(),
indexPattern.getSourceFiltering(),
fieldFilter
);
Expand Down
Loading

0 comments on commit f4b44c7

Please sign in to comment.