wizardField.tsx 9.1 KB

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