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

[Backport 2.10] [Discover 2.0] fixes add filter and dismissible callout #5033

Merged
merged 1 commit into from
Sep 15, 2023
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 @@ -20,22 +20,22 @@ export const DiscoverChartContainer = ({ hits, bucketInterval, chartData }: Sear
indexPattern,
]);

if (!hits || !chartData || !bucketInterval) return null;

return (
hits && (
<DiscoverChart
bucketInterval={bucketInterval}
chartData={chartData}
config={uiSettings}
data={data}
hits={hits}
resetQuery={() => {
window.location.href = `#/view/${savedSearch?.id}`;
window.location.reload();
}}
services={services}
showResetButton={!!savedSearch && !!savedSearch.id}
isTimeBased={isTimeBased}
/>
)
<DiscoverChart
bucketInterval={bucketInterval}
chartData={chartData}
config={uiSettings}
data={data}
hits={hits}
resetQuery={() => {
window.location.href = `#/view/${savedSearch?.id}`;
window.location.reload();
}}
services={services}
showResetButton={!!savedSearch && !!savedSearch.id}
isTimeBased={isTimeBased}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ export const DiscoverTable = ({ history }: Props) => {
refetch$.next();
};
const onAddFilter = useCallback(
(field: IndexPatternField, values: string, operation: '+' | '-') => {
(field: string | IndexPatternField, values: string, operation: '+' | '-') => {
if (!indexPattern) return;

const newFilters = opensearchFilters.generateFilters(
filterManager,
field,
values,
operation,
indexPattern.id
indexPattern.id ?? ''
);
return filterManager.addFilters(newFilters);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_re
import { filterColumns } from '../utils/filter_columns';
import { DEFAULT_COLUMNS_SETTING } from '../../../../common';

const KEY_SHOW_NOTICE = 'discover:deprecation-notice:show';

// eslint-disable-next-line import/no-default-export
export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewProps) {
const { data$, refetch$, indexPattern } = useDiscoverContext();
Expand All @@ -41,8 +43,13 @@ export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewPro
bucketInterval: {},
});

const [isCallOutVisible, setIsCallOutVisible] = useState(true);
const closeCallOut = () => setIsCallOutVisible(false);
const [isCallOutVisible, setIsCallOutVisible] = useState(
localStorage.getItem(KEY_SHOW_NOTICE) !== 'false'
);
const closeCallOut = () => {
localStorage.setItem(KEY_SHOW_NOTICE, 'false');
setIsCallOutVisible(false);
};

let callOut;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useEffect, useState } from 'react';
import React, { useCallback, useEffect, useState } from 'react';
import { ViewProps } from '../../../../../data_explorer/public';
import {
addColumn,
Expand All @@ -15,9 +15,18 @@ import {
import { DiscoverSidebar } from '../../components/sidebar';
import { useDiscoverContext } from '../context';
import { ResultStatus, SearchData } from '../utils/use_search';
import { IndexPatternField, opensearchFilters } from '../../../../../data/public';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { DiscoverViewServices } from '../../../build_services';

// eslint-disable-next-line import/no-default-export
export default function DiscoverPanel(props: ViewProps) {
const { services } = useOpenSearchDashboards<DiscoverViewServices>();
const {
data: {
query: { filterManager },
},
} = services;
const { data$, indexPattern } = useDiscoverContext();
const [fetchState, setFetchState] = useState<SearchData>(data$.getValue());

Expand All @@ -36,6 +45,22 @@ export default function DiscoverPanel(props: ViewProps) {
};
}, [data$, fetchState]);

const onAddFilter = useCallback(
(field: string | IndexPatternField, values: string, operation: '+' | '-') => {
if (!indexPattern) return;

const newFilters = opensearchFilters.generateFilters(
filterManager,
field,
values,
operation,
indexPattern.id ?? ''
);
return filterManager.addFilters(newFilters);
},
[filterManager, indexPattern]
);

return (
<DiscoverSidebar
columns={columns || []}
Expand All @@ -61,7 +86,7 @@ export default function DiscoverPanel(props: ViewProps) {
);
}}
selectedIndexPattern={indexPattern}
onAddFilter={() => {}}
onAddFilter={onAddFilter}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { IndexPattern } from '../../../opensearch_dashboards_services';
*/
export function filterColumns(
columns: string[],
indexPattern: IndexPattern,
indexPattern: IndexPattern | undefined,
defaultColumns: string[]
) {
const fieldsName = indexPattern?.fields.getAll().map((fld) => fld.name) || [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BehaviorSubject, Subject, merge } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { i18n } from '@osd/i18n';
import { useEffect } from 'react';
import { cloneDeep } from 'lodash';
import { RequestAdapter } from '../../../../../inspector/public';
import { DiscoverServices } from '../../../build_services';
import { search } from '../../../../../data/public';
Expand All @@ -30,7 +31,6 @@ import {
getResponseInspectorStats,
} from '../../../opensearch_dashboards_services';
import { SEARCH_ON_PAGE_LOAD_SETTING } from '../../../../common';
import { SortOrder } from '../../../saved_searches/types';

export enum ResultStatus {
UNINITIALIZED = 'uninitialized',
Expand Down Expand Up @@ -253,9 +253,29 @@ export const useSearch = (services: DiscoverServices) => {
(async () => {
const savedSearchInstance = await getSavedSearchById(savedSearchId || '');
setSavedSearch(savedSearchInstance);

// sync initial app filters from savedObject to filterManager
const filters = cloneDeep(savedSearchInstance.searchSource.getOwnField('filter'));
const query =
savedSearchInstance.searchSource.getField('query') ||
data.query.queryString.getDefaultQuery();
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);
})();

return () => {};
// 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]);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
vis?: Vis;
}

const KEY_SHOW_NOTICE = 'discover:deprecation-notice:show';

export function DiscoverLegacy({
addColumn,
fetch,
Expand Down Expand Up @@ -132,7 +134,9 @@
vis,
}: DiscoverLegacyProps) {
const [isSidebarClosed, setIsSidebarClosed] = useState(false);
const [isCallOutVisible, setIsCallOutVisible] = useState(true);
const [isCallOutVisible, setIsCallOutVisible] = useState(

Check warning on line 137 in src/plugins/discover_legacy/public/application/components/discover_legacy.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover_legacy/public/application/components/discover_legacy.tsx#L137

Added line #L137 was not covered by tests
localStorage.getItem(KEY_SHOW_NOTICE) !== 'false'
);
const { TopNavMenu } = getServices().navigation.ui;
const { savedSearch, indexPatternList } = opts;
const bucketAggConfig = vis?.data?.aggs?.aggs[1];
Expand All @@ -142,7 +146,10 @@
: undefined;
const [fixedScrollEl, setFixedScrollEl] = useState<HTMLElement | undefined>();

const closeCallOut = () => setIsCallOutVisible(false);
const closeCallOut = () => {
localStorage.setItem(KEY_SHOW_NOTICE, 'false');
setIsCallOutVisible(false);

Check warning on line 151 in src/plugins/discover_legacy/public/application/components/discover_legacy.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover_legacy/public/application/components/discover_legacy.tsx#L149-L151

Added lines #L149 - L151 were not covered by tests
};

let callOut;

Expand Down
Loading