metricRulePresets.tsx 3.1 KB

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