useIndexedResourceQuery.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {useDiscoverQuery} from 'sentry/utils/discover/discoverQuery';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import {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/starfish/types';
  9. const {SPAN_DESCRIPTION, HTTP_RESPONSE_CONTENT_LENGTH} = 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. SPAN_DESCRIPTION,
  35. `measurements.${HTTP_RESPONSE_CONTENT_LENGTH}`,
  36. ],
  37. name: 'Indexed Resource Query',
  38. query: queryConditions.join(' '),
  39. version: 2,
  40. dataset: DiscoverDatasets.SPANS_INDEXED,
  41. },
  42. pageFilters.selection
  43. );
  44. if (sorts) {
  45. eventView.sorts = sorts;
  46. }
  47. const result = useDiscoverQuery({
  48. eventView,
  49. limit,
  50. location,
  51. orgSlug,
  52. referrer,
  53. options: {
  54. enabled,
  55. refetchOnWindowFocus: false,
  56. },
  57. });
  58. const data =
  59. result?.data?.data.map(row => ({
  60. project: row.project as string,
  61. 'transaction.id': row['transaction.id'] as string,
  62. [SPAN_DESCRIPTION]: row[SPAN_DESCRIPTION]?.toString(),
  63. 'measurements.http.response_content_length': row[
  64. `measurements.${HTTP_RESPONSE_CONTENT_LENGTH}`
  65. ] as number,
  66. })) ?? [];
  67. return {...result, data};
  68. };