index.tsx 6.4 KB

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