getAlertsUrl.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import * as qs from 'query-string';
  2. import type {PageFilters} from 'sentry/types/core';
  3. import type {Project} from 'sentry/types/project';
  4. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  5. import {Dataset} from 'sentry/views/alerts/rules/metric/types';
  6. export function getAlertsUrl({
  7. project,
  8. query,
  9. aggregate,
  10. orgSlug,
  11. pageFilters,
  12. name,
  13. interval,
  14. dataset = Dataset.GENERIC_METRICS,
  15. }: {
  16. aggregate: string;
  17. orgSlug: string;
  18. pageFilters: PageFilters;
  19. dataset?: Dataset;
  20. interval?: string;
  21. name?: string;
  22. project?: Project;
  23. query?: string;
  24. }) {
  25. const statsPeriod = getStatsPeriod(pageFilters);
  26. const environment = pageFilters.environments;
  27. const supportedInterval = getInterval(interval);
  28. const queryParams = {
  29. aggregate,
  30. dataset,
  31. project: project?.slug,
  32. eventTypes: 'transaction',
  33. query,
  34. statsPeriod,
  35. environment,
  36. name,
  37. interval: supportedInterval,
  38. };
  39. return normalizeUrl(
  40. `/organizations/${orgSlug}/alerts/new/metric/?${qs.stringify(queryParams)}`
  41. );
  42. }
  43. // Alert rules only support 24h, 3d, 7d, 14d periods
  44. function getStatsPeriod(pageFilters: PageFilters) {
  45. const {period} = pageFilters.datetime;
  46. switch (period) {
  47. case '24h':
  48. case '3d':
  49. case '7d':
  50. return period;
  51. case '1h':
  52. return '24h'; // Explore allows 1h, but alerts only allows 24h minimum
  53. default:
  54. return '7d';
  55. }
  56. }
  57. function getInterval(interval?: string) {
  58. switch (interval) {
  59. case '5m':
  60. case '15m':
  61. case '30m':
  62. case '1h':
  63. case '3h':
  64. case '4h':
  65. case '6h':
  66. case '24h':
  67. return interval;
  68. case '1m':
  69. return '5m'; // Explore allows 1m, but alerts only allows 5m minimum
  70. default:
  71. return '1h';
  72. }
  73. }