Skip to content

Commit

Permalink
[ES|QL] Apply the timerange to the fields fetch in the editor (elasti…
Browse files Browse the repository at this point in the history
…c#208490)

## Summary

Provides the timerange in the fetch fields api. This:

- fixes the bug where the fields are not suggested when there are system
named params such as `_tstart` and `_tend`
- makes it more performant as now it checks for fields in the selected
timerange making it more performant (especially for data in the frozen
tiers)

<img width="834" alt="image"
src="https://github.com/user-attachments/assets/ec0e6f87-3149-4a3f-a620-d7eab6a852a2"
/>
  • Loading branch information
stratoula authored Jan 30, 2025
1 parent 17e3f82 commit a63781a
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ESQLEditor } from './esql_editor';
import type { ESQLEditorProps } from './types';
import { ReactWrapper } from 'enzyme';
import { coreMock } from '@kbn/core/server/mocks';
import { dataPluginMock } from '@kbn/data-plugin/public/mocks';

describe('ESQLEditor', () => {
const uiConfig: Record<string, any> = {};
Expand All @@ -30,6 +31,7 @@ describe('ESQLEditor', () => {
client: uiSettings,
},
core: coreMock.createStart(),
data: dataPluginMock.createStartContract(),
};

function renderESQLEditorComponent(testProps: ESQLEditorProps) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { isEqual } from 'lodash';
import { CodeEditor, CodeEditorProps } from '@kbn/code-editor';
import type { CoreStart } from '@kbn/core/public';
import type { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public';
import type { AggregateQuery } from '@kbn/es-query';
import type { AggregateQuery, TimeRange } from '@kbn/es-query';
import type { ExpressionsStart } from '@kbn/expressions-plugin/public';
import { useKibana } from '@kbn/kibana-react-plugin/public';
import { ESQLLang, ESQL_LANG_ID, monaco, type ESQLCallbacks } from '@kbn/monaco';
Expand Down Expand Up @@ -120,6 +120,7 @@ export const ESQLEditor = memo(function ESQLEditor({
fieldsMetadata,
uiSettings,
uiActions,
data,
} = kibana.services;

const variablesService = kibana.services?.esql?.variablesService;
Expand Down Expand Up @@ -388,7 +389,7 @@ export const ESQLEditor = memo(function ESQLEditor({
const { cache: esqlFieldsCache, memoizedFieldsFromESQL } = useMemo(() => {
// need to store the timing of the first request so we can atomically clear the cache per query
const fn = memoize(
(...args: [{ esql: string }, ExpressionsStart, undefined, AbortController?]) => ({
(...args: [{ esql: string }, ExpressionsStart, TimeRange, AbortController?]) => ({
timestamp: Date.now(),
result: fetchFieldsFromESQL(...args),
}),
Expand Down Expand Up @@ -422,11 +423,12 @@ export const ESQLEditor = memo(function ESQLEditor({
};
// Check if there's a stale entry and clear it
clearCacheWhenOld(esqlFieldsCache, esqlQuery.esql);
const timeRange = data.query.timefilter.timefilter.getTime();
try {
const table = await memoizedFieldsFromESQL(
esqlQuery,
expressions,
undefined,
timeRange,
abortController
).result;
const columns: ESQLRealField[] =
Expand Down Expand Up @@ -470,19 +472,21 @@ export const ESQLEditor = memo(function ESQLEditor({
return callbacks;
}, [
fieldsMetadata,
kibana.services?.esql?.getJoinIndicesAutocomplete,
dataSourcesCache,
query.esql,
memoizedSources,
dataViews,
core,
esqlFieldsCache,
data.query.timefilter.timefilter,
memoizedFieldsFromESQL,
expressions,
abortController,
indexManagementApiService,
histogramBarTarget,
variablesService,
kibana.services?.esql?.getJoinIndicesAutocomplete,
variablesService?.esqlVariables,
variablesService?.areSuggestionsEnabled,
]);

const queryRunButtonProperties = useMemo(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/platform/packages/private/kbn-esql-editor/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { FieldsMetadataPublicStart } from '@kbn/fields-metadata-plugin/publ
import type { UsageCollectionStart } from '@kbn/usage-collection-plugin/public';
import type { Storage } from '@kbn/kibana-utils-plugin/public';
import type { UiActionsStart } from '@kbn/ui-actions-plugin/public';
import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
import type { ESQLControlVariable } from '@kbn/esql-validation-autocomplete';

export interface ESQLEditorProps {
Expand Down Expand Up @@ -112,6 +113,7 @@ export interface EsqlPluginStartBase {
export interface ESQLEditorDeps {
core: CoreStart;
dataViews: DataViewsPublicPluginStart;
data: DataPublicPluginStart;
expressions: ExpressionsStart;
storage: Storage;
uiActions: UiActionsStart;
Expand Down
4 changes: 4 additions & 0 deletions src/platform/plugins/shared/esql/public/kibana_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { ExpressionsStart } from '@kbn/expressions-plugin/public';
import type { Storage } from '@kbn/kibana-utils-plugin/public';
import type { IndexManagementPluginSetup } from '@kbn/index-management-shared-types';
import type { FieldsMetadataPublicStart } from '@kbn/fields-metadata-plugin/public';
import type { DataPublicPluginStart } from '@kbn/data-plugin/public';
import type { UsageCollectionStart } from '@kbn/usage-collection-plugin/public';
import type { UiActionsStart } from '@kbn/ui-actions-plugin/public';
import type { EsqlPluginStart } from './plugin';
Expand All @@ -23,6 +24,7 @@ export let core: CoreStart;
interface ServiceDeps {
core: CoreStart;
dataViews: DataViewsPublicPluginStart;
data: DataPublicPluginStart;
expressions: ExpressionsStart;
storage: Storage;
uiActions: UiActionsStart;
Expand All @@ -49,6 +51,7 @@ export const setKibanaServices = (
esql: EsqlPluginStart,
kibanaCore: CoreStart,
dataViews: DataViewsPublicPluginStart,
data: DataPublicPluginStart,
expressions: ExpressionsStart,
storage: Storage,
uiActions: UiActionsStart,
Expand All @@ -61,6 +64,7 @@ export const setKibanaServices = (
core,
dataViews,
expressions,
data,
storage,
uiActions,
indexManagementApiService: indexManagement?.apiService,
Expand Down
1 change: 1 addition & 0 deletions src/platform/plugins/shared/esql/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class EsqlPlugin implements Plugin<{}, EsqlPluginStart> {
start,
core,
dataViews,
data,
expressions,
storage,
uiActions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('ValueControlForm', () => {
client: uiSettings,
},
core: coreMock.createStart(),
data: dataMock,
};

describe('Interval type', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { setKibanaServices } from '@kbn/esql/public/kibana_services';
import { coreMock } from '@kbn/core/public/mocks';
import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks';
import { dataPluginMock } from '@kbn/data-plugin/public/mocks';
import { expressionsPluginMock } from '@kbn/expressions-plugin/public/mocks';
import { uiActionsPluginMock } from '@kbn/ui-actions-plugin/public/mocks';
import type { Storage } from '@kbn/kibana-utils-plugin/public';
Expand Down Expand Up @@ -46,6 +47,7 @@ setKibanaServices(
},
coreMock.createStart(),
dataViewPluginMocks.createStartContract(),
dataPluginMock.createStartContract(),
expressionsPluginMock.createStartContract(),
storage,
uiActionsPluginMock.createStartContract()
Expand Down

0 comments on commit a63781a

Please sign in to comment.