metricRulePresets.tsx 2.8 KB

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