resourceTable.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import {Fragment} from 'react';
  2. import GridEditable, {
  3. COL_WIDTH_UNDEFINED,
  4. GridColumnHeader,
  5. GridColumnOrder,
  6. } from 'sentry/components/gridEditable';
  7. import Pagination from 'sentry/components/pagination';
  8. import {t} from 'sentry/locale';
  9. import {RateUnits} from 'sentry/utils/discover/fields';
  10. import {useLocation} from 'sentry/utils/useLocation';
  11. import ResourceSize from 'sentry/views/performance/browser/resources/shared/resourceSize';
  12. import {ValidSort} from 'sentry/views/performance/browser/resources/utils/useResourceSort';
  13. import {useResourcesQuery} from 'sentry/views/performance/browser/resources/utils/useResourcesQuery';
  14. import {DurationCell} from 'sentry/views/starfish/components/tableCells/durationCell';
  15. import {renderHeadCell} from 'sentry/views/starfish/components/tableCells/renderHeadCell';
  16. import {SpanDescriptionCell} from 'sentry/views/starfish/components/tableCells/spanDescriptionCell';
  17. import {ThroughputCell} from 'sentry/views/starfish/components/tableCells/throughputCell';
  18. import {ModuleName, SpanFunction, SpanMetricsField} from 'sentry/views/starfish/types';
  19. import {DataTitles, getThroughputTitle} from 'sentry/views/starfish/views/spans/types';
  20. const {
  21. SPAN_DESCRIPTION,
  22. SPAN_OP,
  23. SPAN_SELF_TIME,
  24. HTTP_RESPONSE_CONTENT_LENGTH,
  25. PROJECT_ID,
  26. SPAN_GROUP,
  27. } = SpanMetricsField;
  28. const {SPM} = SpanFunction;
  29. type Row = {
  30. 'avg(http.response_content_length)': number;
  31. 'avg(span.self_time)': number;
  32. 'http.decoded_response_content_length': number;
  33. 'project.id': number;
  34. 'resource.render_blocking_status': string;
  35. 'span.description': string;
  36. 'span.domain': string;
  37. 'span.group': string;
  38. 'span.op': `resource.${'script' | 'img' | 'css' | 'iframe' | string}`;
  39. 'spm()': number;
  40. };
  41. type Column = GridColumnHeader<keyof Row>;
  42. type Props = {
  43. sort: ValidSort;
  44. defaultResourceTypes?: string[];
  45. };
  46. function ResourceTable({sort, defaultResourceTypes}: Props) {
  47. const location = useLocation();
  48. const {data, isLoading, pageLinks} = useResourcesQuery({
  49. sort,
  50. defaultResourceTypes,
  51. });
  52. const columnOrder: GridColumnOrder<keyof Row>[] = [
  53. {key: SPAN_DESCRIPTION, width: COL_WIDTH_UNDEFINED, name: t('Resource Description')},
  54. {key: SPAN_OP, width: COL_WIDTH_UNDEFINED, name: t('Type')},
  55. {key: `avg(${SPAN_SELF_TIME})`, width: COL_WIDTH_UNDEFINED, name: DataTitles.avg},
  56. {
  57. key: `${SPM}()`,
  58. width: COL_WIDTH_UNDEFINED,
  59. name: getThroughputTitle('http'),
  60. },
  61. {
  62. key: `avg(${HTTP_RESPONSE_CONTENT_LENGTH})`,
  63. width: COL_WIDTH_UNDEFINED,
  64. name: DataTitles['avg(http.response_content_length)'],
  65. },
  66. ];
  67. const tableData: Row[] = data.length
  68. ? data.map(span => ({
  69. ...span,
  70. 'http.decoded_response_content_length': Math.floor(
  71. Math.random() * (1000 - 500) + 500
  72. ),
  73. }))
  74. : [];
  75. const renderBodyCell = (col: Column, row: Row) => {
  76. const {key} = col;
  77. if (key === SPAN_DESCRIPTION) {
  78. return (
  79. <SpanDescriptionCell
  80. moduleName={ModuleName.HTTP}
  81. projectId={row[PROJECT_ID]}
  82. description={row[SPAN_DESCRIPTION]}
  83. group={row[SPAN_GROUP]}
  84. />
  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 <ResourceSize 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 default ResourceTable;