resourceTable.tsx 4.7 KB

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