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

[Alert search bar] Replace the status filter with controls on the observability alerts page #198495

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -10,15 +10,17 @@ import { waitFor } from '@testing-library/react';
import { timefilterServiceMock } from '@kbn/data-plugin/public/query/timefilter/timefilter_service.mock';
import { uiSettingsServiceMock } from '@kbn/core-ui-settings-browser-mocks';

import { ObservabilityAlertSearchBarProps, Services } from './types';
import { ObservabilityAlertSearchBar } from './alert_search_bar';
import { observabilityAlertFeatureIds } from '../../../common/constants';
import { kibanaStartMock } from '../../utils/kibana_react.mock';
import { render } from '../../utils/test_helper';
import { ObservabilityAlertSearchBar } from './alert_search_bar';
import { ObservabilityAlertSearchBarProps, Services } from './types';

const getAlertsSearchBarMock = jest.fn();
const ALERT_SEARCH_BAR_DATA_TEST_SUBJ = 'alerts-search-bar';

describe('ObservabilityAlertSearchBar', () => {
const { http, data, dataViews, notifications, spaces } = kibanaStartMock.startContract().services;
const renderComponent = (
props: Partial<ObservabilityAlertSearchBarProps> = {},
services: Partial<Services> = {}
Expand All @@ -33,6 +35,8 @@ describe('ObservabilityAlertSearchBar', () => {
onStatusChange: jest.fn(),
onEsQueryChange: jest.fn(),
onFiltersChange: jest.fn(),
onControlConfigsChange: jest.fn(),
onControlFiltersChange: jest.fn(),
setSavedQuery: jest.fn(),
rangeTo: 'now',
rangeFrom: 'now-15m',
Expand All @@ -44,6 +48,11 @@ describe('ObservabilityAlertSearchBar', () => {
<div data-test-subj={ALERT_SEARCH_BAR_DATA_TEST_SUBJ} />
),
useToasts: jest.fn(),
http,
data,
dataViews,
notifications,
spaces,
...services,
},
...props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
* 2.0.
*/

import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import React, { useCallback, useEffect } from 'react';

import { AlertFilterControls } from '@kbn/alerts-ui-shared/src/alert_filter_controls';
import { ControlGroupRenderer } from '@kbn/controls-plugin/public';
import { Storage } from '@kbn/kibana-utils-plugin/public';
import { i18n } from '@kbn/i18n';
import { Filter, Query } from '@kbn/es-query';
import { Filter } from '@kbn/es-query';
import { getEsQueryConfig } from '@kbn/data-plugin/common';
import { AlertsStatusFilter } from './components';
import { observabilityAlertFeatureIds } from '../../../common/constants';
import { ALERT_STATUS_QUERY, DEFAULT_QUERIES, DEFAULT_QUERY_STRING } from './constants';
import { DEFAULT_QUERIES, DEFAULT_QUERY_STRING } from './constants';
import { ObservabilityAlertSearchBarProps } from './types';
import { buildEsQuery } from '../../utils/build_es_query';
import { AlertStatus } from '../../../common/typings';
// import { AlertStatus } from '../../../common/typings';

const getAlertStatusQuery = (status: string): Query[] => {
return ALERT_STATUS_QUERY[status]
? [{ query: ALERT_STATUS_QUERY[status], language: 'kuery' }]
: [];
};
// const getAlertStatusQuery = (status: string): Query[] => {
// return ALERT_STATUS_QUERY[status]
// ? [{ query: ALERT_STATUS_QUERY[status], language: 'kuery' }]
// : [];
// };
const toastTitle = i18n.translate('xpack.observability.alerts.searchBar.invalidQueryTitle', {
defaultMessage: 'Invalid query string',
});
Expand All @@ -35,60 +36,78 @@ export function ObservabilityAlertSearchBar({
onKueryChange,
onRangeFromChange,
onRangeToChange,
onStatusChange,
onControlFiltersChange,
// onStatusChange,
onFiltersChange,
showFilterBar = false,
controlFilters = [],
controlConfigs,
filters = defaultFilters,
savedQuery,
setSavedQuery,
kuery,
rangeFrom,
rangeTo,
services: { AlertsSearchBar, timeFilterService, useToasts, uiSettings },
status,
}: ObservabilityAlertSearchBarProps) {
services: {
AlertsSearchBar,
timeFilterService,
http,
notifications,
dataViews,
spaces,
useToasts,
uiSettings,
},
}: // status,
ObservabilityAlertSearchBarProps) {
const toasts = useToasts();
const [spaceId, setSpaceId] = useState<string>();

const clearSavedQuery = useCallback(
() => (setSavedQuery ? setSavedQuery(undefined) : null),
[setSavedQuery]
);
const onAlertStatusChange = useCallback(
(alertStatus: AlertStatus) => {
try {
onEsQueryChange(
buildEsQuery({
timeRange: {
to: rangeTo,
from: rangeFrom,
},
kuery,
queries: [...getAlertStatusQuery(alertStatus), ...defaultSearchQueries],
config: getEsQueryConfig(uiSettings),
})
);
} catch (error) {
toasts.addError(error, {
title: toastTitle,
});
onKueryChange(DEFAULT_QUERY_STRING);
}
},
[
onEsQueryChange,
rangeTo,
rangeFrom,
kuery,
defaultSearchQueries,
uiSettings,
toasts,
onKueryChange,
]
);
// const onAlertStatusChange = useCallback(
// (alertStatus: AlertStatus) => {
// try {
// onEsQueryChange(
// buildEsQuery({
// timeRange: {
// to: rangeTo,
// from: rangeFrom,
// },
// kuery,
// queries: [...getAlertStatusQuery(alertStatus), ...defaultSearchQueries],
// config: getEsQueryConfig(uiSettings),
// })
// );
// } catch (error) {
// toasts.addError(error, {
// title: toastTitle,
// });
// onKueryChange(DEFAULT_QUERY_STRING);
// }
// },
// [
// onEsQueryChange,
// rangeTo,
// rangeFrom,
// kuery,
// defaultSearchQueries,
// uiSettings,
// toasts,
// onKueryChange,
// ]
// );

useEffect(() => {
onAlertStatusChange(status);
}, [onAlertStatusChange, status]);
// useEffect(() => {
// onAlertStatusChange(status);
// }, [onAlertStatusChange, status]);

const filterControlsStorageKey = useMemo(
() => ['observabilitySearchBar', spaceId, appName, 'filterControls'].filter(Boolean).join('.'),
[appName, spaceId]
);

const submitQuery = useCallback(() => {
try {
Expand All @@ -99,8 +118,8 @@ export function ObservabilityAlertSearchBar({
from: rangeFrom,
},
kuery,
queries: [...getAlertStatusQuery(status), ...defaultSearchQueries],
filters,
queries: defaultSearchQueries,
filters: [...filters, ...controlFilters],
config: getEsQueryConfig(uiSettings),
})
);
Expand All @@ -111,22 +130,28 @@ export function ObservabilityAlertSearchBar({
onKueryChange(DEFAULT_QUERY_STRING);
}
}, [
defaultSearchQueries,
filters,
kuery,
onEsQueryChange,
onKueryChange,
rangeFrom,
rangeTo,
status,
rangeFrom,
kuery,
defaultSearchQueries,
filters,
controlFilters,
uiSettings,
toasts,
onKueryChange,
]);

useEffect(() => {
submitQuery();
}, [submitQuery]);

useEffect(() => {
if (spaces) {
spaces.getActiveSpace().then((space) => setSpaceId(space.id));
}
}, [spaces]);

const onQuerySubmit = (
{
dateRange,
Expand Down Expand Up @@ -176,11 +201,30 @@ export function ObservabilityAlertSearchBar({
</EuiFlexItem>

<EuiFlexItem>
<EuiFlexGroup justifyContent="spaceBetween" alignItems="center">
<EuiFlexItem grow={false}>
<AlertsStatusFilter status={status} onChange={onStatusChange} />
</EuiFlexItem>
</EuiFlexGroup>
<AlertFilterControls
dataViewSpec={{
id: 'unified-alerts-dv',
title: '.alerts-*',
}}
spaceId={spaceId}
chainingSystem="HIERARCHICAL"
controlsUrlState={controlConfigs}
filters={controlFilters}
onFiltersChange={onControlFiltersChange}
storageKey={filterControlsStorageKey}
services={{
http,
notifications,
dataViews,
storage: Storage,
}}
ControlGroupRenderer={ControlGroupRenderer}
/>
{/* <EuiFlexGroup justifyContent="spaceBetween" alignItems="center">*/}
{/* <EuiFlexItem grow={false}>*/}
{/* <AlertsStatusFilter status={status} onChange={onStatusChange} />*/}
{/* </EuiFlexItem>*/}
{/* </EuiFlexGroup>*/}
</EuiFlexItem>
</EuiFlexGroup>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,36 @@ function AlertSearchbarWithUrlSync(props: AlertSearchBarWithUrlSyncProps) {
const { urlStorageKey, defaultState = DEFAULT_STATE, ...searchBarProps } = props;
const stateProps = useAlertSearchBarStateContainer(urlStorageKey, undefined, defaultState);
const {
data: {
query: {
timefilter: { timefilter: timeFilterService },
},
},
data,
triggersActionsUi: { getAlertsSearchBar: AlertsSearchBar },
uiSettings,
http,
dataViews,
spaces,
notifications,
} = useKibana().services;
const {
query: {
timefilter: { timefilter: timeFilterService },
},
} = data;

return (
<ObservabilityAlertSearchBar
{...stateProps}
{...searchBarProps}
showFilterBar
services={{ timeFilterService, AlertsSearchBar, useToasts, uiSettings }}
services={{
timeFilterService,
AlertsSearchBar,
http,
data,
dataViews,
notifications,
spaces,
useToasts,
uiSettings,
}}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { FilterControlConfig as ControlConfig } from '@kbn/alerts-ui-shared';
import { Filter } from '@kbn/es-query';
import {
createStateContainer,
Expand Down Expand Up @@ -33,6 +34,12 @@ interface AlertSearchBarStateTransitions {
setSavedQueryId: (
state: AlertSearchBarContainerState
) => (savedQueryId?: string) => AlertSearchBarContainerState;
setControlFilters: (
state: AlertSearchBarContainerState
) => (controlFilters: Filter[]) => AlertSearchBarContainerState;
setControlConfigs: (
state: AlertSearchBarContainerState
) => (controlConfigs: ControlConfig[]) => AlertSearchBarContainerState;
}

const DEFAULT_STATE: AlertSearchBarContainerState = {
Expand All @@ -41,6 +48,8 @@ const DEFAULT_STATE: AlertSearchBarContainerState = {
kuery: '',
status: ALL_ALERTS.status,
filters: [],
controlFilters: [],
controlConfigs: [],
};

const transitions: AlertSearchBarStateTransitions = {
Expand All @@ -50,6 +59,8 @@ const transitions: AlertSearchBarStateTransitions = {
setStatus: (state) => (status) => ({ ...state, status }),
setFilters: (state) => (filters) => ({ ...state, filters }),
setSavedQueryId: (state) => (savedQueryId) => ({ ...state, savedQueryId }),
setControlFilters: (state) => (controlFilters) => ({ ...state, controlFilters }),
setControlConfigs: (state) => (controlConfigs) => ({ ...state, controlConfigs }),
};

const alertSearchBarStateContainer = createStateContainer(DEFAULT_STATE, transitions);
Expand Down
Loading