wizardField.tsx 9.4 KB

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