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

[BUG][Discover] Ensure save query loaded properly from asset #8707

Merged
merged 2 commits into from
Oct 29, 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
2 changes: 2 additions & 0 deletions changelogs/fragments/8707.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Ensure save query loaded properly from asset ([#8707](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8707))
Copy link
Member

@ashwin-pc ashwin-pc Oct 28, 2024

Choose a reason for hiding this comment

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

@ananzh Lets add tests for this hook. Its critical to how the app works and reviewing a PR that modifies it without tests makes me nervous. You dont need to test every flow right now, but lets start to add tests so that the main expected behaviour of the hook is tested and the changes you are making are also tested

Copy link
Member Author

Choose a reason for hiding this comment

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

It is a relative safe change bc it only modify the behavior when there is a saved query id. I will update a test for the changes.

Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@
const { pathname } = useLocation();
const initalSearchComplete = useRef(false);
const [savedSearch, setSavedSearch] = useState<SavedSearch | undefined>(undefined);
const { savedSearch: savedSearchId, sort, interval } = useSelector((state) => state.discover);
const { savedSearch: savedSearchId, sort, interval, savedQuery } = useSelector(
(state) => state.discover

Check warning on line 90 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#L89-L90

Added lines #L89 - L90 were not covered by tests
);
const indexPattern = useIndexPattern(services);
const skipInitialFetch = useRef(false);
const {
Expand Down Expand Up @@ -387,17 +389,20 @@

// sync initial app filters from savedObject to filterManager
const filters = cloneDeep(savedSearchInstance.searchSource.getOwnField('filter'));
const actualFilters = [];

if (filters !== undefined) {
let actualFilters: any[] = [];

Check warning on line 393 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#L393

Added line #L393 was not covered by tests

if (savedQuery) {
actualFilters = data.query.filterManager.getFilters();

Check warning on line 396 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#L396

Added line #L396 was not covered by tests
} else if (filters !== undefined) {
const result = typeof filters === 'function' ? filters() : filters;
if (result !== undefined) {
actualFilters.push(...(Array.isArray(result) ? result : [result]));
}
}

filterManager.setAppFilters(actualFilters);
data.query.queryString.setQuery(query);
data.query.queryString.setQuery(savedQuery ? data.query.queryString.getQuery() : query);
setSavedSearch(savedSearchInstance);

if (savedSearchInstance?.id) {
Expand Down
Loading