wizardField.tsx 9.8 KB

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