Skip to content

Commit

Permalink
use fields.getAll where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkime committed Aug 10, 2020
1 parent 4ee483b commit 58af035
Show file tree
Hide file tree
Showing 34 changed files with 77 additions and 39 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 @@ -403,11 +403,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)];
}

isTimeBased(): boolean {
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 @@ -149,7 +149,7 @@ export interface FieldSpecExportFmt {
}

export interface FieldSpec {
count: number;
count?: number;
script?: string;
lang?: string;
conflictDescriptions?: Record<string, string[]>;
Expand All @@ -158,7 +158,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 @@ -879,6 +881,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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ export class TestScript extends Component<TestScriptProps, TestScriptState> {
const fields: EuiComboBoxOptionOption[] = [];

this.props.indexPattern.fields
.getAll()
.filter((field) => {
const isMultiField = field.subType && field.subType.multi;
return !field.name.startsWith('_') && !isMultiField && !field.scripted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class FieldEditor extends PureComponent<FieldEdiorProps, FieldEditorState
scriptingLangs: [],
fieldTypes: [],
fieldTypeFormats: [],
existingFieldNames: indexPattern.fields.map((f: IFieldType) => f.name),
existingFieldNames: indexPattern.fields.getAll().map((f: IFieldType) => f.name),
fieldFormatId: undefined,
fieldFormatParams: {},
showScriptingHelp: false,
Expand Down Expand Up @@ -197,7 +197,7 @@ export class FieldEditor extends PureComponent<FieldEdiorProps, FieldEditorState

this.setState({
isReady: true,
isCreating: !indexPattern.fields.find((f) => f.name === spec.name),
isCreating: !indexPattern.fields.getByName(spec.name),
isDeprecatedLang: this.deprecatedLangs.includes(spec.lang || ''),
errors: [],
scriptingLangs,
Expand Down Expand Up @@ -804,11 +804,11 @@ export class FieldEditor extends PureComponent<FieldEdiorProps, FieldEditorState
}

const { redirectAway } = this.props.services;
const index = indexPattern.fields.findIndex((f: IFieldType) => f.name === field.name);
const fieldExists = !!indexPattern.fields.getByName(field.name);

let oldField: IndexPatternField['spec'];

if (index > -1) {
if (fieldExists) {
oldField = indexPattern.fields.getByName(field.name)!.spec;
indexPattern.fields.update(field);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export async function listControlFactory(
// dynamic options are only allowed on String fields but the setting defaults to true so it could
// be enabled for non-string fields (since UI input is hidden for non-string fields).
// If field is not string, then disable dynamic options.
const field = indexPattern.fields.find(({ name }) => name === controlParams.fieldName);
const field = indexPattern.fields.getAll().find(({ name }) => name === controlParams.fieldName);
if (field && field.type !== 'string') {
controlParams.options.dynamicOptions = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export function getArgValueSuggestions() {

const valueSplit = partial.split(':');
return indexPattern.fields
.getAll()
.filter((field) => {
return (
field.aggregatable &&
Expand All @@ -136,6 +137,7 @@ export function getArgValueSuggestions() {
}

return indexPattern.fields
.getAll()
.filter((field) => {
return (
field.aggregatable &&
Expand All @@ -155,6 +157,7 @@ export function getArgValueSuggestions() {
}

return indexPattern.fields
.getAll()
.filter((field) => {
return (
'date' === field.type &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui';
import {
fields,
getField,
} from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks.ts';
} from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks';
import { FieldComponent } from './field';

describe('FieldComponent', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui';
// we don't have the types for waitFor just yet, so using "as waitFor" until when we do
import { wait as waitFor } from '@testing-library/react';

import { getField } from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks.ts';
import { getField } from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks';
import { ListSchema } from '../../../lists_plugin_deps';
import { getFoundListSchemaMock } from '../../../../../lists/common/schemas/response/found_list_schema.mock';
import { getListResponseMock } from '../../../../../lists/common/schemas/response/list_schema.mock';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui';
import {
fields,
getField,
} from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks.ts';
} from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks';
import { AutocompleteFieldMatchComponent } from './field_value_match';
import { useFieldValueAutocomplete } from './hooks/use_field_value_autocomplete';
jest.mock('./hooks/use_field_value_autocomplete');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui';
import {
fields,
getField,
} from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks.ts';
} from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks';
import { AutocompleteFieldMatchAnyComponent } from './field_value_match_any';
import { useFieldValueAutocomplete } from './hooks/use_field_value_autocomplete';
jest.mock('./hooks/use_field_value_autocomplete');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import '../../../common/mock/match_media';
import { getField } from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks.ts';
import { getField } from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks';

import {
EXCEPTION_OPERATORS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from './use_field_value_autocomplete';
import { useKibana } from '../../../../common/lib/kibana';
import { stubIndexPatternWithFields } from '../../../../../../../../src/plugins/data/common/index_patterns/index_pattern.stub';
import { getField } from '../../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks.ts';
import { getField } from '../../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks';
import { OperatorTypeEnum } from '../../../../lists_plugin_deps';

jest.mock('../../../../common/lib/kibana');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { mount } from 'enzyme';
import euiLightVars from '@elastic/eui/dist/eui_theme_light.json';
import { EuiComboBox, EuiComboBoxOptionOption } from '@elastic/eui';

import { getField } from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks.ts';
import { getField } from '../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks';
import { OperatorComponent } from './operator';
import { isOperator, isNotOperator } from './operators';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import {
fields,
getField,
} from '../../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks.ts';
} from '../../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks';
import { getFoundListSchemaMock } from '../../../../../../lists/common/schemas/response/found_list_schema.mock';
import { getEmptyValue } from '../../empty_value';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { mount } from 'enzyme';
import euiLightVars from '@elastic/eui/dist/eui_theme_light.json';

import { useKibana } from '../../../../common/lib/kibana';
import { fields } from '../../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks.ts';
import { fields } from '../../../../../../../../src/plugins/data/common/index_patterns/fields/fields.mocks';
import { getExceptionListItemSchemaMock } from '../../../../../../lists/common/schemas/response/exception_list_item_schema.mock';
import { getEntryMatchMock } from '../../../../../../lists/common/schemas/types/entry_match.mock';
import { getEntryMatchAnyMock } from '../../../../../../lists/common/schemas/types/entry_match_any.mock';
Expand Down
Loading

0 comments on commit 58af035

Please sign in to comment.