forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RAM] Add shareable rules list (elastic#132437)
* Shareable rules list * Hide snooze panel in rules list * Address comments and added tests * Fix tests * Fix tests * Fix lint * Address design comments and fix tests Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
383239e
commit fbaf058
Showing
33 changed files
with
2,624 additions
and
1,067 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
111 changes: 111 additions & 0 deletions
111
...k/plugins/triggers_actions_ui/public/application/hooks/use_load_rule_aggregations.test.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,111 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useLoadRuleAggregations } from './use_load_rule_aggregations'; | ||
import { RuleStatus } from '../../types'; | ||
|
||
const MOCK_TAGS = ['a', 'b', 'c']; | ||
|
||
const MOCK_AGGS = { | ||
ruleEnabledStatus: { enabled: 2, disabled: 0 }, | ||
ruleExecutionStatus: { ok: 1, active: 2, error: 3, pending: 4, unknown: 5, warning: 6 }, | ||
ruleMutedStatus: { muted: 0, unmuted: 2 }, | ||
ruleTags: MOCK_TAGS, | ||
}; | ||
|
||
jest.mock('../lib/rule_api', () => ({ | ||
loadRuleAggregations: jest.fn(), | ||
})); | ||
|
||
const { loadRuleAggregations } = jest.requireMock('../lib/rule_api'); | ||
|
||
const onError = jest.fn(); | ||
|
||
describe('useLoadRuleAggregations', () => { | ||
beforeEach(() => { | ||
loadRuleAggregations.mockResolvedValue(MOCK_AGGS); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call loadRuleAggregations API and handle result', async () => { | ||
const params = { | ||
searchText: '', | ||
typesFilter: [], | ||
actionTypesFilter: [], | ||
ruleExecutionStatusesFilter: [], | ||
ruleStatusesFilter: [], | ||
tagsFilter: [], | ||
}; | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => | ||
useLoadRuleAggregations({ | ||
...params, | ||
onError, | ||
}) | ||
); | ||
|
||
await act(async () => { | ||
result.current.loadRuleAggregations(); | ||
await waitForNextUpdate(); | ||
}); | ||
|
||
expect(loadRuleAggregations).toBeCalledWith(expect.objectContaining(params)); | ||
expect(result.current.rulesStatusesTotal).toEqual(MOCK_AGGS.ruleExecutionStatus); | ||
}); | ||
|
||
it('should call loadRuleAggregation API with params and handle result', async () => { | ||
const params = { | ||
searchText: 'test', | ||
typesFilter: ['type1', 'type2'], | ||
actionTypesFilter: ['action1', 'action2'], | ||
ruleExecutionStatusesFilter: ['status1', 'status2'], | ||
ruleStatusesFilter: ['enabled', 'snoozed'] as RuleStatus[], | ||
tagsFilter: ['tag1', 'tag2'], | ||
}; | ||
|
||
const { result, waitForNextUpdate } = renderHook(() => | ||
useLoadRuleAggregations({ | ||
...params, | ||
onError, | ||
}) | ||
); | ||
|
||
await act(async () => { | ||
result.current.loadRuleAggregations(); | ||
await waitForNextUpdate(); | ||
}); | ||
|
||
expect(loadRuleAggregations).toBeCalledWith(expect.objectContaining(params)); | ||
expect(result.current.rulesStatusesTotal).toEqual(MOCK_AGGS.ruleExecutionStatus); | ||
}); | ||
|
||
it('should call onError if API fails', async () => { | ||
loadRuleAggregations.mockRejectedValue(''); | ||
const params = { | ||
searchText: '', | ||
typesFilter: [], | ||
actionTypesFilter: [], | ||
ruleExecutionStatusesFilter: [], | ||
ruleStatusesFilter: [], | ||
tagsFilter: [], | ||
}; | ||
|
||
const { result } = renderHook(() => | ||
useLoadRuleAggregations({ | ||
...params, | ||
onError, | ||
}) | ||
); | ||
|
||
await act(async () => { | ||
result.current.loadRuleAggregations(); | ||
}); | ||
|
||
expect(onError).toBeCalled(); | ||
}); | ||
}); |
83 changes: 83 additions & 0 deletions
83
x-pack/plugins/triggers_actions_ui/public/application/hooks/use_load_rule_aggregations.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,83 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
import { useState, useCallback, useMemo } from 'react'; | ||
import { RuleExecutionStatusValues } from '@kbn/alerting-plugin/common'; | ||
import { loadRuleAggregations, LoadRuleAggregationsProps } from '../lib/rule_api'; | ||
import { useKibana } from '../../common/lib/kibana'; | ||
|
||
type UseLoadRuleAggregationsProps = Omit<LoadRuleAggregationsProps, 'http'> & { | ||
onError: (message: string) => void; | ||
}; | ||
|
||
export function useLoadRuleAggregations({ | ||
searchText, | ||
typesFilter, | ||
actionTypesFilter, | ||
ruleExecutionStatusesFilter, | ||
ruleStatusesFilter, | ||
tagsFilter, | ||
onError, | ||
}: UseLoadRuleAggregationsProps) { | ||
const { http } = useKibana().services; | ||
|
||
const [rulesStatusesTotal, setRulesStatusesTotal] = useState<Record<string, number>>( | ||
RuleExecutionStatusValues.reduce<Record<string, number>>( | ||
(prev: Record<string, number>, status: string) => ({ | ||
...prev, | ||
[status]: 0, | ||
}), | ||
{} | ||
) | ||
); | ||
|
||
const internalLoadRuleAggregations = useCallback(async () => { | ||
try { | ||
const rulesAggs = await loadRuleAggregations({ | ||
http, | ||
searchText, | ||
typesFilter, | ||
actionTypesFilter, | ||
ruleExecutionStatusesFilter, | ||
ruleStatusesFilter, | ||
tagsFilter, | ||
}); | ||
if (rulesAggs?.ruleExecutionStatus) { | ||
setRulesStatusesTotal(rulesAggs.ruleExecutionStatus); | ||
} | ||
} catch (e) { | ||
onError( | ||
i18n.translate( | ||
'xpack.triggersActionsUI.sections.rulesList.unableToLoadRuleStatusInfoMessage', | ||
{ | ||
defaultMessage: 'Unable to load rule status info', | ||
} | ||
) | ||
); | ||
} | ||
}, [ | ||
http, | ||
searchText, | ||
typesFilter, | ||
actionTypesFilter, | ||
ruleExecutionStatusesFilter, | ||
ruleStatusesFilter, | ||
tagsFilter, | ||
onError, | ||
setRulesStatusesTotal, | ||
]); | ||
|
||
return useMemo( | ||
() => ({ | ||
loadRuleAggregations: internalLoadRuleAggregations, | ||
rulesStatusesTotal, | ||
setRulesStatusesTotal, | ||
}), | ||
[internalLoadRuleAggregations, rulesStatusesTotal, setRulesStatusesTotal] | ||
); | ||
} |
Oops, something went wrong.