|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +import { EuiText, EuiSelect, EuiExpression } from '@elastic/eui'; |
| 7 | +import { i18n } from '@kbn/i18n'; |
| 8 | +import React from 'react'; |
| 9 | +import { ALERT_TYPES_CONFIG } from '../../../../common/alert_types'; |
| 10 | +import { ALL_OPTION, useEnvironments } from '../../../hooks/useEnvironments'; |
| 11 | +import { useServiceTransactionTypes } from '../../../hooks/useServiceTransactionTypes'; |
| 12 | +import { useUrlParams } from '../../../hooks/useUrlParams'; |
| 13 | +import { ServiceAlertTrigger } from '../ServiceAlertTrigger'; |
| 14 | +import { PopoverExpression } from '../ServiceAlertTrigger/PopoverExpression'; |
| 15 | +import { SelectAnomalySeverity } from './SelectAnomalySeverity'; |
| 16 | + |
| 17 | +interface Params { |
| 18 | + windowSize: number; |
| 19 | + windowUnit: string; |
| 20 | + serviceName: string; |
| 21 | + transactionType: string; |
| 22 | + environment: string; |
| 23 | + anomalyScore: 0 | 25 | 50 | 75; |
| 24 | +} |
| 25 | + |
| 26 | +interface Props { |
| 27 | + alertParams: Params; |
| 28 | + setAlertParams: (key: string, value: any) => void; |
| 29 | + setAlertProperty: (key: string, value: any) => void; |
| 30 | +} |
| 31 | + |
| 32 | +export function TransactionDurationAnomalyAlertTrigger(props: Props) { |
| 33 | + const { setAlertParams, alertParams, setAlertProperty } = props; |
| 34 | + const { urlParams } = useUrlParams(); |
| 35 | + const transactionTypes = useServiceTransactionTypes(urlParams); |
| 36 | + const { serviceName, start, end } = urlParams; |
| 37 | + const { environmentOptions } = useEnvironments({ serviceName, start, end }); |
| 38 | + |
| 39 | + if (!transactionTypes.length || !serviceName) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + const defaults: Params = { |
| 44 | + windowSize: 15, |
| 45 | + windowUnit: 'm', |
| 46 | + transactionType: transactionTypes[0], |
| 47 | + serviceName, |
| 48 | + environment: urlParams.environment || ALL_OPTION.value, |
| 49 | + anomalyScore: 75, |
| 50 | + }; |
| 51 | + |
| 52 | + const params = { |
| 53 | + ...defaults, |
| 54 | + ...alertParams, |
| 55 | + }; |
| 56 | + |
| 57 | + const fields = [ |
| 58 | + <EuiExpression |
| 59 | + description={i18n.translate( |
| 60 | + 'xpack.apm.transactionDurationAnomalyAlertTrigger.service', |
| 61 | + { |
| 62 | + defaultMessage: 'Service', |
| 63 | + } |
| 64 | + )} |
| 65 | + value={ |
| 66 | + <EuiText className="eui-displayInlineBlock"> |
| 67 | + <h5>{serviceName}</h5> |
| 68 | + </EuiText> |
| 69 | + } |
| 70 | + />, |
| 71 | + <PopoverExpression |
| 72 | + value={ |
| 73 | + params.environment === ALL_OPTION.value |
| 74 | + ? ALL_OPTION.text |
| 75 | + : params.environment |
| 76 | + } |
| 77 | + title={i18n.translate( |
| 78 | + 'xpack.apm.transactionDurationAnomalyAlertTrigger.environment', |
| 79 | + { |
| 80 | + defaultMessage: 'Environment', |
| 81 | + } |
| 82 | + )} |
| 83 | + > |
| 84 | + <EuiSelect |
| 85 | + value={params.environment} |
| 86 | + options={environmentOptions} |
| 87 | + onChange={(e) => |
| 88 | + setAlertParams('environment', e.target.value as Params['environment']) |
| 89 | + } |
| 90 | + compressed |
| 91 | + /> |
| 92 | + </PopoverExpression>, |
| 93 | + <PopoverExpression |
| 94 | + value={params.transactionType} |
| 95 | + title={i18n.translate( |
| 96 | + 'xpack.apm.transactionDurationAnomalyAlertTrigger.type', |
| 97 | + { |
| 98 | + defaultMessage: 'Type', |
| 99 | + } |
| 100 | + )} |
| 101 | + > |
| 102 | + <EuiSelect |
| 103 | + value={params.transactionType} |
| 104 | + options={transactionTypes.map((key) => { |
| 105 | + return { |
| 106 | + text: key, |
| 107 | + value: key, |
| 108 | + }; |
| 109 | + })} |
| 110 | + onChange={(e) => |
| 111 | + setAlertParams( |
| 112 | + 'transactionType', |
| 113 | + e.target.value as Params['transactionType'] |
| 114 | + ) |
| 115 | + } |
| 116 | + compressed |
| 117 | + /> |
| 118 | + </PopoverExpression>, |
| 119 | + <PopoverExpression |
| 120 | + value={params.anomalyScore.toString(10)} |
| 121 | + title={i18n.translate( |
| 122 | + 'xpack.apm.transactionDurationAnomalyAlertTrigger.anomalyScore', |
| 123 | + { |
| 124 | + defaultMessage: 'Has anomaly score', |
| 125 | + } |
| 126 | + )} |
| 127 | + > |
| 128 | + <SelectAnomalySeverity |
| 129 | + value={params.anomalyScore} |
| 130 | + onChange={(value) => { |
| 131 | + setAlertParams('anomalyScore', value); |
| 132 | + }} |
| 133 | + /> |
| 134 | + </PopoverExpression>, |
| 135 | + ]; |
| 136 | + |
| 137 | + return ( |
| 138 | + <ServiceAlertTrigger |
| 139 | + alertTypeName={ |
| 140 | + ALERT_TYPES_CONFIG['apm.transaction_duration_anomaly'].name |
| 141 | + } |
| 142 | + fields={fields} |
| 143 | + defaults={defaults} |
| 144 | + setAlertParams={setAlertParams} |
| 145 | + setAlertProperty={setAlertProperty} |
| 146 | + /> |
| 147 | + ); |
| 148 | +} |
| 149 | + |
| 150 | +// Default export is required for React.lazy loading |
| 151 | +// |
| 152 | +// eslint-disable-next-line import/no-default-export |
| 153 | +export default TransactionDurationAnomalyAlertTrigger; |
0 commit comments