wizardField.tsx 8.0 KB

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