resourceTable.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import {Fragment} 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 {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  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 {ValidSort} from 'sentry/views/performance/browser/resources/utils/useResourceSort';
  17. import {useResourcesQuery} from 'sentry/views/performance/browser/resources/utils/useResourcesQuery';
  18. import {DurationCell} from 'sentry/views/starfish/components/tableCells/durationCell';
  19. import {renderHeadCell} from 'sentry/views/starfish/components/tableCells/renderHeadCell';
  20. import ResourceSizeCell from 'sentry/views/starfish/components/tableCells/resourceSizeCell';
  21. import {SpanDescriptionCell} from 'sentry/views/starfish/components/tableCells/spanDescriptionCell';
  22. import {ThroughputCell} from 'sentry/views/starfish/components/tableCells/throughputCell';
  23. import {TimeSpentCell} from 'sentry/views/starfish/components/tableCells/timeSpentCell';
  24. import {ModuleName, SpanFunction, SpanMetricsField} from 'sentry/views/starfish/types';
  25. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  26. import {DataTitles, getThroughputTitle} from 'sentry/views/starfish/views/spans/types';
  27. const {
  28. SPAN_DESCRIPTION,
  29. SPAN_OP,
  30. SPAN_SELF_TIME,
  31. HTTP_RESPONSE_CONTENT_LENGTH,
  32. PROJECT_ID,
  33. SPAN_GROUP,
  34. FILE_EXTENSION,
  35. } = SpanMetricsField;
  36. const {TIME_SPENT_PERCENTAGE} = SpanFunction;
  37. const {SPM} = SpanFunction;
  38. type Row = {
  39. 'avg(http.response_content_length)': number;
  40. 'avg(span.self_time)': number;
  41. 'http.decoded_response_content_length': number;
  42. 'project.id': number;
  43. 'resource.render_blocking_status': string;
  44. 'span.description': string;
  45. 'span.domain': string;
  46. 'span.group': string;
  47. 'span.op': `resource.${'script' | 'img' | 'css' | 'iframe' | string}`;
  48. 'spm()': number;
  49. 'sum(span.self_time)': number;
  50. 'time_spent_percentage()': number;
  51. };
  52. type Column = GridColumnHeader<keyof Row>;
  53. type Props = {
  54. sort: ValidSort;
  55. defaultResourceTypes?: string[];
  56. };
  57. function ResourceTable({sort, defaultResourceTypes}: Props) {
  58. const location = useLocation();
  59. const cursor = decodeScalar(location.query?.[QueryParameterNames.SPANS_CURSOR]);
  60. const {data, isLoading, pageLinks} = useResourcesQuery({
  61. sort,
  62. defaultResourceTypes,
  63. cursor,
  64. });
  65. const columnOrder: GridColumnOrder<keyof Row>[] = [
  66. {key: SPAN_DESCRIPTION, width: COL_WIDTH_UNDEFINED, name: t('Resource Description')},
  67. {key: SPAN_OP, width: COL_WIDTH_UNDEFINED, name: t('Type')},
  68. {
  69. key: `${SPM}()`,
  70. width: COL_WIDTH_UNDEFINED,
  71. name: getThroughputTitle('http'),
  72. },
  73. {key: `avg(${SPAN_SELF_TIME})`, width: COL_WIDTH_UNDEFINED, name: DataTitles.avg},
  74. {
  75. key: `${TIME_SPENT_PERCENTAGE}()`,
  76. width: COL_WIDTH_UNDEFINED,
  77. name: DataTitles.timeSpent,
  78. },
  79. {
  80. key: `avg(${HTTP_RESPONSE_CONTENT_LENGTH})`,
  81. width: COL_WIDTH_UNDEFINED,
  82. name: DataTitles['avg(http.response_content_length)'],
  83. },
  84. ];
  85. const tableData: Row[] = data.length
  86. ? data.map(span => ({
  87. ...span,
  88. 'http.decoded_response_content_length': Math.floor(
  89. Math.random() * (1000 - 500) + 500
  90. ),
  91. }))
  92. : [];
  93. const renderBodyCell = (col: Column, row: Row) => {
  94. const {key} = col;
  95. const getIcon = (spanOp: string, fileExtension: string) => {
  96. if (spanOp === 'resource.script') {
  97. return 'javascript';
  98. }
  99. if (fileExtension === 'css') {
  100. return 'css';
  101. }
  102. return 'unknown';
  103. };
  104. if (key === SPAN_DESCRIPTION) {
  105. return (
  106. <DescriptionWrapper>
  107. <PlatformIcon
  108. platform={getIcon(row[SPAN_OP], row[FILE_EXTENSION]) || 'unknown'}
  109. />
  110. <SpanDescriptionCell
  111. moduleName={ModuleName.HTTP}
  112. projectId={row[PROJECT_ID]}
  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 (['woff', 'woff2', 'ttf', 'otf', 'eot'].includes(fileExtension)) {
  138. return <span>{t('Font')}</span>;
  139. }
  140. return <span>{spanOp}</span>;
  141. }
  142. if (key === 'http.decoded_response_content_length') {
  143. const isUncompressed =
  144. row['http.response_content_length'] ===
  145. row['http.decoded_response_content_length'];
  146. return <span>{isUncompressed ? t('true') : t('false')}</span>;
  147. }
  148. if (key === 'time_spent_percentage()') {
  149. return (
  150. <TimeSpentCell percentage={row[key]} total={row[`sum(${SPAN_SELF_TIME})`]} />
  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. export default ResourceTable;
  189. const DescriptionWrapper = styled('div')`
  190. display: flex;
  191. flex-wrap: wrap;
  192. gap: ${space(1)};
  193. `;