getAlertsUrl.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. project: Project;
  20. dataset?: Dataset;
  21. interval?: string;
  22. name?: string;
  23. query?: string;
  24. }) {
  25. const statsPeriod = getStatsPeriod(pageFilters);
  26. const environment = pageFilters.environments;
  27. const queryParams = {
  28. aggregate: aggregate,
  29. dataset,
  30. project: project.slug,
  31. eventTypes: 'transaction',
  32. query,
  33. statsPeriod,
  34. environment,
  35. name,
  36. interval,
  37. };
  38. return normalizeUrl(
  39. `/organizations/${orgSlug}/alerts/new/metric/?${qs.stringify(queryParams)}`
  40. );
  41. }
  42. // Alert rules only support 24h, 3d, 7d, 14d periods
  43. function getStatsPeriod(pageFilters: PageFilters) {
  44. const {period} = pageFilters.datetime;
  45. switch (period) {
  46. case '24h':
  47. case '3d':
  48. case '7d':
  49. case '14d':
  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. }