getIncidentDiscoverUrl.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import type {NewQuery} from 'sentry/types/organization';
  2. import type {Project} from 'sentry/types/project';
  3. import EventView from 'sentry/utils/discover/eventView';
  4. import {getAggregateAlias} from 'sentry/utils/discover/fields';
  5. import {Dataset} from 'sentry/views/alerts/rules/metric/types';
  6. import type {Incident, IncidentStats} from 'sentry/views/alerts/types';
  7. import {getStartEndFromStats} from 'sentry/views/alerts/utils';
  8. /**
  9. * Gets the URL for a discover view of the incident with the following default
  10. * parameters:
  11. *
  12. * - Ordered by the incident aggregate, descending
  13. * - yAxis maps to the aggregate
  14. * - The following fields are displayed:
  15. * - For Error dataset alerts: [issue, count(), count_unique(user)]
  16. * - For Transaction dataset alerts: [transaction, count()]
  17. * - Start and end are scoped to the same period as the alert rule
  18. */
  19. export function getIncidentDiscoverUrl(opts: {
  20. orgSlug: string;
  21. projects: Project[];
  22. extraQueryParams?: Partial<NewQuery>;
  23. incident?: Incident;
  24. stats?: IncidentStats;
  25. }) {
  26. const {orgSlug, projects, incident, stats, extraQueryParams} = opts;
  27. if (!projects || !projects.length || !incident || !stats) {
  28. return '';
  29. }
  30. const timeWindowString = `${incident.alertRule.timeWindow}m`;
  31. const {start, end} = getStartEndFromStats(stats);
  32. const discoverQuery: NewQuery = {
  33. id: undefined,
  34. name: incident?.title || '',
  35. orderby: `-${getAggregateAlias(incident.alertRule.aggregate)}`,
  36. yAxis: incident.alertRule.aggregate ? [incident.alertRule.aggregate] : undefined,
  37. query: incident?.discoverQuery ?? '',
  38. projects: projects
  39. .filter(({slug}) => incident.projects.includes(slug))
  40. .map(({id}) => Number(id)),
  41. version: 2,
  42. fields:
  43. incident.alertRule.dataset === Dataset.ERRORS
  44. ? ['issue', 'count()', 'count_unique(user)']
  45. : ['transaction', incident.alertRule.aggregate],
  46. start,
  47. end,
  48. ...extraQueryParams,
  49. };
  50. const discoverView = EventView.fromSavedQuery(discoverQuery);
  51. const {query, ...toObject} = discoverView.getResultsViewUrlTarget(orgSlug);
  52. return {
  53. query: {...query, interval: timeWindowString},
  54. ...toObject,
  55. };
  56. }