Skip to content

Commit

Permalink
Merge branch 'master' into ml-forecast-functional-test
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Oct 27, 2021
2 parents 8d62540 + 34b2257 commit b7e6087
Show file tree
Hide file tree
Showing 27 changed files with 204 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const MOCK_VALUES = {

const MOCK_ACTIONS = {
// RelevanceTuningLogic
updatePrecision: jest.fn(),
setPrecision: jest.fn(),
};

describe('PrecisionSlider', () => {
Expand All @@ -49,12 +49,12 @@ describe('PrecisionSlider', () => {
expect(wrapper.find('[data-test-subj="PrecisionRange"]').prop('value')).toEqual(2);
});

it('calls updatePrecision on change', () => {
it('updates the precision on change', () => {
wrapper
.find('[data-test-subj="PrecisionRange"]')
.simulate('change', { target: { value: 10 } });

expect(MOCK_ACTIONS.updatePrecision).toHaveBeenCalledWith(10);
expect(MOCK_ACTIONS.setPrecision).toHaveBeenCalledWith(10);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const PrecisionSlider: React.FC = () => {
searchSettings: { precision },
} = useValues(RelevanceTuningLogic);

const { updatePrecision } = useActions(RelevanceTuningLogic);
const { setPrecision } = useActions(RelevanceTuningLogic);

const stepDescription = STEP_DESCRIPTIONS[precision];

Expand Down Expand Up @@ -102,7 +102,7 @@ export const PrecisionSlider: React.FC = () => {
data-test-subj="PrecisionRange"
value={precision}
onChange={(e) => {
updatePrecision(parseInt((e.target as HTMLInputElement).value, 10));
setPrecision(parseInt((e.target as HTMLInputElement).value, 10));
}}
min={1}
max={11}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ describe('RelevanceTuningLogic', () => {
});
});

describe('updatePrecision', () => {
describe('setPrecision', () => {
it('should set precision inside search settings and set unsavedChanges to true', () => {
mount();
RelevanceTuningLogic.actions.updatePrecision(9);
RelevanceTuningLogic.actions.setPrecision(9);

expect(RelevanceTuningLogic.values).toEqual({
...DEFAULT_VALUES,
Expand Down Expand Up @@ -1007,15 +1007,24 @@ describe('RelevanceTuningLogic', () => {
});
});

describe('updateSearchValue', () => {
it('should update the query then update search results', () => {
describe('setSearchQuery', () => {
it('shoulds update search results', () => {
mount();
jest.spyOn(RelevanceTuningLogic.actions, 'getSearchResults');

RelevanceTuningLogic.actions.setSearchQuery('foo');

expect(RelevanceTuningLogic.actions.getSearchResults).toHaveBeenCalled();
});
});

describe('setPrecision', () => {
it('shoulds update search results', () => {
mount();
jest.spyOn(RelevanceTuningLogic.actions, 'setSearchQuery');
jest.spyOn(RelevanceTuningLogic.actions, 'getSearchResults');

RelevanceTuningLogic.actions.updateSearchValue('foo');
RelevanceTuningLogic.actions.setPrecision(9);

expect(RelevanceTuningLogic.actions.setSearchQuery).toHaveBeenCalledWith('foo');
expect(RelevanceTuningLogic.actions.getSearchResults).toHaveBeenCalled();
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ interface RelevanceTuningActions {
optionType: keyof Pick<Boost, 'operation' | 'function'>;
value: string;
};
updatePrecision(precision: number): { precision: number };
updateSearchValue(query: string): string;
setPrecision(precision: number): { precision: number };
}

interface RelevanceTuningValues {
Expand Down Expand Up @@ -144,8 +143,7 @@ export const RelevanceTuningLogic = kea<
optionType,
value,
}),
updatePrecision: (precision) => ({ precision }),
updateSearchValue: (query) => query,
setPrecision: (precision) => ({ precision }),
}),
reducers: () => ({
searchSettings: [
Expand All @@ -158,7 +156,7 @@ export const RelevanceTuningLogic = kea<
onInitializeRelevanceTuning: (_, { searchSettings }) => searchSettings,
setSearchSettings: (_, { searchSettings }) => searchSettings,
setSearchSettingsResponse: (_, { searchSettings }) => searchSettings,
updatePrecision: (currentSearchSettings, { precision }) => ({
setPrecision: (currentSearchSettings, { precision }) => ({
...currentSearchSettings,
precision,
}),
Expand Down Expand Up @@ -191,7 +189,7 @@ export const RelevanceTuningLogic = kea<
unsavedChanges: [
false,
{
updatePrecision: () => true,
setPrecision: () => true,
setSearchSettings: () => true,
setSearchSettingsResponse: () => false,
},
Expand Down Expand Up @@ -268,7 +266,11 @@ export const RelevanceTuningLogic = kea<

const { engineName } = EngineLogic.values;
const { http } = HttpLogic.values;
const { search_fields: searchFields, boosts } = removeBoostStateProps(values.searchSettings);
const {
search_fields: searchFields,
boosts,
precision,
} = removeBoostStateProps(values.searchSettings);
const url = `/internal/app_search/engines/${engineName}/search`;

actions.setResultsLoading(true);
Expand All @@ -283,6 +285,7 @@ export const RelevanceTuningLogic = kea<
body: JSON.stringify({
boosts: isEmpty(filteredBoosts) ? undefined : filteredBoosts,
search_fields: isEmpty(searchFields) ? undefined : searchFields,
precision,
}),
});

Expand Down Expand Up @@ -472,8 +475,10 @@ export const RelevanceTuningLogic = kea<
},
});
},
updateSearchValue: (query) => {
actions.setSearchQuery(query);
setSearchQuery: () => {
actions.getSearchResults();
},
setPrecision: () => {
actions.getSearchResults();
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('RelevanceTuningPreview', () => {
const result3 = { id: { raw: 3 } };

const actions = {
updateSearchValue: jest.fn(),
setSearchQuery: jest.fn(),
};

const values = {
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('RelevanceTuningPreview', () => {

wrapper.find(EuiFieldSearch).simulate('change', { target: { value: 'some search text' } });

expect(actions.updateSearchValue).toHaveBeenCalledWith('some search text');
expect(actions.setSearchQuery).toHaveBeenCalledWith('some search text');
});

it('will show user a prompt to enter a query if they have not entered one', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const noResultsCallout = (
);

export const RelevanceTuningPreview: React.FC = () => {
const { updateSearchValue } = useActions(RelevanceTuningLogic);
const { setSearchQuery } = useActions(RelevanceTuningLogic);
const { searchResults, schema } = useValues(RelevanceTuningLogic);
const { engineName, isMetaEngine } = useValues(EngineLogic);

Expand All @@ -59,7 +59,7 @@ export const RelevanceTuningPreview: React.FC = () => {
</EuiTitle>
<EuiSpacer />
<EuiFieldSearch
onChange={(e) => updateSearchValue(e.target.value)}
onChange={(e) => setSearchQuery(e.target.value)}
placeholder={i18n.translate(
'xpack.enterpriseSearch.appSearch.engine.relevanceTuning.preview.searchPlaceholder',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const filterIfTerm = (array: string[], filterTerm: string): string[] => {
return filterTerm === '' ? array : array.filter((item) => item.includes(filterTerm));
};

export const removeBoostStateProps = (searchSettings: SearchSettings) => {
export const removeBoostStateProps = (searchSettings: SearchSettings): SearchSettings => {
const updatedSettings = cloneDeep(searchSettings);
const { boosts } = updatedSettings;
const keys = Object.keys(boosts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ export const TimestampFromString = new rt.Type<number, string>(
export const sourceConfigurationConfigFilePropertiesRT = rt.type({
sources: rt.type({
default: rt.partial({
logAlias: rt.string, // Cannot be deprecated until 8.0.0. Will be converted to an indexName reference.
metricAlias: rt.string,
fields: rt.partial({
timestamp: rt.string,
message: rt.array(rt.string),
Expand Down
2 changes: 0 additions & 2 deletions x-pack/plugins/infra/server/deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ export const configDeprecations: ConfigDeprecationProvider = ({ deprecate }) =>
return completeConfig;
}
),
deprecate('sources.default.logAlias', '8.0.0'),
deprecate('sources.default.metricAlias', '8.0.0'),
];

export const getInfraDeprecationsFactory =
Expand Down
21 changes: 1 addition & 20 deletions x-pack/plugins/infra/server/lib/sources/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,26 +212,7 @@ export class InfraSources {
fold(constant({}), identity)
);

// NOTE: Legacy logAlias needs converting to a logIndices reference until we can remove
// config file sources in 8.0.0.
if (staticSourceConfiguration && staticSourceConfiguration.logAlias) {
const convertedStaticSourceConfiguration: InfraStaticSourceConfiguration & {
logAlias?: string;
} = {
...staticSourceConfiguration,
logIndices: {
type: 'index_name',
indexName: staticSourceConfiguration.logAlias,
},
};
delete convertedStaticSourceConfiguration.logAlias;
return mergeSourceConfiguration(
defaultSourceConfiguration,
convertedStaticSourceConfiguration
);
} else {
return mergeSourceConfiguration(defaultSourceConfiguration, staticSourceConfiguration);
}
return mergeSourceConfiguration(defaultSourceConfiguration, staticSourceConfiguration);
}

private async getSavedSourceConfiguration(
Expand Down
2 changes: 0 additions & 2 deletions x-pack/plugins/infra/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ export const config: PluginConfigDescriptor = {
schema.object({
default: schema.maybe(
schema.object({
logAlias: schema.maybe(schema.string()), // NOTE / TODO: Should be deprecated in 8.0.0
metricAlias: schema.maybe(schema.string()),
fields: schema.maybe(
schema.object({
timestamp: schema.maybe(schema.string()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type AbstractSourceDescriptor = {
export type EMSTMSSourceDescriptor = AbstractSourceDescriptor & {
// id: EMS TMS layer id. Used when !isAutoSelect
isAutoSelect: boolean;
lightModeDefault: string;
};

export type EMSFileSourceDescriptor = AbstractSourceDescriptor & {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { setEmsTmsDefaultModes } from './set_ems_tms_default_modes';

describe('setEmsTmsDefaultModes', () => {
test('Should handle missing layerListJSON attribute', () => {
const attributes = {
title: 'my map',
};
expect(setEmsTmsDefaultModes({ attributes })).toEqual({
title: 'my map',
});
});

test('Should add lightModeDefault to existing EMS_TMS source descriptors', () => {
const layerListJSON = JSON.stringify([
{
sourceDescriptor: {
type: 'EMS_TMS',
},
},
]);
const attributes = {
title: 'my map',
layerListJSON,
};
expect(setEmsTmsDefaultModes({ attributes })).toEqual({
title: 'my map',
layerListJSON: '[{"sourceDescriptor":{"type":"EMS_TMS","lightModeDefault":"road_map"}}]',
});
});

// test edge case where sample data maps set lightModeDefault but still run migration
test('Should not change lightModeDefault if provided', () => {
const layerListJSON = JSON.stringify([
{
sourceDescriptor: {
type: 'EMS_TMS',
lightModeDefault: 'road_map_desaturated',
},
},
]);
const attributes = {
title: 'my map',
layerListJSON,
};
expect(setEmsTmsDefaultModes({ attributes })).toEqual({
title: 'my map',
layerListJSON:
'[{"sourceDescriptor":{"type":"EMS_TMS","lightModeDefault":"road_map_desaturated"}}]',
});
});
});
40 changes: 40 additions & 0 deletions x-pack/plugins/maps/common/migrations/set_ems_tms_default_modes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { SOURCE_TYPES } from '../constants';
import { LayerDescriptor, EMSTMSSourceDescriptor } from '../descriptor_types';
import { MapSavedObjectAttributes } from '../map_saved_object_type';

// LightModeDefault added to EMSTMSSourceDescriptor in 8.0.0
// to avoid changing auto selected light mode tiles for maps created < 8.0.0
// < 8.0.0 did not specify defaults and used bright for light mode
// > 8.0.0 changed default light mode from bright to desaturated
export function setEmsTmsDefaultModes({
attributes,
}: {
attributes: MapSavedObjectAttributes;
}): MapSavedObjectAttributes {
if (!attributes || !attributes.layerListJSON) {
return attributes;
}

const layerList: LayerDescriptor[] = JSON.parse(attributes.layerListJSON);
layerList.forEach((layerDescriptor: LayerDescriptor) => {
if (layerDescriptor.sourceDescriptor?.type === SOURCE_TYPES.EMS_TMS) {
const sourceDescriptor = layerDescriptor.sourceDescriptor as EMSTMSSourceDescriptor;
// auto select bright tiles for EMS_TMS layers created before 8.0.0
if (!sourceDescriptor.lightModeDefault) {
sourceDescriptor.lightModeDefault = 'road_map';
}
}
});

return {
...attributes,
layerListJSON: JSON.stringify(layerList),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('EMS is enabled', () => {
source: undefined,
sourceDescriptor: {
isAutoSelect: true,
lightModeDefault: 'road_map_desaturated',
type: 'EMS_TMS',
},
style: { type: 'TILE' },
Expand Down
Loading

0 comments on commit b7e6087

Please sign in to comment.