|
| 1 | +import type FormModel from 'sentry/components/forms/model'; |
| 2 | +import {getFormFieldValue} from 'sentry/components/workflowEngine/form/getFormFieldValue'; |
| 3 | +import {t} from 'sentry/locale'; |
| 4 | +import {DataConditionType} from 'sentry/types/workflowEngine/dataConditions'; |
| 5 | +import getDuration from 'sentry/utils/duration/getDuration'; |
| 6 | +import {unreachable} from 'sentry/utils/unreachable'; |
| 7 | +import {useSetAutomaticName} from 'sentry/views/detectors/components/forms/common/useSetAutomaticName'; |
| 8 | +import { |
| 9 | + METRIC_DETECTOR_FORM_FIELDS, |
| 10 | + type MetricDetectorFormData, |
| 11 | +} from 'sentry/views/detectors/components/forms/metric/metricFormData'; |
| 12 | +import {getDatasetConfig} from 'sentry/views/detectors/datasetConfig/getDatasetConfig'; |
| 13 | +import {getStaticDetectorThresholdSuffix} from 'sentry/views/detectors/utils/metricDetectorSuffix'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Automatically generates a name for the metric detector form based on the values: |
| 17 | + * - Dataset |
| 18 | + * - Aggregate function |
| 19 | + * - Detection type |
| 20 | + * - Direction (above or below) |
| 21 | + * - Threshold value |
| 22 | + * - Interval |
| 23 | + * |
| 24 | + * e.g. "p95(span.duration) above 100ms compared to past 1 hour" |
| 25 | + */ |
| 26 | +export function useAutoMetricDetectorName() { |
| 27 | + useSetAutomaticName((form: FormModel): string | null => { |
| 28 | + const detectionType = getFormFieldValue<MetricDetectorFormData['detectionType']>( |
| 29 | + form, |
| 30 | + METRIC_DETECTOR_FORM_FIELDS.detectionType |
| 31 | + ); |
| 32 | + const aggregate = getFormFieldValue<MetricDetectorFormData['aggregateFunction']>( |
| 33 | + form, |
| 34 | + METRIC_DETECTOR_FORM_FIELDS.aggregateFunction |
| 35 | + ); |
| 36 | + const interval = getFormFieldValue<MetricDetectorFormData['interval']>( |
| 37 | + form, |
| 38 | + METRIC_DETECTOR_FORM_FIELDS.interval |
| 39 | + ); |
| 40 | + const dataset = getFormFieldValue<MetricDetectorFormData['dataset']>( |
| 41 | + form, |
| 42 | + METRIC_DETECTOR_FORM_FIELDS.dataset |
| 43 | + ); |
| 44 | + const datasetConfig = getDatasetConfig(dataset); |
| 45 | + |
| 46 | + if (!aggregate || !interval || !detectionType || !dataset) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + |
| 50 | + switch (detectionType) { |
| 51 | + case 'static': { |
| 52 | + const conditionType = getFormFieldValue<MetricDetectorFormData['conditionType']>( |
| 53 | + form, |
| 54 | + METRIC_DETECTOR_FORM_FIELDS.conditionType |
| 55 | + ); |
| 56 | + const conditionValue = getFormFieldValue< |
| 57 | + MetricDetectorFormData['conditionValue'] |
| 58 | + >(form, METRIC_DETECTOR_FORM_FIELDS.conditionValue); |
| 59 | + |
| 60 | + if (!conditionType || !conditionValue) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + const suffix = getStaticDetectorThresholdSuffix(aggregate); |
| 65 | + const direction = |
| 66 | + conditionType === DataConditionType.GREATER ? t('above') : t('below'); |
| 67 | + |
| 68 | + return t( |
| 69 | + '%(aggregate)s %(aboveOrBelow)s %(value)s%(unit)s over past %(interval)s', |
| 70 | + { |
| 71 | + aggregate: datasetConfig.formatAggregateForTitle?.(aggregate) ?? aggregate, |
| 72 | + aboveOrBelow: direction, |
| 73 | + value: conditionValue, |
| 74 | + unit: suffix, |
| 75 | + interval: getDuration(interval), |
| 76 | + } |
| 77 | + ); |
| 78 | + } |
| 79 | + case 'percent': { |
| 80 | + const conditionType = getFormFieldValue<MetricDetectorFormData['conditionType']>( |
| 81 | + form, |
| 82 | + METRIC_DETECTOR_FORM_FIELDS.conditionType |
| 83 | + ); |
| 84 | + const conditionValue = getFormFieldValue< |
| 85 | + MetricDetectorFormData['conditionValue'] |
| 86 | + >(form, METRIC_DETECTOR_FORM_FIELDS.conditionValue); |
| 87 | + |
| 88 | + if (!conditionType || !conditionValue) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + const direction = |
| 93 | + conditionType === DataConditionType.GREATER ? t('higher') : t('lower'); |
| 94 | + |
| 95 | + return t( |
| 96 | + '%(aggregate)s %(aboveOrBelow)s by %(value)s%% compared to past %(interval)s', |
| 97 | + { |
| 98 | + aggregate: datasetConfig.formatAggregateForTitle?.(aggregate) ?? aggregate, |
| 99 | + aboveOrBelow: direction, |
| 100 | + value: conditionValue, |
| 101 | + interval: getDuration(interval), |
| 102 | + } |
| 103 | + ); |
| 104 | + } |
| 105 | + case 'dynamic': |
| 106 | + return t('%(aggregate)s is anomalous', { |
| 107 | + aggregate: datasetConfig.formatAggregateForTitle?.(aggregate) ?? aggregate, |
| 108 | + }); |
| 109 | + default: |
| 110 | + unreachable(detectionType); |
| 111 | + return null; |
| 112 | + } |
| 113 | + }); |
| 114 | +} |
0 commit comments