useResourcesQuery.ts 2.9 KB

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