-
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.
- Loading branch information
Showing
27 changed files
with
1,653 additions
and
994 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
81 changes: 81 additions & 0 deletions
81
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,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 { | ||
loadRuleAggregations: internalLoadRuleAggregations, | ||
rulesStatusesTotal, | ||
setRulesStatusesTotal, | ||
}; | ||
} |
121 changes: 121 additions & 0 deletions
121
x-pack/plugins/triggers_actions_ui/public/application/hooks/use_load_rules.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,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); | ||
|
||
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) { | ||
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 { | ||
rulesState, | ||
setRulesState, | ||
loadRules: internalLoadRules, | ||
noData, | ||
initialLoad, | ||
}; | ||
} |
42 changes: 42 additions & 0 deletions
42
x-pack/plugins/triggers_actions_ui/public/application/hooks/use_load_tags.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,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, | ||
}; | ||
} |
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
16 changes: 16 additions & 0 deletions
16
...ctions_ui/public/application/internal/shareable_components_sandbox/rules_list_sandbox.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,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>; | ||
}; |
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
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
Oops, something went wrong.