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

[Maps] Update style when metrics change #83586

Merged
merged 35 commits into from Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
6105ba7
init commit
thomasneirynck Nov 17, 2020
47560f9
some type fixes
thomasneirynck Nov 18, 2020
f54ca8a
Merge branch 'master' of github.com:elastic/kibana into maps/update_s…
thomasneirynck Nov 18, 2020
71e49df
add type check
thomasneirynck Nov 18, 2020
b7d8bbd
only take into account invalid dynamic fields
thomasneirynck Nov 18, 2020
c507a77
tmp debug
thomasneirynck Nov 18, 2020
e861f26
Merge branch 'master' of github.com:elastic/kibana into maps/update_s…
thomasneirynck Nov 20, 2020
ffad517
ts type fixes
thomasneirynck Nov 20, 2020
e002047
retype
thomasneirynck Nov 23, 2020
ef1f225
Merge branch 'master' of github.com:elastic/kibana into maps/update_s…
thomasneirynck Nov 23, 2020
9590abd
fix field-lookup after refactor
thomasneirynck Nov 23, 2020
e1e7ea0
type fixes
thomasneirynck Nov 23, 2020
c941774
Merge branch 'master' of github.com:elastic/kibana into maps/update_s…
thomasneirynck Nov 24, 2020
54eacb3
add test covergage
thomasneirynck Nov 24, 2020
001e0e1
add edge case test
thomasneirynck Nov 24, 2020
6b70fcb
fix test for new behavior
thomasneirynck Nov 24, 2020
361486a
fix rum test
thomasneirynck Nov 24, 2020
67c1362
type fix
thomasneirynck Nov 24, 2020
4f72d40
Merge branch 'master' of github.com:elastic/kibana into maps/update_s…
thomasneirynck Nov 24, 2020
f794114
Merge branch 'master' into maps/update_style
thomasneirynck Nov 25, 2020
09e8dba
Merge branch 'master' of github.com:elastic/kibana into maps/update_s…
thomasneirynck Nov 25, 2020
0efe401
revert uptime change
thomasneirynck Nov 25, 2020
3f9ffdf
Merge branch 'master' into maps/update_style
kibanamachine Nov 30, 2020
6ea286b
feedback
thomasneirynck Nov 30, 2020
5d6fa3e
rearrange
thomasneirynck Nov 30, 2020
ac2f943
switch inner-outer loop
thomasneirynck Nov 30, 2020
bb65b1d
use foreach
thomasneirynck Nov 30, 2020
17c987d
Merge branch 'master' of github.com:elastic/kibana into maps/update_s…
thomasneirynck Dec 2, 2020
e87c721
simplify
thomasneirynck Dec 2, 2020
1455ef9
Merge branch 'master' of github.com:elastic/kibana into maps/update_s…
thomasneirynck Dec 4, 2020
b09404f
tidy up
thomasneirynck Dec 4, 2020
f86ed22
type fix
thomasneirynck Dec 4, 2020
98c8ab9
still perform correction, to deal with edge cases
thomasneirynck Dec 4, 2020
d732c7a
remove cruft
thomasneirynck Dec 4, 2020
7616c62
remove unused exports
thomasneirynck Dec 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 37 additions & 14 deletions x-pack/plugins/maps/public/actions/layer_actions.ts
Expand Up @@ -45,6 +45,8 @@ import { IVectorLayer } from '../classes/layers/vector_layer/vector_layer';
import { LAYER_STYLE_TYPE, LAYER_TYPE } from '../../common/constants';
import { IVectorStyle } from '../classes/styles/vector/vector_style';
import { notifyLicensedFeatureUsage } from '../licensed_features';
import { IESAggField } from '../classes/fields/agg';
import { IField } from '../classes/fields/field';

export function trackCurrentLayerState(layerId: string) {
return {
Expand Down Expand Up @@ -280,18 +282,37 @@ export function updateSourceProp(
value: unknown,
newLayerType?: LAYER_TYPE
) {
return async (dispatch: ThunkDispatch<MapStoreState, void, AnyAction>) => {
dispatch({
type: UPDATE_SOURCE_PROP,
layerId,
propName,
value,
});
if (newLayerType) {
dispatch(updateLayerType(layerId, newLayerType));
return async (
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
dispatch: ThunkDispatch<MapStoreState, void, AnyAction>,
getState: () => MapStoreState
) => {
if (propName === 'metrics') {
const layer = getLayerById(layerId, getState());
const oldFields = await (layer as IVectorLayer).getFields();
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
dispatch({
type: UPDATE_SOURCE_PROP,
layerId,
propName,
value,
});
if (newLayerType) {
dispatch(updateLayerType(layerId, newLayerType));
}
await dispatch(updateStyleProperties(layerId, oldFields as IESAggField[]));
dispatch(syncDataForLayerId(layerId));
} else {
dispatch({
type: UPDATE_SOURCE_PROP,
layerId,
propName,
value,
});
if (newLayerType) {
dispatch(updateLayerType(layerId, newLayerType));
}
// await dispatch(updateStyleProperties(layerId));
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
dispatch(syncDataForLayerId(layerId));
}
await dispatch(clearMissingStyleProperties(layerId));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

only needs to occur for metrics changes

dispatch(syncDataForLayerId(layerId));
};
}

Expand Down Expand Up @@ -422,7 +443,7 @@ function removeLayerFromLayerList(layerId: string) {
};
}

export function clearMissingStyleProperties(layerId: string) {
export function updateStyleProperties(layerId: string, previousFields: IField[]) {
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
return async (
dispatch: ThunkDispatch<MapStoreState, void, AnyAction>,
getState: () => MapStoreState
Expand All @@ -441,8 +462,9 @@ export function clearMissingStyleProperties(layerId: string) {
const {
hasChanges,
nextStyleDescriptor,
} = await (style as IVectorStyle).getDescriptorWithMissingStylePropsRemoved(
} = await (style as IVectorStyle).getDescriptorWithUpdatedStyleProps(
nextFields,
previousFields,
getMapColors(getState())
);
if (hasChanges && nextStyleDescriptor) {
Expand Down Expand Up @@ -491,7 +513,8 @@ export function setJoinsForLayer(layer: ILayer, joins: JoinDescriptor[]) {
joins,
});

await dispatch(clearMissingStyleProperties(layer.getId()));
const previousFields = await (layer as IVectorLayer).getFields();
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
await dispatch(updateStyleProperties(layer.getId(), previousFields));
dispatch(syncDataForLayerId(layer.getId()));
};
}
Expand Down
Expand Up @@ -97,4 +97,8 @@ export class CountAggField implements IESAggField {
canReadFromGeoJson(): boolean {
return this._canReadFromGeoJson;
}

isEqual(field: IESAggField) {
return field.getName() === this.getName();
}
}
Expand Up @@ -83,4 +83,8 @@ export class TopTermPercentageField implements IESAggField {
canReadFromGeoJson(): boolean {
return this._canReadFromGeoJson;
}

isEqual(field: IESAggField) {
return field.getName() === this.getName();
}
}
5 changes: 5 additions & 0 deletions x-pack/plugins/maps/public/classes/fields/field.ts
Expand Up @@ -32,6 +32,7 @@ export interface IField {
supportsFieldMeta(): boolean;

canReadFromGeoJson(): boolean;
isEqual(field: IField): boolean;
}

export class AbstractField implements IField {
Expand Down Expand Up @@ -99,4 +100,8 @@ export class AbstractField implements IField {
canReadFromGeoJson(): boolean {
return true;
}

isEqual(field: IField) {
return this._origin === field.getOrigin() && this._fieldName === field.getName();
}
}
Expand Up @@ -18,6 +18,7 @@ import {
VECTOR_STYLES,
RawValue,
FieldFormatter,
TOP_TERM_PERCENTAGE_SUFFIX,
Copy link
Contributor

Choose a reason for hiding this comment

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

Clean up all import changes in this file, since they are no longer used

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like you missed cleaning up these imports

} from '../../../../../common/constants';
import { OrdinalFieldMetaPopover } from '../components/field_meta/ordinal_field_meta_popover';
import { CategoricalFieldMetaPopover } from '../components/field_meta/categorical_field_meta_popover';
Expand All @@ -26,12 +27,14 @@ import {
FieldMetaOptions,
RangeFieldMeta,
StyleMetaData,
StylePropertyField,
} from '../../../../../common/descriptor_types';
import { IField } from '../../../fields/field';
import { IVectorLayer } from '../../../layers/vector_layer/vector_layer';
import { InnerJoin } from '../../../joins/inner_join';
import { IVectorStyle } from '../vector_style';
import { getComputedFieldName } from '../style_util';
import { IESAggField } from '../../../fields/agg';

export interface IDynamicStyleProperty<T> extends IStyleProperty<T> {
getFieldMetaOptions(): FieldMetaOptions;
Expand All @@ -48,6 +51,10 @@ export interface IDynamicStyleProperty<T> extends IStyleProperty<T> {
pluckOrdinalStyleMetaFromFeatures(features: Feature[]): RangeFieldMeta | null;
pluckCategoricalStyleMetaFromFeatures(features: Feature[]): CategoryFieldMeta | null;
getValueSuggestions(query: string): Promise<string[]>;
rectifyFieldDescriptor(
currentField: IESAggField,
previousFieldDescriptor: StylePropertyField
): Promise<StylePropertyField | undefined>;
enrichGeoJsonAndMbFeatureState(
featureCollection: FeatureCollection,
mbMap: MbMap,
Expand Down Expand Up @@ -240,6 +247,21 @@ export class DynamicStyleProperty<T>
} as RangeFieldMeta);
}

async rectifyFieldDescriptor(
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
currentField: IESAggField,
previousFieldDescriptor: StylePropertyField
): Promise<StylePropertyField | undefined> {
if (previousFieldDescriptor.name.endsWith(TOP_TERM_PERCENTAGE_SUFFIX)) {
// Don't support auto-switching for top-term-percentages
return;
}

return {
origin: previousFieldDescriptor.origin,
name: currentField.getName(),
};
}

pluckCategoricalStyleMetaFromFeatures(features: Feature[]) {
const size = this.getNumberOfCategories();
if (!this.isCategorical() || size <= 0) {
Expand Down
@@ -0,0 +1,147 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { FIELD_ORIGIN, VECTOR_STYLES } from '../../../../common/constants';
import { createStyleFieldsHelper, StyleFieldsHelper } from './style_fields_helper';
import { AbstractField, IField } from '../../fields/field';

class MockField extends AbstractField {
private readonly _dataType: string;
private readonly _supportsAutoDomain: boolean;
constructor({ dataType, supportsAutoDomain }: { dataType: string; supportsAutoDomain: boolean }) {
super({ fieldName: 'foobar_' + dataType, origin: FIELD_ORIGIN.SOURCE });
this._dataType = dataType;
this._supportsAutoDomain = supportsAutoDomain;
}
async getDataType() {
return this._dataType;
}

supportsAutoDomain(): boolean {
return this._supportsAutoDomain;
}
}

describe('StyleFieldHelper', () => {
describe('isFieldDataTypeCompatibleWithStyleType', () => {
async function createHelper(
supportsAutoDomain: boolean
): Promise<{
styleFieldHelper: StyleFieldsHelper;
stringField: IField;
numberField: IField;
dateField: IField;
}> {
const stringField = new MockField({
dataType: 'string',
supportsAutoDomain,
});
const numberField = new MockField({
dataType: 'number',
supportsAutoDomain,
});
const dateField = new MockField({
dataType: 'date',
supportsAutoDomain,
});
return {
styleFieldHelper: await createStyleFieldsHelper([stringField, numberField, dateField]),
stringField,
numberField,
dateField,
};
}

test('Should validate colors for all data types', async () => {
const { styleFieldHelper, stringField, numberField, dateField } = await createHelper(true);

[
VECTOR_STYLES.FILL_COLOR,
VECTOR_STYLES.LINE_COLOR,
VECTOR_STYLES.LABEL_COLOR,
VECTOR_STYLES.LABEL_BORDER_COLOR,
].forEach((styleType) => {
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(stringField, styleType)
).toEqual(true);
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(numberField, styleType)
).toEqual(true);
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(dateField, styleType)
).toEqual(true);
});
});

test('Should validate sizes for all number types', async () => {
const { styleFieldHelper, stringField, numberField, dateField } = await createHelper(true);

[VECTOR_STYLES.LINE_WIDTH, VECTOR_STYLES.LABEL_SIZE, VECTOR_STYLES.ICON_SIZE].forEach(
(styleType) => {
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(stringField, styleType)
).toEqual(false);
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(numberField, styleType)
).toEqual(true);
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(dateField, styleType)
).toEqual(true);
}
);
});

test('Should not validate sizes if autodomain is not enabled', async () => {
const { styleFieldHelper, stringField, numberField, dateField } = await createHelper(false);

[VECTOR_STYLES.LINE_WIDTH, VECTOR_STYLES.LABEL_SIZE, VECTOR_STYLES.ICON_SIZE].forEach(
(styleType) => {
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(stringField, styleType)
).toEqual(false);
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(numberField, styleType)
).toEqual(false);
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(dateField, styleType)
).toEqual(false);
}
);
});

test('Should validate orientation only number types', async () => {
const { styleFieldHelper, stringField, numberField, dateField } = await createHelper(true);

[VECTOR_STYLES.ICON_ORIENTATION].forEach((styleType) => {
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(stringField, styleType)
).toEqual(false);
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(numberField, styleType)
).toEqual(true);
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(dateField, styleType)
).toEqual(false);
});
});

test('Should not validate label_border_size', async () => {
const { styleFieldHelper, stringField, numberField, dateField } = await createHelper(true);

[VECTOR_STYLES.LABEL_BORDER_SIZE].forEach((styleType) => {
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(stringField, styleType)
).toEqual(false);
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(numberField, styleType)
).toEqual(false);
expect(
styleFieldHelper.isFieldDataTypeCompatibleWithStyleType(dateField, styleType)
).toEqual(false);
});
});
});
});
Expand Up @@ -69,6 +69,11 @@ export class StyleFieldsHelper {
this._ordinalFields = ordinalFields;
}

isFieldDataTypeCompatibleWithStyleType(field: IField, styleName: VECTOR_STYLES): boolean {
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
const fieldList = this.getFieldsForStyle(styleName);
return !!fieldList.find((styleField) => field.getName() === styleField.name);
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
}

getFieldsForStyle(styleName: VECTOR_STYLES): StyleField[] {
switch (styleName) {
case VECTOR_STYLES.ICON_ORIENTATION:
Expand Down