forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Data Explorer] Implement data fetch logic in Discover 2.0
Signed-off-by: ananzh <[email protected]>
- Loading branch information
Showing
8 changed files
with
310 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/plugins/discover/public/application/components/data_grid/discover_table_component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
// ToDo: impletment DiscoverTableComponent which return | ||
// should handle data columns, rows | ||
{ | ||
/* <DataGridTable | ||
columns={state.columns || []} | ||
indexPattern={indexPattern} | ||
rows={rows} | ||
sort={(state.sort as Array<[any, any]>) || []} | ||
onAddColumn={addColumn} | ||
onFilter={onAddFilter} | ||
onRemoveColumn={onRemoveColumn} | ||
onSetColumns={onSetColumns} | ||
onSort={onSort} | ||
displayTimeColumn={displayTimeColumn} | ||
services={services} | ||
/>;*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/plugins/discover/public/application/view_components/canvas/discover_canvas_service.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { SavedObject } from 'src/core/types/saved_objects'; | ||
import { History } from 'history'; | ||
import { SearchSource } from 'src/plugins/data/common'; | ||
import { DiscoverServices } from '../../../build_services'; | ||
import { SavedSearch } from '../../../saved_searches'; | ||
import { IndexPatternData } from '../utils/index_pattern_helper'; | ||
import { useDiscoverCanvasService } from './use_discover_canvas_service'; | ||
|
||
export interface DiscoverCanvasAppProps { | ||
services: DiscoverServices; | ||
history: History; | ||
savedSearch: SavedSearch | undefined; | ||
indexPatternList: Array<SavedObject<any>>; | ||
} | ||
|
||
export const DiscoverCanvasService = ({ | ||
history, | ||
services, | ||
savedSearch, | ||
indexPatternList, | ||
}: DiscoverCanvasAppProps) => { | ||
const { data$ } = useDiscoverCanvasService({ services, savedSearch, history }); | ||
// if (savedSearch && savedSearch.searchSource) { | ||
// const ip = (savedSearch.searchSource as SearchSource).getField('index') as IndexPatternData; | ||
// } | ||
// ToDo: DiscoverCanvasApplication | ||
return <div>{'DiscoverCanvasApp'}</div>; | ||
}; |
38 changes: 38 additions & 0 deletions
38
...s/discover/public/application/view_components/canvas/utils/use_discover_canvas_service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { useMemo } from 'react'; | ||
import { DiscoverServices } from '../../../../build_services'; | ||
import { SavedSearch } from '../../../../saved_searches'; | ||
import { useSavedSearch } from '../utils/use_saved_search'; | ||
|
||
export interface DiscoverCanvasServiceProps { | ||
services: DiscoverServices; | ||
history: History; | ||
savedSearch: SavedSearch; | ||
} | ||
|
||
export const useDiscoverCanvasService = ({ | ||
services, | ||
history, | ||
savedSearch, | ||
}: DiscoverCanvasServiceProps) => { | ||
const indexPattern = savedSearch.searchSource.getField('index'); | ||
const searchSource = useMemo(() => { | ||
savedSearch.searchSource.setField('index', indexPattern); | ||
return savedSearch.searchSource.createChild(); | ||
}, [savedSearch, indexPattern]); | ||
|
||
const { data$, refetch$ } = useSavedSearch({ | ||
indexPattern, | ||
searchSource, | ||
services, | ||
}); | ||
|
||
return { | ||
data$, | ||
refetch$, | ||
}; | ||
}; |
File renamed without changes.
154 changes: 154 additions & 0 deletions
154
src/plugins/discover/public/application/view_components/utils/use_saved_search.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { useCallback, useMemo, useRef } from 'react'; | ||
import { SearchSource, IndexPattern } from 'src/plugins/data/public'; | ||
import { BehaviorSubject, Subject } from 'rxjs'; | ||
import { useEffect } from 'react'; | ||
import { DiscoverServices } from '../../../build_services'; | ||
|
||
import { validateTimeRange } from '../../../application/helpers/validate_time_range'; | ||
import { RequestAdapter } from '../../../../inspector/public'; | ||
|
||
export enum FetchStatus { | ||
UNINITIALIZED = 'uninitialized', | ||
LOADING = 'loading', | ||
COMPLETE = 'complete', | ||
ERROR = 'error', | ||
} | ||
|
||
export interface SavedSearchData { | ||
status: FetchStatus; | ||
fetchCounter?: number; | ||
fieldCounts?: Record<string, number>; | ||
fetchError?: Error; | ||
hits?: number; | ||
rows?: any[]; // ToDo: type | ||
} | ||
|
||
export type SavedSearchRefetch = 'refetch' | undefined; | ||
|
||
export type DataSubject = BehaviorSubject<SavedSearchData>; | ||
export type RefetchSubject = BehaviorSubject<SavedSearchRefetch>; | ||
|
||
export const useSavedSearch = ({ | ||
indexPattern, | ||
searchSource, | ||
services, | ||
}: { | ||
indexPattern: IndexPattern; | ||
searchSource: SearchSource; | ||
services: DiscoverServices; | ||
}) => { | ||
const { data, filterManager } = services; | ||
const timefilter = data.query.timefilter.timefilter; | ||
const fetchStateRef = useRef<{ | ||
abortController: AbortController | undefined; | ||
fieldCounts: Record<string, number>; | ||
fetchStatus: FetchStatus; | ||
}>({ | ||
abortController: undefined, | ||
fieldCounts: {}, | ||
fetchStatus: FetchStatus.UNINITIALIZED, | ||
}); | ||
|
||
const data$ = useMemo( | ||
() => new BehaviorSubject<any>({ state: FetchStatus.UNINITIALIZED }), | ||
[] | ||
); | ||
const refetch$ = useMemo(() => new Subject<any>(), []); | ||
|
||
const fetch = useCallback(() => { | ||
if (!validateTimeRange(timefilter.getTime(), services.toastNotifications)) { | ||
return Promise.reject(); | ||
} | ||
const inspectorAdapters = { requests: new RequestAdapter() }; | ||
|
||
if (fetchStateRef.current.abortController) fetchStateRef.current.abortController.abort(); | ||
fetchStateRef.current.abortController = new AbortController(); | ||
|
||
updateDataSource(searchSource, { indexPattern, services, sort }); | ||
|
||
const fetchResp = searchSource.fetch$({ | ||
abortSignal: fetchStateRef.current.abortController.signal, | ||
}); | ||
|
||
fetchResp.pipe().subscribe( | ||
(resp) => { | ||
const hits = resp.rawResponse.hits.total as number; | ||
const rows = resp.rawResponse.hits.hits; | ||
|
||
for (const row of rows) { | ||
const fields = Object.keys(indexPattern.flattenHit(row)); | ||
for (const fieldName of fields) { | ||
fetchStateRef.current.fieldCounts[fieldName] = | ||
(fetchStateRef.current.fieldCounts[fieldName] || 0) + 1; | ||
} | ||
} | ||
fetchStateRef.current.fieldCounts = fetchStateRef.current.fieldCounts!; | ||
fetchStateRef.current.fetchStatus = FetchStatus.COMPLETE; | ||
data$.next({ | ||
status: FetchStatus.COMPLETE, | ||
fieldCounts: fetchStateRef.current.fieldCounts, | ||
hits, | ||
rows, | ||
}); | ||
}, | ||
(error) => { | ||
// ToDo: implement error handling in react | ||
// export function addFatalError( | ||
// fatalErrors: FatalErrorsSetup, | ||
// error: AngularHttpError | Error | string, | ||
// location?: string | ||
// ) { | ||
// // add support for angular http errors to newPlatformFatalErrors | ||
// if (isAngularHttpError(error)) { | ||
// error = formatAngularHttpError(error); | ||
// } | ||
// fatalErrors.add(error, location); | ||
// } | ||
}, | ||
() => {} | ||
); | ||
}, [data$, timefilter, services, searchSource, indexPattern, fetchStateRef]); | ||
|
||
useEffect(() => { | ||
// ToDo: implement | ||
// const fetch$ = merge( | ||
// refetch$, | ||
// filterManager.getFetches$(), | ||
// timefilter.getFetch$(), | ||
// timefilter.getAutoRefreshFetch$(), | ||
// data.query.queryString.getUpdates$() | ||
// ).pipe(debounceTime(100)); | ||
// subscriptions.add( | ||
// subscribeWithScope( | ||
// $scope, | ||
// fetch$, | ||
// { | ||
// next: $scope.fetch, | ||
// }, | ||
// (error) => addFatalError(core.fatalErrors, error) | ||
// ) | ||
// ); | ||
// subscriptions.add( | ||
// subscribeWithScope( | ||
// $scope, | ||
// timefilter.getTimeUpdate$(), | ||
// { | ||
// next: () => { | ||
// $scope.updateTime(); | ||
// }, | ||
// }, | ||
// (error) => addFatalError(core.fatalErrors, error) | ||
// ) | ||
// ); | ||
}); | ||
|
||
return { | ||
data$, | ||
refetch$, | ||
}; | ||
}; |