useIndexedResourceQuery.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {useDiscoverQuery} from 'sentry/utils/discover/discoverQuery';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import type {Sort} from 'sentry/utils/discover/fields';
  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 {SpanIndexedField} from 'sentry/views/insights/types';
  9. const {SPAN_DESCRIPTION, HTTP_RESPONSE_CONTENT_LENGTH, RAW_DOMAIN} = SpanIndexedField;
  10. type Options = {
  11. enabled?: boolean;
  12. limit?: number;
  13. queryConditions?: string[];
  14. referrer?: string;
  15. sorts?: Sort[];
  16. };
  17. export const useIndexedResourcesQuery = ({
  18. queryConditions = [],
  19. limit = 50,
  20. sorts,
  21. referrer,
  22. enabled = true,
  23. }: Options) => {
  24. const pageFilters = usePageFilters();
  25. const location = useLocation();
  26. const {slug: orgSlug} = useOrganization();
  27. // TODO - we should be using metrics data here
  28. const eventView = EventView.fromNewQueryWithPageFilters(
  29. {
  30. fields: [
  31. `any(id)`,
  32. 'project',
  33. 'span.group',
  34. RAW_DOMAIN,
  35. SPAN_DESCRIPTION,
  36. `measurements.${HTTP_RESPONSE_CONTENT_LENGTH}`,
  37. ],
  38. name: 'Indexed Resource Query',
  39. query: queryConditions.join(' '),
  40. version: 2,
  41. dataset: DiscoverDatasets.SPANS_INDEXED,
  42. },
  43. pageFilters.selection
  44. );
  45. if (sorts) {
  46. eventView.sorts = sorts;
  47. }
  48. const result = useDiscoverQuery({
  49. eventView,
  50. limit,
  51. location,
  52. orgSlug,
  53. referrer,
  54. options: {
  55. enabled,
  56. refetchOnWindowFocus: false,
  57. },
  58. });
  59. const data =
  60. result?.data?.data.map(row => ({
  61. project: row.project as string,
  62. 'transaction.id': row['transaction.id'] as string,
  63. [SPAN_DESCRIPTION]: row[SPAN_DESCRIPTION]?.toString(),
  64. [RAW_DOMAIN]: row[RAW_DOMAIN]?.toString(),
  65. 'measurements.http.response_content_length': row[
  66. `measurements.${HTTP_RESPONSE_CONTENT_LENGTH}`
  67. ] as number,
  68. })) ?? [];
  69. return {...result, data};
  70. };