Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
} from '@kbn/discover-plugin/public';
import { SerializedPanelState } from '@kbn/presentation-publishing';
import { css } from '@emotion/react';
import { SavedSearchAttributes } from '@kbn/saved-search-plugin/common';
import { type SavedSearch, toSavedSearchAttributes } from '@kbn/saved-search-plugin/common';
import { isOfAggregateQueryType } from '@kbn/es-query';
import { SavedSearchComponentProps } from '../types';
import { SavedSearchComponentErrorContent } from './error';
Expand Down Expand Up @@ -68,16 +68,21 @@ export const SavedSearchComponent: React.FC<SavedSearchComponentProps> = (props)
searchSource.setField('filter', filters);
const { searchSourceJSON, references } = searchSource.serialize();
// By-value saved object structure
const attributes: Partial<SavedSearchAttributes> = {
const savedSearch: SavedSearch = {
searchSource,
kibanaSavedObjectMeta: {
searchSourceJSON,
},
columns,
sort: getDefaultSort(dataView, undefined, undefined, isOfAggregateQueryType(query)),
managed: false,
};
setInitialSerializedState({
rawState: {
attributes: { ...attributes, references },
attributes: {
...toSavedSearchAttributes(savedSearch, searchSourceJSON),
references,
},
timeRange,
nonPersistedDisplayOptions: {
solutionNavIdOverride,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const ACTION_VIEW_SAVED_SEARCH = 'ACTION_VIEW_SAVED_SEARCH';
export const DEFAULT_HEADER_ROW_HEIGHT_LINES = 3;

/** This constant refers to the parts of the saved search state that can be edited from a dashboard */
export const EDITABLE_SAVED_SEARCH_KEYS: Readonly<Array<keyof SavedSearchAttributes>> = [
export const EDITABLE_SAVED_SEARCH_KEYS = [
'sort',
'columns',
'rowHeight',
Expand All @@ -39,7 +39,7 @@ export const EDITABLE_SAVED_SEARCH_KEYS: Readonly<Array<keyof SavedSearchAttribu
'headerRowHeight',
'density',
'grid',
] as const;
] as const satisfies ReadonlyArray<keyof SavedSearchAttributes>;

/** This constant refers to the dashboard panel specific state */
export const EDITABLE_PANEL_KEYS: Readonly<Array<keyof SearchEmbeddableSerializedState>> = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,28 @@ import type { SavedSearchUnwrapResult } from '@kbn/saved-search-plugin/public';
import { discoverServiceMock } from '../../__mocks__/services';
import type { SearchEmbeddableSerializedState } from '../types';
import { deserializeState, serializeState } from './serialization_utils';
import type { DiscoverSessionTab } from '@kbn/saved-search-plugin/server';

describe('Serialization utils', () => {
const uuid = 'mySearchEmbeddable';

const tabs: DiscoverSessionTab[] = [
{
id: 'tab-1',
label: 'Tab 1',
attributes: {
kibanaSavedObjectMeta: {
searchSourceJSON: '{"indexRefName":"kibanaSavedObjectMeta.searchSourceJSON.index"}',
},
sort: [['order_date', 'desc']],
columns: ['_source'],
grid: {},
hideChart: false,
sampleSize: 100,
isTextBasedQuery: false,
},
},
];
const mockedSavedSearchAttributes: SearchEmbeddableSerializedState['attributes'] = {
kibanaSavedObjectMeta: {
searchSourceJSON: '{"indexRefName":"kibanaSavedObjectMeta.searchSourceJSON.index"}',
Expand All @@ -32,6 +50,7 @@ describe('Serialization utils', () => {
hideChart: false,
sampleSize: 100,
isTextBasedQuery: false,
tabs,
references: [
{
name: 'kibanaSavedObjectMeta.searchSourceJSON.index',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ export const serializeState = ({

if (savedObjectId) {
const editableAttributesBackup = initialState.rawSavedObjectAttributes ?? {};
const [{ attributes }] = savedSearchAttributes.tabs;

// only save the current state that is **different** than the saved object state
const overwriteState = EDITABLE_SAVED_SEARCH_KEYS.reduce((prev, key) => {
if (deepEqual(savedSearchAttributes[key], editableAttributesBackup[key])) {
if (deepEqual(attributes[key], editableAttributesBackup[key])) {
return prev;
}
return { ...prev, [key]: savedSearchAttributes[key] };
return { ...prev, [key]: attributes[key] };
}, {});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { dataPluginMock } from '@kbn/data-plugin/server/mocks';
import type { DataView } from '@kbn/data-views-plugin/common';
import { createStubDataView } from '@kbn/data-views-plugin/common/stubs';
import type { SavedSearchAttributes } from '@kbn/saved-search-plugin/common';
import type { DiscoverSessionTabAttributes } from '@kbn/saved-search-plugin/server';
import type { LocatorServicesDeps as Services } from '.';
import type { DiscoverAppLocatorParams } from '../../common';
import { DOC_HIDE_TIME_COLUMN_SETTING } from '@kbn/discover-utils';
Expand All @@ -37,6 +38,20 @@ const defaultSavedSearch: SavedObject<SavedSearchAttributes> = {
searchSourceJSON:
'{"query":{"query":"","language":"kuery"},"filter":[],"indexRefName":"testIndexRefName"}',
},
tabs: [
{
id: 'tab-1',
label: 'Tab 1',
attributes: {
columns: ['response', 'url', 'clientip', 'machine.os', 'tags'],
sort: [['test', '134']],
kibanaSavedObjectMeta: {
searchSourceJSON:
'{"query":{"query":"","language":"kuery"},"filter":[],"indexRefName":"testIndexRefName"}',
},
},
},
],
} as unknown as SavedSearchAttributes,
};

Expand Down Expand Up @@ -135,7 +150,26 @@ test('with search source using columns when DOC_HIDE_TIME_COLUMN_SETTING is true
});

test('with saved search containing ["_source"]', async () => {
mockSavedSearch.attributes.columns = ['_source'];
mockSavedSearch = {
...defaultSavedSearch,
attributes: {
...defaultSavedSearch.attributes,
tabs: [
{
id: 'tab-1',
label: 'Tab 1',
attributes: {
columns: ['_source'],
sort: [['test', '134']],
kibanaSavedObjectMeta: {
searchSourceJSON:
'{"query":{"query":"","language":"kuery"},"filter":[],"indexRefName":"testIndexRefName"}',
},
} as DiscoverSessionTabAttributes,
},
],
},
};

const provider = columnsFromLocatorFactory(mockServices);
const columns = await provider(mockPayload[0].params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { dataPluginMock } from '@kbn/data-plugin/server/mocks';
import type { DataView } from '@kbn/data-views-plugin/common';
import { createStubDataView } from '@kbn/data-views-plugin/common/stubs';
import type { SavedSearchAttributes } from '@kbn/saved-search-plugin/common';
import type { DiscoverSessionTabAttributes } from '@kbn/saved-search-plugin/server';
import type { LocatorServicesDeps as Services } from '.';
import type { DiscoverAppLocatorParams } from '../../common';
import { DOC_HIDE_TIME_COLUMN_SETTING } from '@kbn/discover-utils';
Expand All @@ -37,6 +38,20 @@ const defaultSavedSearch: SavedObject<SavedSearchAttributes> = {
searchSourceJSON:
'{"query":{"query":"","language":"kuery"},"filter":[],"indexRefName":"testIndexRefName"}',
},
tabs: [
{
id: 'tab-1',
label: 'Tab 1',
attributes: {
columns: ['response', 'url', 'clientip', 'machine.os', 'tags'],
sort: [['test', '134']],
kibanaSavedObjectMeta: {
searchSourceJSON:
'{"query":{"query":"","language":"kuery"},"filter":[],"indexRefName":"testIndexRefName"}',
},
},
},
],
} as unknown as SavedSearchAttributes,
};

Expand Down Expand Up @@ -172,7 +187,26 @@ test('with locator params containing a timeRange', async () => {
});

test('with saved search containing ["_source"]', async () => {
mockSavedSearch.attributes.columns = ['_source'];
mockSavedSearch = {
...defaultSavedSearch,
attributes: {
...defaultSavedSearch.attributes,
tabs: [
{
id: 'tab-1',
label: 'Tab 1',
attributes: {
columns: ['_source'],
sort: [['test', '134']],
kibanaSavedObjectMeta: {
searchSourceJSON:
'{"query":{"query":"","language":"kuery"},"filter":[],"indexRefName":"testIndexRefName"}',
},
} as DiscoverSessionTabAttributes,
},
],
},
};

const provider = searchSourceFromLocatorFactory(mockServices);
const searchSource = await provider(mockPayload[0].params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type { ExecutionContext } from '@kbn/expressions-plugin/common';
import type { KibanaContext, ExpressionFunctionKibanaContext } from '@kbn/data-plugin/common';
import { fromSavedSearchAttributes } from '../service/saved_searches_utils';
import type { SavedSearchAttributes, SavedSearch } from '../types';

import type { KibanaContextStartDependencies } from './kibana_context';
import { getKibanaContextFn } from './kibana_context';

Expand Down Expand Up @@ -57,6 +56,19 @@ describe('kibanaContextFn', () => {
query: [],
}),
},
tabs: [
{
id: 'test',
label: 'Test',
attributes: {
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
query: [],
}),
},
},
},
],
} as SavedSearchAttributes,
[],
undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ export const fromSavedSearchAttributes = <
ReturnType = Serialized extends true ? SerializableSavedSearch : SavedSearch
>(
id: string | undefined,
attributes: SavedSearchAttributes,
{ title, description, tabs }: SavedSearchAttributes,
tags: string[] | undefined,
searchSource: SavedSearch['searchSource'] | SerializedSearchSourceFields,
managed: boolean,
serialized: Serialized = false as Serialized
) =>
({
) => {
const [{ attributes }] = tabs;
return {
id,
...(serialized
? { serializedSearchSource: searchSource as SerializedSearchSourceFields }
: { searchSource }),
title: attributes.title,
title,
sort: attributes.sort,
columns: attributes.columns,
description: attributes.description,
description,
tags,
grid: attributes.grid,
hideChart: attributes.hideChart,
Expand All @@ -48,6 +49,7 @@ export const fromSavedSearchAttributes = <
breakdownField: attributes.breakdownField,
visContext: attributes.visContext,
density: attributes.density,
tabs: attributes.tabs,
tabs,
managed,
} as ReturnType);
} as ReturnType;
};
Loading
Loading