resourceSummaryTable.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {GridColumnHeader, GridColumnOrder} from 'sentry/components/gridEditable';
  4. import GridEditable, {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  5. import Link from 'sentry/components/links/link';
  6. import type {CursorHandler} from 'sentry/components/pagination';
  7. import Pagination from 'sentry/components/pagination';
  8. import {t} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import {browserHistory} from 'sentry/utils/browserHistory';
  11. import {decodeScalar} from 'sentry/utils/queryString';
  12. import {useLocation} from 'sentry/utils/useLocation';
  13. import {useParams} from 'sentry/utils/useParams';
  14. import {useResourcePagesQuery} from 'sentry/views/insights/browser/resources/queries/useResourcePageQuery';
  15. import {RESOURCE_THROUGHPUT_UNIT} from 'sentry/views/insights/browser/resources/settings';
  16. import {useResourceModuleFilters} from 'sentry/views/insights/browser/resources/utils/useResourceFilters';
  17. import {useResourceSummarySort} from 'sentry/views/insights/browser/resources/utils/useResourceSummarySort';
  18. import {FullSpanDescription} from 'sentry/views/insights/common/components/fullSpanDescription';
  19. import {DurationCell} from 'sentry/views/insights/common/components/tableCells/durationCell';
  20. import {renderHeadCell} from 'sentry/views/insights/common/components/tableCells/renderHeadCell';
  21. import ResourceSizeCell from 'sentry/views/insights/common/components/tableCells/resourceSizeCell';
  22. import {WiderHovercard} from 'sentry/views/insights/common/components/tableCells/spanDescriptionCell';
  23. import {ThroughputCell} from 'sentry/views/insights/common/components/tableCells/throughputCell';
  24. import {QueryParameterNames} from 'sentry/views/insights/common/views/queryParameters';
  25. import {
  26. DataTitles,
  27. getThroughputTitle,
  28. } from 'sentry/views/insights/common/views/spans/types';
  29. import {
  30. ModuleName,
  31. SpanIndexedField,
  32. SpanMetricsField,
  33. } from 'sentry/views/insights/types';
  34. const {
  35. RESOURCE_RENDER_BLOCKING_STATUS,
  36. SPAN_SELF_TIME,
  37. HTTP_RESPONSE_CONTENT_LENGTH,
  38. TRANSACTION,
  39. USER_GEO_SUBREGION,
  40. } = SpanMetricsField;
  41. type Row = {
  42. 'avg(http.response_content_length)': number;
  43. 'avg(span.self_time)': number;
  44. 'resource.render_blocking_status': '' | 'non-blocking' | 'blocking';
  45. 'spm()': number;
  46. transaction: string;
  47. };
  48. type Column = GridColumnHeader<keyof Row>;
  49. function ResourceSummaryTable() {
  50. const location = useLocation();
  51. const {groupId} = useParams();
  52. const sort = useResourceSummarySort();
  53. const filters = useResourceModuleFilters();
  54. const cursor = decodeScalar(location.query?.[QueryParameterNames.PAGES_CURSOR]);
  55. const {data, isPending, pageLinks} = useResourcePagesQuery(groupId, {
  56. sort,
  57. cursor,
  58. subregions: filters[USER_GEO_SUBREGION],
  59. renderBlockingStatus: filters[RESOURCE_RENDER_BLOCKING_STATUS],
  60. });
  61. const columnOrder: GridColumnOrder<keyof Row>[] = [
  62. {key: 'transaction', width: COL_WIDTH_UNDEFINED, name: 'Found on page'},
  63. {
  64. key: 'spm()',
  65. width: COL_WIDTH_UNDEFINED,
  66. name: getThroughputTitle('http'),
  67. },
  68. {
  69. key: `avg(${SPAN_SELF_TIME})`,
  70. width: COL_WIDTH_UNDEFINED,
  71. name: t('Avg Duration'),
  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. key: RESOURCE_RENDER_BLOCKING_STATUS,
  80. width: COL_WIDTH_UNDEFINED,
  81. name: t('Render Blocking'),
  82. },
  83. ];
  84. const renderBodyCell = (col: Column, row: Row) => {
  85. const {key} = col;
  86. if (key === 'spm()') {
  87. return <ThroughputCell rate={row[key]} unit={RESOURCE_THROUGHPUT_UNIT} />;
  88. }
  89. if (key === 'avg(span.self_time)') {
  90. return <DurationCell milliseconds={row[key]} />;
  91. }
  92. if (key === 'avg(http.response_content_length)') {
  93. return <ResourceSizeCell bytes={row[key]} />;
  94. }
  95. if (key === 'transaction') {
  96. const blockingStatus = row['resource.render_blocking_status'];
  97. let query = `!has:${RESOURCE_RENDER_BLOCKING_STATUS}`;
  98. if (blockingStatus) {
  99. query = `${RESOURCE_RENDER_BLOCKING_STATUS}:${blockingStatus}`;
  100. }
  101. const link = (
  102. <Link
  103. to={{
  104. pathname: location.pathname,
  105. query: {
  106. ...location.query,
  107. transaction: row[key],
  108. query: [query],
  109. },
  110. }}
  111. >
  112. {row[key]}
  113. </Link>
  114. );
  115. return (
  116. <DescriptionWrapper>
  117. <WiderHovercard
  118. position="right"
  119. body={
  120. <Fragment>
  121. <TitleWrapper>{t('Example')}</TitleWrapper>
  122. <FullSpanDescription
  123. group={groupId}
  124. moduleName={ModuleName.RESOURCE}
  125. filters={{
  126. [SpanIndexedField.RESOURCE_RENDER_BLOCKING_STATUS]:
  127. row[RESOURCE_RENDER_BLOCKING_STATUS],
  128. [SpanIndexedField.TRANSACTION]: row[TRANSACTION],
  129. }}
  130. />
  131. </Fragment>
  132. }
  133. >
  134. {link}
  135. </WiderHovercard>
  136. </DescriptionWrapper>
  137. );
  138. }
  139. if (key === RESOURCE_RENDER_BLOCKING_STATUS) {
  140. const value = row[key];
  141. if (value === 'blocking') {
  142. return <span>{t('Yes')}</span>;
  143. }
  144. if (value === 'non-blocking') {
  145. return <span>{t('No')}</span>;
  146. }
  147. return <span>{'-'}</span>;
  148. }
  149. return <span>{row[key]}</span>;
  150. };
  151. const handleCursor: CursorHandler = (newCursor, pathname, query) => {
  152. browserHistory.push({
  153. pathname,
  154. query: {...query, [QueryParameterNames.PAGES_CURSOR]: newCursor},
  155. });
  156. };
  157. return (
  158. <Fragment>
  159. <GridEditable
  160. data={data || []}
  161. isLoading={isPending}
  162. columnOrder={columnOrder}
  163. columnSortBy={[
  164. {
  165. key: sort.field,
  166. order: sort.kind,
  167. },
  168. ]}
  169. grid={{
  170. renderHeadCell: column =>
  171. renderHeadCell({
  172. column,
  173. location,
  174. sort,
  175. }),
  176. renderBodyCell,
  177. }}
  178. />
  179. <Pagination pageLinks={pageLinks} onCursor={handleCursor} />
  180. </Fragment>
  181. );
  182. }
  183. export const getActionName = (transactionOp: string) => {
  184. switch (transactionOp) {
  185. case 'ui.action.click':
  186. return 'Click';
  187. default:
  188. return transactionOp;
  189. }
  190. };
  191. const TitleWrapper = styled('div')`
  192. margin-bottom: ${space(1)};
  193. `;
  194. const DescriptionWrapper = styled('div')`
  195. .inline-flex {
  196. display: inline-flex;
  197. }
  198. `;
  199. export default ResourceSummaryTable;