From cefd57fd94ddc916603128ae2cf7cf18c5962b38 Mon Sep 17 00:00:00 2001 From: Joshua Li Date: Wed, 4 Oct 2023 16:45:01 -0700 Subject: [PATCH] [Backport 2.x][Feature] Support SQL direct query in Observability (#988) (#1072) * [Feature] Support SQL direct query in Observability (#988) * remove unused files Signed-off-by: Eric Wei * missing snapshots Signed-off-by: Eric Wei * remove unused files Signed-off-by: Eric Wei * missing snapshots Signed-off-by: Eric Wei * create generic use polling hook Signed-off-by: Eric * add unit tests Signed-off-by: Eric * remove logging Signed-off-by: Eric * datasource registeration for observability Signed-off-by: Eric * Manage datasources (#967) * fix name change bug and modify test to test behavior Signed-off-by: Derek Ho * get rid of lint Signed-off-by: Derek Ho * test for flyout Signed-off-by: Derek Ho * flyout to medium size Signed-off-by: Derek Ho * make accelerate extensible Signed-off-by: Derek Ho * get datasources and hook up to pplservice Signed-off-by: Derek Ho * get flint working Signed-off-by: Derek Ho * add datasource page with steps and buttons on bottom bar Signed-off-by: Derek Ho * datasources as a new plugin and mostly working Signed-off-by: Derek Ho * hook up manage to show datasources call Signed-off-by: Derek Ho * update two tables with descriptions Signed-off-by: Derek Ho * make some updates to the page Signed-off-by: Derek Ho * cleanup unused files for data connections Signed-off-by: Derek Ho * cleanup and add overview panel columns Signed-off-by: Derek Ho * render tabs Signed-off-by: Derek Ho * add unit tests Signed-off-by: Derek Ho * update data test subj and snapshot Signed-off-by: Derek Ho * Add datasources to management overview Signed-off-by: Derek Ho * remove spark logo and update snapshot Signed-off-by: Derek Ho * refactor routes out Signed-off-by: Derek Ho * separate out the roles Signed-off-by: Derek Ho * bump version back to 3.0 Signed-off-by: Derek Ho --------- Signed-off-by: Derek Ho * Add acceleration management UI (#989) * add acceleration management UI skeleton Signed-off-by: Shenoy Pratik * Create new documentation link for acc Signed-off-by: Shenoy Pratik * fix typos and minor bugs Signed-off-by: Shenoy Pratik * update snapshot Signed-off-by: Shenoy Pratik * update window location to hash Signed-off-by: Shenoy Pratik * remove unused headers Signed-off-by: Shenoy Pratik --------- Signed-off-by: Shenoy Pratik * dummy data redirection for datasource selector and language selector Signed-off-by: Eric * flint datasource registration Signed-off-by: Eric * tempararily passing down setup deps for datasource Signed-off-by: Eric * add node server endpoints for direct query and job status Signed-off-by: Eric * add server side endpoints for direct query Signed-off-by: Eric * add s3 datasource class Signed-off-by: Eric * s3 datasource registeration Signed-off-by: Eric * changes to use new endpoints Signed-off-by: Eric * remove unused files Signed-off-by: Eric Wei * missing snapshots Signed-off-by: Eric Wei * initial direct query support in explorer Signed-off-by: Eric * add datasource pluggable Signed-off-by: Eric * remove unused files Signed-off-by: Eric Wei * missing snapshots Signed-off-by: Eric Wei * remove unused dependency as well as passing down pluggable Signed-off-by: Eric * changes for adopting new endpoints Signed-off-by: Eric * add initial commits for context swithing Signed-off-by: Eric * add query running page Signed-off-by: Eric * relayout as an effort of match look and feel Signed-off-by: Eric * add changes for index pattern to work with context switch Signed-off-by: Eric * remove search section from explorer home Signed-off-by: Eric * visualization page for direct query Signed-off-by: Eric * add direct query visualization page Signed-off-by: Eric * merge main Signed-off-by: Eric * add discover redirection Signed-off-by: Eric * merge main and resolve conflicts Signed-off-by: Eric * s3 datasource and layout changes Signed-off-by: Eric * use newly changed handler name Signed-off-by: Eric * fixes for sidebar and datasource Signed-off-by: Eric * add fix for redirection issue Signed-off-by: Eric * console/comments cleanup Signed-off-by: Eric * remove outdated polling tests Signed-off-by: Eric * delete few outdated tests to resolve testing issues and skipped search tests for now Signed-off-by: Eric * adopt empty prompt Signed-off-by: Eric * remove unused oui/eui component Signed-off-by: Eric * adopt new endpoint cchanges Signed-off-by: Eric * fix data grid issue Signed-off-by: Eric * disable autosuggest for sql Signed-off-by: Eric * remove manual link for SQL Signed-off-by: Eric * query cancelling Signed-off-by: Eric --------- Signed-off-by: Eric Wei Signed-off-by: Eric Signed-off-by: Derek Ho Signed-off-by: Shenoy Pratik Co-authored-by: Derek Ho Co-authored-by: Shenoy Pratik (cherry picked from commit e21dcf2dab235404b1af0e4abb6792c0be8212ae) * remove datasource routers Signed-off-by: Joshua Li --------- Signed-off-by: Joshua Li Co-authored-by: Eric Wei --- common/constants/data_connections.ts | 20 + common/constants/explorer.ts | 1 + common/constants/shared.ts | 7 + public/components/app.tsx | 2 + .../components/common/search/autocomplete.tsx | 4 +- .../components/common/search/search.test.tsx | 4 +- public/components/common/search/search.tsx | 104 ++++- .../components/common/search/sql_search.tsx | 348 ++++++++++++++++ .../components/__tests__/testing_constants.ts | 75 ++++ .../datasources/datasources_selection.tsx | 129 ++++++ .../explorer/direct_query_running.tsx | 36 ++ .../event_analytics/explorer/explorer.scss | 11 +- .../event_analytics/explorer/explorer.tsx | 382 +++++++++--------- .../event_analytics/explorer/log_explorer.tsx | 31 +- .../explorer/sidebar/sidebar.tsx | 374 ++++++++++------- .../visualizations/direct_query_vis.tsx | 30 ++ .../explorer/visualizations/index.tsx | 27 +- .../components/event_analytics/home/home.tsx | 29 +- .../hooks/use_fetch_direct_events.ts | 228 +++++++++++ .../event_analytics/hooks/use_fetch_events.ts | 46 ++- public/components/event_analytics/index.tsx | 4 +- .../redux/slices/count_distribution_slice.ts | 5 +- .../redux/slices/query_slice.ts | 5 + .../redux/slices/search_meta_data_slice.ts | 47 +++ public/components/hooks/index.ts | 6 + .../hooks/use_direct_query_search.ts | 4 + public/components/hooks/use_polling.ts | 56 +++ public/components/index.tsx | 6 +- .../datasource_pluggable.ts | 29 ++ .../framework/datasource_pluggables/types.ts | 22 + public/framework/datasources/s3_datasource.ts | 30 ++ public/framework/redux/reducers/index.ts | 2 + public/plugin.ts | 87 +++- .../data_fetchers/sql/sql_data_fetcher.ts | 45 +++ public/services/requests/sql.ts | 41 ++ public/types.ts | 4 +- .../opensearch_observability_plugin.ts | 23 +- server/routes/sql.ts | 38 ++ 38 files changed, 1919 insertions(+), 423 deletions(-) create mode 100644 common/constants/data_connections.ts create mode 100644 public/components/common/search/sql_search.tsx create mode 100644 public/components/datasources/components/__tests__/testing_constants.ts create mode 100644 public/components/event_analytics/explorer/datasources/datasources_selection.tsx create mode 100644 public/components/event_analytics/explorer/direct_query_running.tsx create mode 100644 public/components/event_analytics/explorer/visualizations/direct_query_vis.tsx create mode 100644 public/components/event_analytics/hooks/use_fetch_direct_events.ts create mode 100644 public/components/event_analytics/redux/slices/search_meta_data_slice.ts create mode 100644 public/components/hooks/index.ts create mode 100644 public/components/hooks/use_direct_query_search.ts create mode 100644 public/components/hooks/use_polling.ts create mode 100644 public/framework/datasource_pluggables/datasource_pluggable.ts create mode 100644 public/framework/datasource_pluggables/types.ts create mode 100644 public/framework/datasources/s3_datasource.ts create mode 100644 public/services/data_fetchers/sql/sql_data_fetcher.ts create mode 100644 public/services/requests/sql.ts create mode 100644 server/routes/sql.ts diff --git a/common/constants/data_connections.ts b/common/constants/data_connections.ts new file mode 100644 index 0000000000..41520976e3 --- /dev/null +++ b/common/constants/data_connections.ts @@ -0,0 +1,20 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { DatasourceType } from '../../common/types/data_connections'; + +export const OPENSEARCH_DOCUMENTATION_URL = 'https://opensearch.org/docs/latest/data-sources/index'; + +export const OPENSEARCH_ACC_DOCUMENTATION_URL = + 'https://opensearch.org/docs/latest/data-acceleration/index'; +export const QUERY_RESTRICTED = 'query-restricted'; +export const QUERY_ALL = 'query-all'; + +export const DatasourceTypeToDisplayName: { [key in DatasourceType]: string } = { + PROMETHEUS: 'Prometheus', + S3GLUE: 'S3', +}; + +export type AuthMethod = 'noauth' | 'basicauth' | 'awssigv4'; diff --git a/common/constants/explorer.ts b/common/constants/explorer.ts index f586f5fca9..6b01e8c340 100644 --- a/common/constants/explorer.ts +++ b/common/constants/explorer.ts @@ -88,6 +88,7 @@ export const REDUX_EXPL_SLICE_QUERY_TABS = 'queryTabs'; export const REDUX_EXPL_SLICE_VISUALIZATION = 'explorerVisualization'; export const REDUX_EXPL_SLICE_COUNT_DISTRIBUTION = 'countDistributionVisualization'; export const REDUX_EXPL_SLICE_PATTERNS = 'patterns'; +export const REDUX_EXPL_SLICE_SEARCH_META_DATA = 'searchMetaData'; export const PLOTLY_GAUGE_COLUMN_NUMBER = 4; export const APP_ANALYTICS_TAB_ID_REGEX = /application-analytics-tab.+/; export const DEFAULT_AVAILABILITY_QUERY = 'stats count() by span( timestamp, 1h )'; diff --git a/common/constants/shared.ts b/common/constants/shared.ts index 2d10c08e2a..c8e47aa871 100644 --- a/common/constants/shared.ts +++ b/common/constants/shared.ts @@ -14,6 +14,8 @@ export const DSL_CAT = '/cat.indices'; export const DSL_MAPPING = '/indices.getFieldMapping'; export const OBSERVABILITY_BASE = '/api/observability'; export const INTEGRATIONS_BASE = '/api/integrations'; +export const JOBS_BASE = '/query/jobs'; +export const DATACONNECTIONS_BASE = '/api/dataconnections'; export const EVENT_ANALYTICS = '/event_analytics'; export const SAVED_OBJECTS = '/saved_objects'; export const SAVED_QUERY = '/query'; @@ -23,6 +25,9 @@ export const SAVED_VISUALIZATION = '/vis'; export const PPL_ENDPOINT = '/_plugins/_ppl'; export const SQL_ENDPOINT = '/_plugins/_sql'; export const DSL_ENDPOINT = '/_plugins/_dsl'; +export const DATACONNECTIONS_ENDPOINT = '/_plugins/_query/_datasources'; +export const JOBS_ENDPOINT_BASE = '/_plugins/_async_query'; +export const JOB_RESULT_ENDPOINT = '/result'; export const observabilityID = 'observability-logs'; export const observabilityTitle = 'Observability'; @@ -227,3 +232,5 @@ export const VISUALIZATION_ERROR = { NO_DATA: 'No data found.', INVALID_DATA: 'Invalid visualization data', }; + +export const S3_DATASOURCE_TYPE = 'S3_DATASOURCE'; diff --git a/public/components/app.tsx b/public/components/app.tsx index ac3fa5772c..03d9b8f4e3 100644 --- a/public/components/app.tsx +++ b/public/components/app.tsx @@ -55,6 +55,7 @@ export const App = ({ timestampUtils, queryManager, startPage, + dataSourcePluggables, }: ObservabilityAppDeps) => { const { chrome, http, notifications, savedObjects: coreSavedObjects } = CoreStartProp; const parentBreadcrumb = { @@ -85,6 +86,7 @@ export const App = ({ parentBreadcrumb={parentBreadcrumb} parentBreadcrumbs={[parentBreadcrumb]} setBreadcrumbs={chrome.setBreadcrumbs} + dataSourcePluggables={dataSourcePluggables} /> diff --git a/public/components/common/search/autocomplete.tsx b/public/components/common/search/autocomplete.tsx index a8c4007a74..4b9ab22cb9 100644 --- a/public/components/common/search/autocomplete.tsx +++ b/public/components/common/search/autocomplete.tsx @@ -28,6 +28,7 @@ interface AutocompleteProps extends IQueryBarProps { placeholder?: string; possibleCommands?: Array<{ label: string }>; append?: any; + isSuggestionDisabled?: boolean; } export const Autocomplete = (props: AutocompleteProps) => { @@ -45,6 +46,7 @@ export const Autocomplete = (props: AutocompleteProps) => { placeholder = 'Enter PPL query', possibleCommands, append, + isSuggestionDisabled = false, } = props; const [autocompleteState, setAutocompleteState] = useState>({ @@ -143,7 +145,7 @@ export const Autocomplete = (props: AutocompleteProps) => { {...(panelsFilter && { append, fullWidth: true })} disabled={isDisabled} /> - {autocompleteState.isOpen && ( + {autocompleteState.isOpen && !isSuggestionDisabled && (
{ +describe.skip('Search bar', () => { it('handles query change', () => { const query = 'rawQuery'; const tempQuery = 'rawQuery'; @@ -62,7 +62,7 @@ describe('Search bar', () => { popoverItems={popoverItems} isLiveTailOn={isLiveTailOn} countDistribution={countDistribution} - curVisId={'line'} + curVisId={'line'} spanValue={false} setSubType={'metric'} setMetricMeasure={'hours (h)'} diff --git a/public/components/common/search/search.tsx b/public/components/common/search/search.tsx index 45fdb6c3f1..6250973ac0 100644 --- a/public/components/common/search/search.tsx +++ b/public/components/common/search/search.tsx @@ -5,8 +5,9 @@ import './search.scss'; -import React, { useState } from 'react'; -import { isEqual } from 'lodash'; +import React, { useState, useEffect } from 'react'; +import { useDispatch, useSelector } from 'react-redux'; +import { isEqual, lowerCase } from 'lodash'; import { EuiFlexGroup, EuiButton, @@ -18,6 +19,7 @@ import { EuiContextMenuPanel, EuiToolTip, EuiCallOut, + EuiComboBox, } from '@elastic/eui'; import { DatePicker } from './date_picker'; import '@algolia/autocomplete-theme-classic'; @@ -25,9 +27,18 @@ import { Autocomplete } from './autocomplete'; import { SavePanel } from '../../event_analytics/explorer/save_panel'; import { PPLReferenceFlyout } from '../helpers'; import { uiSettingsService } from '../../../../common/utils'; -import { APP_ANALYTICS_TAB_ID_REGEX } from '../../../../common/constants/explorer'; +import { APP_ANALYTICS_TAB_ID_REGEX, RAW_QUERY } from '../../../../common/constants/explorer'; import { LiveTailButton, StopLiveButton } from '../live_tail/live_tail_button'; import { PPL_SPAN_REGEX } from '../../../../common/constants/shared'; +import { coreRefs } from '../../../framework/core_refs'; +import { useFetchEvents } from '../../../components/event_analytics/hooks'; +import { SQLService } from '../../../services/requests/sql'; +import { + selectSearchMetaData, + update as updateSearchMetaData, +} from '../../event_analytics/redux/slices/search_meta_data_slice'; +import { usePolling } from '../../../components/hooks/use_polling'; +import { changeQuery } from '../../../components/event_analytics/redux/slices/query_slice'; export interface IQueryBarProps { query: string; tempQuery: string; @@ -52,7 +63,6 @@ export const Search = (props: any) => { query, tempQuery, handleQueryChange, - handleQuerySearch, handleTimePickerChange, dslService, startTime, @@ -85,11 +95,34 @@ export const Search = (props: any) => { liveTailName, curVisId, setSubType, + setIsQueryRunning, } = props; + const explorerSearchMetadata = useSelector(selectSearchMetaData)[tabId]; + const dispatch = useDispatch(); const appLogEvents = tabId.match(APP_ANALYTICS_TAB_ID_REGEX); const [isSavePanelOpen, setIsSavePanelOpen] = useState(false); const [isFlyoutVisible, setIsFlyoutVisible] = useState(false); + const [queryLang, setQueryLang] = useState([]); + const [jobId, setJobId] = useState(''); + const sqlService = new SQLService(coreRefs.http); + const { application } = coreRefs; + + const { + data: pollingResult, + loading: pollingLoading, + error: pollingError, + startPolling, + stopPolling, + } = usePolling((params) => { + return sqlService.fetchWithJobId(params); + }, 5000); + + const requestParams = { tabId }; + const { getLiveTail, getEvents, getAvailableFields, dispatchOnGettingHis } = useFetchEvents({ + pplService: new SQLService(coreRefs.http), + requestParams, + }); const closeFlyout = () => { setIsFlyoutVisible(false); @@ -129,6 +162,50 @@ export const Search = (props: any) => { /> ); + const handleQueryLanguageChange = (lang) => { + if (lang[0].label === 'DQL') { + return application.navigateToUrl( + `../app/data-explorer/discover#?_a=(discover:(columns:!(_source),isDirty:!f,sort:!()),metadata:(indexPattern:'${explorerSearchMetadata.datasources[0].value}',view:discover))&_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&_q=(filters:!(),query:(language:kuery,query:''))` + ); + } + dispatch( + updateSearchMetaData({ + tabId, + data: { lang: lang[0].label }, + }) + ); + setQueryLang(lang); + }; + + const onQuerySearch = (lang) => { + handleTimeRangePickerRefresh(); + }; + + useEffect(() => { + if (pollingResult && (pollingResult.status === 'SUCCESS' || pollingResult.datarows)) { + // update page with data + dispatchOnGettingHis(pollingResult, ''); + // stop polling + stopPolling(); + setIsQueryRunning(false); + } + }, [pollingResult, pollingError]); + + useEffect(() => { + if (explorerSearchMetadata.datasources?.[0]?.type === 'DEFAULT_INDEX_PATTERNS') { + const queryWithSelectedSource = `source = ${explorerSearchMetadata.datasources[0].label}`; + handleQueryChange(queryWithSelectedSource); + dispatch( + changeQuery({ + tabId, + query: { + [RAW_QUERY]: queryWithSelectedSource, + }, + }) + ); + } + }, [explorerSearchMetadata.datasources]); + return (
@@ -141,14 +218,25 @@ export const Search = (props: any) => { )} - + + + + { + onQuerySearch(queryLang); + }} dslService={dslService} getSuggestions={getSuggestions} onItemSelect={onItemSelect} @@ -178,7 +266,9 @@ export const Search = (props: any) => { liveStreamChecked={props.liveStreamChecked} onLiveStreamChange={props.onLiveStreamChange} handleTimePickerChange={(timeRange: string[]) => handleTimePickerChange(timeRange)} - handleTimeRangePickerRefresh={handleTimeRangePickerRefresh} + handleTimeRangePickerRefresh={() => { + onQuerySearch(queryLang); + }} /> )} diff --git a/public/components/common/search/sql_search.tsx b/public/components/common/search/sql_search.tsx new file mode 100644 index 0000000000..14e57def3b --- /dev/null +++ b/public/components/common/search/sql_search.tsx @@ -0,0 +1,348 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import './search.scss'; + +import React, { useState, useEffect } from 'react'; +import { useDispatch, useSelector } from 'react-redux'; +import { isEqual, lowerCase } from 'lodash'; +import { + EuiFlexGroup, + EuiButton, + EuiFlexItem, + EuiPopover, + EuiButtonEmpty, + EuiPopoverFooter, + EuiBadge, + EuiToolTip, + EuiComboBox, + EuiTextArea, +} from '@elastic/eui'; +import { DatePicker } from './date_picker'; +import '@algolia/autocomplete-theme-classic'; +import { Autocomplete } from './autocomplete'; +import { SavePanel } from '../../event_analytics/explorer/save_panel'; +import { PPLReferenceFlyout } from '../helpers'; +import { uiSettingsService } from '../../../../common/utils'; +import { APP_ANALYTICS_TAB_ID_REGEX, RAW_QUERY } from '../../../../common/constants/explorer'; +import { PPL_SPAN_REGEX } from '../../../../common/constants/shared'; +import { coreRefs } from '../../../framework/core_refs'; +import { useFetchEvents } from '../../../components/event_analytics/hooks'; +import { SQLService } from '../../../services/requests/sql'; +import { usePolling } from '../../../components/hooks/use_polling'; +import { + selectSearchMetaData, + update as updateSearchMetaData, +} from '../../event_analytics/redux/slices/search_meta_data_slice'; +export interface IQueryBarProps { + query: string; + tempQuery: string; + handleQueryChange: (query: string) => void; + handleQuerySearch: () => void; + dslService: any; +} + +export interface IDatePickerProps { + startTime: string; + endTime: string; + setStartTime: () => void; + setEndTime: () => void; + setTimeRange: () => void; + setIsOutputStale: () => void; + handleTimePickerChange: (timeRange: string[]) => any; + handleTimeRangePickerRefresh: () => any; +} + +export const DirectSearch = (props: any) => { + const { + query, + tempQuery, + handleQueryChange, + handleTimePickerChange, + dslService, + startTime, + endTime, + setStartTime, + setEndTime, + setIsOutputStale, + selectedPanelName, + selectedCustomPanelOptions, + setSelectedPanelName, + setSelectedCustomPanelOptions, + handleSavingObject, + isPanelTextFieldInvalid, + savedObjects, + showSavePanelOptionsList, + showSaveButton = true, + handleTimeRangePickerRefresh, + isLiveTailPopoverOpen, + closeLiveTailPopover, + popoverItems, + isLiveTailOn, + selectedSubTabId, + searchBarConfigs = {}, + getSuggestions, + onItemSelect, + tabId = '', + baseQuery = '', + stopLive, + setIsLiveTailPopoverOpen, + liveTailName, + curVisId, + setSubType, + setIsQueryRunning, + } = props; + + const explorerSearchMetadata = useSelector(selectSearchMetaData)[tabId]; + const dispatch = useDispatch(); + const appLogEvents = tabId.match(APP_ANALYTICS_TAB_ID_REGEX); + const [isSavePanelOpen, setIsSavePanelOpen] = useState(false); + const [isFlyoutVisible, setIsFlyoutVisible] = useState(false); + const [queryLang, setQueryLang] = useState([]); + const [jobId, setJobId] = useState(''); + const sqlService = new SQLService(coreRefs.http); + const { application } = coreRefs; + + const { + data: pollingResult, + loading: pollingLoading, + error: pollingError, + startPolling, + stopPolling, + } = usePolling((params) => { + return sqlService.fetchWithJobId(params); + }, 5000); + + const requestParams = { tabId }; + const { getLiveTail, getEvents, getAvailableFields, dispatchOnGettingHis } = useFetchEvents({ + pplService: new SQLService(coreRefs.http), + requestParams, + }); + + const closeFlyout = () => { + setIsFlyoutVisible(false); + }; + + const showFlyout = () => { + setIsFlyoutVisible(true); + }; + + let flyout; + if (isFlyoutVisible) { + flyout = ; + } + + const Savebutton = ( + { + setIsSavePanelOpen((staleState) => { + return !staleState; + }); + }} + data-test-subj="eventExplorer__saveManagementPopover" + iconType="arrowDown" + > + Save + + ); + + const handleQueryLanguageChange = (lang) => { + if (lang[0].label === 'DQL') { + return application.navigateToUrl( + `../app/data-explorer/discover#?_a=(discover:(columns:!(_source),isDirty:!f,sort:!()),metadata:(indexPattern:'${explorerSearchMetadata.datasources[0].value}',view:discover))&_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-15m,to:now))&_q=(filters:!(),query:(language:kuery,query:''))` + ); + } + dispatch( + updateSearchMetaData({ + tabId, + data: { lang: lang[0].label }, + }) + ); + setQueryLang(lang); + }; + + const onQuerySearch = (lang) => { + setIsQueryRunning(true); + dispatch( + updateSearchMetaData({ + tabId, + data: { + isPolling: true, + }, + }) + ); + sqlService + .fetch({ + lang: lowerCase(lang[0].label), + query: tempQuery || query, + datasource: explorerSearchMetadata.datasources[0].name, + }) + .then((result) => { + if (result.queryId) { + setJobId(result.queryId); + startPolling({ + queryId: result.queryId, + }); + } else { + console.log('no query id found in response'); + } + }) + .catch((e) => { + setIsQueryRunning(false); + console.error(e); + }) + .finally(() => {}); + }; + + useEffect(() => { + // cancel direct query + if (pollingResult && (pollingResult.status === 'SUCCESS' || pollingResult.datarows)) { + // stop polling + stopPolling(); + setIsQueryRunning(false); + dispatch( + updateSearchMetaData({ + tabId, + data: { + isPolling: false, + }, + }) + ); + // update page with data + dispatchOnGettingHis(pollingResult, ''); + } + }, [pollingResult, pollingError]); + + useEffect(() => { + if (explorerSearchMetadata.isPolling === false) { + stopPolling(); + setIsQueryRunning(false); + } + }, [explorerSearchMetadata.isPolling]); + + return ( +
+ + {appLogEvents && ( + + + + Base Query + + + + )} + + + + + { + onQuerySearch(queryLang); + }} + dslService={dslService} + getSuggestions={getSuggestions} + onItemSelect={onItemSelect} + tabId={tabId} + isSuggestionDisabled={queryLang[0]?.label === 'SQL'} + /> + {queryLang[0]?.label && ( + showFlyout()} + onClickAriaLabel={'pplLinkShowFlyout'} + > + PPL + + )} + + + + { + onQuerySearch(queryLang); + }} + fill + > + Search + + + + {showSaveButton && searchBarConfigs[selectedSubTabId]?.showSaveButton && ( + <> + + setIsSavePanelOpen(false)} + > + + + + + setIsSavePanelOpen(false)} + data-test-subj="eventExplorer__querySaveCancel" + > + Cancel + + + + { + handleSavingObject(); + setIsSavePanelOpen(false); + }} + data-test-subj="eventExplorer__querySaveConfirm" + > + Save + + + + + + + + )} + + {flyout} +
+ ); +}; diff --git a/public/components/datasources/components/__tests__/testing_constants.ts b/public/components/datasources/components/__tests__/testing_constants.ts new file mode 100644 index 0000000000..5cad339575 --- /dev/null +++ b/public/components/datasources/components/__tests__/testing_constants.ts @@ -0,0 +1,75 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export const showDatasourceData = [ + { + name: 'my_spark3', + connector: 'SPARK', + allowedRoles: [], + properties: { + 'spark.connector': 'emr', + 'spark.datasource.flint.host': '0.0.0.0', + 'spark.datasource.flint.integration': + 'https://aws.oss.sonatype.org/content/repositories/snapshots/org/opensearch/opensearch-spark-standalone_2.12/0.1.0-SNAPSHOT/opensearch-spark-standalone_2.12-0.1.0-20230731.182705-3.jar', + 'spark.datasource.flint.port': '9200', + 'spark.datasource.flint.scheme': 'http', + 'emr.cluster': 'j-3UNQLT1MPBGLG', + }, + }, + { + name: 'my_spark4', + connector: 'SPARK', + allowedRoles: [], + properties: { + 'spark.connector': 'emr', + 'spark.datasource.flint.host': '15.248.1.68', + 'spark.datasource.flint.integration': + 'https://aws.oss.sonatype.org/content/repositories/snapshots/org/opensearch/opensearch-spark-standalone_2.12/0.1.0-SNAPSHOT/opensearch-spark-standalone_2.12-0.1.0-20230731.182705-3.jar', + 'spark.datasource.flint.port': '9200', + 'spark.datasource.flint.scheme': 'http', + 'emr.cluster': 'j-3UNQLT1MPBGLG', + }, + }, + { + name: 'my_spark', + connector: 'SPARK', + allowedRoles: [], + properties: { + 'spark.connector': 'emr', + 'spark.datasource.flint.host': '0.0.0.0', + 'spark.datasource.flint.port': '9200', + 'spark.datasource.flint.scheme': 'http', + 'spark.datasource.flint.region': 'xxx', + 'emr.cluster': 'xxx', + }, + }, + { + name: 'my_spark2', + connector: 'SPARK', + allowedRoles: [], + properties: { + 'spark.connector': 'emr', + 'spark.datasource.flint.host': '0.0.0.0', + 'spark.datasource.flint.port': '9200', + 'spark.datasource.flint.scheme': 'http', + 'emr.cluster': 'j-3UNQLT1MPBGLG', + }, + }, +]; + +export const describeDatasource = { + name: 'my_spark3', + connector: 'SPARK', + allowedRoles: [], + properties: { + 'spark.connector': 'emr', + 'spark.datasource.flint.host': '0.0.0.0', + 'spark.datasource.flint.integration': + 'https://aws.oss.sonatype.org/content/repositories/snapshots/org/opensearch/opensearch-spark-standalone_2.12/0.1.0-SNAPSHOT/opensearch-spark-standalone_2.12-0.1.0-20230731.182705-3.jar', + 'spark.datasource.flint.port': '9200', + 'spark.datasource.flint.scheme': 'http', + 'emr.cluster': 'j-3UNQLT1MPBGLG', + }, +}; diff --git a/public/components/event_analytics/explorer/datasources/datasources_selection.tsx b/public/components/event_analytics/explorer/datasources/datasources_selection.tsx new file mode 100644 index 0000000000..86b0fcf1a8 --- /dev/null +++ b/public/components/event_analytics/explorer/datasources/datasources_selection.tsx @@ -0,0 +1,129 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ +import React, { useCallback, useEffect, useState, useContext } from 'react'; +import { batch, useDispatch, useSelector } from 'react-redux'; +import { DataSourceSelectable } from '../../../../../../../src/plugins/data/public'; +import { + selectSearchMetaData, + update as updateSearchMetaData, +} from '../../../event_analytics/redux/slices/search_meta_data_slice'; +import { coreRefs } from '../../../../framework/core_refs'; +import { reset as resetFields } from '../../redux/slices/field_slice'; +import { reset as resetPatterns } from '../../redux/slices/patterns_slice'; +import { reset as resetQueryResults } from '../../redux/slices/query_result_slice'; +import { reset as resetVisConfig } from '../../redux/slices/viualization_config_slice'; +import { reset as resetVisualization } from '../../redux/slices/visualization_slice'; +import { reset as resetCountDistribution } from '../../redux/slices/count_distribution_slice'; +import { LogExplorerRouterContext } from '../..'; + +export const DataSourceSelection = ({ tabId }) => { + const { dataSources } = coreRefs; + const dispatch = useDispatch(); + const routerContext = useContext(LogExplorerRouterContext); + const explorerSearchMetadata = useSelector(selectSearchMetaData)[tabId]; + const [activeDataSources, setActiveDataSources] = useState([]); + const [dataSourceOptionList, setDataSourceOptionList] = useState([]); + const [selectedSources, setSelectedSources] = useState([...explorerSearchMetadata.datasources]); + + const resetStateOnDatasourceChange = () => { + dispatch( + resetFields({ + tabId, + }) + ); + dispatch( + resetPatterns({ + tabId, + }) + ); + dispatch( + resetQueryResults({ + tabId, + }) + ); + dispatch( + resetVisConfig({ + tabId, + }) + ); + dispatch( + resetVisualization({ + tabId, + }) + ); + dispatch( + resetCountDistribution({ + tabId, + }) + ); + }; + + const handleSourceChange = (selectedSource) => { + batch(() => { + resetStateOnDatasourceChange(); + dispatch( + updateSearchMetaData({ + tabId, + data: { + datasources: selectedSource, + }, + }) + ); + }); + setSelectedSources(selectedSource); + }; + + useEffect(() => { + setSelectedSources([...(explorerSearchMetadata.datasources || [])]); + return () => {}; + }, [explorerSearchMetadata.datasources]); + + const handleDataSetFetchError = useCallback(() => { + return (error) => {}; + }, []); + + useEffect(() => { + const subscription = dataSources.dataSourceService.dataSources$.subscribe( + (currentDataSources) => { + setActiveDataSources([...Object.values(currentDataSources)]); + } + ); + + return () => subscription.unsubscribe(); + }, []); + + useEffect(() => { + // update datasource if url contains + const datasourceName = routerContext?.searchParams.get('datasourceName'); + const datasourceType = routerContext?.searchParams.get('datasourceType'); + if (datasourceName && datasourceType) { + dispatch( + updateSearchMetaData({ + tabId, + data: { + datasources: [ + { + label: datasourceName, + type: datasourceType, + }, + ], + }, + }) + ); + } + }, []); + + return ( + + ); +}; diff --git a/public/components/event_analytics/explorer/direct_query_running.tsx b/public/components/event_analytics/explorer/direct_query_running.tsx new file mode 100644 index 0000000000..9536909ce1 --- /dev/null +++ b/public/components/event_analytics/explorer/direct_query_running.tsx @@ -0,0 +1,36 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import React from 'react'; +import { useDispatch } from 'react-redux'; +import { EuiProgress, EuiEmptyPrompt, EuiButton } from '@elastic/eui'; +import { update as updateSearchMetaData } from '../redux/slices/search_meta_data_slice'; + +export const DirectQueryRunning = ({ tabId }: { tabId: string }) => { + const dispatch = useDispatch(); + return ( + } + title={

Query Processing

} + body={ + { + dispatch( + updateSearchMetaData({ + tabId, + data: { + isPolling: false, + }, + }) + ); + }} + > + Cancel + + } + /> + ); +}; diff --git a/public/components/event_analytics/explorer/explorer.scss b/public/components/event_analytics/explorer/explorer.scss index 9f2d601982..8617e26dfe 100644 --- a/public/components/event_analytics/explorer/explorer.scss +++ b/public/components/event_analytics/explorer/explorer.scss @@ -17,4 +17,13 @@ .mainContentTabs .euiResizableContainer { height: calc(100vh - 298px); } - \ No newline at end of file + +.explorer-loading-spinner { + position: relative; + left: 50%; + top: 50vh; + width: 20px; + height: 20px; + margin-left: -5vw; + margin-top: -20vh; +} diff --git a/public/components/event_analytics/explorer/explorer.tsx b/public/components/event_analytics/explorer/explorer.tsx index 9ceaaa9f44..aae1a2de47 100644 --- a/public/components/event_analytics/explorer/explorer.tsx +++ b/public/components/event_analytics/explorer/explorer.tsx @@ -5,18 +5,19 @@ import dateMath from '@elastic/datemath'; import { - EuiButtonIcon, EuiContextMenuItem, EuiFlexGroup, EuiFlexItem, - EuiHorizontalRule, EuiLink, EuiLoadingSpinner, + EuiPage, + EuiPageBody, + EuiPageSideBar, + EuiPanel, EuiSpacer, EuiTabbedContent, EuiTabbedContentTab, EuiText, - EuiTitle, } from '@elastic/eui'; import { FormattedMessage } from '@osd/i18n/react'; import classNames from 'classnames'; @@ -31,6 +32,7 @@ import React, { useState, } from 'react'; import { batch, useDispatch, useSelector } from 'react-redux'; +import _ from 'lodash'; import { LogExplorerRouterContext } from '..'; import { CREATE_TAB_PARAM, @@ -38,7 +40,6 @@ import { DATE_PICKER_FORMAT, DEFAULT_AVAILABILITY_QUERY, EVENT_ANALYTICS_DOCUMENTATION_URL, - NEW_TAB, PATTERNS_EXTRACTOR_REGEX, PATTERNS_REGEX, RAW_QUERY, @@ -52,10 +53,10 @@ import { SELECTED_TIMESTAMP, TAB_CHART_ID, TAB_CHART_TITLE, - TAB_CREATED_TYPE, TAB_EVENT_ID, TAB_EVENT_TITLE, TIME_INTERVAL_OPTIONS, + DEFAULT_EMPTY_EXPLORER_FIELDS, } from '../../../../common/constants/explorer'; import { LIVE_END_TIME, @@ -109,6 +110,10 @@ import { change as updateVizConfig, selectVisualizationConfig, } from '../redux/slices/viualization_config_slice'; +import { + update as updateSearchMetaData, + selectSearchMetaData, +} from '../../event_analytics/redux/slices/search_meta_data_slice'; import { formatError, getDefaultVisConfig } from '../utils'; import { getContentTabTitle, getDateRange } from '../utils/utils'; import { DataGrid } from './events_views/data_grid'; @@ -119,6 +124,9 @@ import { Sidebar } from './sidebar'; import { TimechartHeader } from './timechart_header'; import { ExplorerVisualizations } from './visualizations'; import { CountDistribution } from './visualizations/count_distribution'; +import { DataSourceSelection } from './datasources/datasources_selection'; +import { DirectQueryRunning } from './direct_query_running'; +import { DirectQueryVisualization } from './visualizations/direct_query_vis'; export const Explorer = ({ pplService, @@ -143,6 +151,7 @@ export const Explorer = ({ callback, callbackInApp, queryManager = new QueryManager(), + dataSourcePluggables, }: IExplorerProps) => { const routerContext = useContext(LogExplorerRouterContext); const dispatch = useDispatch(); @@ -163,6 +172,7 @@ export const Explorer = ({ pplService, requestParams, }); + const appLogEvents = tabId.startsWith('application-analytics-tab'); const query = useSelector(selectQueries)[tabId]; const explorerData = useSelector(selectQueryResult)[tabId]; @@ -170,6 +180,7 @@ export const Explorer = ({ const countDistribution = useSelector(selectCountDistribution)[tabId]; const explorerVisualizations = useSelector(selectExplorerVisualization)[tabId]; const userVizConfigs = useSelector(selectVisualizationConfig)[tabId] || {}; + const explorerSearchMeta = useSelector(selectSearchMetaData)[tabId]; const [selectedContentTabId, setSelectedContentTab] = useState(TAB_EVENT_ID); const [selectedCustomPanelOptions, setSelectedCustomPanelOptions] = useState([]); const [selectedPanelName, setSelectedPanelName] = useState(''); @@ -188,6 +199,17 @@ export const Explorer = ({ const [browserTabFocus, setBrowserTabFocus] = useState(true); const [liveTimestamp, setLiveTimestamp] = useState(DATE_PICKER_FORMAT); const [triggerAvailability, setTriggerAvailability] = useState(false); + const [isQueryRunning, setIsQueryRunning] = useState(false); + const currentPluggable = useMemo(() => { + return ( + dataSourcePluggables[explorerSearchMeta.datasources[0]?.type] || + dataSourcePluggables.DEFAULT_INDEX_PATTERNS + ); + }, [explorerSearchMeta.datasources]); + const { ui } = + currentPluggable?.getComponentSetForVariation('languages', explorerSearchMeta.lang || 'SQL') || + {}; + const SearchBar = ui?.SearchBar || Search; const selectedIntervalRef = useRef<{ text: string; @@ -347,17 +369,6 @@ export const Explorer = ({ }, [appBasedRef.current]); useEffect(() => { - let objectId; - if (queryRef.current![TAB_CREATED_TYPE] === NEW_TAB || appLogEvents) { - objectId = queryRef.current!.savedObjectId || ''; - } else { - objectId = queryRef.current!.savedObjectId || savedObjectId; - } - if (objectId) { - updateTabData(objectId); - } else { - fetchData(startTime, endTime); - } if ( routerContext && routerContext.searchParams.get(CREATE_TAB_PARAM_KEY) === CREATE_TAB_PARAM[TAB_CHART_ID] @@ -367,10 +378,8 @@ export const Explorer = ({ }, []); useEffect(() => { - if (appLogEvents) { - if (savedObjectId) { - updateTabData(savedObjectId); - } + if (savedObjectId) { + updateTabData(savedObjectId); } }, [savedObjectId]); @@ -421,12 +430,8 @@ export const Explorer = ({ } }; - const sidebarClassName = classNames({ - closed: isSidebarClosed, - }); - const mainSectionClassName = classNames({ - 'col-md-10': !isSidebarClosed, + 'col-md-8': !isSidebarClosed, 'col-md-12': isSidebarClosed, }); @@ -473,95 +478,64 @@ export const Explorer = ({ return 0; }, [countDistribution?.data]); + const dateRange = getDateRange(startTime, endTime, query); + + const [storedExplorerFields, setStoredExplorerFields] = useState(explorerFields); + const mainContent = useMemo(() => { return ( - <> -
- {!isSidebarClosed && ( -
- -
- )} - { - setIsSidebarClosed((staleState) => { - return !staleState; - }); - }} - data-test-subj="collapseSideBarButton" - aria-controls="discover-sidebar" - aria-expanded={isSidebarClosed ? 'false' : 'true'} - aria-label="Toggle sidebar" - className="dscCollapsibleSidebar__collapseButton" - /> -
-
- {explorerData && !isEmpty(explorerData.jsonData) ? ( -
-
+
+ {explorerData && !isEmpty(explorerData.jsonData) ? ( + + + + {/* */} {countDistribution?.data && !isLiveTailOnRef.current && ( <> - - - { - return sum + n; - }, - 0 - )} - showResetButton={false} - onResetQuery={() => {}} - /> - - - { - const intervalOptionsIndex = timeIntervalOptions.findIndex( - (item) => item.value === selectedIntrv - ); - const intrv = selectedIntrv.replace(/^auto_/, ''); - getCountVisualizations(intrv); - selectedIntervalRef.current = timeIntervalOptions[intervalOptionsIndex]; - getPatterns(intrv, getErrorHandler('Error fetching patterns')); - }} - stateInterval={selectedIntervalRef.current?.value} - /> - - - - - {}} + /> + { + const intervalOptionsIndex = timeIntervalOptions.findIndex( + (item) => item.value === selectedIntrv + ); + const intrv = selectedIntrv.replace(/^auto_/, ''); + getCountVisualizations(intrv); + selectedIntervalRef.current = timeIntervalOptions[intervalOptionsIndex]; + getPatterns(intrv, getErrorHandler('Error fetching patterns')); + }} + stateInterval={selectedIntervalRef.current?.value} + startTime={appLogEvents ? startTime : dateRange[0]} + endTime={appLogEvents ? endTime : dateRange[1]} + /> + )} - + + + + + + + + + + + + +
)} - {countDistribution?.data && ( - -

- Events - - {' '} - ( - {reduce( - countDistribution.data['count()'], - (sum, n) => { - return sum + n; - }, - 0 - )} - ) - -

-
- )} - + 0 + ? storedExplorerFields.selectedFields + : DEFAULT_EMPTY_EXPLORER_FIELDS + } />
-
-
- ) : ( - - )} -
- + +
+
+ ) : ( + + )} +
); }, [ isPanelTextFieldInvalid, @@ -642,6 +606,8 @@ export const Explorer = ({ isOverridingTimestamp, query, isLiveTailOnRef.current, + isOverridingPattern, + isQueryRunning, ]); const visualizations: IVisualizationContainerProps = useMemo(() => { @@ -668,7 +634,7 @@ export const Explorer = ({ }; const explorerVis = useMemo(() => { - return ( + return explorerSearchMeta.datasources?.[0]?.type === 'DEFAULT_INDEX_PATTERNS' ? ( + ) : ( + ); - }, [query, curVisId, explorerFields, explorerVisualizations, explorerData, visualizations]); + }, [ + query, + curVisId, + explorerFields, + explorerVisualizations, + explorerData, + visualizations, + explorerSearchMeta.datasources, + ]); const contentTabs = [ { @@ -831,6 +807,8 @@ export const Explorer = ({ selectedCustomPanelOptions, ]); + // live tail + const liveTailLoop = async ( name: string, startingTime: string, @@ -898,8 +876,6 @@ export const Explorer = ({ ); }); - const dateRange = getDateRange(startTime, endTime, query); - const handleLiveTailSearch = useCallback( async (startingTime: string, endingTime: string) => { await updateQueryInStore(tempQuery); @@ -934,49 +910,93 @@ export const Explorer = ({ uiSettingsService.get('theme:darkMode') && ' explorer-dark' }`} > - handleTimePickerChange(timeRange)} - selectedPanelName={selectedPanelNameRef.current} - selectedCustomPanelOptions={selectedCustomPanelOptions} - setSelectedPanelName={setSelectedPanelName} - setSelectedCustomPanelOptions={setSelectedCustomPanelOptions} - handleSavingObject={handleSavingObject} - isPanelTextFieldInvalid={isPanelTextFieldInvalid} - savedObjects={savedObjects} - showSavePanelOptionsList={isEqual(selectedContentTabId, TAB_CHART_ID)} - handleTimeRangePickerRefresh={handleTimeRangePickerRefresh} - isLiveTailPopoverOpen={isLiveTailPopoverOpen} - closeLiveTailPopover={() => setIsLiveTailPopoverOpen(false)} - popoverItems={popoverItems} - isLiveTailOn={isLiveTailOnRef.current} - selectedSubTabId={selectedContentTabId} - searchBarConfigs={searchBarConfigs} - getSuggestions={parseGetSuggestions} - onItemSelect={onItemSelect} - tabId={tabId} - baseQuery={appBaseQuery} - stopLive={stopLive} - setIsLiveTailPopoverOpen={setIsLiveTailPopoverOpen} - liveTailName={liveTailNameRef.current} - curVisId={curVisId} - setSubType={setSubType} - /> - tab.id === selectedContentTabId)} - onTabClick={(selectedTab: EuiTabbedContentTab) => handleContentTabClick(selectedTab)} - tabs={contentTabs} - size="s" - /> + + + + + + + +
+ 0 + ? storedExplorerFields + : explorerFields + } + setStoredExplorerFields={setStoredExplorerFields} + /> +
+
+
+
+ + handleTimePickerChange(timeRange)} + selectedPanelName={selectedPanelNameRef.current} + selectedCustomPanelOptions={selectedCustomPanelOptions} + setSelectedPanelName={setSelectedPanelName} + setSelectedCustomPanelOptions={setSelectedCustomPanelOptions} + handleSavingObject={handleSavingObject} + isPanelTextFieldInvalid={isPanelTextFieldInvalid} + savedObjects={savedObjects} + showSavePanelOptionsList={isEqual(selectedContentTabId, TAB_CHART_ID)} + handleTimeRangePickerRefresh={handleTimeRangePickerRefresh} + isLiveTailPopoverOpen={isLiveTailPopoverOpen} + closeLiveTailPopover={() => setIsLiveTailPopoverOpen(false)} + popoverItems={popoverItems} + isLiveTailOn={isLiveTailOnRef.current} + selectedSubTabId={selectedContentTabId} + searchBarConfigs={searchBarConfigs} + getSuggestions={parseGetSuggestions} + onItemSelect={onItemSelect} + tabId={tabId} + baseQuery={appBaseQuery} + stopLive={stopLive} + setIsLiveTailPopoverOpen={setIsLiveTailPopoverOpen} + liveTailName={liveTailNameRef.current} + curVisId={curVisId} + setSubType={setSubType} + http={http} + setIsQueryRunning={setIsQueryRunning} + /> + {explorerSearchMeta.isPolling ? ( + + ) : ( + tab.id === selectedContentTabId)} + onTabClick={(selectedTab: EuiTabbedContentTab) => + handleContentTabClick(selectedTab) + } + tabs={contentTabs} + size="s" + /> + )} + +
); diff --git a/public/components/event_analytics/explorer/log_explorer.tsx b/public/components/event_analytics/explorer/log_explorer.tsx index 4fced3eeaf..f7f93d8c78 100644 --- a/public/components/event_analytics/explorer/log_explorer.tsx +++ b/public/components/event_analytics/explorer/log_explorer.tsx @@ -28,8 +28,8 @@ import { TAB_ID_TXT_PFX, TAB_TITLE, } from '../../../../common/constants/explorer'; -import { ILogExplorerProps } from '../../../../common/types/explorer'; import { initializeTabData, removeTabData } from '../../application_analytics/helpers/utils'; +import { EmptyTabParams, ILogExplorerProps } from '../../../../common/types/explorer'; import { selectQueryResult } from '../redux/slices/query_result_slice'; import { selectQueries } from '../redux/slices/query_slice'; import { selectQueryTabs, setSelectedQueryTab } from '../redux/slices/query_tab_slice'; @@ -46,6 +46,8 @@ const searchBarConfigs = { }, }; +const getExistingEmptyTab = ({ tabIds }: EmptyTabParams) => tabIds[0]; + export const LogExplorer = ({ pplService, dslService, @@ -53,10 +55,10 @@ export const LogExplorer = ({ timestampUtils, setToast, savedObjectId, - getExistingEmptyTab, notifications, http, queryManager, + dataSourcePluggables, }: ILogExplorerProps) => { const history = useHistory(); const routerContext = useContext(LogExplorerRouterContext); @@ -223,14 +225,23 @@ export const LogExplorer = ({ return ( <> - tab.id === curSelectedTabId)} - onTabClick={(selectedTab: EuiTabbedContentTab) => handleTabClick(selectedTab)} - data-test-subj="eventExplorer__topLevelTabbing" - size="s" + ); diff --git a/public/components/event_analytics/explorer/sidebar/sidebar.tsx b/public/components/event_analytics/explorer/sidebar/sidebar.tsx index f949047f56..9ae6fb1275 100644 --- a/public/components/event_analytics/explorer/sidebar/sidebar.tsx +++ b/public/components/event_analytics/explorer/sidebar/sidebar.tsx @@ -6,7 +6,17 @@ import React, { useState, useCallback, useContext } from 'react'; import { batch, useDispatch } from 'react-redux'; import { isEmpty } from 'lodash'; -import { EuiTitle, EuiSpacer, EuiFieldSearch, EuiAccordion } from '@elastic/eui'; +import { + EuiTitle, + EuiSpacer, + EuiFieldSearch, + EuiAccordion, + EuiHorizontalRule, + EuiDragDropContext, + EuiDroppable, + EuiDraggable, + EuiPanel, +} from '@elastic/eui'; import { I18nProvider } from '@osd/i18n/react'; import { Field } from './field'; import { ExplorerFields, IExplorerFields, IField } from '../../../../../common/types/explorer'; @@ -25,6 +35,8 @@ interface ISidebarProps { isFieldToggleButtonDisabled: boolean; handleOverridePattern: (pattern: IField) => void; handleOverrideTimestamp: (timestamp: IField) => void; + storedExplorerFields: IExplorerFields; + setStoredExplorerFields: (explorer: IExplorerFields) => void; } export const Sidebar = (props: ISidebarProps) => { @@ -39,8 +51,9 @@ export const Sidebar = (props: ISidebarProps) => { isFieldToggleButtonDisabled, handleOverridePattern, handleOverrideTimestamp, + storedExplorerFields, + setStoredExplorerFields, } = props; - const dispatch = useDispatch(); const { tabId } = useContext(TabContext); const [showFields, setShowFields] = useState(false); @@ -87,188 +100,251 @@ export const Sidebar = (props: ISidebarProps) => { }); }; + const checkWithStoredFields = () => { + if ( + explorerFields.selectedFields.length === 0 && + storedExplorerFields.selectedFields.length !== 0 + ) { + return storedExplorerFields; + } + return explorerFields; + }; + const handleAddField = useCallback( (field: IField) => { - updateStoreFields( - toggleFields(explorerFields, field, AVAILABLE_FIELDS, SELECTED_FIELDS), - tabId, + const nextFields = toggleFields( + checkWithStoredFields(), + field, + AVAILABLE_FIELDS, SELECTED_FIELDS ); + updateStoreFields(nextFields, tabId, SELECTED_FIELDS); + setStoredExplorerFields(nextFields); }, [explorerFields, tabId] ); const handleRemoveField = useCallback( (field: IField) => { - updateStoreFields( - toggleFields(explorerFields, field, SELECTED_FIELDS, AVAILABLE_FIELDS), - tabId, + const nextFields = toggleFields( + checkWithStoredFields(), + field, + SELECTED_FIELDS, AVAILABLE_FIELDS ); + updateStoreFields(nextFields, tabId, AVAILABLE_FIELDS); + setStoredExplorerFields(nextFields); }, [explorerFields, tabId] ); + const onDragEnd = ({}) => { + console.log('source, destination'); + }; + return ( -
-
- { - setSearchTerm(e.target.value); - }} - placeholder="Search field names" - value={searchTerm} - data-test-subj="eventExplorer__sidebarSearch" - /> -
- -
- {((explorerData && !isEmpty(explorerData.jsonData) && !isEmpty(explorerFields)) || - !isEmpty(explorerFields.availableFields)) && ( - <> - {explorerFields?.queriedFields && explorerFields.queriedFields?.length > 0 && ( + +
+
+ { + setSearchTerm(e.target.value); + }} + placeholder="Search field names" + value={searchTerm} + data-test-subj="eventExplorer__sidebarSearch" + /> +
+ +
+ {((explorerData && !isEmpty(explorerData.jsonData) && !isEmpty(explorerFields)) || + !isEmpty(explorerFields.availableFields)) && ( + <> + {explorerFields?.queriedFields && explorerFields.queriedFields?.length > 0 && ( + + Query fields + + } + paddingSize="xs" + > + + + {explorerFields.queriedFields && + explorerFields.queriedFields.map((field, index) => { + return ( + + + + + + ); + })} + + + )} + - Query fields + + Selected Fields } paddingSize="xs" > -
    + - {explorerFields.queriedFields && - explorerFields.queriedFields.map((field) => { + {explorerData && + !isEmpty(explorerData?.jsonData) && + storedExplorerFields?.selectedFields && + storedExplorerFields?.selectedFields.map((field, index) => { return ( -
  • - -
  • + + + + ); })} -
+
- )} - - - Selected Fields - - } - paddingSize="xs" - > -
    - {explorerData && - !isEmpty(explorerData.jsonData) && - explorerFields.selectedFields && - explorerFields.selectedFields.map((field) => { - return ( -
  • - -
  • - ); - })} -
-
- - - Available Fields - - } - paddingSize="xs" - > -
    + + Available Fields + + } + paddingSize="xs" > - {explorerFields.availableFields && - explorerFields.availableFields - .filter((field) => searchTerm === '' || field.name.indexOf(searchTerm) !== -1) - .map((field) => { - return ( -
  • - -
  • - ); - })} -
-
- - )} -
-
+ + + {storedExplorerFields?.availableFields && + storedExplorerFields?.availableFields + .filter( + (field) => searchTerm === '' || field.name.indexOf(searchTerm) !== -1 + ) + .map((field, index) => { + return ( + + + + + + ); + })} + + + + )} +
+
+
); }; diff --git a/public/components/event_analytics/explorer/visualizations/direct_query_vis.tsx b/public/components/event_analytics/explorer/visualizations/direct_query_vis.tsx new file mode 100644 index 0000000000..411d950eeb --- /dev/null +++ b/public/components/event_analytics/explorer/visualizations/direct_query_vis.tsx @@ -0,0 +1,30 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import React from 'react'; +import { EuiFlexGroup, EuiFlexItem, EuiCallOut, EuiLink, EuiTitle } from '@elastic/eui'; + +export const DirectQueryVisualization = () => { + return ( + + + +

+ Index data to visualize. +

+
+
+ + +

Index data to visualize or select indexed data.

+
+

+ For external data only materialized views or covering indexes can be visualized. Ask your + administrator to create these indexes to visualize them. +

+
+
+ ); +}; diff --git a/public/components/event_analytics/explorer/visualizations/index.tsx b/public/components/event_analytics/explorer/visualizations/index.tsx index 41b6eab6aa..f97db37f3f 100644 --- a/public/components/event_analytics/explorer/visualizations/index.tsx +++ b/public/components/event_analytics/explorer/visualizations/index.tsx @@ -3,11 +3,9 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { isEmpty } from 'lodash'; import React from 'react'; import { EuiResizableContainer } from '@elastic/eui'; import { QueryManager } from 'common/query_manager'; -import { RAW_QUERY, SELECTED_TIMESTAMP } from '../../../../../common/constants/explorer'; import { IField, IQuery, @@ -16,9 +14,8 @@ import { } from '../../../../../common/types/explorer'; import { WorkspacePanel } from './workspace_panel'; import { ConfigPanel } from './config_panel'; -import { Sidebar } from '../sidebar'; import { DataConfigPanelItem } from './config_panel/config_panes/config_controls/data_configurations_panel'; -import { PPL_STATS_REGEX, VIS_CHART_TYPES } from '../../../../../common/constants/shared'; +import { VIS_CHART_TYPES } from '../../../../../common/constants/shared'; import { TreemapConfigPanelItem } from './config_panel/config_panes/config_controls/treemap_config_panel_item'; import { LogsViewConfigPanelItem } from './config_panel/config_panes/config_controls/logs_view_config_panel_item'; @@ -36,14 +33,11 @@ interface IExplorerVisualizationsProps { } export const ExplorerVisualizations = ({ - query, curVisId, setCurVisId, explorerVis, explorerFields, - explorerData, visualizations, - handleOverrideTimestamp, callback, queryManager, }: IExplorerVisualizationsProps) => { @@ -99,22 +93,7 @@ export const ExplorerVisualizations = ({ paddingSize="none" className="vis__leftPanel" > -
-
- -
+
{!isMarkDown && (
{ tabsState, } = props; const history = useHistory(); - const dispatch = useDispatch(); - const [searchQuery, setSearchQuery] = useState(''); const [selectedDateRange, setSelectedDateRange] = useState(['now-15m', 'now']); const [savedHistories, setSavedHistories] = useState([]); const [selectedHistories, setSelectedHistories] = useState([]); @@ -360,31 +358,6 @@ const EventAnalyticsHome = (props: IHomeProps) => { - - - - {}} - setEndTime={() => {}} - showSaveButton={false} - runButtonText="New Query" - getSuggestions={parseGetSuggestions} - onItemSelect={onItemSelect} - /> - - - - diff --git a/public/components/event_analytics/hooks/use_fetch_direct_events.ts b/public/components/event_analytics/hooks/use_fetch_direct_events.ts new file mode 100644 index 0000000000..9b92026f98 --- /dev/null +++ b/public/components/event_analytics/hooks/use_fetch_direct_events.ts @@ -0,0 +1,228 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { useState, useRef } from 'react'; +import { batch } from 'react-redux'; +import { isEmpty } from 'lodash'; +import { useDispatch, useSelector } from 'react-redux'; +import { IField } from 'common/types/explorer'; +import { + FINAL_QUERY, + SELECTED_FIELDS, + UNSELECTED_FIELDS, + AVAILABLE_FIELDS, + QUERIED_FIELDS, +} from '../../../../common/constants/explorer'; +import { fetchSuccess, reset as queryResultReset } from '../redux/slices/query_result_slice'; +import { reset as patternsReset } from '../redux/slices/patterns_slice'; +import { selectQueries } from '../redux/slices/query_slice'; +import { reset as visualizationReset } from '../redux/slices/visualization_slice'; +import { updateFields, sortFields, selectFields } from '../redux/slices/field_slice'; +import PPLService from '../../../services/requests/ppl'; +import { PPL_STATS_REGEX } from '../../../../common/constants/shared'; + +interface IFetchEventsParams { + pplService: PPLService; + requestParams: { tabId: string }; +} + +export const useFetchDirectEvents = ({ pplService, requestParams }: IFetchEventsParams) => { + const dispatch = useDispatch(); + const [isEventsLoading, setIsEventsLoading] = useState(false); + const queries = useSelector(selectQueries); + const fields = useSelector(selectFields); + const [response, setResponse] = useState(); + const queriesRef = useRef(); + const fieldsRef = useRef(); + const responseRef = useRef(); + queriesRef.current = queries; + fieldsRef.current = fields; + responseRef.current = response; + + const fetchEvents = ( + { query }: { query: string }, + format: string, + handler: (res: any) => unknown, + errorHandler?: (error: any) => void + ) => { + setIsEventsLoading(true); + return pplService + .fetch({ query, format }, errorHandler) + .then((res: any) => handler(res)) + .catch((err: any) => { + console.error(err); + throw err; + }) + .finally(() => setIsEventsLoading(false)); + }; + + const addSchemaRowMapping = (queryResult) => { + const pplRes = queryResult; + + const data: any[] = []; + + _.forEach(pplRes.datarows, (row) => { + const record: any = {}; + + for (let i = 0; i < pplRes.schema.length; i++) { + const cur = pplRes.schema[i]; + + if (typeof row[i] === 'object') { + record[cur.name] = JSON.stringify(row[i]); + } else if (typeof row[i] === 'boolean') { + record[cur.name] = row[i].toString(); + } else { + record[cur.name] = row[i]; + } + } + + data.push(record); + }); + return { + ...queryResult, + jsonData: data, + }; + }; + + const dispatchOnGettingHis = (res: any, query: string) => { + const processedRes = addSchemaRowMapping(res); + const selectedFields: string[] = fieldsRef.current![requestParams.tabId][SELECTED_FIELDS].map( + (field: IField) => field.name + ); + batch(() => { + dispatch( + queryResultReset({ + tabId: requestParams.tabId, + }) + ); + dispatch( + fetchSuccess({ + tabId: requestParams.tabId, + data: { + ...processedRes, + }, + }) + ); + dispatch( + updateFields({ + tabId: requestParams.tabId, + data: { + [UNSELECTED_FIELDS]: processedRes?.schema ? [...processedRes.schema] : [], + [QUERIED_FIELDS]: query.match(PPL_STATS_REGEX) ? [...processedRes.schema] : [], // when query contains stats, need populate this + [AVAILABLE_FIELDS]: processedRes?.schema ? [...processedRes.schema] : [], + [SELECTED_FIELDS]: [], + }, + }) + ); + dispatch( + sortFields({ + tabId: requestParams.tabId, + data: [AVAILABLE_FIELDS, UNSELECTED_FIELDS], + }) + ); + dispatch( + visualizationReset({ + tabId: requestParams.tabId, + }) + ); + }); + }; + + const dispatchOnNoHis = (res: any) => { + setResponse(res); + batch(() => { + dispatch( + queryResultReset({ + tabId: requestParams.tabId, + }) + ); + dispatch( + updateFields({ + tabId: requestParams.tabId, + data: { + [SELECTED_FIELDS]: [], + [UNSELECTED_FIELDS]: [], + [QUERIED_FIELDS]: [], + [AVAILABLE_FIELDS]: res?.schema ? [...res.schema] : [], + }, + }) + ); + dispatch( + sortFields({ + tabId: requestParams.tabId, + data: [AVAILABLE_FIELDS], + }) + ); + dispatch( + visualizationReset({ + tabId: requestParams.tabId, + }) + ); + dispatch( + patternsReset({ + tabId: requestParams.tabId, + }) + ); + }); + }; + + const getLiveTail = (query: string = '', errorHandler?: (error: any) => void) => { + const cur = queriesRef.current; + const searchQuery = isEmpty(query) ? cur![requestParams.tabId][FINAL_QUERY] : query; + fetchEvents( + { query: searchQuery }, + 'jdbc', + (res: any) => { + if (!isEmpty(res.jsonData)) { + if (!isEmpty(responseRef.current)) { + res.jsonData = res.jsonData.concat(responseRef.current.jsonData); + res.datarows = res.datarows.concat(responseRef.current.datarows); + res.total = res.total + responseRef.current.total; + res.size = res.size + responseRef.current.size; + } + dispatchOnGettingHis(res, searchQuery); + } + if (isEmpty(res.jsonData) && isEmpty(responseRef.current)) { + dispatchOnNoHis(res); + } + }, + errorHandler + ); + }; + + const getEvents = (query: string = '', errorHandler?: (error: any) => void) => { + if (isEmpty(query)) return; + return dispatchOnGettingHis(res, ''); + }; + + const getAvailableFields = (query: string) => { + fetchEvents({ query }, 'jdbc', (res: any) => { + batch(() => { + dispatch( + updateFields({ + tabId: requestParams.tabId, + data: { + [AVAILABLE_FIELDS]: res?.schema ? [...res.schema] : [], + }, + }) + ); + dispatch( + sortFields({ + tabId: requestParams.tabId, + data: [AVAILABLE_FIELDS, UNSELECTED_FIELDS], + }) + ); + }); + }); + }; + + return { + isEventsLoading, + getLiveTail, + getEvents, + getAvailableFields, + fetchEvents, + }; +}; diff --git a/public/components/event_analytics/hooks/use_fetch_events.ts b/public/components/event_analytics/hooks/use_fetch_events.ts index cfd229eb04..16a77c4362 100644 --- a/public/components/event_analytics/hooks/use_fetch_events.ts +++ b/public/components/event_analytics/hooks/use_fetch_events.ts @@ -58,11 +58,37 @@ export const useFetchEvents = ({ pplService, requestParams }: IFetchEventsParams .finally(() => setIsEventsLoading(false)); }; + const addSchemaRowMapping = (queryResult) => { + const pplRes = queryResult; + + const data: any[] = []; + + _.forEach(pplRes.datarows, (row) => { + const record: any = {}; + + for (let i = 0; i < pplRes.schema.length; i++) { + const cur = pplRes.schema[i]; + + if (typeof row[i] === 'object') { + record[cur.name] = JSON.stringify(row[i]); + } else if (typeof row[i] === 'boolean') { + record[cur.name] = row[i].toString(); + } else { + record[cur.name] = row[i]; + } + } + + data.push(record); + }); + return { + ...queryResult, + jsonData: data, + }; + }; + const dispatchOnGettingHis = (res: any, query: string) => { - const selectedFields: string[] = fieldsRef.current![requestParams.tabId][SELECTED_FIELDS].map( - (field: IField) => field.name - ); - setResponse(res); + const processedRes = addSchemaRowMapping(res); + setResponse(processedRes); batch(() => { dispatch( queryResultReset({ @@ -73,7 +99,7 @@ export const useFetchEvents = ({ pplService, requestParams }: IFetchEventsParams fetchSuccess({ tabId: requestParams.tabId, data: { - ...res, + ...processedRes, }, }) ); @@ -81,9 +107,9 @@ export const useFetchEvents = ({ pplService, requestParams }: IFetchEventsParams updateFields({ tabId: requestParams.tabId, data: { - [UNSELECTED_FIELDS]: res?.schema ? [...res.schema] : [], - [QUERIED_FIELDS]: query.match(PPL_STATS_REGEX) ? [...res.schema] : [], // when query contains stats, need populate this - [AVAILABLE_FIELDS]: res?.schema ? [...res.schema] : [], + [UNSELECTED_FIELDS]: processedRes?.schema ? [...processedRes.schema] : [], + [QUERIED_FIELDS]: query.match(PPL_STATS_REGEX) ? [...processedRes.schema] : [], // when query contains stats, need populate this + [AVAILABLE_FIELDS]: processedRes?.schema ? [...processedRes.schema] : [], [SELECTED_FIELDS]: [], }, }) @@ -165,6 +191,7 @@ export const useFetchEvents = ({ pplService, requestParams }: IFetchEventsParams }; const getEvents = (query: string = '', errorHandler?: (error: any) => void) => { + if (isEmpty(query)) return; const cur = queriesRef.current; const searchQuery = isEmpty(query) ? cur![requestParams.tabId][FINAL_QUERY] : query; fetchEvents( @@ -173,6 +200,8 @@ export const useFetchEvents = ({ pplService, requestParams }: IFetchEventsParams (res: any) => { if (!isEmpty(res.jsonData)) { return dispatchOnGettingHis(res, searchQuery); + } else if (!isEmpty(res.data?.resp)) { + return dispatchOnGettingHis(JSON.parse(res.data?.resp), searchQuery); } // when no hits and needs to get available fields to override default timestamp dispatchOnNoHis(res); @@ -208,5 +237,6 @@ export const useFetchEvents = ({ pplService, requestParams }: IFetchEventsParams getEvents, getAvailableFields, fetchEvents, + dispatchOnGettingHis, }; }; diff --git a/public/components/event_analytics/index.tsx b/public/components/event_analytics/index.tsx index 69c469af1e..e0c89b2433 100644 --- a/public/components/event_analytics/index.tsx +++ b/public/components/event_analytics/index.tsx @@ -93,9 +93,9 @@ export const EventAnalytics = ({ timestampUtils={timestampUtils} http={http} setToast={setToast} - getExistingEmptyTab={getExistingEmptyTab} notifications={notifications} queryManager={queryManager} + dataSourcePluggables={props.dataSourcePluggables} /> ); @@ -120,7 +120,7 @@ export const EventAnalytics = ({ dslService={dslService} pplService={pplService} setToast={setToast} - getExistingEmptyTab={getExistingEmptyTab} + dataSourcePluggables={props.dataSourcePluggables} /> ); }} diff --git a/public/components/event_analytics/redux/slices/count_distribution_slice.ts b/public/components/event_analytics/redux/slices/count_distribution_slice.ts index 5f0ec00fbf..59aa1e03f2 100644 --- a/public/components/event_analytics/redux/slices/count_distribution_slice.ts +++ b/public/components/event_analytics/redux/slices/count_distribution_slice.ts @@ -20,11 +20,14 @@ export const countDistributionSlice = createSlice({ ...payload.data, }; }, + reset: (state, { payload }) => { + state[payload.tabId] = {}; + }, }, extraReducers: (builder) => {}, }); -export const { render } = countDistributionSlice.actions; +export const { render, reset } = countDistributionSlice.actions; export const selectCountDistribution = createSelector( (state) => state.countDistribution, diff --git a/public/components/event_analytics/redux/slices/query_slice.ts b/public/components/event_analytics/redux/slices/query_slice.ts index ec7a475270..8267d72f96 100644 --- a/public/components/event_analytics/redux/slices/query_slice.ts +++ b/public/components/event_analytics/redux/slices/query_slice.ts @@ -71,6 +71,11 @@ export const queriesSlice = createSlice({ remove: (state, { payload }) => { delete state[payload.tabId]; }, + reset: (state, { payload }) => { + state[payload.tabId] = { + ...initialQueryState, + }; + }, }, extraReducers: (builder) => {}, }); diff --git a/public/components/event_analytics/redux/slices/search_meta_data_slice.ts b/public/components/event_analytics/redux/slices/search_meta_data_slice.ts new file mode 100644 index 0000000000..eee7083fab --- /dev/null +++ b/public/components/event_analytics/redux/slices/search_meta_data_slice.ts @@ -0,0 +1,47 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { createSlice, createSelector } from '@reduxjs/toolkit'; +import { initialTabId } from '../../../../framework/redux/store/shared_state'; +import { REDUX_EXPL_SLICE_SEARCH_META_DATA } from '../../../../../common/constants/explorer'; + +const initialState = { + [initialTabId]: { + lang: 'PPL', + datasources: [], + isPolling: false, + }, +}; + +export const searchMetaDataSlice = createSlice({ + name: REDUX_EXPL_SLICE_SEARCH_META_DATA, + initialState, + reducers: { + update: (state, { payload }) => { + state[payload.tabId] = { + ...state[payload.tabId], + ...payload.data, + }; + }, + reset: (state, { payload }) => { + state[payload.tabId] = {}; + }, + init: (state, { payload }) => { + state[payload.tabId] = {}; + }, + remove: (state, { payload }) => { + delete state[payload.tabId]; + }, + }, +}); + +export const { update, remove, init } = searchMetaDataSlice.actions; + +export const selectSearchMetaData = createSelector( + (state) => state.searchMetadata, + (searchMetadata) => searchMetadata +); + +export const searchMetaDataSliceReducer = searchMetaDataSlice.reducer; diff --git a/public/components/hooks/index.ts b/public/components/hooks/index.ts new file mode 100644 index 0000000000..d3c599c18d --- /dev/null +++ b/public/components/hooks/index.ts @@ -0,0 +1,6 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export { usePolling } from './use_polling'; diff --git a/public/components/hooks/use_direct_query_search.ts b/public/components/hooks/use_direct_query_search.ts new file mode 100644 index 0000000000..a850c1690e --- /dev/null +++ b/public/components/hooks/use_direct_query_search.ts @@ -0,0 +1,4 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ diff --git a/public/components/hooks/use_polling.ts b/public/components/hooks/use_polling.ts new file mode 100644 index 0000000000..42fe5b9d35 --- /dev/null +++ b/public/components/hooks/use_polling.ts @@ -0,0 +1,56 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { useState, useRef } from 'react'; + +type FetchFunction = (params?: P) => Promise; + +interface UsePollingReturn { + data: T | null; + loading: boolean; + error: Error | null; + startPolling: (params?: any) => void; + stopPolling: () => void; +} + +export function usePolling( + fetchFunction: FetchFunction, + interval: number = 5000 +): UsePollingReturn { + const [data, setData] = useState(null); + const [error, setError] = useState(null); + const [loading, setLoading] = useState(true); + const intervalRef = useRef(undefined); + + const shouldPoll = useRef(false); + + const startPolling = (params?: P) => { + shouldPoll.current = true; + const intervalId = setInterval(() => { + if (shouldPoll.current) { + fetchData(params); + } + }, interval); + intervalRef.current = intervalId; + }; + + const stopPolling = () => { + shouldPoll.current = false; + clearInterval(intervalRef.current); + }; + + const fetchData = async (params?: P) => { + try { + const result = await fetchFunction(params); + setData(result); + } catch (err) { + setError(err); + } finally { + setLoading(false); + } + }; + + return { data, loading, error, startPolling, stopPolling }; +} diff --git a/public/components/index.tsx b/public/components/index.tsx index 8191752974..1058af43a0 100644 --- a/public/components/index.tsx +++ b/public/components/index.tsx @@ -7,7 +7,7 @@ import React from 'react'; import ReactDOM from 'react-dom'; import { QueryManager } from 'common/query_manager'; import { AppMountParameters, CoreStart } from '../../../../src/core/public'; -import { AppPluginStartDependencies } from '../types'; +import { AppPluginStartDependencies, SetupDependencies } from '../types'; import { App } from './app'; export const Observability = ( @@ -19,7 +19,8 @@ export const Observability = ( savedObjects: any, timestampUtils: any, queryManager: QueryManager, - startPage: string + startPage: string, + dataSourcePluggables ) => { ReactDOM.render( , AppMountParametersProp.element ); diff --git a/public/framework/datasource_pluggables/datasource_pluggable.ts b/public/framework/datasource_pluggables/datasource_pluggable.ts new file mode 100644 index 0000000000..d5212d2e13 --- /dev/null +++ b/public/framework/datasource_pluggables/datasource_pluggable.ts @@ -0,0 +1,29 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { IDataSourceComponentSet, IDataSourcePluggableComponents } from './types'; + +export class DataSourcePluggable { + private components: IDataSourcePluggableComponents = {}; + + public addVariationSet( + variationKey: string, + variationValue: string, + componentSet: IDataSourceComponentSet + ) { + if (!this.components[variationKey]) { + this.components[variationKey] = {}; + } + this.components[variationKey][variationValue] = componentSet; + return this; + } + + public getComponentSetForVariation( + variationKey: string, + variationValue: string + ): IDataSourceComponentSet | undefined { + return this.components[variationKey]?.[variationValue]; + } +} diff --git a/public/framework/datasource_pluggables/types.ts b/public/framework/datasource_pluggables/types.ts new file mode 100644 index 0000000000..2c3a379633 --- /dev/null +++ b/public/framework/datasource_pluggables/types.ts @@ -0,0 +1,22 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { IDataFetcher } from '../../services/data_fetchers/fetch_interface'; + +export interface IDataSourceComponentSet { + ui: { + QueryEditor: React.ReactNode; + ConfigEditor: React.ReactNode; + SidePanel: React.ReactNode; + }; + services: { + data_fetcher: IDataFetcher; + }; +} + +export interface IDataSourcePluggableComponents { + languages?: Record; + // Other variation keys can be added in the future +} diff --git a/public/framework/datasources/s3_datasource.ts b/public/framework/datasources/s3_datasource.ts new file mode 100644 index 0000000000..ef95f09551 --- /dev/null +++ b/public/framework/datasources/s3_datasource.ts @@ -0,0 +1,30 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { DataSource } from '../../../../../src/plugins/data/public'; + +interface DataSourceConfig { + name: string; + type: string; + metadata: any; +} + +export class S3DataSource extends DataSource { + constructor({ name, type, metadata }: DataSourceConfig) { + super(name, type, metadata); + } + + async getDataSet(dataSetParams?: any) { + return [this.getName()]; + } + + async testConnection(): Promise { + throw new Error('This operation is not supported for this class.'); + } + + async runQuery(queryParams: any) { + return null; + } +} diff --git a/public/framework/redux/reducers/index.ts b/public/framework/redux/reducers/index.ts index d392b8265d..6fad432d47 100644 --- a/public/framework/redux/reducers/index.ts +++ b/public/framework/redux/reducers/index.ts @@ -15,6 +15,7 @@ import { explorerVisualizationConfigReducer } from '../../../components/event_an import { patternsReducer } from '../../../components/event_analytics/redux/slices/patterns_slice'; import { metricsReducers } from '../../../components/metrics/redux/slices/metrics_slice'; import { panelReducer } from '../../../components/custom_panels/redux/panel_slice'; +import { searchMetaDataSliceReducer } from '../../../components/event_analytics/redux/slices/search_meta_data_slice'; const combinedReducer = combineReducers({ // explorer reducers @@ -28,6 +29,7 @@ const combinedReducer = combineReducers({ patterns: patternsReducer, metrics: metricsReducers, customPanel: panelReducer, + searchMetadata: searchMetaDataSliceReducer, }); export type RootState = ReturnType; diff --git a/public/plugin.ts b/public/plugin.ts index 021a9f56ad..17144abf17 100644 --- a/public/plugin.ts +++ b/public/plugin.ts @@ -4,7 +4,6 @@ */ import './index.scss'; - import { i18n } from '@osd/i18n'; import { AppCategory, @@ -39,6 +38,11 @@ import { observabilityIntegrationsTitle, observabilityIntegrationsPluginOrder, observabilityPluginOrder, + DATACONNECTIONS_BASE, + S3_DATASOURCE_TYPE, + observabilityDataConnectionsID, + observabilityDataConnectionsPluginOrder, + observabilityDataConnectionsTitle, } from '../common/constants/shared'; import { QueryManager } from '../common/query_manager'; import { VISUALIZATION_SAVED_OBJECT } from '../common/types/observability_saved_object_attributes'; @@ -52,7 +56,6 @@ import { convertLegacyNotebooksUrl } from './components/notebooks/components/hel import { convertLegacyTraceAnalyticsUrl } from './components/trace_analytics/components/common/legacy_route_helpers'; import { SavedObject } from '../../../src/core/public'; import { coreRefs } from './framework/core_refs'; - import { OBSERVABILITY_EMBEDDABLE, OBSERVABILITY_EMBEDDABLE_DESCRIPTION, @@ -72,6 +75,10 @@ import { ObservabilityStart, SetupDependencies, } from './types'; +import { S3DataSource } from './framework/datasources/s3_datasource'; +import { DataSourcePluggable } from './framework/datasource_pluggables/datasource_pluggable'; +import { DirectSearch } from './components/common/search/sql_search'; +import { Search } from './components/common/search/search'; export class ObservabilityPlugin implements @@ -121,13 +128,63 @@ export class ObservabilityPlugin }, }); + // Adding a variation entails associating a key-value pair, where a change in the key results in + // a switch of UI/services to its corresponding context. In the following cases, for an S3 datasource, + // selecting SQL will render SQL-specific UI components or services, while selecting PPL will + // render a set of UI components or services specific to PPL. + const openSearchLocalDataSourcePluggable = new DataSourcePluggable().addVariationSet( + 'languages', + 'PPL', + { + ui: { + QueryEditor: null, + ConfigEditor: null, + SidePanel: null, + SearchBar: Search, + }, + services: {}, + } + ); + + const s3DataSourcePluggable = new DataSourcePluggable() + .addVariationSet('languages', 'SQL', { + ui: { + QueryEditor: null, + ConfigEditor: null, + SidePanel: null, + SearchBar: DirectSearch, + }, + services: { + data_fetcher: null, + }, + }) + .addVariationSet('languages', 'PPL', { + ui: { + QueryEditor: null, + ConfigEditor: null, + SidePanel: null, + SearchBar: DirectSearch, + }, + services: { + data_fetcher: null, + }, + }); + + // below datasource types is referencing: + // https://github.com/opensearch-project/sql/blob/feature/job-apis/core/src/main/java/org/opensearch/sql/datasource/model/DataSourceType.java + const dataSourcePluggables = { + DEFAULT_INDEX_PATTERNS: openSearchLocalDataSourcePluggable, + spark: s3DataSourcePluggable, + s3glue: s3DataSourcePluggable, + // prometheus: openSearchLocalDataSourcePluggable + }; + const appMountWithStartPage = (startPage: string) => async (params: AppMountParameters) => { const { Observability } = await import('./components/index'); const [coreStart, depsStart] = await core.getStartServices(); const dslService = new DSLService(coreStart.http); const savedObjects = new SavedObjects(coreStart.http); const timestampUtils = new TimestampUtils(dslService, pplService); - return Observability( coreStart, depsStart as AppPluginStartDependencies, @@ -137,7 +194,8 @@ export class ObservabilityPlugin savedObjects, timestampUtils, qm, - startPage + startPage, + dataSourcePluggables // just pass down for now due to time constraint, later may better expose this as context ); }; @@ -235,13 +293,32 @@ export class ObservabilityPlugin return {}; } - public start(core: CoreStart): ObservabilityStart { + public start(core: CoreStart, startDeps: AppPluginStartDependencies): ObservabilityStart { const pplService: PPLService = new PPLService(core.http); coreRefs.http = core.http; coreRefs.savedObjectsClient = core.savedObjects.client; coreRefs.pplService = pplService; coreRefs.toasts = core.notifications.toasts; + coreRefs.chrome = core.chrome; + coreRefs.dataSources = startDeps.data.dataSources; + coreRefs.application = core.application; + + const { dataSourceService, dataSourceFactory } = startDeps.data.dataSources; + + // register all s3 datasources + dataSourceFactory.registerDataSourceType(S3_DATASOURCE_TYPE, S3DataSource); + core.http.get(`${DATACONNECTIONS_BASE}`).then((s3DataSources) => { + s3DataSources.map((s3ds) => { + dataSourceService.registerDataSource( + dataSourceFactory.getDataSourceInstance(S3_DATASOURCE_TYPE, { + name: s3ds.name, + type: s3ds.connector.toLowerCase(), + metadata: s3ds, + }) + ); + }); + }); return {}; } diff --git a/public/services/data_fetchers/sql/sql_data_fetcher.ts b/public/services/data_fetchers/sql/sql_data_fetcher.ts new file mode 100644 index 0000000000..6a1fc967c3 --- /dev/null +++ b/public/services/data_fetchers/sql/sql_data_fetcher.ts @@ -0,0 +1,45 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { isEmpty } from 'lodash'; +import { IDefaultTimestampState, IQuery } from '../../../../common/types/explorer'; +import { IDataFetcher } from '../fetch_interface'; +import { DataFetcherBase } from '../fetcher_base'; +import { + buildRawQuery, + composeFinalQuery, + getIndexPatternFromRawQuery, +} from '../../../../common/utils'; +import { + FILTERED_PATTERN, + PATTERNS_REGEX, + PATTERN_REGEX, + RAW_QUERY, + SELECTED_DATE_RANGE, + SELECTED_PATTERN_FIELD, + SELECTED_TIMESTAMP, + TAB_CHART_ID, +} from '../../../../common/constants/explorer'; +import { PPL_BASE, PPL_SEARCH, PPL_STATS_REGEX } from '../../../../common/constants/shared'; +import { CoreStart } from '../../../../../../src/core/public'; +import { useFetchEvents } from '../../../components/event_analytics/hooks'; +import { SQLService } from '../../../services/requests/sql'; + +export class SQLDataFetcher extends DataFetcherBase implements IDataFetcher { + constructor(private readonly http: CoreStart['http']) { + super(); + } + + async search(query: string, callback) { + callback(query); + + const sqlService = new SQLService(this.http); + return sqlService.fetch({ + query, + lang: 'sql', + datasource: '', + }); + } +} diff --git a/public/services/requests/sql.ts b/public/services/requests/sql.ts new file mode 100644 index 0000000000..e87d4d7267 --- /dev/null +++ b/public/services/requests/sql.ts @@ -0,0 +1,41 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { CoreStart } from '../../../../../src/core/public'; +import { PPL_BASE, PPL_SEARCH } from '../../../common/constants/shared'; + +export class SQLService { + private http; + constructor(http: CoreStart['http']) { + this.http = http; + } + + fetch = async ( + params: { + query: string; + lang: string; + datasource: string; + }, + errorHandler?: (error: any) => void + ) => { + return this.http + .post('/api/observability/query/jobs', { + body: JSON.stringify(params), + }) + .catch((error) => { + console.error('fetch error: ', error.body); + if (errorHandler) errorHandler(error); + throw error; + }); + }; + + fetchWithJobId = async (params: { queryId: string }, errorHandler?: (error: any) => void) => { + return this.http.get(`/api/observability/query/jobs/${params.queryId}`).catch((error) => { + console.error('fetch error: ', error.body); + if (errorHandler) errorHandler(error); + throw error; + }); + }; +} diff --git a/public/types.ts b/public/types.ts index 48f0ad9de6..4b37b9c06e 100644 --- a/public/types.ts +++ b/public/types.ts @@ -3,9 +3,10 @@ * SPDX-License-Identifier: Apache-2.0 */ +import { CoreStart } from '../../../src/core/public'; import { SavedObjectsClient } from '../../../src/core/server'; import { DashboardStart } from '../../../src/plugins/dashboard/public'; -import { DataPublicPluginSetup } from '../../../src/plugins/data/public'; +import { DataPublicPluginSetup, DataPublicPluginStart } from '../../../src/plugins/data/public'; import { EmbeddableSetup, EmbeddableStart } from '../../../src/plugins/embeddable/public'; import { NavigationPublicPluginStart } from '../../../src/plugins/navigation/public'; import { UiActionsStart } from '../../../src/plugins/ui_actions/public'; @@ -16,6 +17,7 @@ export interface AppPluginStartDependencies { embeddable: EmbeddableStart; dashboard: DashboardStart; savedObjectsClient: SavedObjectsClient; + data: DataPublicPluginStart; } export interface SetupDependencies { diff --git a/server/adaptors/opensearch_observability_plugin.ts b/server/adaptors/opensearch_observability_plugin.ts index fbdbac72be..0694d850c3 100644 --- a/server/adaptors/opensearch_observability_plugin.ts +++ b/server/adaptors/opensearch_observability_plugin.ts @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { OPENSEARCH_PANELS_API } from '../../common/constants/shared'; +import { JOBS_ENDPOINT_BASE, OPENSEARCH_PANELS_API } from '../../common/constants/shared'; export function OpenSearchObservabilityPlugin(Client: any, config: any, components: any) { const clientAction = components.clientAction.factory; @@ -116,4 +116,25 @@ export function OpenSearchObservabilityPlugin(Client: any, config: any, componen }, method: 'DELETE', }); + + observability.getJobStatus = clientAction({ + url: { + fmt: `${JOBS_ENDPOINT_BASE}/<%=queryId%>`, + req: { + queryId: { + type: 'string', + required: true, + }, + }, + }, + method: 'GET', + }); + + observability.runDirectQuery = clientAction({ + url: { + fmt: `${JOBS_ENDPOINT_BASE}`, + }, + method: 'POST', + needBody: true, + }); } diff --git a/server/routes/sql.ts b/server/routes/sql.ts new file mode 100644 index 0000000000..725b5ca84d --- /dev/null +++ b/server/routes/sql.ts @@ -0,0 +1,38 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { schema } from '@osd/config-schema'; +import { IRouter, IOpenSearchDashboardsResponse, ResponseError } from '../../../../src/core/server'; +import PPLFacet from '../services/facets/ppl_facet'; +import { PPL_BASE, PPL_SEARCH } from '../../common/constants/shared'; + +export function registerSqlRoute({ router, facet }: { router: IRouter; facet: PPLFacet }) { + router.post( + { + path: `/api/sql/search`, + validate: { + body: schema.object({ + query: schema.string(), + format: schema.string(), + }), + }, + }, + async (context, req, res): Promise> => { + const queryRes: any = await facet.describeQuery(req); + if (queryRes.success) { + const result: any = { + body: { + ...queryRes.data, + }, + }; + return res.ok(result); + } + return res.custom({ + statusCode: queryRes.data.statusCode || queryRes.data.status || 500, + body: queryRes.data.body || queryRes.data.message || '', + }); + } + ); +}