resourceTable.tsx 7.3 KB

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