Skip to content

Commit

Permalink
[Metrics UI] Fix passing space ID into anomaly alerts (#91222)
Browse files Browse the repository at this point in the history
* [Metrics UI] Fix passing space ID into anomaly alerts

* Fix jest
  • Loading branch information
Zacqary authored Feb 12, 2021
1 parent 7a55267 commit 7537326
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/infra/common/alerting/metrics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface MetricAnomalyParams {
metric: rt.TypeOf<typeof metricAnomalyMetricRT>;
alertInterval?: string;
sourceId?: string;
spaceId?: string;
threshold: Exclude<ANOMALY_THRESHOLD, ANOMALY_THRESHOLD.LOW>;
influencerFilter: rt.TypeOf<typeof metricAnomalyInfluencerFilterRT> | undefined;
}
Expand Down Expand Up @@ -112,6 +113,7 @@ const metricAnomalyAlertPreviewRequestParamsRT = rt.intersection([
metric: metricAnomalyMetricRT,
threshold: rt.number,
alertType: rt.literal(METRIC_ANOMALY_ALERT_TYPE_ID),
spaceId: rt.string,
}),
rt.partial({
influencerFilter: metricAnomalyInfluencerFilterRT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ jest.mock('../../../hooks/use_kibana', () => ({
}),
}));

jest.mock('../../../hooks/use_kibana_space', () => ({
useActiveKibanaSpace: () => ({
space: { id: 'default' },
}),
}));

jest.mock('../../../containers/ml/infra_ml_capabilities', () => ({
useInfraMLCapabilities: () => ({
isLoading: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ import { ANOMALY_THRESHOLD } from '../../../../common/infra_ml';
import { validateMetricAnomaly } from './validation';
import { InfluencerFilter } from './influencer_filter';
import { useKibanaContextForPlugin } from '../../../hooks/use_kibana';
import { useActiveKibanaSpace } from '../../../hooks/use_kibana_space';

export interface AlertContextMeta {
metric?: InfraWaffleMapOptions['metric'];
nodeType?: InventoryItemType;
}

type AlertParams = AlertTypeParams &
MetricAnomalyParams & { sourceId: string; hasInfraMLCapabilities: boolean };
MetricAnomalyParams & { sourceId: string; spaceId: string; hasInfraMLCapabilities: boolean };

type Props = Omit<
AlertTypeParamsExpressionProps<AlertParams, AlertContextMeta>,
Expand All @@ -62,6 +63,8 @@ export const defaultExpression = {
export const Expression: React.FC<Props> = (props) => {
const { hasInfraMLCapabilities, isLoading: isLoadingMLCapabilities } = useInfraMLCapabilities();
const { http, notifications } = useKibanaContextForPlugin().services;
const { space } = useActiveKibanaSpace();

const {
setAlertParams,
alertParams,
Expand Down Expand Up @@ -176,7 +179,11 @@ export const Expression: React.FC<Props> = (props) => {
if (!alertParams.sourceId) {
setAlertParams('sourceId', source?.id || 'default');
}
}, [metadata, derivedIndexPattern, defaultExpression, source]); // eslint-disable-line react-hooks/exhaustive-deps

if (!alertParams.spaceId) {
setAlertParams('spaceId', space?.id || 'default');
}
}, [metadata, derivedIndexPattern, defaultExpression, source, space]); // eslint-disable-line react-hooks/exhaustive-deps

if (isLoadingMLCapabilities) return <EuiLoadingContent lines={10} />;
if (!hasInfraMLCapabilities) return <SubscriptionSplashContent />;
Expand Down Expand Up @@ -263,6 +270,7 @@ export const Expression: React.FC<Props> = (props) => {
'threshold',
'nodeType',
'sourceId',
'spaceId',
'influencerFilter'
)}
validate={validateMetricAnomaly}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const createMetricAnomalyExecutor = (libs: InfraBackendLibs, ml?: MlPlugi
alertInterval,
influencerFilter,
sourceId,
spaceId,
nodeType,
threshold,
} = params as MetricAnomalyParams;
Expand All @@ -67,7 +68,7 @@ export const createMetricAnomalyExecutor = (libs: InfraBackendLibs, ml?: MlPlugi

const { data } = await evaluateCondition({
sourceId: sourceId ?? 'default',
spaceId: 'default',
spaceId: spaceId ?? 'default',
mlSystem,
mlAnomalyDetectors,
startTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const registerMetricAnomalyAlertType = (
schema.string({ validate: validateIsStringElasticsearchJSONFilter })
),
sourceId: schema.string(),
spaceId: schema.string(),
},
{ unknowns: 'allow' }
),
Expand Down

0 comments on commit 7537326

Please sign in to comment.