wizardField.tsx 9.5 KB

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