utils.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import {Dataset, SessionsAggregate} from 'sentry/views/alerts/rules/metric/types';
  2. import {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<Dataset, Partial<Record<MetricAlertType, 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.GENERIC_METRICS]: {
  19. throughput: 'count()',
  20. trans_duration: 'transaction.duration',
  21. apdex: 'apdex',
  22. failure_rate: 'failure_rate()',
  23. lcp: 'measurements.lcp',
  24. fid: 'measurements.fid',
  25. cls: 'measurements.cls',
  26. },
  27. [Dataset.SESSIONS]: {
  28. crash_free_sessions: SessionsAggregate.CRASH_FREE_SESSIONS,
  29. crash_free_users: SessionsAggregate.CRASH_FREE_USERS,
  30. },
  31. [Dataset.METRICS]: {
  32. crash_free_sessions: SessionsAggregate.CRASH_FREE_SESSIONS,
  33. crash_free_users: SessionsAggregate.CRASH_FREE_USERS,
  34. },
  35. };
  36. /**
  37. * Given an aggregate and dataset object, will return the corresponding wizard alert type
  38. * e.g. {aggregate: 'count()', dataset: 'events'} will yield 'num_errors'
  39. * @param template
  40. */
  41. export function getAlertTypeFromAggregateDataset({
  42. aggregate,
  43. dataset,
  44. }: Pick<WizardRuleTemplate, 'aggregate' | 'dataset'>): MetricAlertType {
  45. const identifierForDataset = alertTypeIdentifiers[dataset];
  46. const matchingAlertTypeEntry = Object.entries(identifierForDataset).find(
  47. ([_alertType, identifier]) => identifier && aggregate.includes(identifier)
  48. );
  49. const alertType =
  50. matchingAlertTypeEntry && (matchingAlertTypeEntry[0] as MetricAlertType);
  51. return alertType ? alertType : 'custom';
  52. }