useResourcesQuery.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 {ValidSort} from 'sentry/views/performance/browser/resources/utils/useResourceSort';
  9. import {SpanMetricsField} from 'sentry/views/starfish/types';
  10. const {
  11. SPAN_DOMAIN,
  12. SPAN_GROUP,
  13. SPAN_DESCRIPTION,
  14. SPAN_OP,
  15. SPAN_SELF_TIME,
  16. RESOURCE_RENDER_BLOCKING_STATUS,
  17. HTTP_RESPONSE_CONTENT_LENGTH,
  18. } = SpanMetricsField;
  19. type Props = {
  20. sort: ValidSort;
  21. defaultResourceTypes?: string[];
  22. };
  23. export const useResourcesQuery = ({sort, defaultResourceTypes}: Props) => {
  24. const pageFilters = usePageFilters();
  25. const location = useLocation();
  26. const resourceFilters = useResourceModuleFilters();
  27. const {slug: orgSlug} = useOrganization();
  28. const queryConditions = [
  29. `${SPAN_OP}:${
  30. resourceFilters.type || `[${defaultResourceTypes?.join(',')}]` || 'resource.*'
  31. }`,
  32. ...(resourceFilters.transaction
  33. ? [`transaction:"${resourceFilters.transaction}"`]
  34. : []),
  35. ...(resourceFilters[SPAN_DOMAIN]
  36. ? [`${SPAN_DOMAIN}:${resourceFilters[SPAN_DOMAIN]}`]
  37. : []),
  38. ...(resourceFilters['resource.render_blocking_status']
  39. ? [
  40. `resource.render_blocking_status:${resourceFilters['resource.render_blocking_status']}`,
  41. ]
  42. : [`!resource.render_blocking_status:blocking`]),
  43. ];
  44. // TODO - we should be using metrics data here
  45. const eventView = EventView.fromNewQueryWithPageFilters(
  46. {
  47. fields: [
  48. SPAN_DESCRIPTION,
  49. SPAN_OP,
  50. 'count()',
  51. `avg(${SPAN_SELF_TIME})`,
  52. 'spm()',
  53. SPAN_GROUP,
  54. SPAN_DOMAIN,
  55. `avg(${HTTP_RESPONSE_CONTENT_LENGTH})`,
  56. ],
  57. name: 'Resource module - resource table',
  58. query: queryConditions.join(' '),
  59. orderby: '-count',
  60. version: 2,
  61. dataset: DiscoverDatasets.SPANS_METRICS,
  62. },
  63. pageFilters.selection
  64. );
  65. if (sort) {
  66. eventView.sorts = [sort];
  67. }
  68. const result = useDiscoverQuery({eventView, limit: 100, location, orgSlug});
  69. const data = result?.data?.data.map(row => ({
  70. [SPAN_OP]: row[SPAN_OP].toString() as `resource.${string}`,
  71. [SPAN_DESCRIPTION]: row[SPAN_DESCRIPTION].toString(),
  72. ['avg(span.self_time)']: row[`avg(${SPAN_SELF_TIME})`] as number,
  73. 'count()': row['count()'] as number,
  74. 'spm()': row['spm()'] as number,
  75. [SPAN_GROUP]: row[SPAN_GROUP].toString(),
  76. [RESOURCE_RENDER_BLOCKING_STATUS]: row[RESOURCE_RENDER_BLOCKING_STATUS] as
  77. | ''
  78. | 'non-blocking'
  79. | 'blocking',
  80. [SPAN_DOMAIN]: row[SPAN_DOMAIN][0]?.toString(),
  81. [`avg(http.response_content_length)`]: row[
  82. `avg(${HTTP_RESPONSE_CONTENT_LENGTH})`
  83. ] as number,
  84. }));
  85. return {...result, data: data || []};
  86. };