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

Adjusted source of QueryStringManager functions for flyout. #8864

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions src/plugins/data/public/ui/filter_bar/filter_options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import {
import { FilterEditor } from './filter_editor';
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public';
import { SavedQueryManagementComponent } from '../saved_query_management';
import { QueryStringManager, SavedQuery, SavedQueryService } from '../../query';
import { QueryStringContract, SavedQuery, SavedQueryService } from '../../query';
import { SavedQueryMeta } from '../saved_query_form';
import { getUseNewSavedQueriesUI } from '../../services';

Expand All @@ -79,7 +79,7 @@ interface Props {
useSaveQueryMenu: boolean;
isQueryEditorControl: boolean;
saveQuery: (savedQueryMeta: SavedQueryMeta, saveAsNew?: boolean) => Promise<void>;
queryStringManager: QueryStringManager;
queryStringManager: QueryStringContract;
}
const maxFilterWidth = 600;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
} from '@elastic/eui';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { i18n } from '@osd/i18n';
import { QueryStringManager, SavedQuery, SavedQueryService } from '../../query';
import { QueryStringContract, SavedQuery, SavedQueryService } from '../../query';
import { SavedQueryCard } from './saved_query_card';
import { Query } from '../../../common';

Expand All @@ -33,7 +33,7 @@
onClose: () => void;
onQueryOpen: (query: SavedQuery) => void;
handleQueryDelete: (query: SavedQuery) => Promise<void>;
queryStringManager: QueryStringManager;
queryStringManager: QueryStringContract;
AWSHurneyt marked this conversation as resolved.
Show resolved Hide resolved
}

interface SavedQuerySearchableItem {
Expand Down Expand Up @@ -68,33 +68,38 @@

const fetchAllSavedQueriesForSelectedTab = useCallback(async () => {
setIsLoading(true);
const query = queryStringManager.getQuery();
let templateQueries: any[] = [];
try {
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?.()) || [];
// 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);
}
// 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);
// Set queries based on the current tab
if (currentTabIdRef.current === 'mutable-saved-queries') {
setSavedQueries(mutableSavedQueries);
const allQueries = await savedQueryService.getAllSavedQueries();
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 refactor this component. we shouldn't need to fire a request to get all saved queries if the user opened saved queries and then switched to template queries and then switch back to saved queries and fire a request. we already have their 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.

Yes i think we already noticed the queries taking way longer to show up than they should

const mutableSavedQueries = allQueries.filter((q) => !q.attributes.isTemplate);
if (currentTabIdRef.current === 'mutable-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.

is this required? line 89 should be covering it right?

Copy link
Author

@AWSHurneyt AWSHurneyt Nov 14, 2024

Choose a reason for hiding this comment

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

@jowg-amazon could you clarify?

Copy link
Contributor

Choose a reason for hiding this comment

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

We were noticing that the getAllSavedQueries call can take a little while to return. If a user switches tabs before this call is finished, we were seeing that setSavedQueries(mutableSavedQueries) was overwriting the saved queries incorrectly. This check makes sure that we're still on the mutable saved queries tab.

Copy link
Member

Choose a reason for hiding this comment

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

instead of just have a state variable that sets a boolean if it hasTemplateQueries, why don't we just have a state variable for templateQueries and a state variable mutableSavedQueries.

Or even more so just set saved queries to all queries and then in the component if the selected tab is template-saved-queries you filter out all the saved queries not matching template-saved-queries etc.

setSavedQueries(mutableSavedQueries);
}
} else if (currentTabIdRef.current === 'template-saved-queries') {
setSavedQueries(templateQueries);
}
} else if (currentTabIdRef.current === 'template-saved-queries') {
setSavedQueries(templateQueries);
} catch (e) {
console.error("Error occurred while fetching saved queries.", e);

Check failure on line 99 in src/plugins/data/public/ui/saved_query_flyouts/open_saved_query_flyout.tsx

View workflow job for this annotation

GitHub Actions / Lint and validate

Unexpected console statement

Check failure on line 99 in src/plugins/data/public/ui/saved_query_flyouts/open_saved_query_flyout.tsx

View workflow job for this annotation

GitHub Actions / Lint and validate

Replace `"Error·occurred·while·fetching·saved·queries."` with `'Error·occurred·while·fetching·saved·queries.'`
} finally {
setIsLoading(false);
}
setIsLoading(false);
}, [savedQueryService, currentTabIdRef, setSavedQueries, queryStringManager]);

const updatePageIndex = useCallback((index: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import {
import { i18n } from '@osd/i18n';
import React, { useCallback, useEffect, useState, Fragment, useRef } from 'react';
import { sortBy } from 'lodash';
import { QueryStringManager, SavedQuery, SavedQueryService } from '../..';
import { QueryStringContract, SavedQuery, SavedQueryService } from '../..';
import { SavedQueryListItem } from './saved_query_list_item';
import {
toMountPoint,
Expand All @@ -70,7 +70,7 @@ interface Props {
onClearSavedQuery: () => void;
closeMenuPopover: () => void;
saveQuery: (savedQueryMeta: SavedQueryMeta, saveAsNew?: boolean) => Promise<void>;
queryStringManager: QueryStringManager;
queryStringManager: QueryStringContract;
}

export function SavedQueryManagementComponent({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ export function createSearchBar({ core, storage, data }: StatefulSearchBarDeps)
isRefreshPaused={refreshInterval.pause}
filters={filters}
query={query}
queryStringManager={data.query.queryString}
onFiltersUpdated={defaultFiltersUpdated(data.query)}
onRefreshChange={defaultOnRefreshChange(data.query)}
savedQuery={savedQuery}
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/data/public/ui/search_bar/search_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@
withOpenSearchDashboards,
} from '../../../../opensearch_dashboards_react/public';
import { Filter, IIndexPattern, Query, TimeRange, UI_SETTINGS } from '../../../common';
import {

Check failure on line 41 in src/plugins/data/public/ui/search_bar/search_bar.tsx

View workflow job for this annotation

GitHub Actions / Lint and validate

Replace `⏎··SavedQuery,⏎··SavedQueryAttributes,⏎··TimeHistoryContract,⏎··QueryStatus,⏎` with `·SavedQuery,·SavedQueryAttributes,·TimeHistoryContract,·QueryStatus·`
SavedQuery,
SavedQueryAttributes,
TimeHistoryContract,
QueryStatus,
AWSHurneyt marked this conversation as resolved.
Show resolved Hide resolved
QueryStringManager,
} from '../../query';
import { IDataPluginServices } from '../../types';
import { FilterBar } from '../filter_bar/filter_bar';
Expand Down Expand Up @@ -101,7 +100,6 @@
onRefresh?: (payload: { dateRange: TimeRange }) => void;
indicateNoData?: boolean;
queryStatus?: QueryStatus;
queryStringManager: QueryStringManager;
}

export type SearchBarProps = SearchBarOwnProps & SearchBarInjectedDeps;
Expand Down Expand Up @@ -474,7 +472,7 @@
useSaveQueryMenu={useSaveQueryMenu}
isQueryEditorControl={isQueryEditorControl}
saveQuery={this.onSave}
queryStringManager={this.props.queryStringManager}
queryStringManager={this.queryStringManager}
/>
)
);
Expand Down
Loading