Skip to content

Commit

Permalink
[Discover] Add types to es_query metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
davismcphee committed Oct 21, 2022
1 parent c49d6ba commit a955023
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 30 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 {
onAddAdHocDataViews: (dataViews: DataView[]) => 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) => {
onAddAdHocDataViews(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 @@ -85,11 +83,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 @@ -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 @@ -16,7 +16,7 @@ import { IErrorObject } from '@kbn/triggers-actions-ui-plugin/public';
import { SearchBarProps } from '@kbn/unified-search-plugin/public';
import { mapAndFlattenFilters, type SavedQuery } 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 @@ -66,10 +66,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 @@ -11,6 +11,7 @@ import { EuiComboBoxOptionOption } from '@elastic/eui';
import { DataPublicPluginStart } from '@kbn/data-plugin/public';
import { DataViewEditorStart } from '@kbn/data-view-editor-plugin/public';
import { EXPRESSION_ERRORS } from './constants';
import type { DataViewSelectPopoverMetaData } from '../components/data_view_select_popover';

export interface Comparator {
text: string;
Expand All @@ -32,6 +33,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 a955023

Please sign in to comment.