useResourcesQuery.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 {EMPTY_OPTION_VALUE} from 'sentry/utils/tokenizeSearch';
  5. import {useLocation} from 'sentry/utils/useLocation';
  6. import useOrganization from 'sentry/utils/useOrganization';
  7. import usePageFilters from 'sentry/utils/usePageFilters';
  8. import {
  9. FONT_FILE_EXTENSIONS,
  10. IMAGE_FILE_EXTENSIONS,
  11. } from 'sentry/views/performance/browser/resources/shared/constants';
  12. import {ResourceSpanOps} from 'sentry/views/performance/browser/resources/shared/types';
  13. import {
  14. ModuleFilters,
  15. useResourceModuleFilters,
  16. } from 'sentry/views/performance/browser/resources/utils/useResourceFilters';
  17. import {ValidSort} from 'sentry/views/performance/browser/resources/utils/useResourceSort';
  18. import {SpanFunction, SpanMetricsField} from 'sentry/views/starfish/types';
  19. const {
  20. SPAN_DOMAIN,
  21. SPAN_GROUP,
  22. SPAN_DESCRIPTION,
  23. SPAN_OP,
  24. SPAN_SELF_TIME,
  25. RESOURCE_RENDER_BLOCKING_STATUS,
  26. HTTP_RESPONSE_CONTENT_LENGTH,
  27. PROJECT_ID,
  28. FILE_EXTENSION,
  29. } = SpanMetricsField;
  30. const {TIME_SPENT_PERCENTAGE} = SpanFunction;
  31. type Props = {
  32. referrer: string;
  33. sort: ValidSort;
  34. cursor?: string;
  35. defaultResourceTypes?: string[];
  36. limit?: number;
  37. query?: string;
  38. };
  39. export const DEFAULT_RESOURCE_FILTERS = ['!span.description:"browser-extension://*"'];
  40. export const getResourcesEventViewQuery = (
  41. resourceFilters: Partial<ModuleFilters>,
  42. defaultResourceTypes: string[] | undefined
  43. ): string[] => {
  44. return [
  45. ...DEFAULT_RESOURCE_FILTERS,
  46. ...(resourceFilters.transaction
  47. ? [`transaction:"${resourceFilters.transaction}"`]
  48. : []),
  49. ...getDomainFilter(resourceFilters[SPAN_DOMAIN]),
  50. ...(resourceFilters[RESOURCE_RENDER_BLOCKING_STATUS]
  51. ? [
  52. `${RESOURCE_RENDER_BLOCKING_STATUS}:${resourceFilters[RESOURCE_RENDER_BLOCKING_STATUS]}`,
  53. ]
  54. : []),
  55. ...getResourceTypeFilter(resourceFilters[SPAN_OP], defaultResourceTypes),
  56. ];
  57. };
  58. export const useResourcesQuery = ({
  59. sort,
  60. defaultResourceTypes,
  61. query,
  62. limit,
  63. cursor,
  64. referrer,
  65. }: Props) => {
  66. const pageFilters = usePageFilters();
  67. const location = useLocation();
  68. const resourceFilters = useResourceModuleFilters();
  69. const {slug: orgSlug} = useOrganization();
  70. const queryConditions = [
  71. ...(!query ? getResourcesEventViewQuery(resourceFilters, defaultResourceTypes) : []),
  72. query,
  73. ];
  74. // TODO - we should be using metrics data here
  75. const eventView = EventView.fromNewQueryWithPageFilters(
  76. {
  77. fields: [
  78. SPAN_DESCRIPTION,
  79. SPAN_OP,
  80. 'count()',
  81. `avg(${SPAN_SELF_TIME})`,
  82. 'spm()',
  83. SPAN_GROUP,
  84. `avg(${HTTP_RESPONSE_CONTENT_LENGTH})`,
  85. 'project.id',
  86. `${TIME_SPENT_PERCENTAGE}()`,
  87. `sum(${SPAN_SELF_TIME})`,
  88. ],
  89. name: 'Resource module - resource table',
  90. query: queryConditions.join(' '),
  91. orderby: '-count',
  92. version: 2,
  93. dataset: DiscoverDatasets.SPANS_METRICS,
  94. },
  95. pageFilters.selection
  96. );
  97. if (sort) {
  98. eventView.sorts = [sort];
  99. }
  100. const result = useDiscoverQuery({
  101. eventView,
  102. limit: limit ?? 100,
  103. location,
  104. orgSlug,
  105. options: {
  106. refetchOnWindowFocus: false,
  107. },
  108. cursor,
  109. referrer,
  110. });
  111. const data = result?.data?.data.map(row => ({
  112. [SPAN_OP]: row[SPAN_OP].toString() as `resource.${string}`,
  113. [SPAN_DESCRIPTION]: row[SPAN_DESCRIPTION].toString(),
  114. ['avg(span.self_time)']: row[`avg(${SPAN_SELF_TIME})`] as number,
  115. 'count()': row['count()'] as number,
  116. 'spm()': row['spm()'] as number,
  117. [SPAN_GROUP]: row[SPAN_GROUP].toString(),
  118. [PROJECT_ID]: row[PROJECT_ID] as number,
  119. [`avg(http.response_content_length)`]: row[
  120. `avg(${HTTP_RESPONSE_CONTENT_LENGTH})`
  121. ] as number,
  122. [`time_spent_percentage()`]: row[`${TIME_SPENT_PERCENTAGE}()`] as number,
  123. ['count_unique(transaction)']: row['count_unique(transaction)'] as number,
  124. [`sum(span.self_time)`]: row[`sum(${SPAN_SELF_TIME})`] as number,
  125. }));
  126. return {...result, data: data || []};
  127. };
  128. export const getDomainFilter = (selectedDomain: string | undefined) => {
  129. if (!selectedDomain) {
  130. return [];
  131. }
  132. if (selectedDomain === EMPTY_OPTION_VALUE) {
  133. return [`!has:${SPAN_DOMAIN}`];
  134. }
  135. return [`${SPAN_DOMAIN}:"${selectedDomain}"`];
  136. };
  137. const SPAN_OP_FILTER = {
  138. [ResourceSpanOps.SCRIPT]: [`${SPAN_OP}:${ResourceSpanOps.SCRIPT}`],
  139. [ResourceSpanOps.CSS]: [`${FILE_EXTENSION}:css`],
  140. [ResourceSpanOps.FONT]: [`${FILE_EXTENSION}:[${FONT_FILE_EXTENSIONS.join(',')}]`],
  141. [ResourceSpanOps.IMAGE]: [
  142. `${FILE_EXTENSION}:[${IMAGE_FILE_EXTENSIONS.join(',')}]`,
  143. `${SPAN_OP}:resource.img`,
  144. ],
  145. };
  146. export const getResourceTypeFilter = (
  147. selectedSpanOp: string | undefined,
  148. defaultResourceTypes: string[] | undefined
  149. ) => {
  150. let resourceFilter: string[] = [`${SPAN_OP}:resource.*`];
  151. if (selectedSpanOp) {
  152. resourceFilter = [SPAN_OP_FILTER[selectedSpanOp].join(' OR ')] || [
  153. `${SPAN_OP}:${selectedSpanOp}`,
  154. ];
  155. } else if (defaultResourceTypes) {
  156. resourceFilter = [
  157. defaultResourceTypes.map(type => SPAN_OP_FILTER[type].join(' OR ')).join(' OR '),
  158. ];
  159. }
  160. return ['(', ...resourceFilter, ')'];
  161. };