index.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import round from 'lodash/round';
  2. import {t} from 'sentry/locale';
  3. import {Organization, SessionFieldWithOperation} from 'sentry/types';
  4. import {IssueAlertRule} from 'sentry/types/alerts';
  5. import {defined} from 'sentry/utils';
  6. import {getUtcDateString} from 'sentry/utils/dates';
  7. import {axisLabelFormatter, tooltipFormatter} from 'sentry/utils/discover/charts';
  8. import {aggregateOutputType} from 'sentry/utils/discover/fields';
  9. import toArray from 'sentry/utils/toArray';
  10. import {
  11. Dataset,
  12. Datasource,
  13. EventTypes,
  14. MetricRule,
  15. SavedMetricRule,
  16. SessionsAggregate,
  17. } from 'sentry/views/alerts/rules/metric/types';
  18. import {AlertRuleStatus, Incident, IncidentStats} from '../types';
  19. /**
  20. * Gets start and end date query parameters from stats
  21. */
  22. export function getStartEndFromStats(stats: IncidentStats) {
  23. const start = getUtcDateString(stats.eventStats.data[0][0] * 1000);
  24. const end = getUtcDateString(
  25. stats.eventStats.data[stats.eventStats.data.length - 1][0] * 1000
  26. );
  27. return {start, end};
  28. }
  29. export function isIssueAlert(
  30. data: IssueAlertRule | SavedMetricRule | MetricRule
  31. ): data is IssueAlertRule {
  32. return !data.hasOwnProperty('triggers');
  33. }
  34. export const DATA_SOURCE_LABELS = {
  35. [Dataset.ERRORS]: t('Errors'),
  36. [Dataset.TRANSACTIONS]: t('Transactions'),
  37. [Datasource.ERROR_DEFAULT]: 'event.type:error OR event.type:default',
  38. [Datasource.ERROR]: 'event.type:error',
  39. [Datasource.DEFAULT]: 'event.type:default',
  40. [Datasource.TRANSACTION]: 'event.type:transaction',
  41. };
  42. // Maps a datasource to the relevant dataset and event_types for the backend to use
  43. export const DATA_SOURCE_TO_SET_AND_EVENT_TYPES = {
  44. [Datasource.ERROR_DEFAULT]: {
  45. dataset: Dataset.ERRORS,
  46. eventTypes: [EventTypes.ERROR, EventTypes.DEFAULT],
  47. },
  48. [Datasource.ERROR]: {
  49. dataset: Dataset.ERRORS,
  50. eventTypes: [EventTypes.ERROR],
  51. },
  52. [Datasource.DEFAULT]: {
  53. dataset: Dataset.ERRORS,
  54. eventTypes: [EventTypes.DEFAULT],
  55. },
  56. [Datasource.TRANSACTION]: {
  57. dataset: Dataset.TRANSACTIONS,
  58. eventTypes: [EventTypes.TRANSACTION],
  59. },
  60. };
  61. // Converts the given dataset and event types array to a datasource for the datasource dropdown
  62. export function convertDatasetEventTypesToSource(
  63. dataset: Dataset,
  64. eventTypes: EventTypes[]
  65. ) {
  66. // transactions and generic_metrics only have one datasource option regardless of event type
  67. if (dataset === Dataset.TRANSACTIONS || dataset === Dataset.GENERIC_METRICS) {
  68. return Datasource.TRANSACTION;
  69. }
  70. // if no event type was provided use the default datasource
  71. if (!eventTypes) {
  72. return Datasource.ERROR;
  73. }
  74. if (eventTypes.includes(EventTypes.DEFAULT) && eventTypes.includes(EventTypes.ERROR)) {
  75. return Datasource.ERROR_DEFAULT;
  76. }
  77. if (eventTypes.includes(EventTypes.DEFAULT)) {
  78. return Datasource.DEFAULT;
  79. }
  80. return Datasource.ERROR;
  81. }
  82. /**
  83. * Attempt to guess the data source of a discover query
  84. *
  85. * @returns An object containing the datasource and new query without the datasource.
  86. * Returns null on no datasource.
  87. */
  88. export function getQueryDatasource(
  89. query: string
  90. ): {query: string; source: Datasource} | null {
  91. let match = query.match(
  92. /\(?\bevent\.type:(error|default|transaction)\)?\WOR\W\(?event\.type:(error|default|transaction)\)?/i
  93. );
  94. if (match) {
  95. // should be [error, default] or [default, error]
  96. const eventTypes = match.slice(1, 3).sort().join(',');
  97. if (eventTypes !== 'default,error') {
  98. return null;
  99. }
  100. return {source: Datasource.ERROR_DEFAULT, query: query.replace(match[0], '').trim()};
  101. }
  102. match = query.match(/(^|\s)event\.type:(error|default|transaction)/i);
  103. if (match && Datasource[match[2].toUpperCase()]) {
  104. return {
  105. source: Datasource[match[2].toUpperCase()],
  106. query: query.replace(match[0], '').trim(),
  107. };
  108. }
  109. return null;
  110. }
  111. export function isSessionAggregate(aggregate: string) {
  112. return Object.values(SessionsAggregate).includes(aggregate as SessionsAggregate);
  113. }
  114. export const SESSION_AGGREGATE_TO_FIELD = {
  115. [SessionsAggregate.CRASH_FREE_SESSIONS]: SessionFieldWithOperation.SESSIONS,
  116. [SessionsAggregate.CRASH_FREE_USERS]: SessionFieldWithOperation.USERS,
  117. };
  118. export function alertAxisFormatter(value: number, seriesName: string, aggregate: string) {
  119. if (isSessionAggregate(aggregate)) {
  120. return defined(value) ? `${round(value, 2)}%` : '\u2015';
  121. }
  122. return axisLabelFormatter(value, aggregateOutputType(seriesName));
  123. }
  124. export function alertTooltipValueFormatter(
  125. value: number,
  126. seriesName: string,
  127. aggregate: string
  128. ) {
  129. if (isSessionAggregate(aggregate)) {
  130. return defined(value) ? `${value}%` : '\u2015';
  131. }
  132. return tooltipFormatter(value, aggregateOutputType(seriesName));
  133. }
  134. export const ALERT_CHART_MIN_MAX_BUFFER = 1.03;
  135. export function shouldScaleAlertChart(aggregate: string) {
  136. // We want crash free rate charts to be scaled because they are usually too
  137. // close to 100% and therefore too fine to see the spikes on 0%-100% scale.
  138. return isSessionAggregate(aggregate);
  139. }
  140. export function alertDetailsLink(organization: Organization, incident: Incident) {
  141. return `/organizations/${organization.slug}/alerts/rules/details/${
  142. incident.alertRule.status === AlertRuleStatus.SNAPSHOT &&
  143. incident.alertRule.originalAlertRuleId
  144. ? incident.alertRule.originalAlertRuleId
  145. : incident.alertRule.id
  146. }/`;
  147. }
  148. /**
  149. * Noramlizes a status string
  150. */
  151. export function getQueryStatus(status: string | string[]): string {
  152. if (Array.isArray(status) || status === '') {
  153. return 'all';
  154. }
  155. return ['open', 'closed'].includes(status) ? status : 'all';
  156. }
  157. const ALERT_LIST_QUERY_DEFAULT_TEAMS = ['myteams', 'unassigned'];
  158. /**
  159. * Noramlize a team slug from the query
  160. */
  161. export function getTeamParams(team?: string | string[]): string[] {
  162. if (team === undefined) {
  163. return ALERT_LIST_QUERY_DEFAULT_TEAMS;
  164. }
  165. if (team === '') {
  166. return [];
  167. }
  168. return toArray(team);
  169. }