useResourcePagesQuery.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 {
  8. DEFAULT_RESOURCE_FILTERS,
  9. getDomainFilter,
  10. getResourceTypeFilter,
  11. } from 'sentry/views/insights/browser/common/queries/useResourcesQuery';
  12. import {useResourceModuleFilters} from 'sentry/views/insights/browser/resources/utils/useResourceFilters';
  13. import {SpanMetricsField} from 'sentry/views/insights/types';
  14. const {SPAN_DOMAIN, SPAN_OP} = SpanMetricsField;
  15. /**
  16. * Gets a list of pages that have a resource.
  17. */
  18. export const useResourcePagesQuery = (
  19. defaultResourceTypes?: string[],
  20. search?: string
  21. ) => {
  22. const location = useLocation();
  23. const pageFilters = usePageFilters();
  24. const {slug: orgSlug} = useOrganization();
  25. const resourceFilters = useResourceModuleFilters();
  26. const {[SPAN_DOMAIN]: spanDomain} = resourceFilters;
  27. const fields = ['transaction', 'count()']; // count() is only here because an aggregation is required for the query to work
  28. const queryConditions = [
  29. ...DEFAULT_RESOURCE_FILTERS,
  30. ...getResourceTypeFilter(resourceFilters[SPAN_OP], defaultResourceTypes),
  31. ...getDomainFilter(spanDomain),
  32. ...(search && search.length > 0
  33. ? [`${SpanMetricsField.TRANSACTION}:*${[search]}*`]
  34. : []),
  35. ]; // TODO: We will need to consider other ops
  36. const eventView = EventView.fromNewQueryWithPageFilters(
  37. {
  38. fields, // for some reason we need a function, otherwise the query fails
  39. name: 'Resource module - page selector',
  40. version: 2,
  41. query: queryConditions.join(' '),
  42. dataset: DiscoverDatasets.SPANS_METRICS,
  43. },
  44. pageFilters.selection
  45. );
  46. const result = useDiscoverQuery({
  47. eventView,
  48. referrer: 'api.performance.browser.resources.page-selector',
  49. location,
  50. orgSlug,
  51. limit: 100,
  52. });
  53. const pages = result?.data?.data.map(row => row.transaction.toString()).sort() || [];
  54. return {...result, data: pages};
  55. };