wizardField.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import {css} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import SelectControl from 'sentry/components/forms/controls/selectControl';
  4. import type {FormFieldProps} from 'sentry/components/forms/formField';
  5. import FormField from 'sentry/components/forms/formField';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import type {Organization} from 'sentry/types/organization';
  9. import type {Project} from 'sentry/types/project';
  10. import type {QueryFieldValue} from 'sentry/utils/discover/fields';
  11. import {explodeFieldString, generateFieldAsString} from 'sentry/utils/discover/fields';
  12. import {hasCustomMetrics} from 'sentry/utils/metrics/features';
  13. import MriField from 'sentry/views/alerts/rules/metric/mriField';
  14. import type {Dataset} from 'sentry/views/alerts/rules/metric/types';
  15. import type {AlertType} from 'sentry/views/alerts/wizard/options';
  16. import {
  17. AlertWizardAlertNames,
  18. AlertWizardRuleTemplates,
  19. } from 'sentry/views/alerts/wizard/options';
  20. import {QueryField} from 'sentry/views/discover/table/queryField';
  21. import {FieldValueKind} from 'sentry/views/discover/table/types';
  22. import {generateFieldOptions} from 'sentry/views/discover/utils';
  23. import {getFieldOptionConfig} from './metricField';
  24. type MenuOption = {label: string; value: AlertType};
  25. type GroupedMenuOption = {label: string; options: Array<MenuOption>};
  26. type Props = Omit<FormFieldProps, 'children'> & {
  27. organization: Organization;
  28. project: Project;
  29. alertType?: AlertType;
  30. /**
  31. * Optionally set a width for each column of selector
  32. */
  33. columnWidth?: number;
  34. inFieldLabels?: boolean;
  35. };
  36. export default function WizardField({
  37. organization,
  38. columnWidth,
  39. inFieldLabels,
  40. alertType,
  41. project,
  42. ...fieldProps
  43. }: Props) {
  44. const menuOptions: GroupedMenuOption[] = [
  45. {
  46. label: t('ERRORS'),
  47. options: [
  48. {
  49. label: AlertWizardAlertNames.num_errors,
  50. value: 'num_errors',
  51. },
  52. {
  53. label: AlertWizardAlertNames.users_experiencing_errors,
  54. value: 'users_experiencing_errors',
  55. },
  56. ],
  57. },
  58. ...((organization.features.includes('crash-rate-alerts')
  59. ? [
  60. {
  61. label: t('SESSIONS'),
  62. options: [
  63. {
  64. label: AlertWizardAlertNames.crash_free_sessions,
  65. value: 'crash_free_sessions',
  66. },
  67. {
  68. label: AlertWizardAlertNames.crash_free_users,
  69. value: 'crash_free_users',
  70. },
  71. ],
  72. },
  73. ]
  74. : []) as GroupedMenuOption[]),
  75. {
  76. label: t('PERFORMANCE'),
  77. options: [
  78. {
  79. label: AlertWizardAlertNames.throughput,
  80. value: 'throughput',
  81. },
  82. {
  83. label: AlertWizardAlertNames.trans_duration,
  84. value: 'trans_duration',
  85. },
  86. {
  87. label: AlertWizardAlertNames.apdex,
  88. value: 'apdex',
  89. },
  90. {
  91. label: AlertWizardAlertNames.failure_rate,
  92. value: 'failure_rate',
  93. },
  94. {
  95. label: AlertWizardAlertNames.lcp,
  96. value: 'lcp',
  97. },
  98. {
  99. label: AlertWizardAlertNames.fid,
  100. value: 'fid',
  101. },
  102. {
  103. label: AlertWizardAlertNames.cls,
  104. value: 'cls',
  105. },
  106. ...(hasCustomMetrics(organization)
  107. ? [
  108. {
  109. label: AlertWizardAlertNames.custom_transactions,
  110. value: 'custom_transactions' as const,
  111. },
  112. ]
  113. : []),
  114. ],
  115. },
  116. {
  117. label: hasCustomMetrics(organization) ? t('METRICS') : t('CUSTOM'),
  118. options: [
  119. hasCustomMetrics(organization)
  120. ? {
  121. label: AlertWizardAlertNames.custom_metrics,
  122. value: 'custom_metrics',
  123. }
  124. : {
  125. label: AlertWizardAlertNames.custom_transactions,
  126. value: 'custom_transactions',
  127. },
  128. ],
  129. },
  130. ];
  131. return (
  132. <FormField {...fieldProps}>
  133. {({onChange, model, disabled}) => {
  134. const aggregate = model.getValue('aggregate');
  135. const dataset: Dataset = model.getValue('dataset');
  136. const selectedTemplate: AlertType = alertType || 'custom_metrics';
  137. const {fieldOptionsConfig, hidePrimarySelector, hideParameterSelector} =
  138. getFieldOptionConfig({
  139. dataset: dataset as Dataset,
  140. alertType,
  141. });
  142. const fieldOptions = generateFieldOptions({organization, ...fieldOptionsConfig});
  143. const fieldValue = getFieldValue(aggregate ?? '', model);
  144. const fieldKey =
  145. fieldValue?.kind === FieldValueKind.FUNCTION
  146. ? `function:${fieldValue.function[0]}`
  147. : '';
  148. const selectedField = fieldOptions[fieldKey]?.value;
  149. const numParameters: number =
  150. selectedField?.kind === FieldValueKind.FUNCTION
  151. ? selectedField.meta.parameters.length
  152. : 0;
  153. const gridColumns =
  154. 1 +
  155. numParameters -
  156. (hideParameterSelector ? 1 : 0) -
  157. (hidePrimarySelector ? 1 : 0);
  158. return (
  159. <Container hideGap={gridColumns < 1}>
  160. <SelectControl
  161. value={selectedTemplate}
  162. options={menuOptions}
  163. disabled={disabled}
  164. onChange={(option: MenuOption) => {
  165. const template = AlertWizardRuleTemplates[option.value];
  166. model.setValue('aggregate', template.aggregate);
  167. model.setValue('dataset', template.dataset);
  168. model.setValue('eventTypes', [template.eventTypes]);
  169. // Keep alertType last
  170. model.setValue('alertType', option.value);
  171. }}
  172. />
  173. {hasCustomMetrics(organization) && alertType === 'custom_metrics' ? (
  174. <MriField
  175. project={project}
  176. aggregate={aggregate}
  177. onChange={newAggregate => onChange(newAggregate, {})}
  178. />
  179. ) : (
  180. <StyledQueryField
  181. filterPrimaryOptions={option =>
  182. option.value.kind === FieldValueKind.FUNCTION
  183. }
  184. fieldOptions={fieldOptions}
  185. fieldValue={fieldValue}
  186. onChange={v => onChange(generateFieldAsString(v), {})}
  187. columnWidth={columnWidth}
  188. gridColumns={gridColumns}
  189. inFieldLabels={inFieldLabels}
  190. shouldRenderTag={false}
  191. disabled={disabled}
  192. hideParameterSelector={hideParameterSelector}
  193. hidePrimarySelector={hidePrimarySelector}
  194. />
  195. )}
  196. </Container>
  197. );
  198. }}
  199. </FormField>
  200. );
  201. }
  202. // swaps out custom percentile values for known percentiles, used while we fade out custom percentiles in metric alerts
  203. // TODO(telemetry-experience): remove once we migrate all custom percentile alerts
  204. const getFieldValue = (aggregate: string | undefined, model) => {
  205. const fieldValue = explodeFieldString(aggregate ?? '');
  206. if (fieldValue?.kind !== FieldValueKind.FUNCTION) {
  207. return fieldValue;
  208. }
  209. if (fieldValue.function[0] !== 'percentile') {
  210. return fieldValue;
  211. }
  212. const newFieldValue: QueryFieldValue = {
  213. kind: FieldValueKind.FUNCTION,
  214. function: [
  215. getApproximateKnownPercentile(fieldValue.function[2] as string),
  216. fieldValue.function[1],
  217. undefined,
  218. undefined,
  219. ],
  220. alias: fieldValue.alias,
  221. };
  222. model.setValue('aggregate', generateFieldAsString(newFieldValue));
  223. return newFieldValue;
  224. };
  225. const getApproximateKnownPercentile = (customPercentile: string) => {
  226. const percentile = parseFloat(customPercentile);
  227. if (percentile <= 0.5) {
  228. return 'p50';
  229. }
  230. if (percentile <= 0.75) {
  231. return 'p75';
  232. }
  233. if (percentile <= 0.9) {
  234. return 'p90';
  235. }
  236. if (percentile <= 0.95) {
  237. return 'p95';
  238. }
  239. if (percentile <= 0.99) {
  240. return 'p99';
  241. }
  242. return 'p100';
  243. };
  244. const Container = styled('div')<{hideGap: boolean}>`
  245. display: grid;
  246. grid-template-columns: 1fr auto;
  247. gap: ${p => (p.hideGap ? space(0) : space(1))};
  248. `;
  249. const StyledQueryField = styled(QueryField)<{gridColumns: number; columnWidth?: number}>`
  250. ${p =>
  251. p.columnWidth &&
  252. css`
  253. width: ${p.gridColumns * p.columnWidth}px;
  254. `}
  255. `;