getIncidentRuleDiscoverUrl.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import {NewQuery, Project} from 'app/types';
  2. import EventView from 'app/utils/discover/eventView';
  3. import {getAggregateAlias} from 'app/utils/discover/fields';
  4. import {Dataset, IncidentRule} from 'app/views/alerts/incidentRules/types';
  5. /**
  6. * Gets the URL for a discover view of the rule with the following default
  7. * parameters:
  8. *
  9. * - Ordered by the rule aggregate, descending
  10. * - yAxis maps to the aggregate
  11. * - The following fields are displayed:
  12. * - For Error dataset alert rules: [issue, count(), count_unique(user)]
  13. * - For Transaction dataset alert rules: [transaction, count()]
  14. * - Start and end are the period's values selected in the chart header
  15. */
  16. export function getIncidentRuleDiscoverUrl(opts: {
  17. orgSlug: string;
  18. projects: Project[];
  19. rule?: IncidentRule;
  20. eventType?: string;
  21. start?: string;
  22. end?: string;
  23. extraQueryParams?: Partial<NewQuery>;
  24. }) {
  25. const {orgSlug, projects, rule, eventType, start, end, extraQueryParams} = opts;
  26. const eventTypeTagFilter = eventType && rule?.query ? eventType : '';
  27. if (!projects || !projects.length || !rule || (!start && !end)) {
  28. return '';
  29. }
  30. const timeWindowString = `${rule.timeWindow}m`;
  31. const discoverQuery: NewQuery = {
  32. id: undefined,
  33. name: (rule && rule.name) || '',
  34. orderby: `-${getAggregateAlias(rule.aggregate)}`,
  35. yAxis: rule.aggregate ? [rule.aggregate] : undefined,
  36. query: (eventTypeTagFilter || rule?.query || eventType) ?? '',
  37. projects: projects
  38. .filter(({slug}) => rule.projects.includes(slug))
  39. .map(({id}) => Number(id)),
  40. version: 2,
  41. fields:
  42. rule.dataset === Dataset.ERRORS
  43. ? ['issue', 'count()', 'count_unique(user)']
  44. : ['transaction', rule.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. }