incompatibleAlertQuery.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. }
  90. /**
  91. * Displays messages to the user on what needs to change in their query
  92. */
  93. export function IncompatibleAlertQuery(props: IncompatibleAlertQueryProps) {
  94. const [isOpen, setIsOpen] = useState(true);
  95. const incompatibleQuery = checkMetricAlertCompatiablity(props.eventView);
  96. const totalErrors = Object.values(incompatibleQuery).filter(val => val).length;
  97. if (!totalErrors || !isOpen) {
  98. return null;
  99. }
  100. return (
  101. <StyledAlert
  102. type="info"
  103. showIcon
  104. trailingItems={
  105. <Button
  106. icon={<IconClose size="sm" />}
  107. aria-label={t('Close')}
  108. size="zero"
  109. onClick={() => setIsOpen(false)}
  110. borderless
  111. />
  112. }
  113. >
  114. {t('The following problems occurred while creating your alert:')}
  115. <StyledUnorderedList>
  116. {incompatibleQuery.hasProjectError && <li>{t('No project was selected')}</li>}
  117. {incompatibleQuery.hasEnvironmentError && (
  118. <li>{t('Too many environments were selected')}</li>
  119. )}
  120. {incompatibleQuery.hasEventTypeError && (
  121. <li>
  122. {tct(
  123. "An event type wasn't selected. [defaultSetting] has been set as the default",
  124. {
  125. defaultSetting: <StyledCode>event.type:error</StyledCode>,
  126. }
  127. )}
  128. </li>
  129. )}
  130. {incompatibleQuery.hasYAxisError && (
  131. <li>
  132. {tct('An alert can’t use the metric [yAxis] just yet.', {
  133. yAxis: <StyledCode>{props.eventView.getYAxis()}</StyledCode>,
  134. })}
  135. </li>
  136. )}
  137. </StyledUnorderedList>
  138. </StyledAlert>
  139. );
  140. }
  141. const StyledAlert = styled(Alert)`
  142. color: ${p => p.theme.textColor};
  143. `;
  144. const StyledUnorderedList = styled('ul')`
  145. margin-bottom: 0;
  146. `;
  147. const StyledCode = styled('code')`
  148. background-color: transparent;
  149. padding: 0;
  150. `;