-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RAM] Add shareable rules list #132437
[RAM] Add shareable rules list #132437
Changes from 2 commits
68e38d8
354242a
2cd89be
d928ce8
17efc94
0a12470
35f0de5
d19b987
1c31509
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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 } 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( | ||
(prev: Record<string, number>, status: string) => | ||
({ | ||
...prev, | ||
[status]: 0, | ||
} as Record<string, number>), | ||
{} | ||
) | ||
); | ||
|
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should memoized the return if not it might have some re-rendering, what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont mind, although the references for all of the properties within the return object do not change. Since we end up destructuring in the component that uses it |
||
loadRuleAggregations: internalLoadRuleAggregations, | ||
rulesStatusesTotal, | ||
setRulesStatusesTotal, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* 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 } from 'react'; | ||
import { isEmpty } from 'lodash'; | ||
import { Rule, Pagination } from '../../types'; | ||
import { loadRules, LoadRulesProps } from '../lib/rule_api'; | ||
import { useKibana } from '../../common/lib/kibana'; | ||
|
||
interface RuleState { | ||
isLoading: boolean; | ||
data: Rule[]; | ||
totalItemCount: number; | ||
} | ||
|
||
type UseLoadRulesProps = Omit<LoadRulesProps, 'http'> & { | ||
hasAnyAuthorizedRuleType: boolean; | ||
onPage: (pagination: Pagination) => void; | ||
onError: (message: string) => void; | ||
}; | ||
|
||
export function useLoadRules({ | ||
page, | ||
searchText, | ||
typesFilter, | ||
actionTypesFilter, | ||
ruleExecutionStatusesFilter, | ||
ruleStatusesFilter, | ||
tagsFilter, | ||
sort, | ||
hasAnyAuthorizedRuleType, | ||
onPage, | ||
onError, | ||
}: UseLoadRulesProps) { | ||
const { http } = useKibana().services; | ||
|
||
const [rulesState, setRulesState] = useState<RuleState>({ | ||
isLoading: false, | ||
data: [], | ||
totalItemCount: 0, | ||
}); | ||
|
||
const [noData, setNoData] = useState<boolean>(true); | ||
const [initialLoad, setInitialLoad] = useState<boolean>(true); | ||
XavierM marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const internalLoadRules = useCallback(async () => { | ||
if (!hasAnyAuthorizedRuleType) { | ||
return; | ||
} | ||
setRulesState((prevRuleState) => ({ ...prevRuleState, isLoading: true })); | ||
try { | ||
const rulesResponse = await loadRules({ | ||
http, | ||
page, | ||
searchText, | ||
typesFilter, | ||
actionTypesFilter, | ||
ruleExecutionStatusesFilter, | ||
ruleStatusesFilter, | ||
tagsFilter, | ||
sort, | ||
}); | ||
setRulesState({ | ||
isLoading: false, | ||
data: rulesResponse.data, | ||
totalItemCount: rulesResponse.total, | ||
}); | ||
|
||
if (!rulesResponse.data?.length && page.index > 0) { | ||
XavierM marked this conversation as resolved.
Show resolved
Hide resolved
|
||
onPage({ ...page, index: 0 }); | ||
} | ||
|
||
const isFilterApplied = !( | ||
isEmpty(searchText) && | ||
isEmpty(typesFilter) && | ||
isEmpty(actionTypesFilter) && | ||
isEmpty(ruleExecutionStatusesFilter) && | ||
isEmpty(ruleStatusesFilter) && | ||
isEmpty(tagsFilter) | ||
); | ||
|
||
setNoData(rulesResponse.data.length === 0 && !isFilterApplied); | ||
} catch (e) { | ||
onError( | ||
i18n.translate('xpack.triggersActionsUI.sections.rulesList.unableToLoadRulesMessage', { | ||
defaultMessage: 'Unable to load rules', | ||
}) | ||
); | ||
setRulesState((prevRuleState) => ({ ...prevRuleState, isLoading: false })); | ||
} | ||
setInitialLoad(false); | ||
}, [ | ||
http, | ||
page, | ||
searchText, | ||
typesFilter, | ||
actionTypesFilter, | ||
ruleExecutionStatusesFilter, | ||
ruleStatusesFilter, | ||
tagsFilter, | ||
sort, | ||
hasAnyAuthorizedRuleType, | ||
setRulesState, | ||
setNoData, | ||
setInitialLoad, | ||
onPage, | ||
onError, | ||
]); | ||
|
||
return { | ||
XavierM marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rulesState, | ||
setRulesState, | ||
loadRules: internalLoadRules, | ||
noData, | ||
initialLoad, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* 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 } from 'react'; | ||
import { loadRuleTags } from '../lib/rule_api'; | ||
import { useKibana } from '../../common/lib/kibana'; | ||
|
||
interface UseLoadTagsProps { | ||
onError: (message: string) => void; | ||
} | ||
|
||
export function useLoadTags(props: UseLoadTagsProps) { | ||
const { onError } = props; | ||
const { http } = useKibana().services; | ||
const [tags, setTags] = useState<string[]>([]); | ||
|
||
const internalLoadTags = useCallback(async () => { | ||
try { | ||
const ruleTagsAggs = await loadRuleTags({ http }); | ||
if (ruleTagsAggs?.ruleTags) { | ||
setTags(ruleTagsAggs.ruleTags); | ||
} | ||
} catch (e) { | ||
onError( | ||
i18n.translate('xpack.triggersActionsUI.sections.rulesList.unableToLoadRuleTags', { | ||
defaultMessage: 'Unable to load rule tags', | ||
}) | ||
); | ||
} | ||
}, [http, setTags, onError]); | ||
|
||
return { | ||
loadTags: internalLoadTags, | ||
tags, | ||
setTags, | ||
}; | ||
} |
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. | ||
*/ | ||
import React from 'react'; | ||
import { getRulesListLazy } from '../../../common/get_rules_list'; | ||
|
||
const style = { | ||
flex: 1, | ||
}; | ||
|
||
export const RulesListSandbox = () => { | ||
return <div style={style}>{getRulesListLazy()}</div>; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit -> I think to avoid the cast