Skip to content

Commit

Permalink
lens embeddable forceDSL flag (elastic#203963)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored Dec 19, 2024
1 parent 841bf73 commit 4e9ef87
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/plugins/unified_histogram/public/chart/histogram.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ export function Histogram({
>
<lens.EmbeddableComponent
{...lensProps}
// forceDSL is set to true to ensure that the Lens always uses DSL to fetch the data
// as some consumers (discover) rely on the total hits count which is not provided by ESQL
forceDSL={true}
abortController={abortController}
disableTriggers={disableTriggers}
disabledActions={disabledActions}
Expand Down
13 changes: 11 additions & 2 deletions x-pack/plugins/lens/public/datasources/form_based/form_based.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -450,15 +450,24 @@ export function getFormBasedDatasource({
);
},

toExpression: (state, layerId, indexPatterns, dateRange, nowInstant, searchSessionId) =>
toExpression: (
state,
layerId,
indexPatterns,
dateRange,
nowInstant,
searchSessionId,
forceDSL
) =>
toExpression(
state,
layerId,
indexPatterns,
uiSettings,
dateRange,
nowInstant,
searchSessionId
searchSessionId,
forceDSL
),

LayerSettingsComponent(props) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ function getExpressionForLayer(
uiSettings: IUiSettingsClient,
dateRange: DateRange,
nowInstant: Date,
searchSessionId?: string
searchSessionId?: string,
forceDSL?: boolean
): ExpressionAstExpression | null {
const { columnOrder } = layer;
if (columnOrder.length === 0 || !indexPattern) {
Expand Down Expand Up @@ -523,7 +524,8 @@ export function toExpression(
uiSettings: IUiSettingsClient,
dateRange: DateRange,
nowInstant: Date,
searchSessionId?: string
searchSessionId?: string,
forceDSL?: boolean
) {
if (state.layers[layerId]) {
return getExpressionForLayer(
Expand All @@ -532,7 +534,8 @@ export function toExpression(
uiSettings,
dateRange,
nowInstant,
searchSessionId
searchSessionId,
forceDSL
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export function getDatasourceExpressionsByLayers(
indexPatterns: IndexPatternMap,
dateRange: DateRange,
nowInstant: Date,
searchSessionId?: string
searchSessionId?: string,
forceDSL?: boolean
): null | Record<string, Ast> {
const datasourceExpressions: Array<[string, Ast | string]> = [];

Expand All @@ -34,7 +35,8 @@ export function getDatasourceExpressionsByLayers(
indexPatterns,
dateRange,
nowInstant,
searchSessionId
searchSessionId,
forceDSL
);
if (result) {
datasourceExpressions.push([layerId, result]);
Expand Down Expand Up @@ -67,6 +69,7 @@ export function buildExpression({
dateRange,
nowInstant,
searchSessionId,
forceDSL,
}: {
title?: string;
description?: string;
Expand All @@ -79,6 +82,7 @@ export function buildExpression({
searchSessionId?: string;
dateRange: DateRange;
nowInstant: Date;
forceDSL?: boolean;
}): Ast | null {
// if an unregistered visualization is passed in the SO
// then this will be set as "undefined". Relax the check to catch both
Expand All @@ -92,7 +96,8 @@ export function buildExpression({
indexPatterns,
dateRange,
nowInstant,
searchSessionId
searchSessionId,
forceDSL
);

const visualizationExpression = visualization.toExpression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ export async function persistedStateToExpression(
timefilter: TimefilterContract;
nowProvider: DataPublicPluginStart['nowProvider'];
eventAnnotationService: EventAnnotationServiceType;
forceDSL?: boolean;
}
): Promise<DocumentToExpressionReturnType> {
const {
Expand Down Expand Up @@ -459,6 +460,7 @@ export async function persistedStateToExpression(
datasourceLayers,
indexPatterns,
dateRange: { fromDate: currentTimeRange.from, toDate: currentTimeRange.to },
forceDSL: services.forceDSL,
nowInstant: services.nowProvider.get(),
}),
activeVisualizationState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ export const InnerWorkspacePanel = React.memo(function InnerWorkspacePanel({
dateRange: framePublicAPI.dateRange,
nowInstant: plugins.data.nowProvider.get(),
searchSessionId,
forceDSL: framePublicAPI.forceDSL,
});

if (ast) {
Expand Down Expand Up @@ -373,6 +374,7 @@ export const InnerWorkspacePanel = React.memo(function InnerWorkspacePanel({
datasourceLayers,
dataViews.indexPatterns,
framePublicAPI.dateRange,
framePublicAPI.forceDSL,
plugins.data.nowProvider,
searchSessionId,
addUserMessages,
Expand Down
5 changes: 4 additions & 1 deletion x-pack/plugins/lens/public/editor_frame_service/service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ export class EditorFrameService {
* This is an asynchronous process.
* @param doc parsed Lens saved object
*/
public documentToExpression = async (doc: LensDocument, services: EditorFramePlugins) => {
public documentToExpression = async (
doc: LensDocument,
services: EditorFramePlugins & { forceDSL?: boolean }
) => {
const [resolvedDatasources, resolvedVisualizations] = await Promise.all([
this.loadDatasources(),
this.loadVisualizations(),
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/lens/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,14 @@ export class LensPlugin {
coreStart,
timefilter: plugins.data.query.timefilter.timefilter,
expressionRenderer: plugins.expressions.ReactExpressionRenderer,
documentToExpression: (doc: LensDocument) =>
documentToExpression: (doc: LensDocument, forceDSL?: boolean) =>
this.editorFrameService!.documentToExpression(doc, {
dataViews: plugins.dataViews,
storage: new Storage(localStorage),
uiSettings: core.uiSettings,
timefilter: plugins.data.query.timefilter.timefilter,
nowProvider: plugins.data.nowProvider,
forceDSL,
eventAnnotationService,
}),
injectFilterReferences: data.query.filterManager.inject.bind(data.query.filterManager),
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/lens/public/react_embeddable/data_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ export function loadEmbeddableData(
handleEvent,
disableTriggers,
updateBlockingErrors,
forceDSL: (parentApi as { forceDSL?: boolean }).forceDSL,
getDisplayOptions: internalApi.getDisplayOptions,
}),
getUsedDataViews(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,20 @@ interface GetExpressionRendererPropsParams {
api: LensApi;
addUserMessages: (messages: UserMessage[]) => void;
updateBlockingErrors: (error: Error) => void;
forceDSL?: boolean;
getDisplayOptions: () => VisualizationDisplayOptions;
}

async function getExpressionFromDocument(
document: LensDocument,
documentToExpression: (doc: LensDocument) => Promise<DocumentToExpressionReturnType>
documentToExpression: (
doc: LensDocument,
forceDSL?: boolean
) => Promise<DocumentToExpressionReturnType>,
forceDSL?: boolean
) {
const { ast, indexPatterns, indexPatternRefs, activeVisualizationState, activeDatasourceState } =
await documentToExpression(document);
await documentToExpression(document, forceDSL);
return {
expression: ast ? toExpression(ast) : null,
indexPatterns,
Expand Down Expand Up @@ -147,6 +152,7 @@ export async function getExpressionRendererParams(
addUserMessages,
updateBlockingErrors,
searchContext,
forceDSL,
getDisplayOptions,
}: GetExpressionRendererPropsParams
): Promise<{
Expand All @@ -165,7 +171,7 @@ export async function getExpressionRendererParams(
indexPatternRefs,
activeVisualizationState,
activeDatasourceState,
} = await getExpressionFromDocument(state.attributes, documentToExpression);
} = await getExpressionFromDocument(state.attributes, documentToExpression, forceDSL);

// Apparently this change produces had lots of issues with solutions not using
// the Embeddable incorrectly. Will comment for now and later on will restore it when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export function initializeDashboardServices(
className: getUnchangingComparator<LensSharedProps, 'className'>(),
overrides: overridesComparator,
disableTriggers: disabledTriggersComparator,
forceDSL: getUnchangingComparator<LensSharedProps, 'forceDSL'>(),
isNewPanel: getUnchangingComparator<{ isNewPanel?: boolean }, 'isNewPanel'>(),
parentApi: getUnchangingComparator<Pick<LensApi, 'parentApi'>, 'parentApi'>(),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export function LensRenderer({
timeRange,
disabledActions,
searchSessionId,
forceDSL,
hidePanelTitles,
lastReloadRequestTime,
...props
Expand Down Expand Up @@ -157,6 +158,7 @@ export function LensRenderer({
...initialStateRef.current,
attributes: props.attributes,
}),
forceDSL,
hidePanelTitle: hidePanelTitles$,
reload$, // trigger a reload (replacement for deprepcated searchSessionId)
})}
Expand Down
19 changes: 16 additions & 3 deletions x-pack/plugins/lens/public/react_embeddable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ export type LensEmbeddableStartServices = Simplify<
coreStart: CoreStart;
capabilities: RecursiveReadonly<Capabilities>;
expressionRenderer: ReactExpressionRendererType;
documentToExpression: (doc: LensDocument) => Promise<DocumentToExpressionReturnType>;
documentToExpression: (
doc: LensDocument,
forceDSL?: boolean
) => Promise<DocumentToExpressionReturnType>;
injectFilterReferences: FilterManager['inject'];
visualizationMap: VisualizationMap;
datasourceMap: DatasourceMap;
Expand Down Expand Up @@ -261,6 +264,7 @@ export interface LensSharedProps {
className?: string;
noPadding?: boolean;
viewMode?: ViewMode;
forceDSL?: boolean;
}

interface LensRequestHandlersProps {
Expand Down Expand Up @@ -323,7 +327,13 @@ export type LensComponentProps = Simplify<
*/
export type LensComponentForwardedProps = Pick<
LensComponentProps,
'style' | 'className' | 'noPadding' | 'abortController' | 'executionContext' | 'viewMode'
| 'style'
| 'className'
| 'noPadding'
| 'abortController'
| 'executionContext'
| 'viewMode'
| 'forceDSL'
>;

/**
Expand All @@ -347,7 +357,10 @@ export type LensRendererProps = Simplify<LensRendererPrivateProps>;
export type LensRuntimeState = Simplify<
Omit<ComponentSerializedProps, 'attributes' | 'references'> & {
attributes: NonNullable<LensSerializedState['attributes']>;
} & Pick<LensComponentForwardedProps, 'viewMode' | 'abortController' | 'executionContext'> &
} & Pick<
LensComponentForwardedProps,
'viewMode' | 'abortController' | 'executionContext' | 'forceDSL'
> &
ContentManagementProps
>;

Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/lens/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ export interface Datasource<T = unknown, P = unknown, Q = Query | AggregateQuery
indexPatterns: IndexPatternMap,
dateRange: DateRange,
nowInstant: Date,
searchSessionId?: string
searchSessionId?: string,
forceDSL?: boolean
) => ExpressionAstExpression | string | null;

getDatasourceSuggestionsForField: (
Expand Down Expand Up @@ -962,6 +963,7 @@ export interface FramePublicAPI {
*/
activeData?: Record<string, Datatable>;
dataViews: DataViewsState;
forceDSL?: boolean;
}

/**
Expand Down

0 comments on commit 4e9ef87

Please sign in to comment.