metricRulePresets.tsx 2.5 KB

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