Skip to content

Commit

Permalink
[ES|QL] Moves the @timestamp assignment to the ESQL util (#186158)
Browse files Browse the repository at this point in the history
## Summary

This PR just moves the @timestamp assignment logic from the various
consumers to the helper function. It is much cleaner that way and I want
to add support of the named parameters so I want to have a central
function to accomplish this
#180805
  • Loading branch information
stratoula authored Jun 17, 2024
1 parent 41dc417 commit d243e4f
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 30 deletions.
10 changes: 9 additions & 1 deletion packages/kbn-esql-utils/src/utils/get_esql_adhoc_dataview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ export async function getESQLAdHocDataview(
indexPattern: string,
dataViewsService: DataViewsPublicPluginStart
) {
return await dataViewsService.create({
const dataView = await dataViewsService.create({
title: indexPattern,
type: ESQL_TYPE,
id: await sha256(`esql-${indexPattern}`),
});

// If the indexPattern is empty string means that the user used either the ROW or META FUNCTIONS / SHOW INFO commands
// we don't want to add the @timestamp field in this case https://github.com/elastic/kibana/issues/163417
if (indexPattern && dataView?.fields?.getByName?.('@timestamp')?.type === 'date') {
dataView.timeFieldName = '@timestamp';
}

return dataView;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ export async function getEsqlDataView(
currentDataView?.isPersisted() ||
indexPatternFromQuery !== currentDataView?.getIndexPattern()
) {
const dataViewObj = await getESQLAdHocDataview(indexPatternFromQuery, services.dataViews);

// If the indexPatternFromQuery is empty string means that the user used either the ROW or SHOW META / SHOW INFO commands
// we don't want to add the @timestamp field in this case https://github.com/elastic/kibana/issues/163417
if (indexPatternFromQuery && dataViewObj.fields.getByName('@timestamp')?.type === 'date') {
dataViewObj.timeFieldName = '@timestamp';
}
return dataViewObj;
return await getESQLAdHocDataview(indexPatternFromQuery, services.dataViews);
}
return currentDataView;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import type { DataView, DataViewsContract } from '@kbn/data-views-plugin/public';
import { getESQLAdHocDataview } from '@kbn/esql-utils';

/**
* Get a saved data view that matches the index pattern (as close as possible)
Expand Down Expand Up @@ -34,14 +35,7 @@ export async function getOrCreateDataViewByIndexPattern(
indexPatternFromQuery &&
(currentDataView?.isPersisted() || indexPatternFromQuery !== currentDataView?.getIndexPattern())
) {
const dataViewObj = await dataViews.create({
title: indexPatternFromQuery,
});

if (dataViewObj.fields.getByName('@timestamp')?.type === 'date') {
dataViewObj.timeFieldName = '@timestamp';
}
return dataViewObj;
return await getESQLAdHocDataview(indexPatternFromQuery, dataViews);
}
return currentDataView;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ export const getSuggestions = async (
? await deps.dataViews.create(dataViewSpec)
: await getESQLAdHocDataview(indexPattern, deps.dataViews);

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.search,
Expand Down
3 changes: 0 additions & 3 deletions x-pack/plugins/lens/public/datasources/text_based/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ export async function getStateFromAggregateQuery(
const dataView = await getESQLAdHocDataview(indexPattern, dataViews);

if (dataView && dataView.id) {
if (dataView?.fields?.getByName('@timestamp')?.type === 'date') {
dataView.timeFieldName = '@timestamp';
}
dataViewId = dataView?.id;
indexPatternRefs = [
...indexPatternRefs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,7 @@ export function VisualizeESQL({
}, [lens]);

const dataViewAsync = useAsync(() => {
return getESQLAdHocDataview(indexPattern, dataViews).then((dataView) => {
if (dataView.fields.getByName('@timestamp')?.type === 'date') {
dataView.timeFieldName = '@timestamp';
}
return dataView;
});
return getESQLAdHocDataview(indexPattern, dataViews);
}, [indexPattern, dataViews]);

const chatFlyoutSecondSlotHandler = useContext(ObservabilityAIAssistantMultipaneFlyoutContext);
Expand Down

0 comments on commit d243e4f

Please sign in to comment.