utils.tsx 2.1 KB

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