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