resourceTable.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {PlatformIcon} from 'platformicons';
  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 {space} from 'sentry/styles/space';
  12. import {useLocation} from 'sentry/utils/useLocation';
  13. import {RESOURCE_THROUGHPUT_UNIT} from 'sentry/views/performance/browser/resources';
  14. import ResourceSize from 'sentry/views/performance/browser/resources/shared/resourceSize';
  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 {SpanDescriptionCell} from 'sentry/views/starfish/components/tableCells/spanDescriptionCell';
  20. import {ThroughputCell} from 'sentry/views/starfish/components/tableCells/throughputCell';
  21. import {TimeSpentCell} from 'sentry/views/starfish/components/tableCells/timeSpentCell';
  22. import {ModuleName, SpanFunction, SpanMetricsField} from 'sentry/views/starfish/types';
  23. import {DataTitles, getThroughputTitle} from 'sentry/views/starfish/views/spans/types';
  24. const {
  25. SPAN_DESCRIPTION,
  26. SPAN_OP,
  27. SPAN_SELF_TIME,
  28. HTTP_RESPONSE_CONTENT_LENGTH,
  29. PROJECT_ID,
  30. SPAN_GROUP,
  31. } = SpanMetricsField;
  32. const {TIME_SPENT_PERCENTAGE} = SpanFunction;
  33. const {SPM} = SpanFunction;
  34. type Row = {
  35. 'avg(http.response_content_length)': number;
  36. 'avg(span.self_time)': number;
  37. 'http.decoded_response_content_length': number;
  38. 'project.id': number;
  39. 'resource.render_blocking_status': string;
  40. 'span.description': string;
  41. 'span.domain': string;
  42. 'span.group': string;
  43. 'span.op': `resource.${'script' | 'img' | 'css' | 'iframe' | string}`;
  44. 'spm()': number;
  45. 'sum(span.self_time)': number;
  46. 'time_spent_percentage()': number;
  47. };
  48. type Column = GridColumnHeader<keyof Row>;
  49. type Props = {
  50. sort: ValidSort;
  51. defaultResourceTypes?: string[];
  52. };
  53. function ResourceTable({sort, defaultResourceTypes}: Props) {
  54. const location = useLocation();
  55. const {data, isLoading, pageLinks} = useResourcesQuery({
  56. sort,
  57. defaultResourceTypes,
  58. });
  59. const columnOrder: GridColumnOrder<keyof Row>[] = [
  60. {key: SPAN_DESCRIPTION, width: COL_WIDTH_UNDEFINED, name: t('Resource Description')},
  61. {key: SPAN_OP, width: COL_WIDTH_UNDEFINED, name: t('Type')},
  62. {
  63. key: `${SPM}()`,
  64. width: COL_WIDTH_UNDEFINED,
  65. name: getThroughputTitle('http'),
  66. },
  67. {key: `avg(${SPAN_SELF_TIME})`, width: COL_WIDTH_UNDEFINED, name: DataTitles.avg},
  68. {
  69. key: `${TIME_SPENT_PERCENTAGE}()`,
  70. width: COL_WIDTH_UNDEFINED,
  71. name: DataTitles.timeSpent,
  72. },
  73. {
  74. key: `avg(${HTTP_RESPONSE_CONTENT_LENGTH})`,
  75. width: COL_WIDTH_UNDEFINED,
  76. name: DataTitles['avg(http.response_content_length)'],
  77. },
  78. ];
  79. const tableData: Row[] = data.length
  80. ? data.map(span => ({
  81. ...span,
  82. 'http.decoded_response_content_length': Math.floor(
  83. Math.random() * (1000 - 500) + 500
  84. ),
  85. }))
  86. : [];
  87. const renderBodyCell = (col: Column, row: Row) => {
  88. const {key} = col;
  89. const opPlatformMap = {
  90. 'resource.script': 'javascript',
  91. 'resource.css': 'css',
  92. };
  93. if (key === SPAN_DESCRIPTION) {
  94. return (
  95. <DescriptionWrapper>
  96. <PlatformIcon platform={opPlatformMap[row[SPAN_OP]] || 'unknown'} />
  97. <SpanDescriptionCell
  98. moduleName={ModuleName.HTTP}
  99. projectId={row[PROJECT_ID]}
  100. description={row[SPAN_DESCRIPTION]}
  101. group={row[SPAN_GROUP]}
  102. />
  103. </DescriptionWrapper>
  104. );
  105. }
  106. if (key === 'spm()') {
  107. return <ThroughputCell rate={row[key]} unit={RESOURCE_THROUGHPUT_UNIT} />;
  108. }
  109. if (key === 'avg(http.response_content_length)') {
  110. return <ResourceSize bytes={row[key]} />;
  111. }
  112. if (key === `avg(span.self_time)`) {
  113. return <DurationCell milliseconds={row[key]} />;
  114. }
  115. if (key === SPAN_OP) {
  116. const opNameMap = {
  117. 'resource.script': t('JavaScript'),
  118. 'resource.img': t('Image'),
  119. 'resource.iframe': t('JavaScript (iframe)'),
  120. 'resource.css': t('Stylesheet'),
  121. 'resource.video': t('Video'),
  122. 'resource.audio': t('Audio'),
  123. };
  124. const opName = opNameMap[row[key]] || row[key];
  125. return <span>{opName}</span>;
  126. }
  127. if (key === 'http.decoded_response_content_length') {
  128. const isUncompressed =
  129. row['http.response_content_length'] ===
  130. row['http.decoded_response_content_length'];
  131. return <span>{isUncompressed ? t('true') : t('false')}</span>;
  132. }
  133. if (key === 'time_spent_percentage()') {
  134. return (
  135. <TimeSpentCell percentage={row[key]} total={row[`sum(${SPAN_SELF_TIME})`]} />
  136. );
  137. }
  138. return <span>{row[key]}</span>;
  139. };
  140. return (
  141. <Fragment>
  142. <GridEditable
  143. data={tableData}
  144. isLoading={isLoading}
  145. columnOrder={columnOrder}
  146. columnSortBy={[
  147. {
  148. key: sort.field,
  149. order: sort.kind,
  150. },
  151. ]}
  152. grid={{
  153. renderHeadCell: column =>
  154. renderHeadCell({
  155. column,
  156. location,
  157. sort,
  158. }),
  159. renderBodyCell,
  160. }}
  161. location={location}
  162. />
  163. <Pagination pageLinks={pageLinks} />
  164. </Fragment>
  165. );
  166. }
  167. export default ResourceTable;
  168. const DescriptionWrapper = styled('div')`
  169. display: flex;
  170. flex-wrap: wrap;
  171. gap: ${space(1)};
  172. `;