index.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 enum DatasetOption {
  35. ALL = 'all',
  36. ERRORS = 'errors',
  37. SESSIONS = 'sessions',
  38. PERFORMANCE = 'performance',
  39. }
  40. export const DATA_SOURCE_LABELS = {
  41. [Dataset.ERRORS]: t('Errors'),
  42. [Dataset.TRANSACTIONS]: t('Transactions'),
  43. [Datasource.ERROR_DEFAULT]: 'event.type:error OR event.type:default',
  44. [Datasource.ERROR]: 'event.type:error',
  45. [Datasource.DEFAULT]: 'event.type:default',
  46. [Datasource.TRANSACTION]: 'event.type:transaction',
  47. };
  48. // Maps a datasource to the relevant dataset and event_types for the backend to use
  49. export const DATA_SOURCE_TO_SET_AND_EVENT_TYPES = {
  50. [Datasource.ERROR_DEFAULT]: {
  51. dataset: Dataset.ERRORS,
  52. eventTypes: [EventTypes.ERROR, EventTypes.DEFAULT],
  53. },
  54. [Datasource.ERROR]: {
  55. dataset: Dataset.ERRORS,
  56. eventTypes: [EventTypes.ERROR],
  57. },
  58. [Datasource.DEFAULT]: {
  59. dataset: Dataset.ERRORS,
  60. eventTypes: [EventTypes.DEFAULT],
  61. },
  62. [Datasource.TRANSACTION]: {
  63. dataset: Dataset.TRANSACTIONS,
  64. eventTypes: [EventTypes.TRANSACTION],
  65. },
  66. };
  67. // Converts the given dataset and event types array to a datasource for the datasource dropdown
  68. export function convertDatasetEventTypesToSource(
  69. dataset: Dataset,
  70. eventTypes: EventTypes[]
  71. ) {
  72. // transactions and generic_metrics only have one datasource option regardless of event type
  73. if (dataset === Dataset.TRANSACTIONS || dataset === Dataset.GENERIC_METRICS) {
  74. return Datasource.TRANSACTION;
  75. }
  76. // if no event type was provided use the default datasource
  77. if (!eventTypes) {
  78. return Datasource.ERROR;
  79. }
  80. if (eventTypes.includes(EventTypes.DEFAULT) && eventTypes.includes(EventTypes.ERROR)) {
  81. return Datasource.ERROR_DEFAULT;
  82. }
  83. if (eventTypes.includes(EventTypes.DEFAULT)) {
  84. return Datasource.DEFAULT;
  85. }
  86. return Datasource.ERROR;
  87. }
  88. /**
  89. * Attempt to guess the data source of a discover query
  90. *
  91. * @returns An object containing the datasource and new query without the datasource.
  92. * Returns null on no datasource.
  93. */
  94. export function getQueryDatasource(
  95. query: string
  96. ): {query: string; source: Datasource} | null {
  97. let match = query.match(
  98. /\(?\bevent\.type:(error|default|transaction)\)?\WOR\W\(?event\.type:(error|default|transaction)\)?/i
  99. );
  100. if (match) {
  101. // should be [error, default] or [default, error]
  102. const eventTypes = match.slice(1, 3).sort().join(',');
  103. if (eventTypes !== 'default,error') {
  104. return null;
  105. }
  106. return {source: Datasource.ERROR_DEFAULT, query: query.replace(match[0], '').trim()};
  107. }
  108. match = query.match(/(^|\s)event\.type:(error|default|transaction)/i);
  109. if (match && Datasource[match[2].toUpperCase()]) {
  110. return {
  111. source: Datasource[match[2].toUpperCase()],
  112. query: query.replace(match[0], '').trim(),
  113. };
  114. }
  115. return null;
  116. }
  117. export function isSessionAggregate(aggregate: string) {
  118. return Object.values(SessionsAggregate).includes(aggregate as SessionsAggregate);
  119. }
  120. export const SESSION_AGGREGATE_TO_FIELD = {
  121. [SessionsAggregate.CRASH_FREE_SESSIONS]: SessionFieldWithOperation.SESSIONS,
  122. [SessionsAggregate.CRASH_FREE_USERS]: SessionFieldWithOperation.USERS,
  123. };
  124. export function alertAxisFormatter(value: number, seriesName: string, aggregate: string) {
  125. if (isSessionAggregate(aggregate)) {
  126. return defined(value) ? `${round(value, 2)}%` : '\u2015';
  127. }
  128. return axisLabelFormatter(value, aggregateOutputType(seriesName));
  129. }
  130. export function alertTooltipValueFormatter(
  131. value: number,
  132. seriesName: string,
  133. aggregate: string
  134. ) {
  135. if (isSessionAggregate(aggregate)) {
  136. return defined(value) ? `${value}%` : '\u2015';
  137. }
  138. return tooltipFormatter(value, aggregateOutputType(seriesName));
  139. }
  140. export const ALERT_CHART_MIN_MAX_BUFFER = 1.03;
  141. export function shouldScaleAlertChart(aggregate: string) {
  142. // We want crash free rate charts to be scaled because they are usually too
  143. // close to 100% and therefore too fine to see the spikes on 0%-100% scale.
  144. return isSessionAggregate(aggregate);
  145. }
  146. export function alertDetailsLink(organization: Organization, incident: Incident) {
  147. return `/organizations/${organization.slug}/alerts/rules/details/${
  148. incident.alertRule.status === AlertRuleStatus.SNAPSHOT &&
  149. incident.alertRule.originalAlertRuleId
  150. ? incident.alertRule.originalAlertRuleId
  151. : incident.alertRule.id
  152. }/`;
  153. }
  154. /**
  155. * Noramlizes a status string
  156. */
  157. export function getQueryStatus(status: string | string[]): string {
  158. if (Array.isArray(status) || status === '') {
  159. return 'all';
  160. }
  161. return ['open', 'closed'].includes(status) ? status : 'all';
  162. }
  163. const ALERT_LIST_QUERY_DEFAULT_TEAMS = ['myteams', 'unassigned'];
  164. /**
  165. * Noramlize a team slug from the query
  166. */
  167. export function getTeamParams(team?: string | string[]): string[] {
  168. if (team === undefined) {
  169. return ALERT_LIST_QUERY_DEFAULT_TEAMS;
  170. }
  171. if (team === '') {
  172. return [];
  173. }
  174. return toArray(team);
  175. }
  176. const datasetValues = new Set(Object.values(DatasetOption));
  177. export function getQueryDataset(dataset: any): DatasetOption {
  178. if ((datasetValues as Set<any>).has(dataset)) {
  179. return dataset as DatasetOption;
  180. }
  181. return DatasetOption.ALL;
  182. }
  183. export const datasetToQueryParam: Record<DatasetOption, Dataset[] | undefined> = {
  184. [DatasetOption.ALL]: undefined,
  185. [DatasetOption.ERRORS]: [Dataset.ERRORS],
  186. [DatasetOption.SESSIONS]: [Dataset.METRICS],
  187. [DatasetOption.PERFORMANCE]: [
  188. Dataset.GENERIC_METRICS,
  189. // TODO(telemetry-experience): remove this once we migrated all performance alerts to generic metrics
  190. Dataset.TRANSACTIONS,
  191. ],
  192. };