resourceTable.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import {Fragment} from 'react';
  2. import {Link} from 'react-router';
  3. import * as qs from 'query-string';
  4. import FileSize from 'sentry/components/fileSize';
  5. import GridEditable, {
  6. COL_WIDTH_UNDEFINED,
  7. GridColumnHeader,
  8. GridColumnOrder,
  9. } from 'sentry/components/gridEditable';
  10. import Pagination from 'sentry/components/pagination';
  11. import {t} from 'sentry/locale';
  12. import {RateUnits} from 'sentry/utils/discover/fields';
  13. import {useLocation} from 'sentry/utils/useLocation';
  14. import {BrowserStarfishFields} from 'sentry/views/performance/browser/resources/utils/useResourceFilters';
  15. import {ValidSort} from 'sentry/views/performance/browser/resources/utils/useResourceSort';
  16. import {useResourcesQuery} from 'sentry/views/performance/browser/resources/utils/useResourcesQuery';
  17. import {DurationCell} from 'sentry/views/starfish/components/tableCells/durationCell';
  18. import {renderHeadCell} from 'sentry/views/starfish/components/tableCells/renderHeadCell';
  19. import {ThroughputCell} from 'sentry/views/starfish/components/tableCells/throughputCell';
  20. type Row = {
  21. 'avg(span.self_time)': number;
  22. domain: string;
  23. 'http.decoded_response_content_length': number;
  24. 'http.response_content_length': number;
  25. 'resource.render_blocking_status': string;
  26. 'span.description': string;
  27. 'span.group': string;
  28. 'span.op': 'resource.script' | 'resource.img';
  29. 'spm()': number;
  30. };
  31. type Column = GridColumnHeader<keyof Row>;
  32. type Props = {
  33. sort: ValidSort;
  34. };
  35. function ResourceTable({sort}: Props) {
  36. const location = useLocation();
  37. const {data, isLoading, pageLinks} = useResourcesQuery({sort});
  38. const columnOrder: GridColumnOrder<keyof Row>[] = [
  39. {key: 'span.description', width: COL_WIDTH_UNDEFINED, name: 'Resource name'},
  40. {key: 'span.op', width: COL_WIDTH_UNDEFINED, name: 'Type'},
  41. {key: 'avg(span.self_time)', width: COL_WIDTH_UNDEFINED, name: 'Avg Duration'},
  42. {
  43. key: 'spm()',
  44. width: COL_WIDTH_UNDEFINED,
  45. name: 'Throughput',
  46. },
  47. {
  48. key: 'http.response_content_length',
  49. width: COL_WIDTH_UNDEFINED,
  50. name: 'Resource size',
  51. },
  52. {
  53. key: 'resource.render_blocking_status',
  54. width: COL_WIDTH_UNDEFINED,
  55. name: 'Render blocking',
  56. },
  57. {
  58. key: 'http.decoded_response_content_length',
  59. width: COL_WIDTH_UNDEFINED,
  60. name: 'Uncompressed',
  61. },
  62. ];
  63. const tableData: Row[] = data.length
  64. ? data.map(span => ({
  65. ...span,
  66. 'http.decoded_response_content_length': Math.floor(
  67. Math.random() * (1000 - 500) + 500
  68. ),
  69. 'http.response_content_length': Math.floor(Math.random() * (500 - 50) + 50),
  70. 'resource.render_blocking_status':
  71. Math.random() > 0.5 ? 'blocking' : 'non-blocking',
  72. 'span.group': 'group123',
  73. domain: 's1.sentry-cdn.com',
  74. }))
  75. : [];
  76. const renderBodyCell = (col: Column, row: Row) => {
  77. const {key} = col;
  78. if (key === 'span.description') {
  79. const query = {
  80. ...location.query,
  81. [BrowserStarfishFields.DESCRIPTION]: row[key],
  82. };
  83. return (
  84. <Link to={`/performance/browser/resources/?${qs.stringify(query)}`}>
  85. {row[key]}
  86. </Link>
  87. );
  88. }
  89. if (key === 'spm()') {
  90. return <ThroughputCell rate={row[key]} unit={RateUnits.PER_SECOND} />;
  91. }
  92. if (key === 'http.response_content_length') {
  93. return <FileSize bytes={row[key]} />;
  94. }
  95. if (key === 'avg(span.self_time)') {
  96. return <DurationCell milliseconds={row[key]} />;
  97. }
  98. if (key === 'span.op') {
  99. const opName = row[key] === 'resource.script' ? t('Javascript') : t('Image');
  100. return <span>{opName}</span>;
  101. }
  102. if (key === 'http.decoded_response_content_length') {
  103. const isCompressed =
  104. row['http.response_content_length'] !==
  105. row['http.decoded_response_content_length'];
  106. return <span>{isCompressed ? t('true') : t('false')}</span>;
  107. }
  108. return <span>{row[key]}</span>;
  109. };
  110. return (
  111. <Fragment>
  112. <GridEditable
  113. data={tableData}
  114. isLoading={isLoading}
  115. columnOrder={columnOrder}
  116. columnSortBy={[
  117. {
  118. key: sort.field,
  119. order: sort.kind,
  120. },
  121. ]}
  122. grid={{
  123. renderHeadCell: column =>
  124. renderHeadCell({
  125. column,
  126. location,
  127. sort,
  128. }),
  129. renderBodyCell,
  130. }}
  131. location={location}
  132. />
  133. <Pagination pageLinks={pageLinks} />
  134. </Fragment>
  135. );
  136. }
  137. export const getActionName = (transactionOp: string) => {
  138. switch (transactionOp) {
  139. case 'ui.action.click':
  140. return 'Click';
  141. default:
  142. return transactionOp;
  143. }
  144. };
  145. export default ResourceTable;