Skip to content

Commit

Permalink
MW scoped query FE changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JiaweiWu committed Nov 26, 2023
1 parent c6f17fe commit 0553e5a
Show file tree
Hide file tree
Showing 40 changed files with 643 additions and 592 deletions.
4 changes: 4 additions & 0 deletions packages/kbn-alerts-ui-shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export { AlertLifecycleStatusBadge } from './src/alert_lifecycle_status_badge';
export type { AlertLifecycleStatusBadgeProps } from './src/alert_lifecycle_status_badge';
export { MaintenanceWindowCallout } from './src/maintenance_window_callout';
export { AddMessageVariables } from './src/add_message_variables';

export * from './src/alerts_search_bar/hooks';
export * from './src/alerts_search_bar/apis';
export { AlertsSearchBar } from './src/alerts_search_bar';
export type { AlertsSearchBarProps } from './src/alerts_search_bar/types';
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { HttpStart } from '@kbn/core/public';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { ValidFeatureId } from '@kbn/rule-data-utils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { HttpSetup } from '@kbn/core/public';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { HttpSetup } from '@kbn/core/public';
import type { AsApiContract, RewriteRequestCase } from '@kbn/actions-types';
import type { RuleType } from '@kbn/triggers-actions-ui-types';
Expand Down
12 changes: 12 additions & 0 deletions packages/kbn-alerts-ui-shared/src/alerts_search_bar/apis/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export * from './fetch_aad_fields';
export * from './fetch_alert_fields';
export * from './fetch_alert_index_names';
export * from './fetch_rule_types';
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { DataView, DataViewField } from '@kbn/data-views-plugin/common';
Expand Down
11 changes: 11 additions & 0 deletions packages/kbn-alerts-ui-shared/src/alerts_search_bar/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export * from './use_alert_data_view';
export * from './use_load_rule_types_query';
export * from './use_rule_aad_fields';
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { useEffect, useMemo, useState } from 'react';
Expand All @@ -21,19 +22,14 @@ export interface UseAlertDataViewResult {
}

export interface UseAlertDataViewProps {
featureIds: ValidFeatureId[],
http: HttpStart,
dataViewsService: DataViewsContract,
toasts: ToastsStart,
featureIds: ValidFeatureId[];
http: HttpStart;
dataViewsService: DataViewsContract;
toasts: ToastsStart;
}

export function useAlertDataView(props: UseAlertDataViewProps): UseAlertDataViewResult {
const {
http,
dataViewsService,
toasts,
featureIds,
} = props;
const { http, dataViewsService, toasts, featureIds } = props;

const [dataViews, setDataViews] = useState<DataView[]>([]);
const features = featureIds.sort().join(',');
Expand All @@ -42,7 +38,8 @@ export function useAlertDataView(props: UseAlertDataViewProps): UseAlertDataView
const hasSecurityAndO11yFeatureIds =
featureIds.length > 1 && featureIds.includes(AlertConsumers.SIEM);

const hasNoSecuritySolution = !isOnlySecurity && !hasSecurityAndO11yFeatureIds;
const hasNoSecuritySolution =
featureIds.length > 0 && !isOnlySecurity && !hasSecurityAndO11yFeatureIds;

const queryIndexNameFn = () => {
return fetchAlertIndexNames({ http, features });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { i18n } from '@kbn/i18n';
import { useQuery } from '@tanstack/react-query';
import type { RuleType, RuleTypeIndex } from '@kbn/triggers-actions-ui-types';
import type { ToastsStart, HttpStart } from '@kbn/core/public';

import { ALERTS_FEATURE_ID } from '../constants';
import { fetchRuleTypes } from '../apis/fetch_rule_types';

interface UseLoadRuleTypesQueryProps {
export interface UseLoadRuleTypesQueryProps {
filteredRuleTypes: string[];
enabled?: boolean;
http: HttpStart,
toasts: ToastsStart,
http: HttpStart;
toasts: ToastsStart;
}

const getFilteredIndex = (data: Array<RuleType<string, string>>, filteredRuleTypes: string[]) => {
Expand All @@ -36,12 +37,7 @@ const getFilteredIndex = (data: Array<RuleType<string, string>>, filteredRuleTyp
};

export const useLoadRuleTypesQuery = (props: UseLoadRuleTypesQueryProps) => {
const {
filteredRuleTypes,
enabled = true,
http,
toasts,
} = props;
const { filteredRuleTypes, enabled = true, http, toasts } = props;

const queryFn = () => {
return fetchRuleTypes({ http });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { useMemo } from 'react';
Expand All @@ -25,11 +26,7 @@ export interface UseRuleAADFieldsResult {
}

export function useRuleAADFields(props: UseRuleAADFieldsProps): UseRuleAADFieldsResult {
const {
ruleTypeId,
http,
toasts,
} = props;
const { ruleTypeId, http, toasts } = props;

const queryAadFieldsFn = () => {
return fetchAadFields({ http, ruleTypeId });
Expand Down
41 changes: 21 additions & 20 deletions packages/kbn-alerts-ui-shared/src/alerts_search_bar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { useCallback, useState } from 'react';
import { Query, TimeRange } from '@kbn/es-query';
import { SuggestionsAbstraction } from '@kbn/unified-search-plugin/public/typeahead/suggestions_component';
import type { Query, TimeRange } from '@kbn/es-query';
import type { SuggestionsAbstraction } from '@kbn/unified-search-plugin/public/typeahead/suggestions_component';
import { AlertConsumers } from '@kbn/rule-data-utils';
import { NO_INDEX_PATTERNS } from './constants';
import { SEARCH_BAR_PLACEHOLDER } from './translations';
import { AlertsSearchBarProps, QueryLanguageType } from './types';
import { useAlertDataView } from './hooks/use_alert_data_view';
import type { AlertsSearchBarProps, QueryLanguageType } from './types';
import { useAlertDataView } from './hooks/use_alert_data_view';
import { useRuleAADFields } from './hooks/use_rule_aad_fields';
import { useLoadRuleTypesQuery } from './hooks/use_load_rule_types_query';

Expand Down Expand Up @@ -48,7 +49,7 @@ export const AlertsSearchBar = ({
dataViewsService,
});
const { aadFields, loading: fieldsLoading } = useRuleAADFields({
ruleTypeId: ruleTypeId,
ruleTypeId,
http,
toasts,
});
Expand Down Expand Up @@ -97,29 +98,29 @@ export const AlertsSearchBar = ({
};

return unifiedSearchBar({
appName: appName,
disableQueryLanguageSwitcher: disableQueryLanguageSwitcher,
appName,
disableQueryLanguageSwitcher,
// @ts-expect-error - DataView fields prop and SearchBar indexPatterns props are overly broad
indexPatterns: loading || fieldsLoading ? NO_INDEX_PATTERNS : indexPatterns,
placeholder: placeholder,
placeholder,
query: { query: query ?? '', language: queryLanguage },
filters: filters,
filters,
dateRangeFrom: rangeFrom,
dateRangeTo: rangeTo,
displayStyle: "inPage",
showFilterBar: showFilterBar,
displayStyle: 'inPage',
showFilterBar,
onQuerySubmit: onSearchQuerySubmit,
onFiltersUpdated: onFiltersUpdated,
onRefresh: onRefresh,
showDatePicker: showDatePicker,
onFiltersUpdated,
onRefresh,
showDatePicker,
showQueryInput: true,
saveQueryMenuVisibility: "allowed_by_app_privilege",
showSubmitButton: showSubmitButton,
submitOnBlur: submitOnBlur,
saveQueryMenuVisibility: 'allowed_by_app_privilege',
showSubmitButton,
submitOnBlur,
onQueryChange: onSearchQueryChange,
suggestionsAbstraction: isSecurity ? undefined : SA_ALERTS,
});
}
};

// eslint-disable-next-line import/no-default-export
export { AlertsSearchBar as default };
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { i18n } from '@kbn/i18n';
Expand Down
5 changes: 3 additions & 2 deletions packages/kbn-alerts-ui-shared/src/alerts_search_bar/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { Filter } from '@kbn/es-query';
Expand Down
10 changes: 2 additions & 8 deletions x-pack/plugins/alerting/common/builtin_action_groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
* 2.0.
*/

import type {
RecoveredActionGroupId,
ActionGroup
} from '@kbn/alerting-types';
import type { RecoveredActionGroupId, ActionGroup } from '@kbn/alerting-types';
import { RecoveredActionGroup } from '@kbn/alerting-types';

export type ReservedActionGroups<RecoveryActionGroupId extends string> =
Expand All @@ -26,9 +23,6 @@ export function getBuiltinActionGroups<RecoveryActionGroupId extends string>(
return [customRecoveryGroup ?? RecoveredActionGroup];
}

export type {
RecoveredActionGroupId,
DefaultActionGroupId,
} from '@kbn/alerting-types';
export type { RecoveredActionGroupId, DefaultActionGroupId } from '@kbn/alerting-types';

export { RecoveredActionGroup } from '@kbn/alerting-types';
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export * from './rule_tags_aggregation';
export * from './iso_weekdays';
export * from './saved_objects/rules/mappings';
export * from './rule_circuit_breaker_error_message';
export * from './maintenance_window_scoped_query_error_message';

export type {
MaintenanceWindowModificationMetadata,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

const errorMessageIdentifier = 'invalid scoped query';

export const getScopedQueryErrorMessage = (errorMessage: string) => {
return `${errorMessageIdentifier} - ${errorMessage}`;
};

export const isScopedQueryError = (errorMessage: string) => {
return errorMessage.includes(errorMessageIdentifier);
};
6 changes: 1 addition & 5 deletions x-pack/plugins/alerting/common/rule_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@
* 2.0.
*/

export type {
RuleType,
ActionGroup,
ActionGroupIdsOf,
} from '@kbn/alerting-types';
export type { RuleType, ActionGroup, ActionGroupIdsOf } from '@kbn/alerting-types';
Loading

0 comments on commit 0553e5a

Please sign in to comment.