resourceTable.tsx 4.5 KB

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