utils.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {Dataset, SessionsAggregate} from 'sentry/views/alerts/rules/metric/types';
  2. import {AlertType, 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<Dataset, Partial<Record<AlertType, string>>> = {
  5. [Dataset.ERRORS]: {
  6. num_errors: 'count()',
  7. users_experiencing_errors: 'count_unique(user)',
  8. },
  9. [Dataset.TRANSACTIONS]: {
  10. throughput: 'count()',
  11. trans_duration: 'transaction.duration',
  12. apdex: 'apdex',
  13. failure_rate: 'failure_rate()',
  14. lcp: 'measurements.lcp',
  15. fid: 'measurements.fid',
  16. cls: 'measurements.cls',
  17. },
  18. [Dataset.SESSIONS]: {
  19. crash_free_sessions: SessionsAggregate.CRASH_FREE_SESSIONS,
  20. crash_free_users: SessionsAggregate.CRASH_FREE_USERS,
  21. },
  22. [Dataset.METRICS]: {
  23. crash_free_sessions: SessionsAggregate.CRASH_FREE_SESSIONS,
  24. crash_free_users: SessionsAggregate.CRASH_FREE_USERS,
  25. },
  26. };
  27. /**
  28. * Given an aggregate and dataset object, will return the corresponding wizard alert type
  29. * e.g. {aggregate: 'count()', dataset: 'events'} will yield 'num_errors'
  30. * @param template
  31. */
  32. export function getAlertTypeFromAggregateDataset({
  33. aggregate,
  34. dataset,
  35. }: Pick<WizardRuleTemplate, 'aggregate' | 'dataset'>): AlertType {
  36. const identifierForDataset = alertTypeIdentifiers[dataset];
  37. const matchingAlertTypeEntry = Object.entries(identifierForDataset).find(
  38. ([_alertType, identifier]) => identifier && aggregate.includes(identifier)
  39. );
  40. const alertType = matchingAlertTypeEntry && (matchingAlertTypeEntry[0] as AlertType);
  41. return alertType ? alertType : 'custom';
  42. }