metricRulePresets.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import type {LinkProps} from 'sentry/components/links/link';
  2. import {t} from 'sentry/locale';
  3. import type {MRI, Project} from 'sentry/types';
  4. import {DisplayModes} from 'sentry/utils/discover/types';
  5. import {getDdmUrl, MetricDisplayType} from 'sentry/utils/metrics';
  6. import {parseField} from 'sentry/utils/metrics/mri';
  7. import type {TimePeriodType} from 'sentry/views/alerts/rules/metric/details/constants';
  8. import type {MetricRule} from 'sentry/views/alerts/rules/metric/types';
  9. import {isCustomMetricField} from 'sentry/views/alerts/rules/metric/utils/isCustomMetricField';
  10. import {getMetricRuleDiscoverUrl} from 'sentry/views/alerts/utils/getMetricRuleDiscoverUrl';
  11. interface PresetCta {
  12. /**
  13. * The CTA text
  14. */
  15. buttonText: string;
  16. /**
  17. * The location to direct to upon clicking the CTA.
  18. */
  19. to: LinkProps['to'];
  20. }
  21. interface PresetCtaOpts {
  22. orgSlug: string;
  23. projects: Project[];
  24. timePeriod: TimePeriodType;
  25. query?: string;
  26. rule?: MetricRule;
  27. }
  28. /**
  29. * Get the CTA used for alert rules that do not have a preset
  30. */
  31. export function makeDefaultCta({
  32. orgSlug,
  33. projects,
  34. rule,
  35. timePeriod,
  36. query,
  37. }: PresetCtaOpts): PresetCta {
  38. if (!rule) {
  39. return {
  40. buttonText: t('Open in Discover'),
  41. to: '',
  42. };
  43. }
  44. if (isCustomMetricField(rule.aggregate)) {
  45. const {mri, op} = parseField(rule.aggregate) ?? {};
  46. return {
  47. buttonText: t('Open in DDM'),
  48. to: getDdmUrl(orgSlug, {
  49. start: timePeriod.start,
  50. end: timePeriod.end,
  51. utc: timePeriod.utc,
  52. // 7 days are 9999m in alerts as of a rounding error in the `events-stats` endpoint
  53. // We need to round to 7d here to display it correctly in DDM
  54. statsPeriod: timePeriod.period === '9999m' ? '7d' : timePeriod.period,
  55. project: projects
  56. .filter(({slug}) => rule.projects.includes(slug))
  57. .map(project => project.id),
  58. environment: rule.environment ? [rule.environment] : [],
  59. widgets: [
  60. {
  61. mri: mri as MRI,
  62. op: op as string,
  63. query: rule.query,
  64. displayType: MetricDisplayType.AREA,
  65. },
  66. ],
  67. }),
  68. };
  69. }
  70. const extraQueryParams = {
  71. display: DisplayModes.TOP5,
  72. };
  73. return {
  74. buttonText: t('Open in Discover'),
  75. to: getMetricRuleDiscoverUrl({
  76. orgSlug,
  77. projects,
  78. rule,
  79. timePeriod,
  80. query,
  81. extraQueryParams,
  82. }),
  83. };
  84. }