resourceTable.tsx 7.0 KB

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