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

Conversation

ananzh
Copy link
Member

@ananzh ananzh commented Oct 25, 2024

Description

Saved query workflow

useSavedQuery → populateStateFromSavedQuery with new saved query → queryString.setQuery(new saved query)
useSavedQuery → populateStateFromSavedQuery with new saved query → filterManager.setFilters(new saved query)

Then they are clean out in use_search

useEffect(() => {
    (async () => {
      const savedSearchInstance = await getSavedSearchById(savedSearchId);

      const query =
        savedSearchInstance.searchSource.getField('query') ||
        data.query.queryString.getDefaultQuery();

      const isEnhancementsEnabled = await uiSettings.get('query:enhancements:enabled');
      if (isEnhancementsEnabled && query.dataset) {
        let pattern = await data.indexPatterns.get(
          query.dataset.id,
          query.dataset.type !== 'INDEX_PATTERN'
        );
        if (!pattern) {
          await data.query.queryString.getDatasetService().cacheDataset(query.dataset, {
            uiSettings: services.uiSettings,
            savedObjects: services.savedObjects,
            notifications: services.notifications,
            http: services.http,
            data: services.data,
          });
          pattern = await data.indexPatterns.get(
            query.dataset.id,
            query.dataset.type !== 'INDEX_PATTERN'
          );
          savedSearchInstance.searchSource.setField('index', pattern);
        }
      }

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

      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);
      setSavedSearch(savedSearchInstance);

      if (savedSearchInstance?.id) {
        chrome.recentlyAccessed.add(
          savedSearchInstance.getFullPath(),
          savedSearchInstance.title,
          savedSearchInstance.id,
          {
            type: savedSearchInstance.getOpenSearchType(),
          }
        );
      }
    })();
    // This effect will only run when getSavedSearchById is called, which is
    // only called when the component is first mounted.
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [getSavedSearchById, savedSearchId]);

After change:

  • legacy
2024-10-25_15-53-55.mp4
  • with enhancement
2024-10-25_15-57-36.mp4

Issues Resolved

NA

Screenshot

NA

Testing the changes

Changelog

  • fix: Ensure save query loaded properly from asset

Check List

  • All tests pass
    • yarn test:jest
    • yarn test:jest_integration
  • New functionality includes testing.
  • New functionality has been documented.
  • Update CHANGELOG.md
  • Commits are signed per the DCO using --signoff

Copy link

codecov bot commented Oct 25, 2024

Codecov Report

Attention: Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.

Project coverage is 60.73%. Comparing base (53e8404) to head (b91d2bf).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...ic/application/view_components/utils/use_search.ts 0.00% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8707      +/-   ##
==========================================
+ Coverage   60.71%   60.73%   +0.01%     
==========================================
  Files        3798     3798              
  Lines       90662    90665       +3     
  Branches    14260    14262       +2     
==========================================
+ Hits        55047    55066      +19     
+ Misses      32163    32100      -63     
- Partials     3452     3499      +47     
Flag Coverage Δ
Linux_1 29.06% <0.00%> (-0.01%) ⬇️
Linux_2 56.39% <ø> (?)
Linux_3 ?
Linux_4 ?
Windows_1 29.07% <0.00%> (-0.01%) ⬇️
Windows_2 56.34% <ø> (ø)
Windows_3 37.58% <0.00%> (-0.01%) ⬇️
Windows_4 29.83% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

Copy link
Member

@ashwin-pc ashwin-pc left a comment

Choose a reason for hiding this comment

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

While the change looks fine to me, the fact that its changing how useSearch works makes me nervous. Lets add tests here, both unit tests and functional

yarn.lock Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Can we skip this from the PR?

opensearch-changeset-bot bot added a commit to ananzh/OpenSearch-Dashboards that referenced this pull request Oct 29, 2024
@ruanyl ruanyl merged commit c66b698 into opensearch-project:main Oct 29, 2024
69 of 70 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Oct 29, 2024
* [BUG][Discover] Ensure save query loaded properly from asset

Signed-off-by: Anan Zhuang <[email protected]>

* Changeset file for PR #8707 created/updated

---------

Signed-off-by: Anan Zhuang <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
(cherry picked from commit c66b698)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
ananzh pushed a commit that referenced this pull request Oct 29, 2024
…8740)

* [BUG][Discover] Ensure save query loaded properly from asset



* Changeset file for PR #8707 created/updated

---------



(cherry picked from commit c66b698)

Signed-off-by: Anan Zhuang <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
Qxisylolo pushed a commit to Qxisylolo/OpenSearch-Dashboards that referenced this pull request Oct 30, 2024
…rch-project#8707)

* [BUG][Discover] Ensure save query loaded properly from asset

Signed-off-by: Anan Zhuang <[email protected]>

* Changeset file for PR opensearch-project#8707 created/updated

---------

Signed-off-by: Anan Zhuang <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x bug Something isn't working discover for discover reinvent distinguished-contributor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants