Skip to content

Commit

Permalink
[8.x] [data views] Use map object for field attributes (#193760) (#19…
Browse files Browse the repository at this point in the history
…4252)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[data views] Use map object for field attributes
(#193760)](#193760)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Matthew
Kime","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-27T05:17:44Z","message":"[data
views] Use map object for field attributes (#193760)\n\n##
Summary\r\n\r\nUse map object for field
attributes\r\n\r\n---------\r\n\r\nCo-authored-by: Davis McPhee
<[email protected]>","sha":"760455a0ff995597cefb45e7b1aa1bfbd251b4eb","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Data
Views","release_note:skip","v9.0.0","Team:DataDiscovery","backport:prev-major","ci:project-deploy-observability"],"title":"[data
views] Use map object for field
attributes","number":193760,"url":"https://github.com/elastic/kibana/pull/193760","mergeCommit":{"message":"[data
views] Use map object for field attributes (#193760)\n\n##
Summary\r\n\r\nUse map object for field
attributes\r\n\r\n---------\r\n\r\nCo-authored-by: Davis McPhee
<[email protected]>","sha":"760455a0ff995597cefb45e7b1aa1bfbd251b4eb"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/193760","number":193760,"mergeCommit":{"message":"[data
views] Use map object for field attributes (#193760)\n\n##
Summary\r\n\r\nUse map object for field
attributes\r\n\r\n---------\r\n\r\nCo-authored-by: Davis McPhee
<[email protected]>","sha":"760455a0ff995597cefb45e7b1aa1bfbd251b4eb"}}]}]
BACKPORT-->

Co-authored-by: Matthew Kime <[email protected]>
  • Loading branch information
kibanamachine and mattkime authored Sep 27, 2024
1 parent 34d4498 commit ed3b610
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 40 deletions.
21 changes: 11 additions & 10 deletions src/plugins/data_views/common/data_views/abstract_data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
SerializedFieldFormat,
} from '@kbn/field-formats-plugin/common';
import { ES_FIELD_TYPES, KBN_FIELD_TYPES } from '@kbn/field-types';
import { cloneDeep, merge } from 'lodash';
import { cloneDeep } from 'lodash';
import type { DataViewFieldBase } from '@kbn/es-query';
import type {
DataViewSpec,
Expand Down Expand Up @@ -161,7 +161,7 @@ export abstract class AbstractDataView {
}
return acc;
}, {} as Record<string, FieldAttrSet>)
: [];
: {};

this.allowNoIndex = spec?.allowNoIndex || false;

Expand Down Expand Up @@ -191,7 +191,7 @@ export abstract class AbstractDataView {
this.sourceFilters = [...(spec.sourceFilters || [])];
this.type = spec.type;
this.typeMeta = spec.typeMeta;
this.fieldAttrs = cloneDeep(merge({}, extractedFieldAttrs, spec.fieldAttrs)) || {};
this.fieldAttrs = new Map(Object.entries({ ...extractedFieldAttrs, ...spec.fieldAttrs }));
this.runtimeFieldMap = cloneDeep(spec.runtimeFieldMap) || {};
this.namespaces = spec.namespaces || [];
this.name = spec.name || '';
Expand Down Expand Up @@ -300,10 +300,8 @@ export abstract class AbstractDataView {
attrName: K,
value: FieldAttrSet[K]
) {
if (!this.fieldAttrs[fieldName]) {
this.fieldAttrs[fieldName] = {} as FieldAttrSet;
}
this.fieldAttrs[fieldName][attrName] = value;
const fieldAttrs = this.fieldAttrs.get(fieldName) || {};
this.fieldAttrs.set(fieldName, { ...fieldAttrs, [attrName]: value });
}

/**
Expand Down Expand Up @@ -368,7 +366,7 @@ export abstract class AbstractDataView {
const stringifyOrUndefined = (obj: any) => (obj ? JSON.stringify(obj) : undefined);

return {
fieldAttrs: stringifyOrUndefined(this.fieldAttrs),
fieldAttrs: stringifyOrUndefined(Object.fromEntries(this.fieldAttrs.entries())),
title: this.getIndexPattern(),
timeFieldName: this.timeFieldName,
sourceFilters: stringifyOrUndefined(this.sourceFilters),
Expand All @@ -385,7 +383,7 @@ export abstract class AbstractDataView {

protected toSpecShared(includeFields = true): DataViewSpec {
// if fields aren't included, don't include count
const fieldAttrs = cloneDeep(this.fieldAttrs);
const fieldAttrs = Object.fromEntries(this.fieldAttrs.entries());
if (!includeFields) {
Object.keys(fieldAttrs).forEach((key) => {
delete fieldAttrs[key].count;
Expand Down Expand Up @@ -546,5 +544,8 @@ export abstract class AbstractDataView {
this.runtimeFieldMap[name] = removeFieldAttrs(runtimeField);
}

getFieldAttrs = () => cloneDeep(this.fieldAttrs);
getFieldAttrs = () => {
const clonedFieldAttrs = cloneDeep(Object.fromEntries(this.fieldAttrs.entries()));
return new Map(Object.entries(clonedFieldAttrs));
};
}
4 changes: 2 additions & 2 deletions src/plugins/data_views/common/data_views/data_view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ describe('IndexPattern', () => {
},
},
});
expect(dataView.getFieldAttrs()).toMatchInlineSnapshot(`
expect(Object.fromEntries(dataView.getFieldAttrs().entries())).toMatchInlineSnapshot(`
Object {
"test1": Object {
"count": 5,
Expand Down Expand Up @@ -555,7 +555,7 @@ describe('IndexPattern', () => {
},
},
});
expect(dataView.getFieldAttrs()).toMatchInlineSnapshot(`
expect(Object.fromEntries(dataView.getFieldAttrs().entries())).toMatchInlineSnapshot(`
Object {
"test1": Object {
"count": 2,
Expand Down
28 changes: 16 additions & 12 deletions src/plugins/data_views/common/data_views/data_view_lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ export class DataViewLazy extends AbstractDataView {
return col;
}
if (!cachedField) {
const fldAttrs = this.fieldAttrs.get(field.name) || {};
cachedField = new DataViewField({
...field,
count: this.fieldAttrs?.[field.name]?.count,
customLabel: this.fieldAttrs?.[field.name]?.customLabel,
customDescription: this.fieldAttrs?.[field.name]?.customDescription,
count: fldAttrs.count,
customLabel: fldAttrs.customLabel,
customDescription: fldAttrs.customDescription,
shortDotsEnable: this.shortDotsEnable,
});
this.fieldCache.set(field.name, cachedField);
Expand Down Expand Up @@ -338,6 +339,7 @@ export class DataViewLazy extends AbstractDataView {
runtimeField: RuntimeFieldSpec,
parentName?: string
) => {
const fldAttrs = this.fieldAttrs.get(name) || {};
spec[name] = {
name,
type: castEsToKbnFieldTypeName(fieldType),
Expand All @@ -346,9 +348,9 @@ export class DataViewLazy extends AbstractDataView {
aggregatable: true,
searchable: true,
readFromDocValues: false,
customLabel: this.fieldAttrs?.[name]?.customLabel,
customDescription: this.fieldAttrs?.[name]?.customDescription,
count: this.fieldAttrs?.[name]?.count,
customLabel: fldAttrs.customLabel,
customDescription: fldAttrs.customDescription,
count: fldAttrs.count,
};

if (parentName) {
Expand Down Expand Up @@ -391,14 +393,15 @@ export class DataViewLazy extends AbstractDataView {
if (fld && !fld.scripted && fld.isMapped) {
this.fieldCache.delete(field.name);
}
const fldAttrs = this.fieldAttrs.get(field.name) || {};
fld = new DataViewField({
...field,
scripted: true,
searchable: true,
aggregatable: true,
count: this.fieldAttrs?.[field.name]?.count,
customLabel: this.fieldAttrs?.[field.name]?.customLabel,
customDescription: this.fieldAttrs?.[field.name]?.customDescription,
count: fldAttrs.count,
customLabel: fldAttrs.customLabel,
customDescription: fldAttrs.customDescription,
});
this.fieldCache.set(field.name, fld);
dataViewFields[field.name] = fld;
Expand Down Expand Up @@ -437,11 +440,12 @@ export class DataViewLazy extends AbstractDataView {
fld.spec.runtimeField = undefined; // unset if it was a runtime field but now mapped
fld.spec.isMapped = true;
} else {
const fldAttrs = this.fieldAttrs.get(field.name) || {};
fld = new DataViewField({
...field,
count: this.fieldAttrs?.[field.name]?.count,
customLabel: this.fieldAttrs?.[field.name]?.customLabel,
customDescription: this.fieldAttrs?.[field.name]?.customDescription,
count: fldAttrs.count,
customLabel: fldAttrs.customLabel,
customDescription: fldAttrs.customDescription,
shortDotsEnable: this.shortDotsEnable,
});
this.fieldCache.set(field.name, fld);
Expand Down
26 changes: 16 additions & 10 deletions src/plugins/data_views/common/data_views/data_views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
DataViewSpec,
DataViewAttributes,
FieldAttrs,
FieldAttrsAsObject,
FieldSpec,
DataViewFieldMap,
TypeMeta,
Expand Down Expand Up @@ -187,7 +188,7 @@ export interface DataViewsServicePublicMethods {
* @params fieldAttrs - Field attributes, map by name
* @returns Field map by name
*/
fieldArrayToMap: (fields: FieldSpec[], fieldAttrs?: FieldAttrs | undefined) => DataViewFieldMap;
fieldArrayToMap: (fields: FieldSpec[], fieldAttrs?: FieldAttrsAsObject) => DataViewFieldMap;
/**
* Search for data views based on title
* @param search - Search string
Expand Down Expand Up @@ -657,7 +658,8 @@ export class DataViewsService {
const scripted = this.scriptedFieldsEnabled
? indexPattern.getScriptedFields().map((field) => field.spec)
: [];
const fieldAttrs = indexPattern.getFieldAttrs();

const fieldAttrs = Object.fromEntries(indexPattern.getFieldAttrs().entries());
const fieldsWithSavedAttrs = Object.values(
this.fieldArrayToMap([...fields, ...scripted], fieldAttrs)
);
Expand Down Expand Up @@ -720,7 +722,7 @@ export class DataViewsService {
id: string,
title: string,
options: GetFieldsOptions,
fieldAttrs: FieldAttrs = {},
fieldAttrs: FieldAttrsAsObject = {},
displayErrors: boolean = true
) => {
const fieldsAsArr = Object.values(fields);
Expand Down Expand Up @@ -771,7 +773,7 @@ export class DataViewsService {
* @param fieldAttrs: FieldAttrs
* @returns Record<string, FieldSpec>
*/
fieldArrayToMap = (fields: FieldSpec[], fieldAttrs?: FieldAttrs) =>
fieldArrayToMap = (fields: FieldSpec[], fieldAttrs?: FieldAttrsAsObject) =>
fields.reduce<DataViewFieldMap>((collector, field) => {
collector[field.name] = {
...field,
Expand Down Expand Up @@ -813,7 +815,7 @@ export class DataViewsService {
const parsedTypeMeta = typeMeta ? JSON.parse(typeMeta) : undefined;
const parsedFieldFormatMap = fieldFormatMap ? JSON.parse(fieldFormatMap) : {};
const parsedFields: FieldSpec[] = fields ? JSON.parse(fields) : [];
const parsedFieldAttrs: FieldAttrs = fieldAttrs ? JSON.parse(fieldAttrs) : {};
const parsedFieldAttrs: FieldAttrsAsObject = fieldAttrs ? JSON.parse(fieldAttrs) : {};
const parsedRuntimeFieldMap: Record<string, RuntimeField> = runtimeFieldMap
? JSON.parse(runtimeFieldMap)
: {};
Expand Down Expand Up @@ -872,7 +874,10 @@ export class DataViewsService {
displayErrors
);

const runtimeFieldSpecs = this.getRuntimeFields(runtimeFieldMap, spec.fieldAttrs);
const runtimeFieldSpecs = this.getRuntimeFields(
runtimeFieldMap,
new Map(Object.entries(spec.fieldAttrs || {}))
);
// mapped fields overwrite runtime fields
return { fields: { ...runtimeFieldSpecs, ...fields }, indices: indices || [], etag };
};
Expand Down Expand Up @@ -938,7 +943,7 @@ export class DataViewsService {

private getRuntimeFields = (
runtimeFieldMap: Record<string, RuntimeFieldSpec> | undefined = {},
fieldAttrs: FieldAttrs | undefined = {}
fieldAttrs: FieldAttrs | undefined = new Map()
) => {
const spec: DataViewFieldMap = {};

Expand All @@ -948,6 +953,7 @@ export class DataViewsService {
runtimeField: RuntimeFieldSpec,
parentName?: string
) => {
const fldAttrs = fieldAttrs.get(name) || {};
spec[name] = {
name,
type: castEsToKbnFieldTypeName(fieldType),
Expand All @@ -956,9 +962,9 @@ export class DataViewsService {
aggregatable: true,
searchable: true,
readFromDocValues: false,
customLabel: fieldAttrs?.[name]?.customLabel,
customDescription: fieldAttrs?.[name]?.customDescription,
count: fieldAttrs?.[name]?.count,
customLabel: fldAttrs.customLabel,
customDescription: fldAttrs.customDescription,
count: fldAttrs.count,
};

if (parentName) {
Expand Down
9 changes: 4 additions & 5 deletions src/plugins/data_views/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ export interface DataViewAttributes {
* @public
* Storage of field attributes. Necessary since the field list isn't saved.
*/
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export type FieldAttrs = {
[key: string]: FieldAttrSet;
};
export type FieldAttrs = Map<string, FieldAttrSet>;

/**
* Field attributes that are stored on the data view
Expand All @@ -198,6 +195,8 @@ export type FieldAttrSet = {
count?: number;
};

export type FieldAttrsAsObject = Record<string, FieldAttrSet>;

/**
* Handler for data view notifications
* @public
Expand Down Expand Up @@ -537,7 +536,7 @@ export type DataViewSpec = {
/**
* Map of field attributes by field name, currently customName and count
*/
fieldAttrs?: FieldAttrs;
fieldAttrs?: FieldAttrsAsObject;
/**
* Determines whether failure to load field list should be reported as error
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe('LogViewsClient class', () => {
"deleteFieldFormat": [Function],
"deleteScriptedFieldInternal": [Function],
"etag": undefined,
"fieldAttrs": Object {},
"fieldAttrs": Map {},
"fieldFormatMap": Object {},
"fieldFormats": Object {
"deserialize": [MockFunction],
Expand Down

0 comments on commit ed3b610

Please sign in to comment.