Skip to content

Commit

Permalink
Merge branch 'main' into feature/roles_description_api_changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kc13greiner committed May 1, 2024
2 parents 6c812ba + d887763 commit 6b7b2a2
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 107 deletions.
1 change: 0 additions & 1 deletion packages/kbn-esql-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export {
getESQLWithSafeLimit,
appendToESQLQuery,
TextBasedLanguages,
getESQLQueryColumns,
} from './src';

export { ESQL_LATEST_VERSION } from './constants';
1 change: 0 additions & 1 deletion packages/kbn-esql-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ export {
removeDropCommandsFromESQLQuery,
} from './utils/query_parsing_helpers';
export { appendToESQLQuery } from './utils/append_to_query';
export { getESQLQueryColumns } from './utils/run_query_utils';
52 changes: 0 additions & 52 deletions packages/kbn-esql-utils/src/utils/run_query_utils.ts

This file was deleted.

4 changes: 0 additions & 4 deletions packages/kbn-esql-utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
],
"kbn_references": [
"@kbn/data-views-plugin",
"@kbn/data-plugin",
"@kbn/crypto-browser",
"@kbn/data-view-utils",
"@kbn/esql-ast",
"@kbn/expressions-plugin",
"@kbn/field-types",
"@kbn/es-types",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/
import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks';
import { getESQLQueryColumns } from '@kbn/esql-utils';
import { fetchFieldsFromESQL } from '@kbn/text-based-editor';
import type { LensPluginStartDependencies } from '../../../plugin';
import { createMockStartDependencies } from '../../../editor_frame_service/mocks';
import {
Expand All @@ -18,44 +18,45 @@ import { suggestionsApi } from '../../../lens_suggestions_api';
import { getSuggestions } from './helpers';

const mockSuggestionApi = suggestionsApi as jest.Mock;
const mockFetchData = getESQLQueryColumns as jest.Mock;
const mockFetchData = fetchFieldsFromESQL as jest.Mock;

jest.mock('../../../lens_suggestions_api', () => ({
suggestionsApi: jest.fn(() => mockAllSuggestions),
}));

jest.mock('@kbn/esql-utils', () => {
return {
getESQLQueryColumns: jest.fn().mockResolvedValue(() => [
{
name: '@timestamp',
id: '@timestamp',
meta: {
type: 'date',
jest.mock('@kbn/text-based-editor', () => ({
fetchFieldsFromESQL: jest.fn(() => {
return {
columns: [
{
name: '@timestamp',
id: '@timestamp',
meta: {
type: 'date',
},
},
},
{
name: 'bytes',
id: 'bytes',
meta: {
type: 'number',
{
name: 'bytes',
id: 'bytes',
meta: {
type: 'number',
},
},
},
{
name: 'memory',
id: 'memory',
meta: {
type: 'number',
{
name: 'memory',
id: 'memory',
meta: {
type: 'number',
},
},
},
]),
getIndexPatternFromESQLQuery: jest.fn().mockReturnValue('index1'),
};
});
],
};
}),
}));

describe('getSuggestions', () => {
const query = {
esql: 'from index1 | limit 10 | stats average = avg(bytes)',
esql: 'from index1 | limit 10 | stats average = avg(bytes',
};
const mockStartDependencies =
createMockStartDependencies() as unknown as LensPluginStartDependencies;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,39 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import {
getIndexPatternFromSQLQuery,
getIndexPatternFromESQLQuery,
getESQLAdHocDataview,
getESQLQueryColumns,
} from '@kbn/esql-utils';
import { getIndexPatternFromSQLQuery, getIndexPatternFromESQLQuery } from '@kbn/esql-utils';
import type { AggregateQuery } from '@kbn/es-query';
import { getESQLAdHocDataview } from '@kbn/esql-utils';
import { getLensAttributesFromSuggestion } from '@kbn/visualization-utils';
import { fetchFieldsFromESQL } from '@kbn/text-based-editor';
import type { DataViewSpec } from '@kbn/data-views-plugin/public';
import type { TypedLensByValueInput } from '../../../embeddable/embeddable_component';
import type { LensPluginStartDependencies } from '../../../plugin';
import type { DatasourceMap, VisualizationMap } from '../../../types';
import { suggestionsApi } from '../../../lens_suggestions_api';

export const getQueryColumns = async (
query: AggregateQuery,
deps: LensPluginStartDependencies,
abortController?: AbortController
) => {
// Fetching only columns for ES|QL for performance reasons with limit 0
// Important note: ES doesnt return the warnings for 0 limit,
// I am skipping them in favor of performance now
// but we should think another way to get them (from Lens embeddable or store)
const performantQuery = { ...query };
if ('esql' in performantQuery && performantQuery.esql) {
performantQuery.esql = `${performantQuery.esql} | limit 0`;
}
const table = await fetchFieldsFromESQL(
performantQuery,
deps.expressions,
undefined,
abortController
);
return table?.columns;
};

export const getSuggestions = async (
query: AggregateQuery,
deps: LensPluginStartDependencies,
Expand Down Expand Up @@ -46,12 +65,7 @@ export const getSuggestions = async (
if (dataView.fields.getByName('@timestamp')?.type === 'date' && !dataViewSpec) {
dataView.timeFieldName = '@timestamp';
}

const columns = await getESQLQueryColumns({
esqlQuery: 'esql' in query ? query.esql : '',
search: deps.data.search,
signal: abortController?.signal,
});
const columns = await getQueryColumns(query, deps, abortController);
const context = {
dataViewSpec: dataView?.toSpec(),
fieldName: '',
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/lens/public/editor_frame_service/mocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function createMockSetupDependencies() {

export function createMockStartDependencies() {
return {
data: dataPluginMock.createStartContract(),
data: dataPluginMock.createSetupContract(),
embeddable: embeddablePluginMock.createStartContract(),
expressions: expressionsPluginMock.createStartContract(),
charts: chartPluginMock.createStartContract(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
*/
import { createGetterSetter } from '@kbn/kibana-utils-plugin/common';
import type { CoreStart } from '@kbn/core/public';
import { getESQLQueryColumns } from '@kbn/esql-utils';
import { getLensAttributesFromSuggestion } from '@kbn/visualization-utils';
import { IncompatibleActionError } from '@kbn/ui-actions-plugin/public';
import { PresentationContainer } from '@kbn/presentation-containers';
import { getESQLAdHocDataview, getIndexForESQLQuery } from '@kbn/esql-utils';
import type { Datasource, Visualization } from '../../types';
import type { LensPluginStartDependencies } from '../../plugin';
import { fetchDataFromAggregateQuery } from '../../datasources/text_based/fetch_data_from_aggregate_query';
import { suggestionsApi } from '../../lens_suggestions_api';
import { generateId } from '../../id_generator';
import { executeEditAction } from './edit_action_helpers';
Expand Down Expand Up @@ -66,17 +66,21 @@ export async function executeCreateAction({
// so we are requesting them with limit 0
// this is much more performant than requesting
// all the table
const abortController = new AbortController();
const columns = await getESQLQueryColumns({
esqlQuery: `from ${defaultIndex}`,
search: deps.data.search,
signal: abortController.signal,
});
const performantQuery = {
esql: `from ${defaultIndex} | limit 0`,
};

const table = await fetchDataFromAggregateQuery(
performantQuery,
dataView,
deps.data,
deps.expressions
);

const context = {
dataViewSpec: dataView.toSpec(),
fieldName: '',
textBasedColumns: columns,
textBasedColumns: table?.columns,
query: defaultEsqlQuery,
};

Expand Down

0 comments on commit 6b7b2a2

Please sign in to comment.