metricRulePresets.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 {DiscoverDatasets, 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. dataset?: DiscoverDatasets;
  26. query?: string;
  27. rule?: MetricRule;
  28. }
  29. /**
  30. * Get the CTA used for alert rules that do not have a preset
  31. */
  32. export function makeDefaultCta({
  33. orgSlug,
  34. projects,
  35. rule,
  36. timePeriod,
  37. query,
  38. dataset,
  39. }: PresetCtaOpts): PresetCta {
  40. if (!rule) {
  41. return {
  42. buttonText: t('Open in Discover'),
  43. to: '',
  44. };
  45. }
  46. if (isCustomMetricField(rule.aggregate)) {
  47. const {mri, op} = parseField(rule.aggregate) ?? {};
  48. return {
  49. buttonText: t('Open in DDM'),
  50. to: getDdmUrl(orgSlug, {
  51. start: timePeriod.start,
  52. end: timePeriod.end,
  53. utc: timePeriod.utc,
  54. // 7 days are 9999m in alerts as of a rounding error in the `events-stats` endpoint
  55. // We need to round to 7d here to display it correctly in DDM
  56. statsPeriod: timePeriod.period === '9999m' ? '7d' : timePeriod.period,
  57. project: projects
  58. .filter(({slug}) => rule.projects.includes(slug))
  59. .map(project => project.id),
  60. environment: rule.environment ? [rule.environment] : [],
  61. widgets: [
  62. {
  63. mri: mri as MRI,
  64. op: op as string,
  65. query: rule.query,
  66. displayType: MetricDisplayType.AREA,
  67. },
  68. ],
  69. }),
  70. };
  71. }
  72. const extraQueryParams = {
  73. display: DisplayModes.DEFAULT,
  74. dataset,
  75. };
  76. return {
  77. buttonText: t('Open in Discover'),
  78. to: getMetricRuleDiscoverUrl({
  79. orgSlug,
  80. projects,
  81. rule,
  82. timePeriod,
  83. query,
  84. extraQueryParams,
  85. }),
  86. };
  87. }