utils.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {getUseCaseFromMRI, parseField} from 'sentry/utils/metrics/mri';
  2. import {Dataset, SessionsAggregate} from 'sentry/views/alerts/rules/metric/types';
  3. import {isInsightsMetricAlert} from 'sentry/views/alerts/rules/metric/utils/isInsightsMetricAlert';
  4. import type {MetricAlertType, WizardRuleTemplate} from './options';
  5. // A set of unique identifiers to be able to tie aggregate and dataset back to a wizard alert type
  6. const alertTypeIdentifiers: Record<
  7. Exclude<Dataset, Dataset.ISSUE_PLATFORM | Dataset.REPLAYS>, // IssuePlatform (search_issues) is not used in alerts, so we can exclude it here
  8. Partial<Record<MetricAlertType, string>>
  9. > = {
  10. [Dataset.ERRORS]: {
  11. num_errors: 'count()',
  12. users_experiencing_errors: 'count_unique(user)',
  13. },
  14. [Dataset.TRANSACTIONS]: {
  15. throughput: 'count()',
  16. trans_duration: 'transaction.duration',
  17. apdex: 'apdex',
  18. failure_rate: 'failure_rate()',
  19. lcp: 'measurements.lcp',
  20. fid: 'measurements.fid',
  21. cls: 'measurements.cls',
  22. },
  23. [Dataset.GENERIC_METRICS]: {
  24. throughput: 'count()',
  25. trans_duration: 'transaction.duration',
  26. apdex: 'apdex',
  27. failure_rate: 'failure_rate()',
  28. lcp: 'measurements.lcp',
  29. fid: 'measurements.fid',
  30. cls: 'measurements.cls',
  31. },
  32. [Dataset.SESSIONS]: {
  33. crash_free_sessions: SessionsAggregate.CRASH_FREE_SESSIONS,
  34. crash_free_users: SessionsAggregate.CRASH_FREE_USERS,
  35. },
  36. [Dataset.METRICS]: {
  37. crash_free_sessions: SessionsAggregate.CRASH_FREE_SESSIONS,
  38. crash_free_users: SessionsAggregate.CRASH_FREE_USERS,
  39. },
  40. };
  41. /**
  42. * Given an aggregate and dataset object, will return the corresponding wizard alert type
  43. * e.g. {aggregate: 'count()', dataset: 'events'} will yield 'num_errors'
  44. * @param template
  45. */
  46. export function getAlertTypeFromAggregateDataset({
  47. aggregate,
  48. dataset,
  49. }: Pick<WizardRuleTemplate, 'aggregate' | 'dataset'>): MetricAlertType {
  50. const {mri: mri} = parseField(aggregate) ?? {};
  51. if (isInsightsMetricAlert(aggregate)) {
  52. return 'insights_metrics';
  53. }
  54. if (mri && getUseCaseFromMRI(mri) === 'spans') {
  55. return 'custom_metrics';
  56. }
  57. if (mri && getUseCaseFromMRI(mri) === 'custom') {
  58. return 'custom_metrics';
  59. }
  60. const identifierForDataset = alertTypeIdentifiers[dataset];
  61. const matchingAlertTypeEntry = Object.entries(identifierForDataset).find(
  62. ([_alertType, identifier]) => identifier && aggregate.includes(identifier as string)
  63. );
  64. const alertType =
  65. matchingAlertTypeEntry && (matchingAlertTypeEntry[0] as MetricAlertType);
  66. return alertType ? alertType : 'custom_transactions';
  67. }