resourceTable.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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: Array<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. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  125. extraLinkQueryParams[SpanMetricsField.USER_GEO_SUBREGION] =
  126. filters[SpanMetricsField.USER_GEO_SUBREGION];
  127. }
  128. return (
  129. <DescriptionWrapper>
  130. <ResourceIcon fileExtension={fileExtension} spanOp={row[SPAN_OP]} />
  131. <SpanDescriptionCell
  132. moduleName={ModuleName.RESOURCE}
  133. projectId={row[PROJECT_ID]}
  134. spanOp={row[SPAN_OP]}
  135. description={row[SPAN_DESCRIPTION]}
  136. group={row[SPAN_GROUP]}
  137. extraLinkQueryParams={extraLinkQueryParams}
  138. />
  139. </DescriptionWrapper>
  140. );
  141. }
  142. if (key === 'spm()') {
  143. return <ThroughputCell rate={row[key]} unit={RESOURCE_THROUGHPUT_UNIT} />;
  144. }
  145. if (key === 'avg(http.response_content_length)') {
  146. return <ResourceSizeCell bytes={row[key]} />;
  147. }
  148. if (key === `avg(span.self_time)`) {
  149. return <DurationCell milliseconds={row[key]} />;
  150. }
  151. if (key === SPAN_OP) {
  152. const fileExtension = row[SPAN_DESCRIPTION].split('.').pop() || '';
  153. const spanOp = row[key];
  154. if (fileExtension === 'js' || spanOp === 'resource.script') {
  155. return <span>{t('JavaScript')}</span>;
  156. }
  157. if (fileExtension === 'css') {
  158. return <span>{t('Stylesheet')}</span>;
  159. }
  160. if (FONT_FILE_EXTENSIONS.includes(fileExtension)) {
  161. return <span>{t('Font')}</span>;
  162. }
  163. return <span>{spanOp}</span>;
  164. }
  165. if (key === 'time_spent_percentage()') {
  166. return (
  167. <TimeSpentCell
  168. percentage={row[key]}
  169. total={row[`sum(${SPAN_SELF_TIME})`]}
  170. op={row[SPAN_OP]}
  171. />
  172. );
  173. }
  174. return <span>{row[key]}</span>;
  175. };
  176. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  177. navigate({
  178. pathname,
  179. query: {...query, [QueryParameterNames.SPANS_CURSOR]: newCursor},
  180. });
  181. };
  182. return (
  183. <Fragment>
  184. <GridEditable
  185. data={tableData}
  186. isLoading={isPending}
  187. columnOrder={columnOrder}
  188. columnSortBy={[
  189. {
  190. key: sort.field,
  191. order: sort.kind,
  192. },
  193. ]}
  194. grid={{
  195. renderHeadCell: column =>
  196. renderHeadCell({
  197. column,
  198. location,
  199. sort,
  200. }),
  201. renderBodyCell,
  202. }}
  203. />
  204. <Pagination
  205. pageLinks={pageLinks}
  206. onCursor={handleCursor}
  207. paginationAnalyticsEvent={(direction: string) => {
  208. trackAnalytics('insight.general.table_paginate', {
  209. organization,
  210. source: ModuleName.RESOURCE,
  211. direction,
  212. });
  213. }}
  214. />
  215. </Fragment>
  216. );
  217. }
  218. function ResourceIcon(props: {fileExtension: string; spanOp: string}) {
  219. const {spanOp, fileExtension} = props;
  220. if (spanOp === ResourceSpanOps.SCRIPT) {
  221. return <PlatformIcon platform="javascript" />;
  222. }
  223. if (fileExtension === 'css') {
  224. return <PlatformIcon platform="css" />;
  225. }
  226. if (FONT_FILE_EXTENSIONS.includes(fileExtension)) {
  227. return <PlatformIcon platform="font" />;
  228. }
  229. if (spanOp === ResourceSpanOps.IMAGE || IMAGE_FILE_EXTENSIONS.includes(fileExtension)) {
  230. return <IconImage color="black" legacySize="20px" />;
  231. }
  232. return <PlatformIcon platform="unknown" />;
  233. }
  234. export default ResourceTable;
  235. const DescriptionWrapper = styled('div')`
  236. display: flex;
  237. flex-wrap: wrap;
  238. gap: ${space(1)};
  239. `;