resourceTable.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import {Fragment, useEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import {PlatformIcon} from 'platformicons';
  4. import type {GridColumnHeader, GridColumnOrder} from 'sentry/components/gridEditable';
  5. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  6. import type {CursorHandler} from 'sentry/components/pagination';
  7. import Pagination from 'sentry/components/pagination';
  8. import {IconImage} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import {trackAnalytics} from 'sentry/utils/analytics';
  12. import {DismissId, usePageAlert} from 'sentry/utils/performance/contexts/pageAlert';
  13. import {decodeScalar} from 'sentry/utils/queryString';
  14. import {useLocation} from 'sentry/utils/useLocation';
  15. import {useNavigate} from 'sentry/utils/useNavigate';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import {useResourcesQuery} from 'sentry/views/insights/browser/common/queries/useResourcesQuery';
  18. import {
  19. FONT_FILE_EXTENSIONS,
  20. IMAGE_FILE_EXTENSIONS,
  21. } from 'sentry/views/insights/browser/resources/constants';
  22. import {
  23. DATA_TYPE,
  24. RESOURCE_THROUGHPUT_UNIT,
  25. } from 'sentry/views/insights/browser/resources/settings';
  26. import {ResourceSpanOps} from 'sentry/views/insights/browser/resources/types';
  27. import {useResourceModuleFilters} from 'sentry/views/insights/browser/resources/utils/useResourceFilters';
  28. import type {ValidSort} from 'sentry/views/insights/browser/resources/utils/useResourceSort';
  29. import {DurationCell} from 'sentry/views/insights/common/components/tableCells/durationCell';
  30. import {renderHeadCell} from 'sentry/views/insights/common/components/tableCells/renderHeadCell';
  31. import ResourceSizeCell from 'sentry/views/insights/common/components/tableCells/resourceSizeCell';
  32. import {SpanDescriptionCell} from 'sentry/views/insights/common/components/tableCells/spanDescriptionCell';
  33. import {ThroughputCell} from 'sentry/views/insights/common/components/tableCells/throughputCell';
  34. import {TimeSpentCell} from 'sentry/views/insights/common/components/tableCells/timeSpentCell';
  35. import {QueryParameterNames} from 'sentry/views/insights/common/views/queryParameters';
  36. import {
  37. DataTitles,
  38. getThroughputTitle,
  39. } from 'sentry/views/insights/common/views/spans/types';
  40. import {ModuleName, SpanFunction, SpanMetricsField} from 'sentry/views/insights/types';
  41. const {
  42. SPAN_DESCRIPTION,
  43. SPAN_OP,
  44. SPAN_SELF_TIME,
  45. HTTP_RESPONSE_CONTENT_LENGTH,
  46. PROJECT_ID,
  47. SPAN_GROUP,
  48. } = SpanMetricsField;
  49. const {TIME_SPENT_PERCENTAGE} = SpanFunction;
  50. const {SPM} = SpanFunction;
  51. const RESOURCE_SIZE_ALERT = t(
  52. `If you're noticing unusually large resource sizes, try updating to SDK version 7.82.0 or higher.`
  53. );
  54. type Row = {
  55. 'avg(http.response_content_length)': number;
  56. 'avg(span.self_time)': number;
  57. 'project.id': number;
  58. 'span.description': string;
  59. 'span.group': string;
  60. 'span.op': `resource.${'script' | 'img' | 'css' | 'iframe' | string}`;
  61. 'spm()': number;
  62. 'sum(span.self_time)': number;
  63. 'time_spent_percentage()': number;
  64. };
  65. type Column = GridColumnHeader<keyof Row>;
  66. type Props = {
  67. sort: ValidSort;
  68. defaultResourceTypes?: string[];
  69. };
  70. function ResourceTable({sort, defaultResourceTypes}: Props) {
  71. const navigate = useNavigate();
  72. const location = useLocation();
  73. const organization = useOrganization();
  74. const cursor = decodeScalar(location.query?.[QueryParameterNames.SPANS_CURSOR]);
  75. const filters = useResourceModuleFilters();
  76. const {setPageInfo, pageAlert} = usePageAlert();
  77. const {data, isPending, pageLinks} = useResourcesQuery({
  78. sort,
  79. defaultResourceTypes,
  80. cursor,
  81. referrer: 'api.performance.browser.resources.main-table',
  82. });
  83. const columnOrder: GridColumnOrder<keyof Row>[] = [
  84. {
  85. key: SPAN_DESCRIPTION,
  86. width: COL_WIDTH_UNDEFINED,
  87. name: `${DATA_TYPE} ${t('Description')}`,
  88. },
  89. {
  90. key: `${SPM}()`,
  91. width: COL_WIDTH_UNDEFINED,
  92. name: getThroughputTitle('http'),
  93. },
  94. {key: `avg(${SPAN_SELF_TIME})`, width: COL_WIDTH_UNDEFINED, name: DataTitles.avg},
  95. {
  96. key: `${TIME_SPENT_PERCENTAGE}()`,
  97. width: COL_WIDTH_UNDEFINED,
  98. name: DataTitles.timeSpent,
  99. },
  100. {
  101. key: `avg(${HTTP_RESPONSE_CONTENT_LENGTH})`,
  102. width: COL_WIDTH_UNDEFINED,
  103. name: DataTitles['avg(http.response_content_length)'],
  104. },
  105. ];
  106. const tableData: Row[] = data;
  107. useEffect(() => {
  108. if (pageAlert?.message !== RESOURCE_SIZE_ALERT) {
  109. for (const row of tableData) {
  110. const encodedSize = row[`avg(${HTTP_RESPONSE_CONTENT_LENGTH})`];
  111. if (encodedSize >= 2147483647) {
  112. setPageInfo(RESOURCE_SIZE_ALERT, {dismissId: DismissId.RESOURCE_SIZE_ALERT});
  113. break;
  114. }
  115. }
  116. }
  117. }, [tableData, setPageInfo, pageAlert?.message]);
  118. const renderBodyCell = (col: Column, row: Row) => {
  119. const {key} = col;
  120. if (key === SPAN_DESCRIPTION) {
  121. const fileExtension = row[SPAN_DESCRIPTION].split('.').pop() || '';
  122. const extraLinkQueryParams = {};
  123. if (filters[SpanMetricsField.USER_GEO_SUBREGION]) {
  124. extraLinkQueryParams[SpanMetricsField.USER_GEO_SUBREGION] =
  125. filters[SpanMetricsField.USER_GEO_SUBREGION];
  126. }
  127. return (
  128. <DescriptionWrapper>
  129. <ResourceIcon fileExtension={fileExtension} spanOp={row[SPAN_OP]} />
  130. <SpanDescriptionCell
  131. moduleName={ModuleName.RESOURCE}
  132. projectId={row[PROJECT_ID]}
  133. spanOp={row[SPAN_OP]}
  134. description={row[SPAN_DESCRIPTION]}
  135. group={row[SPAN_GROUP]}
  136. extraLinkQueryParams={extraLinkQueryParams}
  137. />
  138. </DescriptionWrapper>
  139. );
  140. }
  141. if (key === 'spm()') {
  142. return <ThroughputCell rate={row[key]} unit={RESOURCE_THROUGHPUT_UNIT} />;
  143. }
  144. if (key === 'avg(http.response_content_length)') {
  145. return <ResourceSizeCell bytes={row[key]} />;
  146. }
  147. if (key === `avg(span.self_time)`) {
  148. return <DurationCell milliseconds={row[key]} />;
  149. }
  150. if (key === SPAN_OP) {
  151. const fileExtension = row[SPAN_DESCRIPTION].split('.').pop() || '';
  152. const spanOp = row[key];
  153. if (fileExtension === 'js' || spanOp === 'resource.script') {
  154. return <span>{t('JavaScript')}</span>;
  155. }
  156. if (fileExtension === 'css') {
  157. return <span>{t('Stylesheet')}</span>;
  158. }
  159. if (FONT_FILE_EXTENSIONS.includes(fileExtension)) {
  160. return <span>{t('Font')}</span>;
  161. }
  162. return <span>{spanOp}</span>;
  163. }
  164. if (key === 'time_spent_percentage()') {
  165. return (
  166. <TimeSpentCell
  167. percentage={row[key]}
  168. total={row[`sum(${SPAN_SELF_TIME})`]}
  169. op={row[SPAN_OP]}
  170. />
  171. );
  172. }
  173. return <span>{row[key]}</span>;
  174. };
  175. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  176. navigate({
  177. pathname,
  178. query: {...query, [QueryParameterNames.SPANS_CURSOR]: newCursor},
  179. });
  180. };
  181. return (
  182. <Fragment>
  183. <GridEditable
  184. data={tableData}
  185. isLoading={isPending}
  186. columnOrder={columnOrder}
  187. columnSortBy={[
  188. {
  189. key: sort.field,
  190. order: sort.kind,
  191. },
  192. ]}
  193. grid={{
  194. renderHeadCell: column =>
  195. renderHeadCell({
  196. column,
  197. location,
  198. sort,
  199. }),
  200. renderBodyCell,
  201. }}
  202. />
  203. <Pagination
  204. pageLinks={pageLinks}
  205. onCursor={handleCursor}
  206. paginationAnalyticsEvent={(direction: string) => {
  207. trackAnalytics('insight.general.table_paginate', {
  208. organization,
  209. source: ModuleName.RESOURCE,
  210. direction,
  211. });
  212. }}
  213. />
  214. </Fragment>
  215. );
  216. }
  217. function ResourceIcon(props: {fileExtension: string; spanOp: string}) {
  218. const {spanOp, fileExtension} = props;
  219. if (spanOp === ResourceSpanOps.SCRIPT) {
  220. return <PlatformIcon platform="javascript" />;
  221. }
  222. if (fileExtension === 'css') {
  223. return <PlatformIcon platform="css" />;
  224. }
  225. if (FONT_FILE_EXTENSIONS.includes(fileExtension)) {
  226. return <PlatformIcon platform="font" />;
  227. }
  228. if (spanOp === ResourceSpanOps.IMAGE || IMAGE_FILE_EXTENSIONS.includes(fileExtension)) {
  229. return <IconImage color="black" legacySize="20px" />;
  230. }
  231. return <PlatformIcon platform="unknown" />;
  232. }
  233. export default ResourceTable;
  234. const DescriptionWrapper = styled('div')`
  235. display: flex;
  236. flex-wrap: wrap;
  237. gap: ${space(1)};
  238. `;