wizardField.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 InsightsMetricField from 'sentry/views/alerts/rules/metric/insightsMetricField';
  16. import MriField from 'sentry/views/alerts/rules/metric/mriField';
  17. import type {Dataset} from 'sentry/views/alerts/rules/metric/types';
  18. import type {AlertType} from 'sentry/views/alerts/wizard/options';
  19. import {
  20. AlertWizardAlertNames,
  21. AlertWizardRuleTemplates,
  22. } from 'sentry/views/alerts/wizard/options';
  23. import {QueryField} from 'sentry/views/discover/table/queryField';
  24. import {FieldValueKind} from 'sentry/views/discover/table/types';
  25. import {generateFieldOptions} from 'sentry/views/discover/utils';
  26. import {hasEAPAlerts} from 'sentry/views/insights/common/utils/hasEAPAlerts';
  27. import {getFieldOptionConfig} from './metricField';
  28. type MenuOption = {label: React.ReactNode; 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. ...(hasEAPAlerts(organization)
  119. ? [
  120. {
  121. label: (
  122. <span>
  123. {AlertWizardAlertNames.eap_metrics}
  124. <FeatureBadge type="experimental" />
  125. </span>
  126. ),
  127. value: 'eap_metrics' as const,
  128. },
  129. ]
  130. : []),
  131. ],
  132. },
  133. {
  134. label: hasCustomMetrics(organization) ? t('METRICS') : t('CUSTOM'),
  135. options: [
  136. hasCustomMetrics(organization)
  137. ? {
  138. label: AlertWizardAlertNames.custom_metrics,
  139. value: 'custom_metrics',
  140. }
  141. : {
  142. label: AlertWizardAlertNames.custom_transactions,
  143. value: 'custom_transactions',
  144. },
  145. ],
  146. },
  147. ];
  148. return (
  149. <FormField {...fieldProps}>
  150. {({onChange, model, disabled}) => {
  151. const aggregate = model.getValue('aggregate');
  152. const dataset: Dataset = model.getValue('dataset');
  153. const selectedTemplate: AlertType = alertType || 'custom_metrics';
  154. const {fieldOptionsConfig, hidePrimarySelector, hideParameterSelector} =
  155. getFieldOptionConfig({
  156. dataset: dataset as Dataset,
  157. alertType,
  158. });
  159. const fieldOptions = generateFieldOptions({organization, ...fieldOptionsConfig});
  160. const fieldValue = getFieldValue(aggregate ?? '', model);
  161. const fieldKey =
  162. fieldValue?.kind === FieldValueKind.FUNCTION
  163. ? `function:${fieldValue.function[0]}`
  164. : '';
  165. const selectedField = fieldOptions[fieldKey]?.value;
  166. const numParameters: number =
  167. selectedField?.kind === FieldValueKind.FUNCTION
  168. ? selectedField.meta.parameters.length
  169. : 0;
  170. const gridColumns =
  171. 1 +
  172. numParameters -
  173. (hideParameterSelector ? 1 : 0) -
  174. (hidePrimarySelector ? 1 : 0);
  175. return (
  176. <Container alertType={alertType} hideGap={gridColumns < 1}>
  177. <SelectControl
  178. value={selectedTemplate}
  179. options={menuOptions}
  180. disabled={disabled}
  181. onChange={(option: MenuOption) => {
  182. const template = AlertWizardRuleTemplates[option.value];
  183. model.setValue('aggregate', template.aggregate);
  184. model.setValue('dataset', template.dataset);
  185. model.setValue('eventTypes', [template.eventTypes]);
  186. // Keep alertType last
  187. model.setValue('alertType', option.value);
  188. }}
  189. />
  190. {alertType === 'custom_metrics' ? (
  191. <MriField
  192. project={project}
  193. aggregate={aggregate}
  194. onChange={newAggregate => onChange(newAggregate, {})}
  195. />
  196. ) : alertType === 'insights_metrics' ? (
  197. <InsightsMetricField
  198. project={project}
  199. aggregate={aggregate}
  200. onChange={newAggregate => {
  201. return onChange(newAggregate, {});
  202. }}
  203. />
  204. ) : alertType === 'eap_metrics' ? (
  205. <EAPField
  206. aggregate={aggregate}
  207. onChange={newAggregate => {
  208. return onChange(newAggregate, {});
  209. }}
  210. />
  211. ) : (
  212. <StyledQueryField
  213. filterPrimaryOptions={option =>
  214. option.value.kind === FieldValueKind.FUNCTION
  215. }
  216. fieldOptions={fieldOptions}
  217. fieldValue={fieldValue}
  218. onChange={v => onChange(generateFieldAsString(v), {})}
  219. columnWidth={columnWidth}
  220. gridColumns={gridColumns}
  221. inFieldLabels={inFieldLabels}
  222. shouldRenderTag={false}
  223. disabled={disabled}
  224. hideParameterSelector={hideParameterSelector}
  225. hidePrimarySelector={hidePrimarySelector}
  226. />
  227. )}
  228. </Container>
  229. );
  230. }}
  231. </FormField>
  232. );
  233. }
  234. // swaps out custom percentile values for known percentiles, used while we fade out custom percentiles in metric alerts
  235. // TODO(telemetry-experience): remove once we migrate all custom percentile alerts
  236. const getFieldValue = (aggregate: string | undefined, model) => {
  237. const fieldValue = explodeFieldString(aggregate ?? '');
  238. if (fieldValue?.kind !== FieldValueKind.FUNCTION) {
  239. return fieldValue;
  240. }
  241. if (fieldValue.function[0] !== 'percentile') {
  242. return fieldValue;
  243. }
  244. const newFieldValue: QueryFieldValue = {
  245. kind: FieldValueKind.FUNCTION,
  246. function: [
  247. getApproximateKnownPercentile(fieldValue.function[2] as string),
  248. fieldValue.function[1],
  249. undefined,
  250. undefined,
  251. ],
  252. alias: fieldValue.alias,
  253. };
  254. model.setValue('aggregate', generateFieldAsString(newFieldValue));
  255. return newFieldValue;
  256. };
  257. const getApproximateKnownPercentile = (customPercentile: string) => {
  258. const percentile = parseFloat(customPercentile);
  259. if (percentile <= 0.5) {
  260. return 'p50';
  261. }
  262. if (percentile <= 0.75) {
  263. return 'p75';
  264. }
  265. if (percentile <= 0.9) {
  266. return 'p90';
  267. }
  268. if (percentile <= 0.95) {
  269. return 'p95';
  270. }
  271. if (percentile <= 0.99) {
  272. return 'p99';
  273. }
  274. return 'p100';
  275. };
  276. const Container = styled('div')<{hideGap: boolean; alertType?: AlertType}>`
  277. display: grid;
  278. gap: ${p => (p.hideGap ? 0 : space(1))};
  279. grid-template-columns: 1fr auto;
  280. `;
  281. const StyledQueryField = styled(QueryField)<{gridColumns: number; columnWidth?: number}>`
  282. ${p =>
  283. p.columnWidth &&
  284. css`
  285. width: ${p.gridColumns * p.columnWidth}px;
  286. `}
  287. `;