resourceTable.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 {browserHistory} from 'sentry/utils/browserHistory';
  13. import {DismissId, usePageAlert} from 'sentry/utils/performance/contexts/pageAlert';
  14. import {decodeScalar} from 'sentry/utils/queryString';
  15. import {useLocation} from 'sentry/utils/useLocation';
  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 location = useLocation();
  72. const organization = useOrganization();
  73. const cursor = decodeScalar(location.query?.[QueryParameterNames.SPANS_CURSOR]);
  74. const filters = useResourceModuleFilters();
  75. const {setPageInfo, pageAlert} = usePageAlert();
  76. const {data, isPending, pageLinks} = useResourcesQuery({
  77. sort,
  78. defaultResourceTypes,
  79. cursor,
  80. referrer: 'api.performance.browser.resources.main-table',
  81. });
  82. const columnOrder: GridColumnOrder<keyof Row>[] = [
  83. {
  84. key: SPAN_DESCRIPTION,
  85. width: COL_WIDTH_UNDEFINED,
  86. name: `${DATA_TYPE} ${t('Description')}`,
  87. },
  88. {
  89. key: `${SPM}()`,
  90. width: COL_WIDTH_UNDEFINED,
  91. name: getThroughputTitle('http'),
  92. },
  93. {key: `avg(${SPAN_SELF_TIME})`, width: COL_WIDTH_UNDEFINED, name: DataTitles.avg},
  94. {
  95. key: `${TIME_SPENT_PERCENTAGE}()`,
  96. width: COL_WIDTH_UNDEFINED,
  97. name: DataTitles.timeSpent,
  98. },
  99. {
  100. key: `avg(${HTTP_RESPONSE_CONTENT_LENGTH})`,
  101. width: COL_WIDTH_UNDEFINED,
  102. name: DataTitles['avg(http.response_content_length)'],
  103. },
  104. ];
  105. const tableData: Row[] = data;
  106. useEffect(() => {
  107. if (pageAlert?.message !== RESOURCE_SIZE_ALERT) {
  108. for (const row of tableData) {
  109. const encodedSize = row[`avg(${HTTP_RESPONSE_CONTENT_LENGTH})`];
  110. if (encodedSize >= 2147483647) {
  111. setPageInfo(RESOURCE_SIZE_ALERT, {dismissId: DismissId.RESOURCE_SIZE_ALERT});
  112. break;
  113. }
  114. }
  115. }
  116. }, [tableData, setPageInfo, pageAlert?.message]);
  117. const renderBodyCell = (col: Column, row: Row) => {
  118. const {key} = col;
  119. if (key === SPAN_DESCRIPTION) {
  120. const fileExtension = row[SPAN_DESCRIPTION].split('.').pop() || '';
  121. const extraLinkQueryParams = {};
  122. if (filters[SpanMetricsField.USER_GEO_SUBREGION]) {
  123. extraLinkQueryParams[SpanMetricsField.USER_GEO_SUBREGION] =
  124. filters[SpanMetricsField.USER_GEO_SUBREGION];
  125. }
  126. return (
  127. <DescriptionWrapper>
  128. <ResourceIcon fileExtension={fileExtension} spanOp={row[SPAN_OP]} />
  129. <SpanDescriptionCell
  130. moduleName={ModuleName.RESOURCE}
  131. projectId={row[PROJECT_ID]}
  132. spanOp={row[SPAN_OP]}
  133. description={row[SPAN_DESCRIPTION]}
  134. group={row[SPAN_GROUP]}
  135. extraLinkQueryParams={extraLinkQueryParams}
  136. />
  137. </DescriptionWrapper>
  138. );
  139. }
  140. if (key === 'spm()') {
  141. return <ThroughputCell rate={row[key]} unit={RESOURCE_THROUGHPUT_UNIT} />;
  142. }
  143. if (key === 'avg(http.response_content_length)') {
  144. return <ResourceSizeCell bytes={row[key]} />;
  145. }
  146. if (key === `avg(span.self_time)`) {
  147. return <DurationCell milliseconds={row[key]} />;
  148. }
  149. if (key === SPAN_OP) {
  150. const fileExtension = row[SPAN_DESCRIPTION].split('.').pop() || '';
  151. const spanOp = row[key];
  152. if (fileExtension === 'js' || spanOp === 'resource.script') {
  153. return <span>{t('JavaScript')}</span>;
  154. }
  155. if (fileExtension === 'css') {
  156. return <span>{t('Stylesheet')}</span>;
  157. }
  158. if (FONT_FILE_EXTENSIONS.includes(fileExtension)) {
  159. return <span>{t('Font')}</span>;
  160. }
  161. return <span>{spanOp}</span>;
  162. }
  163. if (key === 'time_spent_percentage()') {
  164. return (
  165. <TimeSpentCell
  166. percentage={row[key]}
  167. total={row[`sum(${SPAN_SELF_TIME})`]}
  168. op={row[SPAN_OP]}
  169. />
  170. );
  171. }
  172. return <span>{row[key]}</span>;
  173. };
  174. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  175. browserHistory.push({
  176. pathname,
  177. query: {...query, [QueryParameterNames.SPANS_CURSOR]: newCursor},
  178. });
  179. };
  180. return (
  181. <Fragment>
  182. <GridEditable
  183. data={tableData}
  184. isLoading={isPending}
  185. columnOrder={columnOrder}
  186. columnSortBy={[
  187. {
  188. key: sort.field,
  189. order: sort.kind,
  190. },
  191. ]}
  192. grid={{
  193. renderHeadCell: column =>
  194. renderHeadCell({
  195. column,
  196. location,
  197. sort,
  198. }),
  199. renderBodyCell,
  200. }}
  201. />
  202. <Pagination
  203. pageLinks={pageLinks}
  204. onCursor={handleCursor}
  205. paginationAnalyticsEvent={(direction: string) => {
  206. trackAnalytics('insight.general.table_paginate', {
  207. organization,
  208. source: ModuleName.RESOURCE,
  209. direction,
  210. });
  211. }}
  212. />
  213. </Fragment>
  214. );
  215. }
  216. function ResourceIcon(props: {fileExtension: string; spanOp: string}) {
  217. const {spanOp, fileExtension} = props;
  218. if (spanOp === ResourceSpanOps.SCRIPT) {
  219. return <PlatformIcon platform="javascript" />;
  220. }
  221. if (fileExtension === 'css') {
  222. return <PlatformIcon platform="css" />;
  223. }
  224. if (FONT_FILE_EXTENSIONS.includes(fileExtension)) {
  225. return <PlatformIcon platform="font" />;
  226. }
  227. if (spanOp === ResourceSpanOps.IMAGE || IMAGE_FILE_EXTENSIONS.includes(fileExtension)) {
  228. return <IconImage color="black" legacySize="20px" />;
  229. }
  230. return <PlatformIcon platform="unknown" />;
  231. }
  232. export default ResourceTable;
  233. const DescriptionWrapper = styled('div')`
  234. display: flex;
  235. flex-wrap: wrap;
  236. gap: ${space(1)};
  237. `;