incompatibleAlertQuery.tsx 5.4 KB

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