utils.tsx 2.4 KB

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