-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…69981) * [Metrics UI] Prefill alerts from the global dropdown (#68967) Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> * Fix uncaught typecheck merge conflict Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
b408a9f
commit 35fde53
Showing
18 changed files
with
539 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
x-pack/plugins/infra/public/alerting/inventory/hooks/use_inventory_alert_prefill.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { useState } from 'react'; | ||
import { SnapshotMetricInput } from '../../../../common/http_api/snapshot_api'; | ||
import { InventoryItemType } from '../../../../common/inventory_models/types'; | ||
|
||
export const useInventoryAlertPrefill = () => { | ||
const [nodeType, setNodeType] = useState<InventoryItemType>('host'); | ||
const [filterQuery, setFilterQuery] = useState<string | undefined>(); | ||
const [metric, setMetric] = useState<SnapshotMetricInput>({ type: 'cpu' }); | ||
|
||
return { | ||
nodeType, | ||
filterQuery, | ||
metric, | ||
setNodeType, | ||
setFilterQuery, | ||
setMetric, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
x-pack/plugins/infra/public/alerting/metric_threshold/components/expression.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { mountWithIntl, nextTick } from 'test_utils/enzyme_helpers'; | ||
import { actionTypeRegistryMock } from '../../../../../triggers_actions_ui/public/application/action_type_registry.mock'; | ||
import { alertTypeRegistryMock } from '../../../../../triggers_actions_ui/public/application/alert_type_registry.mock'; | ||
import { coreMock } from '../../../../../../../src/core/public/mocks'; | ||
import { AlertsContextValue } from '../../../../../triggers_actions_ui/public/application/context/alerts_context'; | ||
import { AlertContextMeta } from '../types'; | ||
import { MetricsExplorerMetric } from '../../../../common/http_api/metrics_explorer'; | ||
import React from 'react'; | ||
import { Expressions } from './expression'; | ||
import { act } from 'react-dom/test-utils'; | ||
// eslint-disable-next-line @kbn/eslint/no-restricted-paths | ||
import { Comparator } from '../../../../server/lib/alerting/metric_threshold/types'; | ||
|
||
jest.mock('../../../containers/source/use_source_via_http', () => ({ | ||
useSourceViaHttp: () => ({ | ||
source: { id: 'default' }, | ||
createDerivedIndexPattern: () => ({ fields: [], title: 'metricbeat-*' }), | ||
}), | ||
})); | ||
|
||
describe('Expression', () => { | ||
async function setup(currentOptions: { | ||
metrics?: MetricsExplorerMetric[]; | ||
filterQuery?: string; | ||
groupBy?: string; | ||
}) { | ||
const alertParams = { | ||
criteria: [], | ||
groupBy: undefined, | ||
filterQueryText: '', | ||
}; | ||
|
||
const mocks = coreMock.createSetup(); | ||
const startMocks = coreMock.createStart(); | ||
const [ | ||
{ | ||
application: { capabilities }, | ||
}, | ||
] = await mocks.getStartServices(); | ||
|
||
const context: AlertsContextValue<AlertContextMeta> = { | ||
http: mocks.http, | ||
toastNotifications: mocks.notifications.toasts, | ||
actionTypeRegistry: actionTypeRegistryMock.create() as any, | ||
alertTypeRegistry: alertTypeRegistryMock.create() as any, | ||
docLinks: startMocks.docLinks, | ||
capabilities: { | ||
...capabilities, | ||
actions: { | ||
delete: true, | ||
save: true, | ||
show: true, | ||
}, | ||
}, | ||
metadata: { | ||
currentOptions, | ||
}, | ||
}; | ||
|
||
const wrapper = mountWithIntl( | ||
<Expressions | ||
alertsContext={context} | ||
alertInterval="1m" | ||
alertParams={alertParams} | ||
errors={[]} | ||
setAlertParams={(key, value) => Reflect.set(alertParams, key, value)} | ||
setAlertProperty={() => {}} | ||
/> | ||
); | ||
|
||
const update = async () => | ||
await act(async () => { | ||
await nextTick(); | ||
wrapper.update(); | ||
}); | ||
|
||
await update(); | ||
|
||
return { wrapper, update, alertParams }; | ||
} | ||
|
||
it('should prefill the alert using the context metadata', async () => { | ||
const currentOptions = { | ||
groupBy: 'host.hostname', | ||
filterQuery: 'foo', | ||
metrics: [ | ||
{ aggregation: 'avg', field: 'system.load.1' }, | ||
{ aggregation: 'cardinality', field: 'system.cpu.user.pct' }, | ||
] as MetricsExplorerMetric[], | ||
}; | ||
const { alertParams } = await setup(currentOptions); | ||
expect(alertParams.groupBy).toBe('host.hostname'); | ||
expect(alertParams.filterQueryText).toBe('foo'); | ||
expect(alertParams.criteria).toEqual([ | ||
{ | ||
metric: 'system.load.1', | ||
comparator: Comparator.GT, | ||
threshold: [], | ||
timeSize: 1, | ||
timeUnit: 'm', | ||
aggType: 'avg', | ||
}, | ||
{ | ||
metric: 'system.cpu.user.pct', | ||
comparator: Comparator.GT, | ||
threshold: [], | ||
timeSize: 1, | ||
timeUnit: 'm', | ||
aggType: 'cardinality', | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...lugins/infra/public/alerting/metric_threshold/hooks/use_metric_threshold_alert_prefill.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { isEqual } from 'lodash'; | ||
import { useState } from 'react'; | ||
import { MetricsExplorerMetric } from '../../../../common/http_api/metrics_explorer'; | ||
|
||
interface MetricThresholdPrefillOptions { | ||
groupBy: string | string[] | undefined; | ||
filterQuery: string | undefined; | ||
metrics: MetricsExplorerMetric[]; | ||
} | ||
|
||
export const useMetricThresholdAlertPrefill = () => { | ||
const [prefillOptionsState, setPrefillOptionsState] = useState<MetricThresholdPrefillOptions>({ | ||
groupBy: undefined, | ||
filterQuery: undefined, | ||
metrics: [], | ||
}); | ||
|
||
const { groupBy, filterQuery, metrics } = prefillOptionsState; | ||
|
||
return { | ||
groupBy, | ||
filterQuery, | ||
metrics, | ||
setPrefillOptions(newState: MetricThresholdPrefillOptions) { | ||
if (!isEqual(newState, prefillOptionsState)) setPrefillOptionsState(newState); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import createContainer from 'constate'; | ||
import { useMetricThresholdAlertPrefill } from './metric_threshold/hooks/use_metric_threshold_alert_prefill'; | ||
import { useInventoryAlertPrefill } from './inventory/hooks/use_inventory_alert_prefill'; | ||
|
||
const useAlertPrefill = () => { | ||
const metricThresholdPrefill = useMetricThresholdAlertPrefill(); | ||
const inventoryPrefill = useInventoryAlertPrefill(); | ||
|
||
return { metricThresholdPrefill, inventoryPrefill }; | ||
}; | ||
|
||
export const [AlertPrefillProvider, useAlertPrefillContext] = createContainer(useAlertPrefill); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.