incompatibleAlertQuery.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. const aggregation: Aggregation | undefined = AGGREGATIONS[column.function[0]];
  52. if (!aggregation) {
  53. return false;
  54. }
  55. const isNumericParameter = aggregation.parameters.some(
  56. param => param.kind === 'value' && param.dataType === 'number'
  57. );
  58. // There are other measurements possible, but for the time being, only allow alerting
  59. // on the predefined set of measurements for alerts.
  60. const allowedParameters = [
  61. '',
  62. ...yAxisConfig.fields,
  63. ...(yAxisConfig.measurementKeys ?? []),
  64. ];
  65. const invalidParameter =
  66. !isNumericParameter && !allowedParameters.includes(column.function[1]);
  67. return invalidFunction || invalidParameter;
  68. }
  69. export function checkMetricAlertCompatiablity(
  70. eventView: EventView
  71. ): IncompatibleQueryProperties {
  72. // Must have exactly one project selected and not -1 (all projects)
  73. const hasProjectError = eventView.project.length !== 1 || eventView.project[0] === -1;
  74. // Must have one or zero environments
  75. const hasEnvironmentError = eventView.environment.length > 1;
  76. // Must have event.type of error or transaction
  77. const hasEventTypeError = getQueryDatasource(eventView.query) === null;
  78. // yAxis must be a function and enabled on alerts
  79. const hasYAxisError = incompatibleYAxis(eventView);
  80. return {
  81. hasProjectError,
  82. hasEnvironmentError,
  83. hasEventTypeError,
  84. hasYAxisError,
  85. };
  86. }
  87. interface IncompatibleAlertQueryProps {
  88. eventView: EventView;
  89. orgSlug: string;
  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. const eventTypeError = props.eventView.clone();
  102. eventTypeError.query += ' event.type:error';
  103. const eventTypeTransaction = props.eventView.clone();
  104. eventTypeTransaction.query += ' event.type:transaction';
  105. const eventTypeDefault = props.eventView.clone();
  106. eventTypeDefault.query += ' event.type:default';
  107. const eventTypeErrorDefault = props.eventView.clone();
  108. eventTypeErrorDefault.query += ' event.type:error or event.type:default';
  109. return (
  110. <StyledAlert
  111. type="info"
  112. showIcon
  113. trailingItems={
  114. <Button
  115. icon={<IconClose size="sm" />}
  116. aria-label={t('Close')}
  117. size="zero"
  118. onClick={() => setIsOpen(false)}
  119. borderless
  120. />
  121. }
  122. >
  123. {t('The following problems occurred while creating your alert:')}
  124. <StyledUnorderedList>
  125. {incompatibleQuery.hasProjectError && <li>{t('No project was selected')}</li>}
  126. {incompatibleQuery.hasEnvironmentError && (
  127. <li>{t('Too many environments were selected')}</li>
  128. )}
  129. {incompatibleQuery.hasEventTypeError && (
  130. <li>
  131. {tct(
  132. "An event type wasn't selected. [defaultSetting] has been set as the default",
  133. {
  134. defaultSetting: <StyledCode>event.type:error</StyledCode>,
  135. }
  136. )}
  137. </li>
  138. )}
  139. {incompatibleQuery.hasYAxisError && (
  140. <li>
  141. {tct('An alert can’t use the metric [yAxis] just yet.', {
  142. yAxis: <StyledCode>{props.eventView.getYAxis()}</StyledCode>,
  143. })}
  144. </li>
  145. )}
  146. </StyledUnorderedList>
  147. </StyledAlert>
  148. );
  149. }
  150. const StyledAlert = styled(Alert)`
  151. color: ${p => p.theme.textColor};
  152. `;
  153. const StyledUnorderedList = styled('ul')`
  154. margin-bottom: 0;
  155. `;
  156. const StyledCode = styled('code')`
  157. background-color: transparent;
  158. padding: 0;
  159. `;