-
Notifications
You must be signed in to change notification settings - Fork 22
feat(triggers): Add Mbean selector to Trigger Creation Form #2192
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
base: main
Are you sure you want to change the base?
Changes from all commits
78073e4
4864643
442ab3d
4b5448a
a4902d4
4c0edca
ebd83cf
638c0a7
f6c4404
c73ce87
c210b28
1639368
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 |
|---|---|---|
|
|
@@ -13,13 +13,23 @@ | |
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import { EventTemplateIdentifier } from '@app/CreateRecording/types'; | ||
| import { ColumnConfig, DiagnosticsTable } from '@app/Diagnostics/DiagnosticsTable'; | ||
| import { DeleteWarningModal } from '@app/Modal/DeleteWarningModal'; | ||
| import { DeleteOrDisableWarningType } from '@app/Modal/types'; | ||
| import { CEL_SPEC_HREF } from '@app/Rules/utils'; | ||
| import { SelectTemplateSelectorForm } from '@app/Shared/Components/SelectTemplateSelectorForm'; | ||
| import { LoadingProps } from '@app/Shared/Components/types'; | ||
|
|
||
| import { NotificationCategory, NullableTarget, SmartTrigger } from '@app/Shared/Services/api.types'; | ||
| import { | ||
| EventTemplate, | ||
| MBeanAttributeInfo, | ||
| MbeanAttributeMap, | ||
| NotificationCategory, | ||
| NullableTarget, | ||
| SmartTrigger, | ||
| Target, | ||
| } from '@app/Shared/Services/api.types'; | ||
| import { ServiceContext } from '@app/Shared/Services/Services'; | ||
| import { useSubscriptions } from '@app/utils/hooks/useSubscriptions'; | ||
| import { TableColumn, hashCode, portalRoot, sortResources } from '@app/utils/utils'; | ||
|
|
@@ -52,6 +62,8 @@ import { | |
| FormHelperText, | ||
| HelperText, | ||
| HelperTextItem, | ||
| FormSelect, | ||
| FormSelectOption, | ||
| } from '@patternfly/react-core'; | ||
| import { Modal, ModalVariant } from '@patternfly/react-core/deprecated'; | ||
| import { EllipsisVIcon, SearchIcon } from '@patternfly/react-icons'; | ||
|
|
@@ -61,6 +73,7 @@ import _ from 'lodash'; | |
| import * as React from 'react'; | ||
| import { Trans } from 'react-i18next'; | ||
| import { first, forkJoin, Observable } from 'rxjs'; | ||
| import { SmartTriggersFormData } from './types'; | ||
|
|
||
| export const tableColumns: TableColumn[] = [ | ||
| { | ||
|
|
@@ -633,16 +646,70 @@ export interface CreateSmartTriggersModalProps { | |
| onAccept: (s: string) => void; | ||
| } | ||
|
|
||
| export interface MbeanSelectorOption { | ||
|
Member
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. same here |
||
| value: string; | ||
| label: string; | ||
| disabled: boolean; | ||
| } | ||
|
|
||
| export const CreateSmartTriggersModal: React.FC<CreateSmartTriggersModalProps> = ({ onClose, ...props }) => { | ||
| const submitRef = React.useRef<HTMLDivElement>(null); // Use ref to refer to submit trigger div | ||
| const abortRef = React.useRef<HTMLDivElement>(null); // Use ref to refer to abort trigger div | ||
| const [templates, setTemplates] = React.useState<EventTemplate[]>([]); | ||
| const [mbeans, setMbeans] = React.useState<MbeanAttributeMap[]>([]); | ||
| const [mbeanSelectValue, setMbeanSelectValue] = React.useState<string>(); | ||
| const addSubscription = useSubscriptions(); | ||
| const context = React.useContext(ServiceContext); | ||
| const [formData, setFormData] = React.useState<SmartTriggersFormData>({ | ||
| name: '', | ||
| nameValid: ValidatedOptions.default, | ||
| enabled: true, | ||
| expression: '', // Use this for displaying Match Expression input | ||
| expressionValid: ValidatedOptions.default, | ||
| }); | ||
|
|
||
| // FIXME Triggers currently rely on MbeanMetrics. We can query all | ||
| // registered mbeans/attributes but we need to filter the supported ones. | ||
| const supportedTriggerAttributes: string[] = React.useMemo( | ||
| () => [ | ||
| 'ThreadCount', | ||
| 'DaemonThreadCount', | ||
| 'Arch', | ||
| 'AvailableProcessors', | ||
| 'Version', | ||
| 'SystemCpuLoad', | ||
| 'SystemLoadAverage', | ||
| 'ProcessCpuLoad', | ||
| 'TotalPhysicalMemorySize', | ||
| 'FreePhyiscalMemorySize', | ||
|
Member
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. typo? Phyiscal -> Physical |
||
| 'TotalSwapSpaceSize', | ||
| 'HeapMemoryUsage', | ||
| 'HeapMemoryUsagePercent', | ||
| 'BootClassPath', | ||
| 'ClassPath', | ||
| 'InputArguments', | ||
| 'LibraryPath', | ||
| 'ManagementSpecVersion', | ||
| 'SpecName', | ||
| 'SpecVendor', | ||
| 'StartTime', | ||
| 'SystemProperties', | ||
| 'Uptime', | ||
| 'VmName', | ||
| 'VmVendor', | ||
| 'VmVersion', | ||
| 'BootClassPathSupported', | ||
| ], | ||
| [], | ||
| ); | ||
|
|
||
| const [uploading, setUploading] = React.useState(false); | ||
|
|
||
| const expressionRegex = RegExp('\\[(.*(&&)*|(\\|\\|)*)\\]~([\\w\\-]+)(?:\\.jfc)?'); | ||
| const expressionRegex = RegExp('\\[(.*(&&)*|(\\|\\|)*)\\]'); | ||
|
|
||
| const [expressionInput, setExpressionInput] = React.useState(''); | ||
| const [expressionValid, setExpressionValid] = React.useState(ValidatedOptions.default); | ||
| const [templateValid, setTemplateValid] = React.useState(ValidatedOptions.default); | ||
|
|
||
| const reset = React.useCallback(() => { | ||
| setUploading(false); | ||
|
|
@@ -660,11 +727,11 @@ export const CreateSmartTriggersModal: React.FC<CreateSmartTriggersModalProps> = | |
|
|
||
| const handleSubmit = React.useCallback(() => { | ||
| submitRef.current && submitRef.current.click(); | ||
| props.onAccept(expressionInput); | ||
| props.onAccept(expressionInput + '~' + formData.template?.name); | ||
| setUploading(false); | ||
| onClose(); | ||
| setExpressionInput(''); | ||
| }, [props, onClose, expressionInput, submitRef]); | ||
| }, [props, onClose, expressionInput, submitRef, formData.template?.name]); | ||
|
|
||
| const submitButtonLoadingProps = React.useMemo( | ||
| () => | ||
|
|
@@ -676,6 +743,66 @@ export const CreateSmartTriggersModal: React.FC<CreateSmartTriggersModalProps> = | |
| [uploading], | ||
| ); | ||
|
|
||
| const refreshFormOptions = React.useCallback( | ||
| (target: Target) => { | ||
| if (!target) { | ||
| return; | ||
| } | ||
| addSubscription( | ||
| forkJoin({ | ||
| templates: context.api.getTargetEventTemplates(target), | ||
| mbeans: context.api.getTargetMbeans(target), | ||
| }).subscribe({ | ||
| next: ({ templates, mbeans }) => { | ||
| setTemplates(templates); | ||
| setMbeans(mbeans); | ||
| }, | ||
| }), | ||
| ); | ||
| }, | ||
| [addSubscription, context.api, setTemplates], | ||
| ); | ||
|
|
||
| React.useEffect(() => { | ||
| addSubscription(context.target.target().subscribe(refreshFormOptions)); | ||
| }, [addSubscription, context.target, refreshFormOptions]); | ||
|
|
||
| const selectedSpecifier = React.useMemo(() => { | ||
| const { template } = formData; | ||
| if (template && template.name && template.type) { | ||
| return `${template.name},${template.type}`; | ||
| } | ||
| return ''; | ||
| }, [formData]); | ||
|
|
||
| const MbeanOptions = React.useMemo(() => { | ||
| var attributes: MbeanSelectorOption[] = []; | ||
| mbeans.forEach((m: MbeanAttributeMap) => { | ||
| m.attributes.forEach((i: MBeanAttributeInfo) => { | ||
|
Member
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. If
Contributor
Author
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. You're correct that hardcoding it would achieve the same thing, I wanted to lay the groundwork here for generic mbean attributes down the line but for now we can get rid of the query if you'd prefer and just hardcode the list options.
Member
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. If it's currently functionally equivalent then I think for now let's just go with the hardcoded list so that we can get that UI enhancement in, and work on the broader-scoped change that affects multiple components when we're further out from a dev freeze. Maybe the hardcoded version of this can go directly into #2155 and this PR can build on top of that do to the enhanced filtering based on actual live data pulled from the Agent.
Contributor
Author
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. Sounds good, I'll add it to that PR |
||
| if (supportedTriggerAttributes.includes(i.name)) { | ||
| attributes.push({ | ||
| value: i.name, | ||
| label: `${i.parentBean} - ${i.name} (${i.type})`, | ||
| disabled: false, | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
| return attributes; | ||
| }, [mbeans, supportedTriggerAttributes]); | ||
|
|
||
| const handleTemplateChange = React.useCallback( | ||
| (template: EventTemplateIdentifier) => { | ||
| setFormData((old) => ({ ...old, template })); | ||
| setTemplateValid(ValidatedOptions.success); | ||
| }, | ||
| [setFormData], | ||
| ); | ||
|
|
||
| const onMbeanChange = (_event: React.FormEvent<HTMLSelectElement>, value: string) => { | ||
| setMbeanSelectValue(value); | ||
| }; | ||
|
|
||
| return ( | ||
| <Modal | ||
| isOpen={props.isOpen} | ||
|
|
@@ -686,6 +813,26 @@ export const CreateSmartTriggersModal: React.FC<CreateSmartTriggersModalProps> = | |
| description="Create a customized Smart Trigger. This is a specialized tool available in the Cryostat Agent that listens for a condition to be met for a specified Mbean, after which a recording will be started with the specified template. This is only available for targets using the Cryostat Agent." | ||
| > | ||
| <Form> | ||
| <FormHelperText> | ||
| <HelperText> | ||
| <HelperTextItem>{t('Triggers.AVAILABLE_MBEANS')}</HelperTextItem> | ||
| </HelperText> | ||
| </FormHelperText> | ||
| <FormSelect value={mbeanSelectValue} onChange={onMbeanChange} aria-label="MBean Input" ouiaId="BasicFormSelect"> | ||
| <FormSelectOption | ||
| isDisabled={true} | ||
| key={-1} | ||
| value={''} | ||
| label={'Select an MBean Attribute'} | ||
| isPlaceholder={true} | ||
| /> | ||
| {MbeanOptions.map((option, index) => ( | ||
| <FormSelectOption isDisabled={option.disabled} key={index} value={option.value} label={option.label} /> | ||
| ))} | ||
| </FormSelect> | ||
| <HelperText> | ||
| <HelperTextItem>{`Selected MBean attribute has the name: ${mbeanSelectValue}, Use this to build your expression.`}</HelperTextItem> | ||
| </HelperText> | ||
| <FormGroup label="Smart Trigger definition" isRequired fieldId="definition"> | ||
| <TextArea | ||
| value={expressionInput} | ||
|
|
@@ -722,13 +869,25 @@ export const CreateSmartTriggersModal: React.FC<CreateSmartTriggersModalProps> = | |
| </HelperTextItem> | ||
| </HelperText> | ||
| </FormHelperText> | ||
| <FormHelperText> | ||
| <HelperText> | ||
| <HelperTextItem>{t('Triggers.TEMPLATE_SELECT')}</HelperTextItem> | ||
| </HelperText> | ||
| </FormHelperText> | ||
| <SelectTemplateSelectorForm | ||
| selected={selectedSpecifier} | ||
| templates={templates} | ||
| validated={templateValid} | ||
| disabled={uploading} | ||
| onSelect={handleTemplateChange} | ||
| /> | ||
| <ActionGroup> | ||
| <> | ||
| <Button | ||
| aria-label="submit-button" | ||
| variant="primary" | ||
| onClick={handleSubmit} | ||
| isDisabled={expressionValid != ValidatedOptions.success} | ||
| isDisabled={expressionValid != ValidatedOptions.success || templateValid != ValidatedOptions.success} | ||
| {...submitButtonLoadingProps} | ||
| > | ||
| {uploading ? 'Submitting' : 'Submit'} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright The Cryostat Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import { EventTemplate } from '@app/Shared/Services/api.types'; | ||
| import { ValidatedOptions } from '@patternfly/react-core'; | ||
|
|
||
| export type EventTemplateIdentifier = Pick<EventTemplate, 'name' | 'type'>; | ||
|
|
||
| interface _FormBaseData { | ||
| name: string; | ||
| enabled: boolean; | ||
| expression: string; | ||
| template?: EventTemplateIdentifier; | ||
| } | ||
|
|
||
| interface _FormValidationData { | ||
| nameValid: ValidatedOptions; | ||
| expressionValid: ValidatedOptions; | ||
| } | ||
|
|
||
| export type SmartTriggersFormData = _FormBaseData & _FormValidationData; |
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:
MBean, capital B