Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add SearchOnLoad Preference for dataset #8513

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

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

nit: So you set this one as an optional property. Seems searchOnLoad property is added to some dataset types but not all? Seems we have searchOnLoad on index pattern type, index type and s3 type. what else types need this searchOnLoad that allows this property to be missing? To make it consistent, maybe remove ? because this return !datasetType || (datasetService?.getType(datasetType)?.meta?.searchOnLoad ?? true already set the default value to true right? so ? is not necessary.

Copy link
Member

Choose a reason for hiding this comment

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

nice callout. I think we should have different types for the config we provide and the config we receive from the service. Not a blocker for the PR though

};
/**
* Converts a DataStructure to a Dataset.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
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 @@
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
Copy link
Member

Choose a reason for hiding this comment

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

I know it is a bit too much to ask. maybe we should add a test here given we have more and more logic in useSearch.

Copy link
Member

Choose a reason for hiding this comment

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

+1 we definitely need to add a test for this

// 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 @@
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]
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we check for skipInitialFetch.current instead?

Copy link
Member

Choose a reason for hiding this comment

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

+1.

);
const refetch$ = useMemo(() => new Subject<SearchRefetch>(), []);

Expand Down Expand Up @@ -289,6 +302,9 @@
]);

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 @@
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 @@
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
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
Loading