resourceTable.tsx 6.8 KB

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