Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { omit } from 'lodash';
import { SavedSearchType } from '@kbn/saved-search-plugin/common';
import type { SavedObjectReference } from '@kbn/core/server';
import { extractReferences, parseSearchSourceJSON } from '@kbn/data-plugin/common';
import type { DrilldownTransforms } from '@kbn/embeddable-plugin/common';
import type {
SearchEmbeddableByReferenceState,
SearchEmbeddableState,
StoredSearchEmbeddableState,
} from './types';
import { extract } from './search_inject_extract';

export const SAVED_SEARCH_SAVED_OBJECT_REF_NAME = 'savedObjectRef';

Expand Down Expand Up @@ -47,22 +48,38 @@ export function getTransformIn(transformDrilldownsIn: DrilldownTransforms['trans
}

// by value
const { state: extractedState, references } = extract({
type: SavedSearchType,
attributes: storedState.attributes,
const tabReferences: SavedObjectReference[] = [];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could it contain duplicates if e.g. multiple tabs reference the same data view? Doesn't seem like an issue for now since we only support one tab for by val, but mentioning since it doesn't seem like refs are namespaced by tab ID or anything.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, currently we're not adding a prefix for the tab, but we could definitely add this later.

const tabs = storedState.attributes.tabs.map((tab) => {
try {
const searchSourceValues = parseSearchSourceJSON(
tab.attributes.kibanaSavedObjectMeta.searchSourceJSON
);
const [searchSourceFields, searchSourceReferences] = extractReferences(searchSourceValues);
tabReferences.push(...searchSourceReferences);
return {
...tab,
attributes: {
...tab.attributes,
kibanaSavedObjectMeta: {
...tab.attributes.kibanaSavedObjectMeta,
searchSourceJSON: JSON.stringify(searchSourceFields),
},
},
};
} catch (e) {
return tab;
}
});

return {
state: {
...storedState,
attributes: {
...storedState.attributes,
...extractedState.attributes,
// discover session stores references as part of attributes
references,
...omit(storedState.attributes, 'references'),
tabs,
},
},
references: [...references, ...drilldownReferences],
references: [...tabReferences, ...drilldownReferences],
};
}
return transformIn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { flow, omit } from 'lodash';
import { extractTabs, SavedSearchType } from '@kbn/saved-search-plugin/common';
import { injectReferences, parseSearchSourceJSON } from '@kbn/data-plugin/common';
import type { DrilldownTransforms } from '@kbn/embeddable-plugin/common';
import type { SavedObjectReference } from '@kbn/core/server';
import { transformTitlesOut } from '@kbn/presentation-publishing';
import { flow } from 'lodash';
import type {
SearchEmbeddableByReferenceState,
SearchEmbeddableByValueState,
StoredSearchEmbeddableByValueState,
StoredSearchEmbeddableState,
} from './types';
import { inject } from './search_inject_extract';
import { SAVED_SEARCH_SAVED_OBJECT_REF_NAME } from './get_transform_in';

function isByValue(
Expand All @@ -42,14 +42,34 @@ export function getTransformOut(transformDrilldownsOut: DrilldownTransforms['tra
const state = transformsFlow(storedState);

if (isByValue(state)) {
const tabsState = {
...state,
attributes: extractTabs(state.attributes),
};
const { attributes } = inject({ type: SavedSearchType, ...tabsState }, references ?? []);
const tabsState = { ...state, attributes: extractTabs(state.attributes) };
const tabs = tabsState.attributes.tabs.map((tab) => {
try {
const searchSourceValues = parseSearchSourceJSON(
tab.attributes.kibanaSavedObjectMeta.searchSourceJSON
);
const searchSourceFields = injectReferences(searchSourceValues, references ?? []);
return {
...tab,
attributes: {
...omit(tab.attributes, 'references'),
kibanaSavedObjectMeta: {
...tab.attributes.kibanaSavedObjectMeta,
searchSourceJSON: JSON.stringify(searchSourceFields),
},
},
};
} catch (e) {
return tab;
}
});

return {
...state,
attributes,
attributes: {
...state.attributes,
tabs,
},
} as SearchEmbeddableByValueState;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export { inject, extract } from './search_inject_extract';
export { getSearchEmbeddableTransforms } from './search_embeddable_transforms';
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import type { DrilldownTransforms } from '@kbn/embeddable-plugin/common';
import { getSearchEmbeddableTransforms } from './search_embeddable_transforms';
import { extract, inject } from './search_inject_extract';
import type {
SearchEmbeddableByValueState,
StoredSearchEmbeddableByValueState,
Expand All @@ -18,13 +17,6 @@ import type {
SearchEmbeddableState,
} from './types';

jest.mock('./search_inject_extract', () => {
return {
inject: jest.fn((state, references) => state),
extract: jest.fn((state) => ({ state, references: [] })),
};
});

const mockDrilldownTransforms = {
transformIn: jest.fn().mockImplementation((state: SearchEmbeddableState) => ({
state,
Expand Down Expand Up @@ -82,10 +74,6 @@ describe('searchEmbeddableTransforms', () => {
state,
references
);
expect(inject).toHaveBeenCalledWith(
{ type: 'search', ...{ ...state, attributes: expectedAttributes } },
references
);
expect(result).toEqual({
...state,
attributes: expectedAttributes,
Expand Down Expand Up @@ -204,18 +192,13 @@ describe('searchEmbeddableTransforms', () => {
searchSourceJSON: '{"query":{"match_all":{}}}',
},
tabs: [],
references: [],
},
title: 'Panel Title',
};

const result =
getSearchEmbeddableTransforms(mockDrilldownTransforms).transformIn!(serializedState);

expect(extract).toHaveBeenCalledWith({
type: 'search',
attributes: serializedState.attributes,
});
expect(result.state as StoredSearchEmbeddableByValueState).toEqual(serializedState);
expect(result.references).toEqual([]);
});
Expand Down Expand Up @@ -244,10 +227,6 @@ describe('searchEmbeddableTransforms', () => {

expect(result.references).toEqual([]);
expect(mockDrilldownTransforms.transformIn).toHaveBeenCalledWith(serializedState);
expect(extract).toHaveBeenCalledWith({
type: 'search',
attributes: serializedState.attributes,
});
});
});
});
Expand Down

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion src/platform/plugins/shared/discover/moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ dependsOn:
- '@kbn/unified-field-list'
- '@kbn/cell-actions'
- '@kbn/shared-ux-utility'
- '@kbn/core-saved-objects-server'
- '@kbn/discover-utils'
- '@kbn/search-errors'
- '@kbn/search-response-warnings'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,8 @@ describe('Serialization utils', () => {
serializeDynamicActions: jest.fn(),
});

const attributes = toSavedSearchAttributes(
savedSearch,
searchSource.serialize().searchSourceJSON
);
const searchSourceJSON = JSON.stringify(searchSource.getSerializedFields());
const attributes = toSavedSearchAttributes(savedSearch, searchSourceJSON);

expect(serializedState).toEqual({
attributes: {
Expand All @@ -146,7 +144,6 @@ describe('Serialization utils', () => {
id: expect.any(String),
},
],
references: mockedSavedSearchAttributes.references,
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const serializeState = ({
savedObjectId?: string;
}): SearchEmbeddableState => {
const searchSource = savedSearch.searchSource;
const { searchSourceJSON, references: originalReferences } = searchSource.serialize();
const searchSourceJSON = JSON.stringify(searchSource.getSerializedFields());
Comment thread
davismcphee marked this conversation as resolved.
const savedSearchAttributes = toSavedSearchAttributes(savedSearch, searchSourceJSON);

if (savedObjectId) {
Expand All @@ -110,17 +110,10 @@ export const serializeState = ({
};
}

const state = {
attributes: {
...savedSearchAttributes,
references: originalReferences,
},
};

return {
...serializeTitles(),
...serializeTimeRange(),
...serializeDynamicActions?.(),
...state,
attributes: savedSearchAttributes,
};
};
Loading
Loading