diff --git a/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/helpers.test.ts b/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/helpers.test.ts index bc646ac140c953..57638b61db1a9a 100644 --- a/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/helpers.test.ts +++ b/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/helpers.test.ts @@ -5,6 +5,7 @@ * 2.0. */ import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks'; +import { fetchFieldsFromESQL } from '@kbn/text-based-editor'; import type { LensPluginStartDependencies } from '../../../plugin'; import { createMockStartDependencies } from '../../../editor_frame_service/mocks'; import { @@ -14,7 +15,6 @@ import { mockAllSuggestions, } from '../../../mocks'; import { suggestionsApi } from '../../../lens_suggestions_api'; -import { fetchFieldsFromESQL } from '../../../datasources/text_based/fetch_fields_from_esql'; import { getSuggestions } from './helpers'; const mockSuggestionApi = suggestionsApi as jest.Mock; @@ -24,7 +24,7 @@ jest.mock('../../../lens_suggestions_api', () => ({ suggestionsApi: jest.fn(() => mockAllSuggestions), })); -jest.mock('../../../datasources/text_based/fetch_fields_from_esql', () => ({ +jest.mock('@kbn/text-based-editor', () => ({ fetchFieldsFromESQL: jest.fn(() => { return { columns: [ diff --git a/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/helpers.ts b/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/helpers.ts index 1e1fd600b68797..4555f3f8a576d2 100644 --- a/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/helpers.ts +++ b/x-pack/plugins/lens/public/app_plugin/shared/edit_on_the_fly/helpers.ts @@ -7,12 +7,12 @@ import { i18n } from '@kbn/i18n'; import { getIndexPatternFromSQLQuery, getIndexPatternFromESQLQuery } from '@kbn/es-query'; import type { AggregateQuery, Query, Filter } from '@kbn/es-query'; +import { fetchFieldsFromESQL } from '@kbn/text-based-editor'; import type { DataView, DataViewSpec } from '@kbn/data-views-plugin/public'; import type { Suggestion } from '../../../types'; import type { TypedLensByValueInput } from '../../../embeddable/embeddable_component'; import type { LensPluginStartDependencies } from '../../../plugin'; import type { DatasourceMap, VisualizationMap } from '../../../types'; -import { fetchFieldsFromESQL } from '../../../datasources/text_based/fetch_fields_from_esql'; import { suggestionsApi } from '../../../lens_suggestions_api'; export const getQueryColumns = async (query: AggregateQuery, deps: LensPluginStartDependencies) => { diff --git a/x-pack/plugins/lens/public/datasources/text_based/components/dimension_trigger.tsx b/x-pack/plugins/lens/public/datasources/text_based/components/dimension_trigger.tsx index b1eec31ea3892a..f6062068cee774 100644 --- a/x-pack/plugins/lens/public/datasources/text_based/components/dimension_trigger.tsx +++ b/x-pack/plugins/lens/public/datasources/text_based/components/dimension_trigger.tsx @@ -7,6 +7,7 @@ import React, { useEffect, useState } from 'react'; import { i18n } from '@kbn/i18n'; +import { fetchFieldsFromESQL } from '@kbn/text-based-editor'; import { DimensionTrigger } from '@kbn/visualization-ui-components'; import type { ExpressionsStart } from '@kbn/expressions-plugin/public'; import type { DatasourceDimensionTriggerProps } from '../../../types'; @@ -16,7 +17,6 @@ import { addColumnsToCache, retrieveLayerColumnsFromCache, } from '../fieldlist_cache'; -import { fetchFieldsFromESQL } from '../fetch_fields_from_esql'; export type TextBasedDimensionTrigger = DatasourceDimensionTriggerProps & { columnLabelMap: Record; diff --git a/x-pack/plugins/lens/public/datasources/text_based/fetch_fields_from_esql.ts b/x-pack/plugins/lens/public/datasources/text_based/fetch_fields_from_esql.ts deleted file mode 100644 index e19fde19187ffd..00000000000000 --- a/x-pack/plugins/lens/public/datasources/text_based/fetch_fields_from_esql.ts +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { pluck } from 'rxjs/operators'; -import { lastValueFrom } from 'rxjs'; -import type { Query, AggregateQuery } from '@kbn/es-query'; -import type { ExpressionsStart } from '@kbn/expressions-plugin/public'; -import type { Datatable } from '@kbn/expressions-plugin/public'; -import { textBasedQueryStateToAstWithValidation } from '@kbn/data-plugin/common'; - -interface TextBasedLanguagesErrorResponse { - error: { - message: string; - }; - type: 'error'; -} - -export function fetchFieldsFromESQL(query: Query | AggregateQuery, expressions: ExpressionsStart) { - return textBasedQueryStateToAstWithValidation({ - query, - }) - .then((ast) => { - if (ast) { - const execution = expressions.run(ast, null); - let finalData: Datatable; - let error: string | undefined; - execution.pipe(pluck('result')).subscribe((resp) => { - const response = resp as Datatable | TextBasedLanguagesErrorResponse; - if (response.type === 'error') { - error = response.error.message; - } else { - finalData = response; - } - }); - return lastValueFrom(execution).then(() => { - if (error) { - throw new Error(error); - } else { - return finalData; - } - }); - } - return undefined; - }) - .catch((err) => { - throw new Error(err.message); - }); -} diff --git a/x-pack/plugins/lens/tsconfig.json b/x-pack/plugins/lens/tsconfig.json index 350cd1ad19a9ef..ddffc5c8114c84 100644 --- a/x-pack/plugins/lens/tsconfig.json +++ b/x-pack/plugins/lens/tsconfig.json @@ -103,7 +103,8 @@ "@kbn/lens-formula-docs", "@kbn/visualization-utils", "@kbn/test-eui-helpers", - "@kbn/shared-ux-utility" + "@kbn/shared-ux-utility", + "@kbn/text-based-editor" ], "exclude": [ "target/**/*"