useIssueDetailsDiscoverQuery.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import {getInterval} from 'sentry/components/charts/utils';
  2. import type {Group} from 'sentry/types/group';
  3. import type {NewQuery, SavedQuery} from 'sentry/types/organization';
  4. import EventView from 'sentry/utils/discover/eventView';
  5. import {
  6. type DiscoverQueryProps,
  7. useGenericDiscoverQuery,
  8. } from 'sentry/utils/discover/genericDiscoverQuery';
  9. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  10. import {getPeriod} from 'sentry/utils/duration/getPeriod';
  11. import {getConfigForIssueType} from 'sentry/utils/issueTypeConfig';
  12. import {useLocation} from 'sentry/utils/useLocation';
  13. import useOrganization from 'sentry/utils/useOrganization';
  14. import usePageFilters from 'sentry/utils/usePageFilters';
  15. import {useEventQuery} from 'sentry/views/issueDetails/streamline/eventSearch';
  16. export function useIssueDetailsEventView({
  17. group,
  18. queryProps,
  19. }: {
  20. group: Group;
  21. queryProps?: Partial<SavedQuery>;
  22. }) {
  23. const {selection: pageFilters} = usePageFilters();
  24. const searchQuery = useEventQuery({group});
  25. const periodQuery = getPeriod(pageFilters.datetime);
  26. const interval = getInterval(pageFilters.datetime, 'low');
  27. const config = getConfigForIssueType(group, group.project);
  28. const {query: propQuery = '', ...overrideQueryProps} = queryProps ?? {};
  29. const query = [`issue:${group.shortId}`, searchQuery, propQuery]
  30. .filter(s => s.length > 0)
  31. .join(' ');
  32. const discoverQuery: NewQuery = {
  33. ...periodQuery,
  34. interval,
  35. environment: pageFilters.environments,
  36. dataset: config.usesIssuePlatform
  37. ? DiscoverDatasets.ISSUE_PLATFORM
  38. : DiscoverDatasets.ERRORS,
  39. version: 2,
  40. projects: [Number(group.project.id)],
  41. range: periodQuery.statsPeriod,
  42. yAxis: ['count()', 'count_unique(user)'],
  43. fields: ['title', 'release', 'environment', 'user.display', 'timestamp'],
  44. name: group.title || group.type,
  45. query,
  46. ...overrideQueryProps,
  47. };
  48. return EventView.fromSavedQuery(discoverQuery);
  49. }
  50. export function useIssueDetailsDiscoverQuery<T>({
  51. params: {eventView, route, referrer},
  52. options,
  53. }: {
  54. params: {
  55. eventView: EventView;
  56. referrer: string;
  57. route: string;
  58. };
  59. options?: DiscoverQueryProps['options'];
  60. }) {
  61. const organization = useOrganization();
  62. const location = useLocation();
  63. return useGenericDiscoverQuery<T, DiscoverQueryProps>({
  64. route,
  65. eventView,
  66. location,
  67. orgSlug: organization.slug,
  68. getRequestPayload: () => ({
  69. ...eventView.getEventsAPIPayload(location),
  70. query: eventView.query,
  71. interval: eventView.interval,
  72. yAxis: eventView.yAxis,
  73. partial: 1,
  74. }),
  75. options,
  76. referrer,
  77. });
  78. }