utils.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import {Dataset} from 'app/views/alerts/incidentRules/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(tags[sentry: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. };
  19. /**
  20. * Given an aggregate and dataset object, will return the corresponding wizard alert type
  21. * e.g. {aggregate: 'count()', dataset: 'events'} will yield 'num_errors'
  22. * @param template
  23. */
  24. export function getAlertTypeFromAggregateDataset({
  25. aggregate,
  26. dataset,
  27. }: Pick<WizardRuleTemplate, 'aggregate' | 'dataset'>): AlertType {
  28. const identifierForDataset = alertTypeIdentifiers[dataset];
  29. const matchingAlertTypeEntry = Object.entries(identifierForDataset).find(
  30. ([_alertType, identifier]) => identifier && aggregate.includes(identifier)
  31. );
  32. const alertType = matchingAlertTypeEntry && (matchingAlertTypeEntry[0] as AlertType);
  33. return alertType ? alertType : 'custom';
  34. }