index.tsx 5.6 KB

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