incompatibleAlertQuery.tsx 6.0 KB

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