useResourcePagesQuery.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import {useDiscoverQuery} from 'sentry/utils/discover/discoverQuery';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  4. import {useLocation} from 'sentry/utils/useLocation';
  5. import useOrganization from 'sentry/utils/useOrganization';
  6. import usePageFilters from 'sentry/utils/usePageFilters';
  7. import {useResourceModuleFilters} from 'sentry/views/performance/browser/resources/utils/useResourceFilters';
  8. import {SpanMetricsField} from 'sentry/views/starfish/types';
  9. const {SPAN_DOMAIN, SPAN_OP} = SpanMetricsField;
  10. /**
  11. * Gets a list of pages that have a resource.
  12. */
  13. export const useResourcePagesQuery = () => {
  14. const location = useLocation();
  15. const pageFilters = usePageFilters();
  16. const {slug: orgSlug} = useOrganization();
  17. const resourceFilters = useResourceModuleFilters();
  18. const {[SPAN_DOMAIN]: spanDomain} = resourceFilters;
  19. const fields = ['transaction', 'count()']; // count() is only here because an aggregation is required for the query to work
  20. const queryConditions = [
  21. `${SPAN_OP}:${resourceFilters.type || 'resource.*'}`,
  22. ...(spanDomain ? [`${SPAN_DOMAIN}:${spanDomain}`] : []),
  23. ]; // TODO: We will need to consider other ops
  24. const eventView = EventView.fromNewQueryWithPageFilters(
  25. {
  26. fields, // for some reason we need a function, otherwise the query fails
  27. name: 'Resource module - page selector',
  28. version: 2,
  29. query: queryConditions.join(' '),
  30. dataset: DiscoverDatasets.SPANS_METRICS,
  31. },
  32. pageFilters.selection
  33. );
  34. const result = useDiscoverQuery({
  35. eventView,
  36. referrer: 'api.performance.browser.resources.page-selector',
  37. location,
  38. orgSlug,
  39. limit: 100,
  40. });
  41. const pages = result?.data?.data.map(row => row.transaction.toString()).sort() || [];
  42. return {...result, data: pages};
  43. };