Skip to content

Commit

Permalink
Add searchOnLoad property to datasets (opensearch-project#8513)
Browse files Browse the repository at this point in the history
Signed-off-by: Suchit Sahoo <suchsah@amazon.com>
  • Loading branch information
LDrago27 authored Oct 16, 2024
1 parent 2f23e1e commit 5c8b2e3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const indexPatternTypeConfig: DatasetTypeConfig = {
meta: {
icon: { type: 'indexPatternApp' },
tooltip: 'OpenSearch Index Patterns',
searchOnLoad: true,
},

toDataset: (path) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const indexTypeConfig: DatasetTypeConfig = {
meta: {
icon: { type: 'logoOpenSearch' },
tooltip: 'OpenSearch Indexes',
searchOnLoad: true,
},

toDataset: (path) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface DatasetTypeConfig {
icon: EuiIconProps;
/** Optional tooltip text */
tooltip?: string;
/** Optional preference for search on page load else defaulted to true */
searchOnLoad?: boolean;
};
/**
* Converts a DataStructure to a Dataset.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const useSearch = (services: DiscoverViewServices) => {
const [savedSearch, setSavedSearch] = useState<SavedSearch | undefined>(undefined);
const { savedSearch: savedSearchId, sort, interval } = useSelector((state) => state.discover);
const indexPattern = useIndexPattern(services);
const skipInitialFetch = useRef(false);
const {
data,
filterManager,
Expand All @@ -111,6 +112,15 @@ export const useSearch = (services: DiscoverViewServices) => {
requests: new RequestAdapter(),
};

const getDatasetAutoSearchOnPageLoadPreference = () => {
// Checks the searchOnpageLoadPreference for the current dataset if not specifed defaults to true
const datasetType = data.query.queryString.getQuery().dataset?.type;

const datasetService = data.query.queryString.getDatasetService();

return !datasetType || (datasetService?.getType(datasetType)?.meta?.searchOnLoad ?? true);
};

const shouldSearchOnPageLoad = useCallback(() => {
// A saved search is created on every page load, so we check the ID to see if we're loading a
// previously saved search or if it is just transient
Expand All @@ -125,10 +135,13 @@ export const useSearch = (services: DiscoverViewServices) => {
const data$ = useMemo(
() =>
new BehaviorSubject<SearchData>({
status: shouldSearchOnPageLoad() ? ResultStatus.LOADING : ResultStatus.UNINITIALIZED,
status:
shouldSearchOnPageLoad() && !skipInitialFetch.current
? ResultStatus.LOADING
: ResultStatus.UNINITIALIZED,
queryStatus: { startTime },
}),
[shouldSearchOnPageLoad, startTime]
[shouldSearchOnPageLoad, startTime, skipInitialFetch]
);
const refetch$ = useMemo(() => new Subject<SearchRefetch>(), []);

Expand Down Expand Up @@ -289,6 +302,9 @@ export const useSearch = (services: DiscoverViewServices) => {
]);

useEffect(() => {
if (!getDatasetAutoSearchOnPageLoadPreference()) {
skipInitialFetch.current = true;
}
const fetch$ = merge(
refetch$,
filterManager.getFetches$(),
Expand All @@ -297,8 +313,11 @@ export const useSearch = (services: DiscoverViewServices) => {
timefilter.getAutoRefreshFetch$(),
data.query.queryString.getUpdates$()
).pipe(debounceTime(100));

const subscription = fetch$.subscribe(() => {
if (skipInitialFetch.current) {
skipInitialFetch.current = false; // Reset so future fetches will proceed normally
return; // Skip the first fetch
}
(async () => {
try {
await fetch();
Expand All @@ -316,6 +335,8 @@ export const useSearch = (services: DiscoverViewServices) => {
return () => {
subscription.unsubscribe();
};
// disabling the eslint since we are not adding getDatasetAutoSearchOnPageLoadPreference since this changes when dataset changes and these chnages are already part of data.query.queryString
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
data$,
data.query.queryString,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/query_enhancements/public/datasets/s3_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const s3TypeConfig: DatasetTypeConfig = {
meta: {
icon: { type: S3_ICON },
tooltip: 'Amazon S3 Connections',
searchOnLoad: true,
},

toDataset: (path: DataStructure[]): Dataset => {
Expand Down

0 comments on commit 5c8b2e3

Please sign in to comment.