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

Fix template queries loading and update getSampleQuery interface #8848

Merged
merged 6 commits into from
Nov 13, 2024

Conversation

jowg-amazon
Copy link
Contributor

@jowg-amazon jowg-amazon commented Nov 12, 2024

Description

  • Instead of loading template queries through saved objects api, this pr changes it so that we can load template queries in through the getSampleQuery function directly
  • Updates URL state when opening a saved query so datapicker state is correct
  • Updates getSampleQuery interface so it can also return a promise
 PASS  src/plugins/data/public/ui/saved_query_flyouts/open_saved_query_flyout.test.tsx
  OpenSavedQueryFlyout
    ✓ should render the flyout with correct tabs and content (58 ms)
    ✓ should filter saved queries based on search input (29 ms)
    ✓ should select a query when clicking on it and enable the "Open query" button (14 ms)
    ✓ should call handleQueryDelete when deleting a query (21 ms)
    ✓ should handle pagination controls correctly (43 ms)

Test Suites: 1 passed, 1 total
Tests:       5 passed, 5 total
Snapshots:   0 total
Time:        3.351 s
Ran all test suites matching /src\/plugins\/data\/public\/ui\/saved_query_flyouts\/open_saved_query_flyout.test.tsx/i.

Screenshot 2024-11-11 at 10 52 01 PM
Screenshot 2024-11-11 at 10 51 54 PM
Screenshot 2024-11-12 at 9 23 49 AM
Screenshot 2024-11-11 at 10 51 48 PM
Screenshot 2024-11-11 at 10 51 42 PM

Screen.Recording.2024-11-12.at.9.50.17.AM.mov

Issues Resolved

Screenshot

Testing the changes

Changelog

  • fix: Fix template queries loading and update getSampleQuery interface

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 Nov 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 56.39%. Comparing base (76cf823) to head (c73d357).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8848      +/-   ##
==========================================
- Coverage   60.86%   56.39%   -4.48%     
==========================================
  Files        3800     1239    -2561     
  Lines       90818    25913   -64905     
  Branches    14307     4437    -9870     
==========================================
- Hits        55276    14613   -40663     
+ Misses      32028    10559   -21469     
+ Partials     3514      741    -2773     
Flag Coverage Δ
Linux_1 ?
Linux_2 56.39% <ø> (ø)
Linux_3 ?
Linux_4 ?
Windows_1 ?
Windows_2 ?
Windows_3 ?
Windows_4 ?

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.

Signed-off-by: Joanne Wang <jowg@amazon.com>
@jowg-amazon jowg-amazon changed the title Fix saved query loading and update getSampleQuery interface Fix template queries loading and update getSampleQuery interface Nov 12, 2024
Copy link
Collaborator

@virajsanghvi virajsanghvi left a comment

Choose a reason for hiding this comment

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

Only skimmed so far, but posting initial comments

@@ -179,7 +197,13 @@ export function OpenSavedQueryFlyout({
onChange={onChange}
/>
<EuiSpacer />
{queriesOnCurrentPage.length > 0 ? (
{isLoading ? (
<EuiFlexGroup justifyContent="center" alignItems="center" style={{ height: '200px' }}>
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: why are we hardcoding height here?

Copy link
Member

Choose a reason for hiding this comment

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

+1

query: selectedQuery.attributes.query.query,
language: selectedQuery.attributes.query.language,
};
queryStringManager.setQuery(updatedQuery);
Copy link
Collaborator

Choose a reason for hiding this comment

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

curious, why does this use setQuery over onQueryOpen?

Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to this line (link), we wanted to preserve the data source info in the URL state when the saved query isn't associated with a data source. onQueryOpen clears the selected data source.

Copy link
Member

Choose a reason for hiding this comment

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

This is the line that concerns me a little and want to know more about why we need this. What are we trying to do here?

There are 2 states at play here. Query manager state and the local searchbar state. onQueryOpen updates the search bar state and not the query state because the act of opening a saved query today does not update the query state until the user has decided to fire the query (similar to how you dont want typing the query to update query state until you are ready to fire the query)

Updating the query manager state here for templates changes this behaviour for users when it comes to templates, because now it means that template queries DO change the query state even if the user has not been given the opportunity to confirm that thats the query that they want to run.

This change will not affect datasources that dont run the query by default, but for indices and index patterns, this means that opening a saved query template runs the query automatically while non templates do not.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ashwin-pc That's good to know. Given that context, it sounds like the way forward would involve retrieving the dataset from the query state when the saved query doesn't have a dataset (i.e., when the saved query is a template, or when the saved query is not associated with a dataset), and applying it to the saved query before calling onQueryOpen. Does that sound appropriate?
E.g.,

onClick={() => {
                if (selectedQuery) {
                  if (
                    // Template queries are not associated with data sources. Apply data source from current query
                    selectedQuery.attributes.isTemplate ||
                    // Associating a saved query with a data source is optional. If no data source is present, apply the current source.
                    !selectedQuery.attributes.query.dataset?.dataSource
                  ) {
                    selectedQuery.attributes.query.dataset = queryStringManager?.getQuery().dataset;
                  }
                  onQueryOpen(selectedQuery);
                  onClose();
                }
              }}

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this PR has been merged, I'll adjust this in a follow-up PR.

Comment on lines +206 to +207
if (Array.isArray(sampleQueriesResponse)) {
setSampleQueries([...sampleQueriesResponse, ...newSampleQueries]);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit, could create a helper function for setting sample queries that gets used in both places to prevent divergence

Copy link
Member

Choose a reason for hiding this comment

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

+1 having the savedQueryService take care of this would be ideal. Sample queries and templates are an extension of that

jowg-amazon and others added 2 commits November 12, 2024 12:45
Signed-off-by: Joanne Wang <jowg@amazon.com>
Signed-off-by: Riya Saxena <riysaxen@amazon.com>
Copy link
Collaborator

@virajsanghvi virajsanghvi left a comment

Choose a reason for hiding this comment

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

For some reason codecov is no longer loading, but think the two main things are covered at this point.

I restarted some failed CI that I think is unrelated, but please validate.

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.

Did we sanity test this with the new UI with different datasources and languages?

@@ -179,7 +197,13 @@ export function OpenSavedQueryFlyout({
onChange={onChange}
/>
<EuiSpacer />
{queriesOnCurrentPage.length > 0 ? (
{isLoading ? (
<EuiFlexGroup justifyContent="center" alignItems="center" style={{ height: '200px' }}>
Copy link
Member

Choose a reason for hiding this comment

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

+1

Comment on lines +70 to +98
setIsLoading(true);
const query = queryStringManager.getQuery();
let templateQueries: any[] = [];

// fetch sample query based on dataset type
if (query?.dataset?.type) {
templateQueries =
(await queryStringManager
.getDatasetService()
?.getType(query.dataset.type)
?.getSampleQueries?.()) || [];

// Check if any sample query has isTemplate set to true
const hasTemplates = templateQueries.some((q) => q?.attributes?.isTemplate);
setHasTemplateQueries(hasTemplates);
}

// Set queries based on the current tab
if (currentTabIdRef.current === 'mutable-saved-queries') {
const allQueries = await savedQueryService.getAllSavedQueries();
const mutableSavedQueries = allQueries.filter((q) => !q.attributes.isTemplate);
if (currentTabIdRef.current === 'mutable-saved-queries') {
setSavedQueries(mutableSavedQueries);
}
} else if (currentTabIdRef.current === 'template-saved-queries') {
setSavedQueries(templateQueries);
}
setIsLoading(false);
}, [savedQueryService, currentTabIdRef, setSavedQueries, queryStringManager]);
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean that now while my sample queries load my saved query flyout is unusable? Also if thats true, whats the user experience when these queries take time to load or fail?

Comment on lines +88 to +94
if (currentTabIdRef.current === 'mutable-saved-queries') {
const allQueries = await savedQueryService.getAllSavedQueries();
const mutableSavedQueries = allQueries.filter((q) => !q.attributes.isTemplate);
if (currentTabIdRef.current === 'mutable-saved-queries') {
setSavedQueries(mutableSavedQueries);
}
} else if (currentTabIdRef.current === 'template-saved-queries') {
Copy link
Member

Choose a reason for hiding this comment

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

nit: Can we use enums or constants for the tabs and not raw strings?

query: selectedQuery.attributes.query.query,
language: selectedQuery.attributes.query.language,
};
queryStringManager.setQuery(updatedQuery);
Copy link
Member

Choose a reason for hiding this comment

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

This is the line that concerns me a little and want to know more about why we need this. What are we trying to do here?

There are 2 states at play here. Query manager state and the local searchbar state. onQueryOpen updates the search bar state and not the query state because the act of opening a saved query today does not update the query state until the user has decided to fire the query (similar to how you dont want typing the query to update query state until you are ready to fire the query)

Updating the query manager state here for templates changes this behaviour for users when it comes to templates, because now it means that template queries DO change the query state even if the user has not been given the opportunity to confirm that thats the query that they want to run.

This change will not affect datasources that dont run the query by default, but for indices and index patterns, this means that opening a saved query template runs the query automatically while non templates do not.

@@ -256,6 +258,7 @@ export function SavedQueryManagementComponent({
onClose={() => openSavedQueryFlyout?.close().then()}
onQueryOpen={onLoad}
handleQueryDelete={handleDelete}
queryStringManager={queryStringManager}
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Instead of prop drilling queryStringManager throughout, savedQueryService should ideally have been responsible for templates too.

Comment on lines +206 to +207
if (Array.isArray(sampleQueriesResponse)) {
setSampleQueries([...sampleQueriesResponse, ...newSampleQueries]);
Copy link
Member

Choose a reason for hiding this comment

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

+1 having the savedQueryService take care of this would be ideal. Sample queries and templates are an extension of that

}
})
.catch((error: any) => {
// noop
Copy link
Member

Choose a reason for hiding this comment

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

Lets atleast log this?

Copy link
Member

@kavilla kavilla left a comment

Choose a reason for hiding this comment

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

i would avoid injecting a new prop to global component. will add more comments to the pr

@@ -467,6 +474,7 @@ class SearchBarUI extends Component<SearchBarProps, State> {
useSaveQueryMenu={useSaveQueryMenu}
isQueryEditorControl={isQueryEditorControl}
saveQuery={this.onSave}
queryStringManager={this.props.queryStringManager}
Copy link
Member

Choose a reason for hiding this comment

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

the query string manager being pass in which will impact the state of the components in search bar which is also utilized by other plugins besides the Discover plugin (like dashboards and visualizations) so i would watch out and generally avoid touching this file.

this already was able to access the query string manager like this:

https://github.com/opensearch-project/OpenSearch-Dashboards/blob/main/src/plugins/data/public/ui/search_bar/search_bar.tsx#L123

this should be available in similar locations within this ui

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for calling that out. I'll revert line 205 in create_search_bar.tsx.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this PR has been merged, I'll adjust this in a follow-up PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Addressed in #8864

}, [savedQueryService, selectedTabId, setSavedQueries]);
setIsLoading(true);
const query = queryStringManager.getQuery();
let templateQueries: any[] = [];
Copy link
Member

Choose a reason for hiding this comment

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

we should just be passing around a Query object.

}

// Set queries based on the current tab
if (currentTabIdRef.current === 'mutable-saved-queries') {
Copy link
Member

@kavilla kavilla Nov 13, 2024

Choose a reason for hiding this comment

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

apologies i believe i missed the original design PR for template queries.

Key Issues with Current Approach:

  • Saved Query Purpose Change
    • Historically, saved queries stored exactly what users typed
    • Used for cross-application consistency
    • one major use case: Create query in Discover, apply same query in Dashboards against many saved searches and visualizations
  • Cross-Plugin Impact
    • Potential unexpected behavior in Dashboards when using templated queries
    • Need to verify behavior after dataset type selection
    • Other plugins may not be aware of template functionality
  • Suggested Alternative Structure
    • Make saved queries and saved query templates separate entities
    • Create new saved object type: "saved query template"
    • Keep existing saved query behavior intact
    • Share UI components between both types

This maintains backwards compatibility while cleanly separating template functionality from core saved query behavior.

that said might be a little too late. but have we verified existing saved query impact? as of today with this new ui toggled on? do we need to add a saved object migration for saved queries? could we verify the impact to other plugins for example the dashboards plugin?

@ashwin-pc ashwin-pc dismissed kavilla’s stale review November 13, 2024 18:31

Overriding due to a parallel conversation where some of the existing issues are an existing bug. This is not the final implimentation and requires changes to incorporate the appropriate fixes. The added property should be fine since it does not modify how the existing properties and saved searches work

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.

Conditionally approving it to unblock testing the saved query template changes. Saved query as a feature is still broken. That needs to be fixed in a separate PR.

@ashwin-pc ashwin-pc merged commit 25d3de7 into opensearch-project:main Nov 13, 2024
67 of 70 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Nov 13, 2024
* update sample query impl and fix saved query load

Signed-off-by: Joanne Wang <jowg@amazon.com>

* Changeset file for PR #8848 created/updated

* Changeset file for PR #8848 created/updated

* add loading spinner

Signed-off-by: Joanne Wang <jowg@amazon.com>

* check if tab is shown based on if sample query isTemplate

Signed-off-by: Joanne Wang <jowg@amazon.com>

* added UTs for svaed queries flyout

Signed-off-by: Riya Saxena <riysaxen@amazon.com>

---------

Signed-off-by: Joanne Wang <jowg@amazon.com>
Signed-off-by: Riya Saxena <riysaxen@amazon.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
Co-authored-by: Riya Saxena <riysaxen@amazon.com>
(cherry picked from commit 25d3de7)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
abbyhu2000 pushed a commit that referenced this pull request Nov 15, 2024
…) (#8859)

* update sample query impl and fix saved query load



* Changeset file for PR #8848 created/updated

* Changeset file for PR #8848 created/updated

* add loading spinner



* check if tab is shown based on if sample query isTemplate



* added UTs for svaed queries flyout



---------





(cherry picked from commit 25d3de7)

Signed-off-by: Joanne Wang <jowg@amazon.com>
Signed-off-by: Riya Saxena <riysaxen@amazon.com>
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>
Co-authored-by: Riya Saxena <riysaxen@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants