incompatibleAlertQuery.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Alert} from 'sentry/components/alert';
  4. import {Button} from 'sentry/components/button';
  5. import {IconClose} from 'sentry/icons';
  6. import {t, tct} from 'sentry/locale';
  7. import type EventView from 'sentry/utils/discover/eventView';
  8. import type {Aggregation} from 'sentry/utils/discover/fields';
  9. import {AGGREGATIONS, explodeFieldString} from 'sentry/utils/discover/fields';
  10. import {
  11. errorFieldConfig,
  12. transactionFieldConfig,
  13. } from 'sentry/views/alerts/rules/metric/constants';
  14. import {getQueryDatasource} from 'sentry/views/alerts/utils';
  15. /**
  16. * Discover query supports more features than alert rules
  17. * To create an alert rule from a discover query, some parameters need to be adjusted
  18. */
  19. type IncompatibleQueryProperties = {
  20. /**
  21. * Must have zero or one environments
  22. */
  23. hasEnvironmentError: boolean;
  24. /**
  25. * event.type must be error or transaction
  26. */
  27. hasEventTypeError: boolean;
  28. /**
  29. * Must have exactly one project selected and not -1 (all projects)
  30. */
  31. hasProjectError: boolean;
  32. hasYAxisError: boolean;
  33. };
  34. function incompatibleYAxis(eventView: EventView): boolean {
  35. const column = explodeFieldString(eventView.getYAxis());
  36. if (
  37. column.kind === 'field' ||
  38. column.kind === 'equation' ||
  39. column.kind === 'calculatedField'
  40. ) {
  41. return true;
  42. }
  43. const eventTypeMatch = eventView.query.match(/event\.type:(transaction|error)/);
  44. if (!eventTypeMatch) {
  45. return false;
  46. }
  47. const dataset = eventTypeMatch[1];
  48. const yAxisConfig = dataset === 'error' ? errorFieldConfig : transactionFieldConfig;
  49. const invalidFunction = !yAxisConfig.aggregations.includes(column.function[0]);
  50. // Allow empty parameters, allow all numeric parameters - eg. apdex(300)
  51. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  52. const aggregation: Aggregation | undefined = AGGREGATIONS[column.function[0]];
  53. if (!aggregation) {
  54. return false;
  55. }
  56. const isNumericParameter = aggregation.parameters.some(
  57. param => param.kind === 'value' && param.dataType === 'number'
  58. );
  59. // There are other measurements possible, but for the time being, only allow alerting
  60. // on the predefined set of measurements for alerts.
  61. const allowedParameters = [
  62. '',
  63. ...yAxisConfig.fields,
  64. ...(yAxisConfig.measurementKeys ?? []),
  65. ];
  66. const invalidParameter =
  67. !isNumericParameter && !allowedParameters.includes(column.function[1]);
  68. return invalidFunction || invalidParameter;
  69. }
  70. export function checkMetricAlertCompatiablity(
  71. eventView: EventView
  72. ): IncompatibleQueryProperties {
  73. // Must have exactly one project selected and not -1 (all projects)
  74. const hasProjectError = eventView.project.length !== 1 || eventView.project[0] === -1;
  75. // Must have one or zero environments
  76. const hasEnvironmentError = eventView.environment.length > 1;
  77. // Must have event.type of error or transaction
  78. const hasEventTypeError = getQueryDatasource(eventView.query) === null;
  79. // yAxis must be a function and enabled on alerts
  80. const hasYAxisError = incompatibleYAxis(eventView);
  81. return {
  82. hasProjectError,
  83. hasEnvironmentError,
  84. hasEventTypeError,
  85. hasYAxisError,
  86. };
  87. }
  88. interface IncompatibleAlertQueryProps {
  89. eventView: EventView;
  90. }
  91. /**
  92. * Displays messages to the user on what needs to change in their query
  93. */
  94. export function IncompatibleAlertQuery(props: IncompatibleAlertQueryProps) {
  95. const [isOpen, setIsOpen] = useState(true);
  96. const incompatibleQuery = checkMetricAlertCompatiablity(props.eventView);
  97. const totalErrors = Object.values(incompatibleQuery).filter(val => val).length;
  98. if (!totalErrors || !isOpen) {
  99. return null;
  100. }
  101. return (
  102. <StyledAlert
  103. type="info"
  104. showIcon
  105. trailingItems={
  106. <Button
  107. icon={<IconClose size="sm" />}
  108. aria-label={t('Close')}
  109. size="zero"
  110. onClick={() => setIsOpen(false)}
  111. borderless
  112. />
  113. }
  114. >
  115. {t('The following problems occurred while creating your alert:')}
  116. <StyledUnorderedList>
  117. {incompatibleQuery.hasProjectError && <li>{t('No project was selected')}</li>}
  118. {incompatibleQuery.hasEnvironmentError && (
  119. <li>{t('Too many environments were selected')}</li>
  120. )}
  121. {incompatibleQuery.hasEventTypeError && (
  122. <li>
  123. {tct(
  124. "An event type wasn't selected. [defaultSetting] has been set as the default",
  125. {
  126. defaultSetting: <StyledCode>event.type:error</StyledCode>,
  127. }
  128. )}
  129. </li>
  130. )}
  131. {incompatibleQuery.hasYAxisError && (
  132. <li>
  133. {tct('An alert can’t use the metric [yAxis] just yet.', {
  134. yAxis: <StyledCode>{props.eventView.getYAxis()}</StyledCode>,
  135. })}
  136. </li>
  137. )}
  138. </StyledUnorderedList>
  139. </StyledAlert>
  140. );
  141. }
  142. const StyledAlert = styled(Alert)`
  143. color: ${p => p.theme.textColor};
  144. `;
  145. const StyledUnorderedList = styled('ul')`
  146. margin-bottom: 0;
  147. `;
  148. const StyledCode = styled('code')`
  149. background-color: transparent;
  150. padding: 0;
  151. `;