useResourceDomansQuery.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import * as Sentry from '@sentry/react';
  2. import {useDiscoverQuery} from 'sentry/utils/discover/discoverQuery';
  3. import EventView from 'sentry/utils/discover/eventView';
  4. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  5. import {useLocation} from 'sentry/utils/useLocation';
  6. import useOrganization from 'sentry/utils/useOrganization';
  7. import usePageFilters from 'sentry/utils/usePageFilters';
  8. import {useResourceModuleFilters} from 'sentry/views/performance/browser/resources/utils/useResourceFilters';
  9. import {SpanMetricsField} from 'sentry/views/starfish/types';
  10. const {SPAN_DOMAIN, SPAN_OP} = SpanMetricsField;
  11. /**
  12. * Gets a list of pages that have a resource.
  13. */
  14. export const useResourceDomainsQuery = () => {
  15. const location = useLocation();
  16. const pageFilters = usePageFilters();
  17. const {slug: orgSlug} = useOrganization();
  18. const {transaction} = useResourceModuleFilters();
  19. const fields = [SPAN_DOMAIN, 'count()']; // count() is only here because an aggregation is required for the query to work
  20. const queryConditions = [
  21. `${SPAN_OP}:resource.*`,
  22. `has:${SPAN_DOMAIN}`,
  23. ...(transaction ? [`transaction:${transaction}`] : []),
  24. ];
  25. const eventView = EventView.fromNewQueryWithPageFilters(
  26. {
  27. fields, // for some reason we need a function, otherwise the query fails
  28. name: 'Resource module - page selector',
  29. version: 2,
  30. query: queryConditions.join(' '),
  31. dataset: DiscoverDatasets.SPANS_METRICS,
  32. },
  33. pageFilters.selection
  34. );
  35. const result = useDiscoverQuery({
  36. eventView,
  37. referrer: 'api.performance.browser.resources.page-selector',
  38. location,
  39. orgSlug,
  40. limit: 100,
  41. });
  42. const data: string[] =
  43. result?.data?.data
  44. .map(row => {
  45. const domains = row[SPAN_DOMAIN] as any as string[];
  46. if (domains?.length > 1) {
  47. Sentry.captureException(new Error('More than one domain found in a resource'));
  48. }
  49. return domains[0].toString();
  50. })
  51. .sort() || [];
  52. return {...result, data};
  53. };