Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[discover] esql get timestamp field #19

Open
wants to merge 1 commit into
base: data_views_async_fields_new_class
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/kbn-esql-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

export {
getESQLAdHocDataview,
getESQLAdHocDataviewLazy,
getIndexPatternFromSQLQuery,
getIndexPatternFromESQLQuery,
getLimitFromESQLQuery,
Expand Down
6 changes: 5 additions & 1 deletion packages/kbn-esql-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
*/

export { TextBasedLanguages } from './types';
export { getESQLAdHocDataview, getIndexForESQLQuery } from './utils/get_esql_adhoc_dataview';
export {
getESQLAdHocDataview,
getESQLAdHocDataviewLazy,
getIndexForESQLQuery,
} from './utils/get_esql_adhoc_dataview';
export { getInitialESQLQuery } from './utils/get_initial_esql_query';
export {
getIndexPatternFromSQLQuery,
Expand Down
23 changes: 23 additions & 0 deletions packages/kbn-esql-utils/src/utils/get_esql_adhoc_dataview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,30 @@ export async function getESQLAdHocDataview(
indexPattern: string,
dataViewsService: DataViewsPublicPluginStart
) {
const fld = await dataViewsService.getFieldsForWildcard({
pattern: indexPattern,
fields: ['@timestamp'],
});

return await dataViewsService.create({
timeFieldName: fld.length > 0 ? '@timestamp' : undefined,
title: indexPattern,
type: ESQL_TYPE,
id: await sha256(`esql-${indexPattern}`),
});
}

export async function getESQLAdHocDataviewLazy(
indexPattern: string,
dataViewsService: DataViewsPublicPluginStart
) {
const fld = await dataViewsService.getFieldsForWildcard({
pattern: indexPattern,
fields: ['@timestamp'],
});

return await dataViewsService.createDataViewLazy({
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get a DataViewLazy and whether it has an @timestamp field

timeFieldName: fld.length > 0 ? '@timestamp' : undefined,
title: indexPattern,
type: ESQL_TYPE,
id: await sha256(`esql-${indexPattern}`),
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data_views/common/data_views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from './flatten_hit';
export * from './data_view';
export * from './data_views';
export * from './data_view_lazy';
export * from './abstract_data_views';
export { DataViewPersistableStateService } from './persistable_state';
2 changes: 1 addition & 1 deletion src/plugins/data_views/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export type {
DataViewsServicePublicMethods,
TimeBasedDataView,
} from './data_views';
export { DataView, DataViewLazy } from './data_views';
export { DataView, DataViewLazy, AbstractDataView } from './data_views';
export {
DuplicateDataViewError,
DataViewSavedObjectConflictError,
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/data_views/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type {
DataViewsServicePublic,
DataViewsServicePublicDeps,
} from './data_views_service_public';
export { DataViewsApiClient, DataViewsService, DataView } from './data_views';
export { DataViewsApiClient, DataViewsService, DataView, DataViewLazy } from './data_views';
export type { DataViewListItem } from './data_views';
export { UiSettingsPublicToCommon } from './ui_settings_wrapper';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export async function onSaveSearch({

const saveModal = (
<SaveSearchObjectModal
isTimeBased={dataView?.isTimeBased() ?? false}
isTimeBased={(await dataView?.isTimeBased()) ?? false}
services={services}
title={savedSearch.title ?? ''}
showCopyOnSave={!!savedSearch.id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export function AlertsPopover({
}: AlertsPopoverProps) {
const dataView = stateContainer.internalState.getState().dataView;
const query = stateContainer.appState.getState().query;
const dateFields = dataView?.fields.getByType('date');
// const dateFields = dataView?.fields.getByType('date');
const dateFields = (await dataView?.getFields()).filter((field) => field.type === 'date');
const timeField = dataView?.timeFieldName || dateFields?.[0]?.name;

const { triggersActionsUi } = services;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const buildStateSubscribe =
appState.update({ index: nextDataView.id }, true);
return;
}
savedSearch.searchSource.setField('index', nextDataView);
savedSearch.searchSource.setField('index', await services.dataViews.toDataView(nextDataView));
dataState.reset(savedSearch);
setDataView(nextDataView);
savedSearchDataView = nextDataView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { SortOrder } from '@kbn/saved-search-plugin/public';
import { DataView } from '@kbn/data-views-plugin/common';
import { DataViewLazy } from '@kbn/data-views-plugin/common';
import {
MODIFY_COLUMNS_ON_SWITCH,
SORT_DEFAULT_ORDER_SETTING,
Expand All @@ -23,7 +23,7 @@ import { getDataViewAppState } from '../../utils/get_switch_data_view_app_state'
* Function executed when switching data view in the UI
*/
export async function changeDataView(
id: string | DataView,
id: string | DataViewLazy,
{
services,
internalState,
Expand All @@ -38,19 +38,19 @@ export async function changeDataView(
const { dataViews, uiSettings } = services;
const dataView = internalState.getState().dataView;
const state = appState.getState();
let nextDataView: DataView | null = null;
let nextDataView: DataViewLazy | null = null;
internalState.transitions.setIsDataViewLoading(true);

try {
nextDataView = typeof id === 'string' ? await dataViews.get(id, false) : id;
nextDataView = typeof id === 'string' ? await dataViews.getDataViewLazy(id) : id;
} catch (e) {
//
}

if (nextDataView && dataView) {
const nextAppState = getDataViewAppState(
dataView,
nextDataView,
await services.dataViews.toDataView(dataView),
await services.dataViews.toDataView(nextDataView),
uiSettings.get(DEFAULT_COLUMNS_SETTING, []),
state.columns || [],
(state.sort || []) as SortOrder[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { RequestAdapter } from '@kbn/inspector-plugin/common';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { AggregateQuery, Query } from '@kbn/es-query';
import type { SearchResponse } from '@elastic/elasticsearch/lib/api/types';
import { DataView } from '@kbn/data-views-plugin/common';
import { DataViewLazy } from '@kbn/data-views-plugin/common';
import { reportPerformanceMetricEvent } from '@kbn/ebt-tools';
import type { SearchResponseWarning } from '@kbn/search-response-warnings';
import type { DataTableRecord } from '@kbn/discover-utils/types';
Expand Down Expand Up @@ -163,7 +163,7 @@ export function getDataStateContainer({
getAppState: () => DiscoverAppState;
getInternalState: () => InternalState;
getSavedSearch: () => SavedSearch;
setDataView: (dataView: DataView) => void;
setDataView: (dataView: DataViewLazy) => void;
}): DiscoverDataStateContainer {
const { data, uiSettings, toastNotifications } = services;
const { timefilter } = data.query.timefilter;
Expand Down Expand Up @@ -298,7 +298,9 @@ export function getDataStateContainer({

const fetchQuery = async (resetQuery?: boolean) => {
const query = getAppState().query;
const currentDataView = getSavedSearch().searchSource.getField('index');
const savedSearchDataView = getSavedSearch().searchSource.getField('index');
const currentDataView =
savedSearchDataView && (await services.dataViews.toDataViewLazy(savedSearchDataView));

if (isTextBasedQuery(query)) {
const nextDataView = await getDataViewByTextBasedQueryLang(query, currentDataView, services);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ import {
createStateContainerReactHelpers,
ReduxLikeStateContainer,
} from '@kbn/kibana-utils-plugin/common';
import { DataView, DataViewListItem } from '@kbn/data-views-plugin/common';
import { DataViewLazy, DataViewListItem } from '@kbn/data-views-plugin/common';
import { Filter } from '@kbn/es-query';
import type { DataTableRecord } from '@kbn/discover-utils/types';

export interface InternalState {
dataView: DataView | undefined;
dataView: DataViewLazy | undefined;
isDataViewLoading: boolean;
savedDataViews: DataViewListItem[];
adHocDataViews: DataView[];
adHocDataViews: DataViewLazy[];
expandedDoc: DataTableRecord | undefined;
customFilters: Filter[];
}

export interface InternalStateTransitions {
setDataView: (state: InternalState) => (dataView: DataView) => InternalState;
setDataView: (state: InternalState) => (dataView: DataViewLazy) => InternalState;
setIsDataViewLoading: (state: InternalState) => (isLoading: boolean) => InternalState;
setSavedDataViews: (state: InternalState) => (dataView: DataViewListItem[]) => InternalState;
setAdHocDataViews: (state: InternalState) => (dataViews: DataView[]) => InternalState;
setAdHocDataViews: (state: InternalState) => (dataViews: DataViewLazy[]) => InternalState;
appendAdHocDataViews: (
state: InternalState
) => (dataViews: DataView | DataView[]) => InternalState;
) => (dataViews: DataViewLazy | DataViewLazy[]) => InternalState;
removeAdHocDataViewById: (state: InternalState) => (id: string) => InternalState;
replaceAdHocDataViewWithId: (
state: InternalState
) => (id: string, dataView: DataView) => InternalState;
) => (id: string, dataView: DataViewLazy) => InternalState;
setExpandedDoc: (
state: InternalState
) => (dataView: DataTableRecord | undefined) => InternalState;
Expand All @@ -61,7 +61,7 @@ export function getInternalStateContainer() {
customFilters: [],
},
{
setDataView: (prevState: InternalState) => (nextDataView: DataView) => ({
setDataView: (prevState: InternalState) => (nextDataView: DataViewLazy) => ({
...prevState,
dataView: nextDataView,
}),
Expand All @@ -73,17 +73,17 @@ export function getInternalStateContainer() {
...prevState,
savedDataViews: nextDataViewList,
}),
setAdHocDataViews: (prevState: InternalState) => (newAdHocDataViewList: DataView[]) => ({
setAdHocDataViews: (prevState: InternalState) => (newAdHocDataViewList: DataViewLazy[]) => ({
...prevState,
adHocDataViews: newAdHocDataViewList,
}),
appendAdHocDataViews:
(prevState: InternalState) => (dataViewsAdHoc: DataView | DataView[]) => {
(prevState: InternalState) => (dataViewsAdHoc: DataViewLazy | DataViewLazy[]) => {
// check for already existing data views
const concatList = (
Array.isArray(dataViewsAdHoc) ? dataViewsAdHoc : [dataViewsAdHoc]
).filter((dataView) => {
return !prevState.adHocDataViews.find((el: DataView) => el.id === dataView.id);
return !prevState.adHocDataViews.find((el: DataViewLazy) => el.id === dataView.id);
});
if (!concatList.length) {
return prevState;
Expand All @@ -98,7 +98,7 @@ export function getInternalStateContainer() {
adHocDataViews: prevState.adHocDataViews.filter((dataView) => dataView.id !== id),
}),
replaceAdHocDataViewWithId:
(prevState: InternalState) => (prevId: string, newDataView: DataView) => ({
(prevState: InternalState) => (prevId: string, newDataView: DataViewLazy) => ({
...prevState,
adHocDataViews: prevState.adHocDataViews.map((dataView) =>
dataView.id === prevId ? newDataView : dataView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { BehaviorSubject } from 'rxjs';
import { cloneDeep } from 'lodash';
import { COMPARE_ALL_OPTIONS, FilterCompareOptions } from '@kbn/es-query';
import type { SearchSourceFields } from '@kbn/data-plugin/common';
import type { DataView } from '@kbn/data-views-plugin/common';
import type { DataViewLazy, DataView } from '@kbn/data-views-plugin/common';
import { SavedObjectSaveOpts } from '@kbn/saved-objects-plugin/public';
import { isEqual, isFunction } from 'lodash';
import { restoreStateFromSavedSearch } from '../../../services/saved_searches/restore_from_saved_search';
Expand All @@ -32,7 +32,7 @@ export interface UpdateParams {
/**
* The next data view to be used
*/
nextDataView?: DataView | undefined;
nextDataView?: DataViewLazy | undefined;
/**
* The next AppState that should be used for updating the saved search
*/
Expand Down Expand Up @@ -110,7 +110,7 @@ export interface DiscoverSavedSearchContainer {
* Updates the current state of the saved search
* @param params
*/
update: (params: UpdateParams) => SavedSearch;
update: (params: UpdateParams) => Promise<SavedSearch>;
/**
* Passes filter manager filters to saved search filters
* @param params
Expand Down Expand Up @@ -195,17 +195,19 @@ export function getSavedSearchContainer({
return nextSavedSearch;
};

const update = ({ nextDataView, nextState, useFilterAndQueryServices }: UpdateParams) => {
const update = async ({ nextDataView, nextState, useFilterAndQueryServices }: UpdateParams) => {
addLog('[savedSearch] update', { nextDataView, nextState });

const previousSavedSearch = getState();
const dataView = nextDataView
? nextDataView
: previousSavedSearch.searchSource.getField('index')!;
: await services.dataViews.toDataViewLazy(
previousSavedSearch.searchSource.getField('index')!
);

const nextSavedSearch = updateSavedSearch({
savedSearch: { ...previousSavedSearch },
dataView,
dataView: await services.dataViews.toDataView(dataView),
state: nextState || {},
globalStateContainer,
services,
Expand Down
Loading
Loading