Skip to content

Commit

Permalink
[Discover] Fix Discover navigation from Lens embeddable (elastic#147000)
Browse files Browse the repository at this point in the history
## Summary

Fixes elastic#146761

This PR fixes navigation to Discover from Lens embeddable.


### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

Co-authored-by: Marco Liberati <[email protected]>
  • Loading branch information
2 people authored and nreese committed Dec 15, 2022
1 parent cc7e9bf commit 4857259
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})}
Expand Down
7 changes: 7 additions & 0 deletions test/functional/page_objects/unified_search_page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}"]`
Expand Down
16 changes: 9 additions & 7 deletions x-pack/plugins/lens/public/embeddable/embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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';
Expand Down Expand Up @@ -162,7 +162,7 @@ export interface LensEmbeddableDeps {
}

export interface ViewUnderlyingDataArgs {
indexPatternId: string;
dataViewSpec: DataViewSpec;
timeRange: TimeRange;
filters: Filter[];
query: Query | AggregateQuery | undefined;
Expand Down Expand Up @@ -236,8 +236,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,
Expand Down Expand Up @@ -906,10 +908,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))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -114,7 +114,12 @@ describe('open in discover action', () => {

await createOpenInDiscoverAction(
locator,
{ get: () => ({ isTimeBased: () => true }) } as unknown as DataViewsService,
{
get: () => ({
isTimeBased: () => true,
toSpec: () => ({ id: 'index-pattern-id' }),
}),
} as unknown as DataViewsService,
true
).execute({
embeddable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,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
Expand Down
54 changes: 54 additions & 0 deletions x-pack/test/functional/apps/lens/group1/ad_hoc_data_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
});
}

0 comments on commit 4857259

Please sign in to comment.