Skip to content

Commit

Permalink
extend textBasedDatasource rather than introducing a new valueBased d…
Browse files Browse the repository at this point in the history
…atasource
  • Loading branch information
ppisljar committed Nov 15, 2023
1 parent c873473 commit fe255c1
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 457 deletions.
2 changes: 0 additions & 2 deletions x-pack/plugins/lens/public/async_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,11 @@ export { getEditLensConfiguration } from './app_plugin/shared/edit_on_the_fly/ge

export * from './datasources/form_based/form_based';
export { getTextBasedDatasource } from './datasources/text_based/text_based_languages';
export { getValueBasedDatasource } from './datasources/value_based/text_based_languages';
export { createFormulaPublicApi } from './datasources/form_based/operations/definitions/formula/formula_public_api';
export * from './lens_suggestions_api';

export * from './datasources/text_based';
export * from './datasources/form_based';
export * from './datasources/value_based';
export * from './lens_ui_telemetry';
export * from './lens_ui_errors';
export * from './editor_frame_service/editor_frame';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ export function getTextBasedDatasource({
onRefreshIndexPattern() {},

getUsedDataViews: (state) => {
return Object.values(state.layers).map(({ index }) => index);
return Object.values(state.layers)
.map(({ index }) => index)
.filter((index) => index !== undefined) as string[];
},

getPersistableState({ layers }: TextBasedPrivateState) {
Expand Down Expand Up @@ -310,16 +312,16 @@ export function getTextBasedDatasource({
return (
Boolean(layers) &&
Object.values(layers).some((layer) => {
return Boolean(indexPatterns[layer.index]?.timeFieldName);
return layer.index && Boolean(indexPatterns[layer.index]?.timeFieldName);
})
);
},
getUsedDataView: (state: TextBasedPrivateState, layerId?: string) => {
if (!layerId) {
if (!layerId || !state.layers[layerId].index) {
const layers = Object.values(state.layers);
return layers?.[0]?.index;
return layers?.[0]?.index as string;
}
return state.layers[layerId].index;
return state.layers[layerId].index as string;
},

removeColumn,
Expand Down Expand Up @@ -613,9 +615,11 @@ export function getTextBasedDatasource({
getDatasourceInfo: async (state, references, dataViewsService) => {
const indexPatterns: DataView[] = [];
for (const { index } of Object.values(state.layers)) {
const dataView = await dataViewsService?.get(index);
if (dataView) {
indexPatterns.push(dataView);
if (index) {
const dataView = await dataViewsService?.get(index);
if (dataView) {
indexPatterns.push(dataView);
}
}
}
return Object.entries(state.layers).reduce<DataSourceInfo[]>((acc, [key, layer]) => {
Expand Down
54 changes: 40 additions & 14 deletions x-pack/plugins/lens/public/datasources/text_based/to_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { textBasedQueryStateToExpressionAst } from '@kbn/data-plugin/common';
import type { OriginalColumn } from '../../../common/types';
import { TextBasedPrivateState, TextBasedLayer, IndexPatternRef } from './types';

function getExpressionForLayer(layer: TextBasedLayer, refs: IndexPatternRef[]): Ast | null {
function getExpressionForLayer(
layer: TextBasedLayer,
layerId: string,
refs: IndexPatternRef[]
): Ast | null {
if (!layer.columns || layer.columns?.length === 0) {
return null;
}
Expand All @@ -36,24 +40,46 @@ function getExpressionForLayer(layer: TextBasedLayer, refs: IndexPatternRef[]):
});
const timeFieldName = layer.timeField ?? undefined;

const textBasedQueryToAst = textBasedQueryStateToExpressionAst({
query: layer.query,
timeFieldName,
});
if (!layer.table) {
const textBasedQueryToAst = textBasedQueryStateToExpressionAst({
query: layer.query,
timeFieldName,
});

textBasedQueryToAst.chain.push({
type: 'function',
function: 'lens_map_to_columns',
arguments: {
idMap: [JSON.stringify(idMapper)],
},
});
return textBasedQueryToAst;
textBasedQueryToAst.chain.push({
type: 'function',
function: 'lens_map_to_columns',
arguments: {
idMap: [JSON.stringify(idMapper)],
},
});
return textBasedQueryToAst;
} else {
return {
type: 'expression',
chain: [
{
type: 'function',
function: 'var',
arguments: {
name: [layerId],
},
},
{
type: 'function',
function: 'lens_map_to_columns',
arguments: {
idMap: [JSON.stringify(idMapper)],
},
},
],
};
}
}

export function toExpression(state: TextBasedPrivateState, layerId: string) {
if (state.layers[layerId]) {
return getExpressionForLayer(state.layers[layerId], state.indexPatternRefs);
return getExpressionForLayer(state.layers[layerId], layerId, state.indexPatternRefs);
}

return null;
Expand Down
7 changes: 4 additions & 3 deletions x-pack/plugins/lens/public/datasources/text_based/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import type { DatatableColumn } from '@kbn/expressions-plugin/public';
import type { Datatable, DatatableColumn } from '@kbn/expressions-plugin/public';
import type { AggregateQuery } from '@kbn/es-query';
import type { VisualizeFieldContext } from '@kbn/ui-actions-plugin/public';
import type { VisualizeEditorContext } from '../../types';
Expand All @@ -21,8 +21,9 @@ export interface TextBasedField {
}

export interface TextBasedLayer {
index: string;
query: AggregateQuery | undefined;
index?: string;
query?: AggregateQuery | undefined;
table?: Datatable;
columns: TextBasedLayerColumn[];
allColumns: TextBasedLayerColumn[];
timeField?: string;
Expand Down
43 changes: 0 additions & 43 deletions x-pack/plugins/lens/public/datasources/value_based/index.ts

This file was deleted.

Loading

0 comments on commit fe255c1

Please sign in to comment.