index.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 {formatMetricUsingUnit} from 'sentry/utils/number/formatMetricUsingUnit';
  11. import {
  12. Dataset,
  13. Datasource,
  14. EventTypes,
  15. SessionsAggregate,
  16. } from 'sentry/views/alerts/rules/metric/types';
  17. import type {CombinedAlerts, Incident, IncidentStats} from '../types';
  18. import {AlertRuleStatus, CombinedAlertType} 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(data: CombinedAlerts) {
  30. return data.type === CombinedAlertType.ISSUE;
  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 and generic_metrics only have one datasource option regardless of event type
  65. if (dataset === Dataset.TRANSACTIONS || dataset === Dataset.GENERIC_METRICS) {
  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. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  102. if (match && Datasource[match[2]!.toUpperCase()]) {
  103. return {
  104. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  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: Record<string, SessionFieldWithOperation> = {
  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. const type = aggregateOutputType(seriesName);
  123. if (type === 'duration') {
  124. return formatMetricUsingUnit(value, 'milliseconds');
  125. }
  126. return axisLabelFormatter(value, type);
  127. }
  128. export function alertTooltipValueFormatter(
  129. value: number,
  130. seriesName: string,
  131. aggregate: string
  132. ) {
  133. if (isSessionAggregate(aggregate)) {
  134. return defined(value) ? `${value}%` : '\u2015';
  135. }
  136. return tooltipFormatter(value, aggregateOutputType(seriesName));
  137. }
  138. export const ALERT_CHART_MIN_MAX_BUFFER = 1.03;
  139. export function shouldScaleAlertChart(aggregate: string) {
  140. // We want crash free rate charts to be scaled because they are usually too
  141. // close to 100% and therefore too fine to see the spikes on 0%-100% scale.
  142. return isSessionAggregate(aggregate);
  143. }
  144. export function alertDetailsLink(organization: Organization, incident: Incident) {
  145. return `/organizations/${organization.slug}/alerts/rules/details/${
  146. incident.alertRule.status === AlertRuleStatus.SNAPSHOT &&
  147. incident.alertRule.originalAlertRuleId
  148. ? incident.alertRule.originalAlertRuleId
  149. : incident.alertRule.id
  150. }/`;
  151. }
  152. /**
  153. * Noramlizes a status string
  154. */
  155. export function getQueryStatus(status: string | string[]): string {
  156. if (Array.isArray(status) || status === '') {
  157. return 'all';
  158. }
  159. return ['open', 'closed'].includes(status) ? status : 'all';
  160. }
  161. const ALERT_LIST_QUERY_DEFAULT_TEAMS = ['myteams', 'unassigned'];
  162. /**
  163. * Noramlize a team slug from the query
  164. */
  165. export function getTeamParams(team?: string | string[]): string[] {
  166. if (team === undefined) {
  167. return ALERT_LIST_QUERY_DEFAULT_TEAMS;
  168. }
  169. if (team === '') {
  170. return [];
  171. }
  172. return toArray(team);
  173. }
  174. /**
  175. * Normalize an alert type string
  176. */
  177. export function getQueryAlertType(alertType?: string | string[]): CombinedAlertType[] {
  178. if (alertType === undefined) {
  179. return [];
  180. }
  181. if (alertType === '') {
  182. return [];
  183. }
  184. const validTypes = new Set(Object.values(CombinedAlertType));
  185. return [...validTypes.intersection(new Set(toArray(alertType)))];
  186. }