[8.6] [Discover] Fix Discover navigation from Lens embeddable (#147000) (#147608)

# Backport

This will backport the following commits from `main` to `8.6`:
- [[Discover] Fix Discover navigation from Lens embeddable
(#147000)](https://github.com/elastic/kibana/pull/147000)

<!--- Backport version: 8.9.7 -->

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

<!--BACKPORT [{"author":{"name":"Dmitry
Tomashevich","email":"39378793+dimaanj@users.noreply.github.com"},"sourceCommit":{"committedDate":"2022-12-14T13:17:17Z","message":"[Discover]
Fix Discover navigation from Lens embeddable (#147000)\n\n##
Summary\r\n\r\nFixes #146761\r\n\r\nThis PR fixes navigation to Discover
from Lens embeddable.\r\n\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: Marco Liberati
<dej611@users.noreply.github.com>","sha":"1f04bf89a3df377c79dba225dd47c9a96dc3e395","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Discover","release_note:fix","auto-backport","Team:DataDiscovery","v8.7.0","v8.6.1"],"number":147000,"url":"https://github.com/elastic/kibana/pull/147000","mergeCommit":{"message":"[Discover]
Fix Discover navigation from Lens embeddable (#147000)\n\n##
Summary\r\n\r\nFixes #146761\r\n\r\nThis PR fixes navigation to Discover
from Lens embeddable.\r\n\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: Marco Liberati
<dej611@users.noreply.github.com>","sha":"1f04bf89a3df377c79dba225dd47c9a96dc3e395"}},"sourceBranch":"main","suggestedTargetBranches":["8.6"],"targetPullRequestStates":[{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/147000","number":147000,"mergeCommit":{"message":"[Discover]
Fix Discover navigation from Lens embeddable (#147000)\n\n##
Summary\r\n\r\nFixes #146761\r\n\r\nThis PR fixes navigation to Discover
from Lens embeddable.\r\n\r\n\r\n### Checklist\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\nCo-authored-by: Marco Liberati
<dej611@users.noreply.github.com>","sha":"1f04bf89a3df377c79dba225dd47c9a96dc3e395"}},{"branch":"8.6","label":"v8.6.1","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Dmitry Tomashevich 2022-12-15 16:52:57 +03:00 committed by GitHub
parent 5d961c8ec0
commit 66d0f5590d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 17 deletions

View file

@ -50,7 +50,7 @@ export function DataViewsList({
value: id,
checked: id === currentDataViewId && !Boolean(isTextBasedLangSelected) ? 'on' : undefined,
append: isAdhoc ? (
<EuiBadge color="hollow">
<EuiBadge color="hollow" data-test-subj={`dataViewItemTempBadge-${name}`}>
{i18n.translate('unifiedSearch.query.queryBar.indexPattern.temporaryDataviewLabel', {
defaultMessage: 'Temporary',
})}

View file

@ -75,6 +75,13 @@ export class UnifiedSearchPageObject extends FtrService {
await this.testSubjects.click(adHoc ? 'exploreIndexPatternButton' : 'saveIndexPatternButton');
}
public async isAdHocDataView() {
const dataViewSwitcher = await this.testSubjects.find('discover-dataView-switch-link');
const dataViewName = await dataViewSwitcher.getVisibleText();
await dataViewSwitcher.click();
return await this.testSubjects.exists(`dataViewItemTempBadge-${dataViewName}`);
}
public async selectTextBasedLanguage(language: string) {
await this.find.clickByCssSelector(
`[data-test-subj="text-based-languages-switcher"] [title="${language}"]`

View file

@ -65,7 +65,7 @@ import type {
} from '@kbn/core/public';
import type { SpacesPluginStart } from '@kbn/spaces-plugin/public';
import { BrushTriggerEvent, ClickTriggerEvent, Warnings } from '@kbn/charts-plugin/public';
import { DataViewPersistableStateService } from '@kbn/data-views-plugin/common';
import { DataViewPersistableStateService, DataViewSpec } from '@kbn/data-views-plugin/common';
import { getExecutionContextEvents, trackUiCounterEvents } from '../lens_ui_telemetry';
import { Document } from '../persistence';
import { ExpressionWrapper, ExpressionWrapperProps } from './expression_wrapper';
@ -186,7 +186,7 @@ export interface LensEmbeddableDeps {
}
export interface ViewUnderlyingDataArgs {
indexPatternId: string;
dataViewSpec: DataViewSpec;
timeRange: TimeRange;
filters: Filter[];
query: Query | AggregateQuery | undefined;
@ -260,8 +260,10 @@ function getViewUnderlyingDataArgs({
esQueryConfig
);
const dataViewSpec = indexPatternsCache[meta.id]!.spec;
return {
indexPatternId: meta.id,
dataViewSpec,
timeRange,
filters: newFilters,
query: aggregateQuery.length > 0 ? aggregateQuery[0] : newQuery,
@ -930,10 +932,10 @@ export class Embeddable
const adHocDataviews = await Promise.all(
Object.values(this.savedVis?.state.adHocDataViews || {})
.map((persistedSpec) => {
return DataViewPersistableStateService.inject(
persistedSpec,
this.savedVis?.references || []
);
return DataViewPersistableStateService.inject(persistedSpec, [
...(this.savedVis?.references || []),
...(this.savedVis?.state.internalReferences || []),
]);
})
.map((spec) => this.deps.dataViews.create(spec))
);

View file

@ -19,7 +19,7 @@ describe('open in discover action', () => {
const embeddable = { type: 'NOT_LENS' } as IEmbeddable;
const isCompatible = await createOpenInDiscoverAction(
{} as DiscoverStart,
{} as unknown as Pick<DiscoverStart, 'locator'>,
{} as DataViewsService,
true
).isCompatible({
@ -37,7 +37,7 @@ describe('open in discover action', () => {
// make sure it would work if we had access to Discover
expect(
await createOpenInDiscoverAction(
{} as DiscoverStart,
{} as unknown as Pick<DiscoverStart, 'locator'>,
{} as DataViewsService,
hasDiscoverAccess
).isCompatible({
@ -49,7 +49,7 @@ describe('open in discover action', () => {
hasDiscoverAccess = false;
expect(
await createOpenInDiscoverAction(
{} as DiscoverStart,
{} as unknown as Pick<DiscoverStart, 'locator'>,
{} as DataViewsService,
hasDiscoverAccess
).isCompatible({
@ -65,7 +65,7 @@ describe('open in discover action', () => {
embeddable.canViewUnderlyingData = jest.fn(() => Promise.resolve(false));
expect(
await createOpenInDiscoverAction(
{} as DiscoverStart,
{} as unknown as Pick<DiscoverStart, 'locator'>,
{} as DataViewsService,
true
).isCompatible({
@ -79,7 +79,7 @@ describe('open in discover action', () => {
embeddable.canViewUnderlyingData = jest.fn(() => Promise.resolve(true));
expect(
await createOpenInDiscoverAction(
{} as DiscoverStart,
{} as unknown as Pick<DiscoverStart, 'locator'>,
{} as DataViewsService,
true
).isCompatible({
@ -93,7 +93,7 @@ describe('open in discover action', () => {
it('navigates to discover when executed', async () => {
const viewUnderlyingDataArgs = {
indexPatternId: 'index-pattern-id',
dataViewSpec: { id: 'index-pattern-id' },
timeRange: {},
filters: [],
query: undefined,
@ -115,8 +115,13 @@ describe('open in discover action', () => {
globalThis.open = jest.fn();
await createOpenInDiscoverAction(
discover,
{ get: () => ({ isTimeBased: () => true }) } as unknown as DataViewsService,
discover as unknown as Pick<DiscoverStart, 'locator'>,
{
get: () => ({
isTimeBased: () => true,
toSpec: () => ({ id: 'index-pattern-id' }),
}),
} as unknown as DataViewsService,
true
).execute({
embeddable,

View file

@ -53,7 +53,7 @@ async function getDiscoverLocationParams({
// shouldn't be executed because of the isCompatible check
throw new Error('Underlying data is not ready');
}
const dataView = await dataViews.get(args.indexPatternId);
const dataView = await dataViews.get(args.dataViewSpec.id!);
let filtersToApply = [...(filters || []), ...args.filters];
let timeRangeToApply = args.timeRange;
// if the target data view is time based, attempt to split out a time range from the provided filters

View file

@ -54,6 +54,25 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
});
}
const checkDiscoverNavigationResult = async () => {
await testSubjects.click('embeddablePanelToggleMenuIcon');
await testSubjects.click('embeddablePanelMore-mainMenu');
await testSubjects.click('embeddablePanelAction-ACTION_OPEN_IN_DISCOVER');
const [, discoverHandle] = await browser.getAllWindowHandles();
await browser.switchToWindow(discoverHandle);
await PageObjects.header.waitUntilLoadingHasFinished();
const actualIndexPattern = await (
await testSubjects.find('discover-dataView-switch-link')
).getVisibleText();
expect(actualIndexPattern).to.be('*stash*');
const actualDiscoverQueryHits = await testSubjects.getVisibleText('unifiedHistogramQueryHits');
expect(actualDiscoverQueryHits).to.be('14,005');
expect(await PageObjects.unifiedSearch.isAdHocDataView()).to.be(true);
};
describe('lens ad hoc data view tests', () => {
it('should allow building a chart based on ad hoc data view', async () => {
await setupAdHocDataView();
@ -176,6 +195,41 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
await PageObjects.discover.clickFieldListItemToggle('_bytes-runtimefield');
const newDataViewId = await PageObjects.discover.getCurrentDataViewId();
expect(newDataViewId).not.to.equal(prevDataViewId);
expect(await PageObjects.unifiedSearch.isAdHocDataView()).to.be(true);
await browser.closeCurrentWindow();
});
it('should navigate to discover from embeddable correctly', async () => {
const [lensHandle] = await browser.getAllWindowHandles();
await browser.switchToWindow(lensHandle);
await PageObjects.header.waitUntilLoadingHasFinished();
await setupAdHocDataView();
await PageObjects.lens.configureDimension({
dimension: 'lnsXY_yDimensionPanel > lns-empty-dimension',
operation: 'average',
field: 'bytes',
});
await PageObjects.lens.save(
'embeddable-test-with-adhoc-data-view',
false,
false,
false,
'new'
);
await checkDiscoverNavigationResult();
await browser.closeCurrentWindow();
const [daashboardHandle] = await browser.getAllWindowHandles();
await browser.switchToWindow(daashboardHandle);
await PageObjects.header.waitUntilLoadingHasFinished();
// adhoc data view should be persisted after refresh
await browser.refresh();
await checkDiscoverNavigationResult();
});
});
}