resourceTable.tsx 7.4 KB

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