Skip to content

Commit

Permalink
Merge pull request #7 from davismcphee/typed-adhoc-dataview-alerts
Browse files Browse the repository at this point in the history
[Discover] Add types to es_query metadata
  • Loading branch information
dimaanj authored Nov 1, 2022
2 parents 0192eb0 + c15a18a commit 7ccfba5
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ import { updateSearchSource } from '../../utils/update_search_source';
const container = document.createElement('div');
let isOpen = false;

interface DiscoverAlertMetadata {
adHocDataViewList: DataView[];
}

const ALERT_TYPE_ID = '.es-query';

interface AlertsPopoverProps {
Expand All @@ -36,6 +32,11 @@ interface AlertsPopoverProps {
updateDataViewList: (dataViews: DataView[]) => Promise<void>;
}

interface EsQueryAlertMetaData {
isManagementPage?: boolean;
adHocDataViewList: DataView[];
}

export function AlertsPopover({
searchSource,
anchorElement,
Expand Down Expand Up @@ -72,10 +73,10 @@ export function AlertsPopover({
};
}, [savedQueryId, searchSource, services]);

const discoverMetadata: DiscoverAlertMetadata = useMemo(
const discoverMetadata: EsQueryAlertMetaData = useMemo(
() => ({
isManagementPage: false,
adHocDataViewList: adHocDataViews,
savedDataViewList: [], // might be filled while in flyout
}),
[adHocDataViews]
);
Expand All @@ -85,19 +86,19 @@ export function AlertsPopover({
return;
}

const onFinishFlyoutInteraction = (metadata: DiscoverAlertMetadata) => {
const onFinishFlyoutInteraction = (metadata: EsQueryAlertMetaData) => {
updateDataViewList(metadata.adHocDataViewList);
};

return triggersActionsUi?.getAddAlertFlyout({
metadata: discoverMetadata,
consumer: 'discover',
onClose: (_, metadata) => {
onFinishFlyoutInteraction(metadata as DiscoverAlertMetadata);
onFinishFlyoutInteraction(metadata as EsQueryAlertMetaData);
onClose();
},
onSave: async (metadata) => {
onFinishFlyoutInteraction(metadata as DiscoverAlertMetadata);
onFinishFlyoutInteraction(metadata as EsQueryAlertMetaData);
},
canChangeTrigger: false,
ruleTypeId: ALERT_TYPE_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ import type { DataViewListItem, DataView } from '@kbn/data-views-plugin/public';
import { DataViewSelector } from '@kbn/unified-search-plugin/public';
import { useTriggerUiActionServices } from '../es_query/util';

interface DiscoverAlertMetadata {
export interface DataViewSelectPopoverMetaData extends Record<string, unknown> {
adHocDataViewList: DataView[];
[key: string]: unknown;
}

export interface DataViewSelectPopoverProps {
dataView: DataView;
metadata?: Record<string, unknown>;
metadata?: DataViewSelectPopoverMetaData;
onSelectDataView: (selectedDataView: DataView) => void;
onChangeMetaData: (metadata: Record<string, unknown>) => void;
onChangeMetaData: (metadata: DataViewSelectPopoverMetaData) => void;
}

const toDataViewListItem = (dataView: DataView): DataViewListItem => {
Expand All @@ -45,21 +44,20 @@ const toDataViewListItem = (dataView: DataView): DataViewListItem => {
};

export const DataViewSelectPopover: React.FunctionComponent<DataViewSelectPopoverProps> = ({
metadata = { adHocDataViewList: [], savedDataViewList: [] },
metadata = { adHocDataViewList: [] },
dataView,
onSelectDataView,
onChangeMetaData,
}) => {
const { dataViews, dataViewEditor } = useTriggerUiActionServices();
const context = metadata as DiscoverAlertMetadata;
const [dataViewItems, setDataViewsItems] = useState<DataViewListItem[]>([]);
const [dataViewPopoverOpen, setDataViewPopoverOpen] = useState(false);

const closeDataViewEditor = useRef<() => void | undefined>();

const allDataViewItems = useMemo(
() => [...dataViewItems, ...context.adHocDataViewList.map(toDataViewListItem)],
[context.adHocDataViewList, dataViewItems]
() => [...dataViewItems, ...metadata.adHocDataViewList.map(toDataViewListItem)],
[dataViewItems, metadata.adHocDataViewList]
);

const closeDataViewPopover = useCallback(() => setDataViewPopoverOpen(false), []);
Expand All @@ -83,11 +81,11 @@ export const DataViewSelectPopover: React.FunctionComponent<DataViewSelectPopove
const onAddAdHocDataView = useCallback(
(adHocDataView: DataView) => {
onChangeMetaData({
...context,
adHocDataViewList: [...context.adHocDataViewList, toDataViewListItem(adHocDataView)],
...metadata,
adHocDataViewList: [...metadata.adHocDataViewList, adHocDataView],
});
},
[context, onChangeMetaData]
[metadata, onChangeMetaData]
);

const createDataView = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { getFields, RuleTypeParamsExpressionProps } from '@kbn/triggers-actions-
import { parseDuration } from '@kbn/alerting-plugin/common';
import { hasExpressionValidationErrors } from '../validation';
import { buildSortedEventsQuery } from '../../../../common/build_sorted_events_query';
import { EsQueryAlertParams, SearchType } from '../types';
import { EsQueryAlertMetaData, EsQueryAlertParams, SearchType } from '../types';
import { IndexSelectPopover } from '../../components/index_select_popover';
import { DEFAULT_VALUES } from '../constants';
import { RuleCommonExpressions } from '../rule_common_expressions';
Expand All @@ -33,7 +33,7 @@ interface KibanaDeps {
}

export const EsQueryExpression: React.FC<
RuleTypeParamsExpressionProps<EsQueryAlertParams<SearchType.esQuery>>
RuleTypeParamsExpressionProps<EsQueryAlertParams<SearchType.esQuery>, EsQueryAlertMetaData>
> = ({ ruleParams, setRuleParams, setRuleProperty, errors, data }) => {
const {
index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import { httpServiceMock } from '@kbn/core/public/mocks';
import { dataPluginMock } from '@kbn/data-plugin/public/mocks';
import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks';
import { unifiedSearchPluginMock } from '@kbn/unified-search-plugin/public/mocks';
import { CommonAlertParams, EsQueryAlertParams, SearchType } from '../types';
import {
CommonAlertParams,
type EsQueryAlertMetaData,
EsQueryAlertParams,
SearchType,
} from '../types';
import { EsQueryAlertTypeExpression } from './expression';
import { chartPluginMock } from '@kbn/charts-plugin/public/mocks';
import { Subject } from 'rxjs';
Expand Down Expand Up @@ -144,7 +149,7 @@ dataMock.query.savedQueries.findSavedQueries = jest.fn(() =>

const Wrapper: React.FC<{
ruleParams: EsQueryAlertParams<SearchType.searchSource> | EsQueryAlertParams<SearchType.esQuery>;
metadata?: Record<string, unknown>;
metadata?: EsQueryAlertMetaData;
}> = ({ ruleParams, metadata }) => {
const [currentRuleParams, setCurrentRuleParams] = useState<CommonAlertParams>(ruleParams);
const errors = {
Expand Down Expand Up @@ -186,7 +191,7 @@ const Wrapper: React.FC<{

const setup = (
ruleParams: EsQueryAlertParams<SearchType.searchSource> | EsQueryAlertParams<SearchType.esQuery>,
metadata?: Record<string, unknown>
metadata?: EsQueryAlertMetaData
) => {
return mountWithIntl(
<KibanaContextProvider
Expand Down Expand Up @@ -253,7 +258,7 @@ describe('EsQueryAlertTypeExpression', () => {
test('should render QueryDSL view without the form type chooser', async () => {
let wrapper: ReactWrapper;
await act(async () => {
wrapper = setup(defaultEsQueryRuleParams, {});
wrapper = setup(defaultEsQueryRuleParams, { adHocDataViewList: [], isManagementPage: false });
wrapper = await wrapper.update();
});
expect(findTestSubject(wrapper!, 'queryFormTypeChooserTitle').exists()).toBeFalsy();
Expand All @@ -264,7 +269,10 @@ describe('EsQueryAlertTypeExpression', () => {
test('should render KQL and Lucene view without the form type chooser', async () => {
let wrapper: ReactWrapper;
await act(async () => {
wrapper = setup(defaultSearchSourceRuleParams, { adHocDataViewList: [] });
wrapper = setup(defaultSearchSourceRuleParams, {
adHocDataViewList: [],
isManagementPage: false,
});
wrapper = await wrapper.update();
});
wrapper = await wrapper!.update();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'brace/theme/github';
import { EuiCallOut, EuiHorizontalRule, EuiSpacer } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { RuleTypeParamsExpressionProps } from '@kbn/triggers-actions-ui-plugin/public';
import { EsQueryAlertParams, SearchType } from '../types';
import type { EsQueryAlertMetaData, EsQueryAlertParams, SearchType } from '../types';
import { SearchSourceExpression, SearchSourceExpressionProps } from './search_source_expression';
import { EsQueryExpression } from './es_query_expression';
import { QueryFormTypeChooser } from './query_form_type_chooser';
Expand All @@ -33,12 +33,12 @@ const SearchSourceExpressionMemoized = memo<SearchSourceExpressionProps>(
);

export const EsQueryAlertTypeExpression: React.FunctionComponent<
RuleTypeParamsExpressionProps<EsQueryAlertParams>
RuleTypeParamsExpressionProps<EsQueryAlertParams, EsQueryAlertMetaData>
> = (props) => {
const { ruleParams, errors, setRuleProperty, setRuleParams } = props;
const isSearchSource = isSearchSourceAlert(ruleParams);
// metadata provided only when open alert from Discover page
const isManagementPage = !props.metadata;
const isManagementPage = props.metadata?.isManagementPage ?? true;

const formTypeSelected = useCallback(
(searchType: SearchType | null) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import { EuiSpacer, EuiLoadingSpinner, EuiEmptyPrompt, EuiCallOut } from '@elast
import { ISearchSource } from '@kbn/data-plugin/common';
import { RuleTypeParamsExpressionProps } from '@kbn/triggers-actions-ui-plugin/public';
import { SavedQuery } from '@kbn/data-plugin/public';
import { EsQueryAlertParams, SearchType } from '../types';
import { EsQueryAlertMetaData, EsQueryAlertParams, SearchType } from '../types';
import { SearchSourceExpressionForm } from './search_source_expression_form';
import { DEFAULT_VALUES } from '../constants';
import { useTriggerUiActionServices } from '../util';

export type SearchSourceExpressionProps = RuleTypeParamsExpressionProps<
EsQueryAlertParams<SearchType.searchSource>
EsQueryAlertParams<SearchType.searchSource>,
EsQueryAlertMetaData
>;

export const SearchSourceExpression = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
type ISearchSource,
} from '@kbn/data-plugin/public';
import { STACK_ALERTS_FEATURE_ID } from '../../../../common';
import { CommonAlertParams, EsQueryAlertParams, SearchType } from '../types';
import { CommonAlertParams, EsQueryAlertMetaData, EsQueryAlertParams, SearchType } from '../types';
import { DEFAULT_VALUES } from '../constants';
import { DataViewSelectPopover } from '../../components/data_view_select_popover';
import { RuleCommonExpressions } from '../rule_common_expressions';
Expand Down Expand Up @@ -71,10 +71,10 @@ interface SearchSourceExpressionFormProps {
searchSource: ISearchSource;
ruleParams: EsQueryAlertParams<SearchType.searchSource>;
errors: IErrorObject;
metadata?: Record<string, unknown>;
metadata?: EsQueryAlertMetaData;
initialSavedQuery?: SavedQuery;
setParam: (paramField: string, paramValue: unknown) => void;
onChangeMetaData: (metadata: Record<string, unknown>) => void;
onChangeMetaData: (metadata: EsQueryAlertMetaData) => void;
}

const isSearchSourceParam = (action: LocalStateAction): action is SearchSourceParamsAction => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { DataViewEditorStart } from '@kbn/data-view-editor-plugin/public';
import type { UnifiedSearchPublicPluginStart } from '@kbn/unified-search-plugin/public';
import type { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public';
import { EXPRESSION_ERRORS } from './constants';
import type { DataViewSelectPopoverMetaData } from '../components/data_view_select_popover';

export interface Comparator {
text: string;
Expand All @@ -34,6 +35,10 @@ export interface CommonAlertParams extends RuleTypeParams {
excludeHitsFromPreviousRun: boolean;
}

export interface EsQueryAlertMetaData extends DataViewSelectPopoverMetaData {
isManagementPage?: boolean;
}

export type EsQueryAlertParams<T = SearchType> = T extends SearchType.searchSource
? CommonAlertParams & OnlySearchSourceAlertParams
: CommonAlertParams & OnlyEsQueryAlertParams;
Expand Down

0 comments on commit 7ccfba5

Please sign in to comment.