forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] [Lens][Embeddable] Make UI react faster to click actions like c…
…reate or edit (elastic#210810) (elastic#212052) # Backport This will backport the following commits from `main` to `8.x`: - [[Lens][Embeddable] Make UI react faster to click actions like create or edit (elastic#210810)](elastic#210810) <!--- Backport version: 9.6.4 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Marco Liberati","email":"dej611@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-02-20T17:19:46Z","message":"[Lens][Embeddable] Make UI react faster to click actions like create or edit (elastic#210810)\n\n## Summary\n\nThis PR is based on the idea in elastic#209361 and tries to improve perceived\nperformances for all the scenarios where the `editorFrame` is loaded.\n\nOn fast connections this is now perceived very fast:\n\n\n\nOn Fast 4g is still fast\n\n\n\nOn Slow 4g is acceptable\n\n\n\n\nEven on 3G connection the feedback is much better now\n\n\n\n\nAs a bonus extra tests have been added for the ES|QL creation flow.\n\ncc @thomasneirynck @nreese \n\n### Checklist\n\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n\n---------\n\nCo-authored-by: Nick Partridge <nick.ryan.partridge@gmail.com>","sha":"1e92ae8afbec96f437040a7d3147b20e52478833","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:enhancement","Team:Visualizations","release_note:skip","Feature:Lens","backport:version","v9.1.0","v8.19.0"],"title":"[Lens][Embeddable] Make UI react faster to click actions like create or edit","number":210810,"url":"https://github.com/elastic/kibana/pull/210810","mergeCommit":{"message":"[Lens][Embeddable] Make UI react faster to click actions like create or edit (elastic#210810)\n\n## Summary\n\nThis PR is based on the idea in elastic#209361 and tries to improve perceived\nperformances for all the scenarios where the `editorFrame` is loaded.\n\nOn fast connections this is now perceived very fast:\n\n\n\nOn Fast 4g is still fast\n\n\n\nOn Slow 4g is acceptable\n\n\n\n\nEven on 3G connection the feedback is much better now\n\n\n\n\nAs a bonus extra tests have been added for the ES|QL creation flow.\n\ncc @thomasneirynck @nreese \n\n### Checklist\n\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n\n---------\n\nCo-authored-by: Nick Partridge <nick.ryan.partridge@gmail.com>","sha":"1e92ae8afbec96f437040a7d3147b20e52478833"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/210810","number":210810,"mergeCommit":{"message":"[Lens][Embeddable] Make UI react faster to click actions like create or edit (elastic#210810)\n\n## Summary\n\nThis PR is based on the idea in elastic#209361 and tries to improve perceived\nperformances for all the scenarios where the `editorFrame` is loaded.\n\nOn fast connections this is now perceived very fast:\n\n\n\nOn Fast 4g is still fast\n\n\n\nOn Slow 4g is acceptable\n\n\n\n\nEven on 3G connection the feedback is much better now\n\n\n\n\nAs a bonus extra tests have been added for the ES|QL creation flow.\n\ncc @thomasneirynck @nreese \n\n### Checklist\n\n- [x] [Unit or functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere updated or added to match the most common scenarios\n\n---------\n\nCo-authored-by: Nick Partridge <nick.ryan.partridge@gmail.com>","sha":"1e92ae8afbec96f437040a7d3147b20e52478833"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT-->
- Loading branch information
Showing
17 changed files
with
301 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
x-pack/platform/plugins/shared/lens/public/react_embeddable/esql.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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 { loadESQLAttributes } from './esql'; | ||
import { makeEmbeddableServices } from './mocks'; | ||
import { LensEmbeddableStartServices } from './types'; | ||
import { coreMock } from '@kbn/core/public/mocks'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import * as suggestionModule from '../lens_suggestions_api'; | ||
// Need to do this magic in order to spy on specific functions | ||
import * as esqlUtils from '@kbn/esql-utils'; | ||
import { dataViewMock } from '@kbn/discover-utils/src/__mocks__'; | ||
jest.mock('@kbn/esql-utils', () => ({ | ||
__esModule: true, | ||
...jest.requireActual('@kbn/esql-utils'), | ||
})); | ||
|
||
function getUiSettingsOverrides() { | ||
const core = coreMock.createStart({ basePath: '/testbasepath' }); | ||
return core.uiSettings; | ||
} | ||
|
||
describe('ES|QL attributes creation', () => { | ||
function getServices(servicesOverrides?: Partial<LensEmbeddableStartServices>) { | ||
return { | ||
...makeEmbeddableServices(new BehaviorSubject<string>(''), undefined, { | ||
visOverrides: { id: 'lnsXY' }, | ||
dataOverrides: { id: 'form_based' }, | ||
}), | ||
uiSettings: { ...getUiSettingsOverrides(), get: jest.fn().mockReturnValue(true) }, | ||
...servicesOverrides, | ||
}; | ||
} | ||
it('should not update the attributes if no index is available', async () => { | ||
jest.spyOn(esqlUtils, 'getIndexForESQLQuery').mockResolvedValueOnce(null); | ||
|
||
const attributes = await loadESQLAttributes(getServices()); | ||
expect(attributes).toBeUndefined(); | ||
}); | ||
|
||
it('should not update the attributes if no suggestion is generated', async () => { | ||
jest.spyOn(esqlUtils, 'getIndexForESQLQuery').mockResolvedValueOnce('index'); | ||
jest.spyOn(esqlUtils, 'getESQLAdHocDataview').mockResolvedValueOnce(dataViewMock); | ||
jest.spyOn(esqlUtils, 'getESQLQueryColumns').mockResolvedValueOnce([]); | ||
jest.spyOn(suggestionModule, 'suggestionsApi').mockReturnValue([]); | ||
|
||
const attributes = await loadESQLAttributes(getServices()); | ||
expect(attributes).toBeUndefined(); | ||
}); | ||
|
||
it('should update the attributes if there is a valid suggestion', async () => { | ||
jest.spyOn(esqlUtils, 'getIndexForESQLQuery').mockResolvedValueOnce('index'); | ||
jest.spyOn(esqlUtils, 'getESQLAdHocDataview').mockResolvedValueOnce(dataViewMock); | ||
jest.spyOn(esqlUtils, 'getESQLQueryColumns').mockResolvedValueOnce([]); | ||
jest.spyOn(suggestionModule, 'suggestionsApi').mockReturnValue([ | ||
{ | ||
title: 'MyTitle', | ||
visualizationId: 'lnsXY', | ||
datasourceId: 'form_based', | ||
datasourceState: {}, | ||
visualizationState: {}, | ||
columns: 1, | ||
score: 1, | ||
previewIcon: 'icon', | ||
changeType: 'initial', | ||
keptLayerIds: [], | ||
}, | ||
]); | ||
|
||
const attributes = await loadESQLAttributes(getServices()); | ||
expect(attributes).not.toBeUndefined(); | ||
}); | ||
}); |
88 changes: 88 additions & 0 deletions
88
x-pack/platform/plugins/shared/lens/public/react_embeddable/esql.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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 { | ||
getIndexForESQLQuery, | ||
getESQLAdHocDataview, | ||
getInitialESQLQuery, | ||
getESQLQueryColumns, | ||
} from '@kbn/esql-utils'; | ||
import { getLensAttributesFromSuggestion } from '@kbn/visualization-utils'; | ||
import { isESQLModeEnabled } from './initializers/utils'; | ||
import type { LensEmbeddableStartServices } from './types'; | ||
|
||
export async function loadESQLAttributes({ | ||
dataViews, | ||
data, | ||
visualizationMap, | ||
datasourceMap, | ||
uiSettings, | ||
}: Pick< | ||
LensEmbeddableStartServices, | ||
'dataViews' | 'data' | 'visualizationMap' | 'datasourceMap' | 'uiSettings' | ||
>) { | ||
// Early exit if ESQL is not supported | ||
if (!isESQLModeEnabled({ uiSettings })) { | ||
return; | ||
} | ||
const indexName = await getIndexForESQLQuery({ dataViews }); | ||
// Early exit if there's no data view to use | ||
if (!indexName) { | ||
return; | ||
} | ||
|
||
// From this moment on there are no longer early exists before suggestions | ||
// so make sure to load async modules while doing other async stuff to save some time | ||
const [dataView, { suggestionsApi }] = await Promise.all([ | ||
getESQLAdHocDataview(`from ${indexName}`, dataViews), | ||
import('../async_services'), | ||
]); | ||
|
||
const esqlQuery = getInitialESQLQuery(dataView); | ||
|
||
const defaultEsqlQuery = { | ||
esql: esqlQuery, | ||
}; | ||
|
||
// For the suggestions api we need only the columns | ||
// 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, | ||
search: data.search.search, | ||
signal: abortController.signal, | ||
timeRange: data.query.timefilter.timefilter.getAbsoluteTime(), | ||
}); | ||
|
||
const context = { | ||
dataViewSpec: dataView.toSpec(false), | ||
fieldName: '', | ||
textBasedColumns: columns, | ||
query: defaultEsqlQuery, | ||
}; | ||
|
||
// get the initial attributes from the suggestions api | ||
const allSuggestions = | ||
suggestionsApi({ context, dataView, datasourceMap, visualizationMap }) ?? []; | ||
|
||
// Lens might not return suggestions for some cases, i.e. in case of errors | ||
if (!allSuggestions.length) { | ||
return; | ||
} | ||
const [firstSuggestion] = allSuggestions; | ||
return getLensAttributesFromSuggestion({ | ||
filters: [], | ||
query: defaultEsqlQuery, | ||
suggestion: { | ||
...firstSuggestion, | ||
title: '', // when creating a new panel, we don't want to use the title from the suggestion | ||
}, | ||
dataView, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.