index.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import round from 'lodash/round';
  2. import {t} from 'sentry/locale';
  3. import type {Organization} from 'sentry/types/organization';
  4. import {SessionFieldWithOperation} from 'sentry/types/organization';
  5. import {defined} from 'sentry/utils';
  6. import toArray from 'sentry/utils/array/toArray';
  7. import {getUtcDateString} from 'sentry/utils/dates';
  8. import {axisLabelFormatter, tooltipFormatter} from 'sentry/utils/discover/charts';
  9. import {aggregateOutputType} from 'sentry/utils/discover/fields';
  10. import {formatMetricUsingFixedUnit} from 'sentry/utils/metrics/formatters';
  11. import {parseField, parseMRI} from 'sentry/utils/metrics/mri';
  12. import {
  13. Dataset,
  14. Datasource,
  15. EventTypes,
  16. SessionsAggregate,
  17. } from 'sentry/views/alerts/rules/metric/types';
  18. import {isCustomMetricAlert} from 'sentry/views/alerts/rules/metric/utils/isCustomMetricAlert';
  19. import type {CombinedAlerts, Incident, IncidentStats} from '../types';
  20. import {AlertRuleStatus, CombinedAlertType} from '../types';
  21. /**
  22. * Gets start and end date query parameters from stats
  23. */
  24. export function getStartEndFromStats(stats: IncidentStats) {
  25. const start = getUtcDateString(stats.eventStats.data[0][0] * 1000);
  26. const end = getUtcDateString(
  27. stats.eventStats.data[stats.eventStats.data.length - 1][0] * 1000
  28. );
  29. return {start, end};
  30. }
  31. export function isIssueAlert(data: CombinedAlerts) {
  32. return data.type === CombinedAlertType.ISSUE;
  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. if (isCustomMetricAlert(aggregate)) {
  123. const {mri, aggregation} = parseField(aggregate)!;
  124. const {unit} = parseMRI(mri)!;
  125. return formatMetricUsingFixedUnit(value, unit, aggregation);
  126. }
  127. return axisLabelFormatter(value, aggregateOutputType(seriesName));
  128. }
  129. export function alertTooltipValueFormatter(
  130. value: number,
  131. seriesName: string,
  132. aggregate: string
  133. ) {
  134. if (isSessionAggregate(aggregate)) {
  135. return defined(value) ? `${value}%` : '\u2015';
  136. }
  137. if (isCustomMetricAlert(aggregate)) {
  138. const {mri, aggregation} = parseField(aggregate)!;
  139. const {unit} = parseMRI(mri)!;
  140. return formatMetricUsingFixedUnit(value, unit, aggregation);
  141. }
  142. return tooltipFormatter(value, aggregateOutputType(seriesName));
  143. }
  144. export const ALERT_CHART_MIN_MAX_BUFFER = 1.03;
  145. export function shouldScaleAlertChart(aggregate: string) {
  146. // We want crash free rate charts to be scaled because they are usually too
  147. // close to 100% and therefore too fine to see the spikes on 0%-100% scale.
  148. return isSessionAggregate(aggregate);
  149. }
  150. export function alertDetailsLink(organization: Organization, incident: Incident) {
  151. return `/organizations/${organization.slug}/alerts/rules/details/${
  152. incident.alertRule.status === AlertRuleStatus.SNAPSHOT &&
  153. incident.alertRule.originalAlertRuleId
  154. ? incident.alertRule.originalAlertRuleId
  155. : incident.alertRule.id
  156. }/`;
  157. }
  158. /**
  159. * Noramlizes a status string
  160. */
  161. export function getQueryStatus(status: string | string[]): string {
  162. if (Array.isArray(status) || status === '') {
  163. return 'all';
  164. }
  165. return ['open', 'closed'].includes(status) ? status : 'all';
  166. }
  167. const ALERT_LIST_QUERY_DEFAULT_TEAMS = ['myteams', 'unassigned'];
  168. /**
  169. * Noramlize a team slug from the query
  170. */
  171. export function getTeamParams(team?: string | string[]): string[] {
  172. if (team === undefined) {
  173. return ALERT_LIST_QUERY_DEFAULT_TEAMS;
  174. }
  175. if (team === '') {
  176. return [];
  177. }
  178. return toArray(team);
  179. }