resourceTable.tsx 7.2 KB

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