getIncidentDiscoverUrl.tsx 2.1 KB

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