metricRulePresets.tsx 2.6 KB

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