wizardField.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import {css} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import {FeatureBadge} from 'sentry/components/core/badge/featureBadge';
  4. import {Select} from 'sentry/components/core/select';
  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 EAPField from 'sentry/views/alerts/rules/metric/eapField';
  14. import type {Dataset} from 'sentry/views/alerts/rules/metric/types';
  15. import type {AlertType} from 'sentry/views/alerts/wizard/options';
  16. import {
  17. AlertWizardAlertNames,
  18. AlertWizardRuleTemplates,
  19. } from 'sentry/views/alerts/wizard/options';
  20. import {QueryField} from 'sentry/views/discover/table/queryField';
  21. import {FieldValueKind} from 'sentry/views/discover/table/types';
  22. import {generateFieldOptions} from 'sentry/views/discover/utils';
  23. import {hasEAPAlerts} from 'sentry/views/insights/common/utils/hasEAPAlerts';
  24. import {getFieldOptionConfig} from './metricField';
  25. type MenuOption = {label: React.ReactNode; value: AlertType};
  26. type GroupedMenuOption = {label: string; options: 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. ...fieldProps
  43. }: Props) {
  44. const menuOptions: GroupedMenuOption[] = [
  45. {
  46. label: t('ERRORS'),
  47. options: [
  48. {
  49. label: AlertWizardAlertNames.num_errors,
  50. value: 'num_errors',
  51. },
  52. {
  53. label: AlertWizardAlertNames.users_experiencing_errors,
  54. value: 'users_experiencing_errors',
  55. },
  56. ],
  57. },
  58. ...((organization.features.includes('crash-rate-alerts')
  59. ? [
  60. {
  61. label: t('SESSIONS'),
  62. options: [
  63. {
  64. label: AlertWizardAlertNames.crash_free_sessions,
  65. value: 'crash_free_sessions',
  66. },
  67. {
  68. label: AlertWizardAlertNames.crash_free_users,
  69. value: 'crash_free_users',
  70. },
  71. ],
  72. },
  73. ]
  74. : []) as GroupedMenuOption[]),
  75. {
  76. label: t('PERFORMANCE'),
  77. options: [
  78. {
  79. label: AlertWizardAlertNames.throughput,
  80. value: 'throughput',
  81. },
  82. {
  83. label: AlertWizardAlertNames.trans_duration,
  84. value: 'trans_duration',
  85. },
  86. {
  87. label: AlertWizardAlertNames.apdex,
  88. value: 'apdex',
  89. },
  90. {
  91. label: AlertWizardAlertNames.failure_rate,
  92. value: 'failure_rate',
  93. },
  94. {
  95. label: AlertWizardAlertNames.lcp,
  96. value: 'lcp',
  97. },
  98. {
  99. label: AlertWizardAlertNames.fid,
  100. value: 'fid',
  101. },
  102. {
  103. label: AlertWizardAlertNames.cls,
  104. value: 'cls',
  105. },
  106. ...(hasEAPAlerts(organization)
  107. ? [
  108. {
  109. label: (
  110. <span>
  111. {AlertWizardAlertNames.eap_metrics}
  112. <FeatureBadge
  113. type="beta"
  114. tooltipProps={{
  115. title: t(
  116. 'This feature is available for early adopters and the UX may change'
  117. ),
  118. }}
  119. />
  120. </span>
  121. ),
  122. value: 'eap_metrics' as const,
  123. },
  124. ]
  125. : []),
  126. ],
  127. },
  128. {
  129. label: t('CUSTOM'),
  130. options: [
  131. {
  132. label: AlertWizardAlertNames.custom_transactions,
  133. value: 'custom_transactions',
  134. },
  135. ],
  136. },
  137. ];
  138. return (
  139. <FormField {...fieldProps}>
  140. {({onChange, model, disabled}: any) => {
  141. const aggregate = model.getValue('aggregate');
  142. const dataset: Dataset = model.getValue('dataset');
  143. const selectedTemplate: AlertType = alertType || 'eap_metrics';
  144. const {fieldOptionsConfig, hidePrimarySelector, hideParameterSelector} =
  145. getFieldOptionConfig({
  146. dataset,
  147. alertType,
  148. });
  149. const fieldOptions = generateFieldOptions({organization, ...fieldOptionsConfig});
  150. const fieldValue = getFieldValue(aggregate ?? '', model);
  151. const fieldKey =
  152. fieldValue?.kind === FieldValueKind.FUNCTION
  153. ? `function:${fieldValue.function[0]}`
  154. : '';
  155. const selectedField = fieldOptions[fieldKey]?.value;
  156. const numParameters: number =
  157. selectedField?.kind === FieldValueKind.FUNCTION
  158. ? selectedField.meta.parameters.length
  159. : 0;
  160. const gridColumns =
  161. 1 +
  162. numParameters -
  163. (hideParameterSelector ? 1 : 0) -
  164. (hidePrimarySelector ? 1 : 0);
  165. return (
  166. <Container alertType={alertType} hideGap={gridColumns < 1}>
  167. <Select
  168. value={selectedTemplate}
  169. options={menuOptions}
  170. disabled={disabled}
  171. onChange={(option: MenuOption) => {
  172. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  173. const template = AlertWizardRuleTemplates[option.value];
  174. model.setValue('aggregate', template.aggregate);
  175. model.setValue('dataset', template.dataset);
  176. model.setValue('eventTypes', [template.eventTypes]);
  177. // Keep alertType last
  178. model.setValue('alertType', option.value);
  179. }}
  180. />
  181. {alertType === 'eap_metrics' ? (
  182. <EAPField
  183. aggregate={aggregate}
  184. onChange={newAggregate => {
  185. return onChange(newAggregate, {});
  186. }}
  187. />
  188. ) : (
  189. <StyledQueryField
  190. filterPrimaryOptions={option =>
  191. option.value.kind === FieldValueKind.FUNCTION
  192. }
  193. fieldOptions={fieldOptions}
  194. fieldValue={fieldValue}
  195. onChange={v => onChange(generateFieldAsString(v), {})}
  196. columnWidth={columnWidth}
  197. gridColumns={gridColumns}
  198. inFieldLabels={inFieldLabels}
  199. shouldRenderTag={false}
  200. disabled={disabled}
  201. hideParameterSelector={hideParameterSelector}
  202. hidePrimarySelector={hidePrimarySelector}
  203. />
  204. )}
  205. </Container>
  206. );
  207. }}
  208. </FormField>
  209. );
  210. }
  211. // swaps out custom percentile values for known percentiles, used while we fade out custom percentiles in metric alerts
  212. // TODO(telemetry-experience): remove once we migrate all custom percentile alerts
  213. const getFieldValue = (aggregate: string | undefined, model: any) => {
  214. const fieldValue = explodeFieldString(aggregate ?? '');
  215. if (fieldValue?.kind !== FieldValueKind.FUNCTION) {
  216. return fieldValue;
  217. }
  218. if (fieldValue.function[0] !== 'percentile') {
  219. return fieldValue;
  220. }
  221. const newFieldValue: QueryFieldValue = {
  222. kind: FieldValueKind.FUNCTION,
  223. function: [
  224. getApproximateKnownPercentile(fieldValue.function[2] as string),
  225. fieldValue.function[1],
  226. undefined,
  227. undefined,
  228. ],
  229. alias: fieldValue.alias,
  230. };
  231. model.setValue('aggregate', generateFieldAsString(newFieldValue));
  232. return newFieldValue;
  233. };
  234. const getApproximateKnownPercentile = (customPercentile: string) => {
  235. const percentile = parseFloat(customPercentile);
  236. if (percentile <= 0.5) {
  237. return 'p50';
  238. }
  239. if (percentile <= 0.75) {
  240. return 'p75';
  241. }
  242. if (percentile <= 0.9) {
  243. return 'p90';
  244. }
  245. if (percentile <= 0.95) {
  246. return 'p95';
  247. }
  248. if (percentile <= 0.99) {
  249. return 'p99';
  250. }
  251. return 'p100';
  252. };
  253. const Container = styled('div')<{hideGap: boolean; alertType?: AlertType}>`
  254. display: grid;
  255. gap: ${p => (p.hideGap ? 0 : space(1))};
  256. grid-template-columns: 1fr auto;
  257. `;
  258. const StyledQueryField = styled(QueryField)<{gridColumns: number; columnWidth?: number}>`
  259. ${p =>
  260. p.columnWidth &&
  261. css`
  262. width: ${p.gridColumns * p.columnWidth}px;
  263. `}
  264. `;