Skip to content

Commit

Permalink
Add searchOnLoad property to datasets
Browse files Browse the repository at this point in the history
Signed-off-by: Suchit Sahoo <[email protected]>
  • Loading branch information
LDrago27 committed Oct 10, 2024
1 parent 58e1645 commit aae4ca9
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);

Check warning on line 91 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L91

Added line #L91 was not covered by tests
const {
data,
filterManager,
Expand All @@ -111,6 +112,15 @@ export const useSearch = (services: DiscoverViewServices) => {
requests: new RequestAdapter(),
};

const getDatasetAutoSearchOnPageLoadPreference = () => {

Check warning on line 115 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L115

Added line #L115 was not covered by tests
// Checks the searchOnpageLoadPreference for the current dataset if not specifed defaults to true
const datasetType = data.query.queryString.getQuery().dataset?.type;

Check warning on line 117 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L117

Added line #L117 was not covered by tests

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

Check warning on line 119 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L119

Added line #L119 was not covered by tests

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;

Check warning on line 306 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L306

Added line #L306 was not covered by tests
}
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

Check warning on line 319 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L318-L319

Added lines #L318 - L319 were not covered by tests
}
(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 aae4ca9

Please sign in to comment.