spanSamplesTable.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import {CSSProperties} from 'react';
  2. import {Link} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {LinkButton} from 'sentry/components/button';
  5. import GridEditable, {
  6. COL_WIDTH_UNDEFINED,
  7. GridColumnHeader,
  8. } from 'sentry/components/gridEditable';
  9. import {Tooltip} from 'sentry/components/tooltip';
  10. import {IconProfiling} from 'sentry/icons/iconProfiling';
  11. import {t} from 'sentry/locale';
  12. import {useLocation} from 'sentry/utils/useLocation';
  13. import ResourceSize from 'sentry/views/performance/browser/resources/shared/resourceSize';
  14. import {DurationComparisonCell} from 'sentry/views/starfish/components/samplesTable/common';
  15. import {DurationCell} from 'sentry/views/starfish/components/tableCells/durationCell';
  16. import {
  17. OverflowEllipsisTextContainer,
  18. TextAlignRight,
  19. } from 'sentry/views/starfish/components/textAlign';
  20. import {SpanSample} from 'sentry/views/starfish/queries/useSpanSamples';
  21. import {SpanMetricsField} from 'sentry/views/starfish/types';
  22. const {HTTP_RESPONSE_CONTENT_LENGTH} = SpanMetricsField;
  23. type Keys =
  24. | 'transaction_id'
  25. | 'profile_id'
  26. | 'timestamp'
  27. | 'duration'
  28. | 'p95_comparison'
  29. | 'avg_comparison'
  30. | 'http.response_content_length';
  31. export type SamplesTableColumnHeader = GridColumnHeader<Keys>;
  32. export const DEFAULT_COLUMN_ORDER: SamplesTableColumnHeader[] = [
  33. {
  34. key: 'transaction_id',
  35. name: 'Event ID',
  36. width: COL_WIDTH_UNDEFINED,
  37. },
  38. {
  39. key: 'duration',
  40. name: 'Span Duration',
  41. width: COL_WIDTH_UNDEFINED,
  42. },
  43. {
  44. key: 'avg_comparison',
  45. name: 'Compared to Average',
  46. width: COL_WIDTH_UNDEFINED,
  47. },
  48. ];
  49. type SpanTableRow = {
  50. op: string;
  51. transaction: {
  52. id: string;
  53. 'project.name': string;
  54. timestamp: string;
  55. 'transaction.duration': number;
  56. };
  57. } & SpanSample;
  58. type Props = {
  59. avg: number;
  60. data: SpanTableRow[];
  61. isLoading: boolean;
  62. columnOrder?: SamplesTableColumnHeader[];
  63. highlightedSpanId?: string;
  64. onMouseLeaveSample?: () => void;
  65. onMouseOverSample?: (sample: SpanSample) => void;
  66. };
  67. export function SpanSamplesTable({
  68. isLoading,
  69. data,
  70. avg,
  71. highlightedSpanId,
  72. onMouseLeaveSample,
  73. onMouseOverSample,
  74. columnOrder,
  75. }: Props) {
  76. const location = useLocation();
  77. function handleMouseOverBodyCell(row: SpanTableRow) {
  78. if (onMouseOverSample) {
  79. onMouseOverSample(row);
  80. }
  81. }
  82. function handleMouseLeave() {
  83. if (onMouseLeaveSample) {
  84. onMouseLeaveSample();
  85. }
  86. }
  87. function renderHeadCell(column: GridColumnHeader): React.ReactNode {
  88. if (
  89. column.key === 'p95_comparison' ||
  90. column.key === 'avg_comparison' ||
  91. column.key === 'duration'
  92. ) {
  93. return (
  94. <TextAlignRight>
  95. <OverflowEllipsisTextContainer>{column.name}</OverflowEllipsisTextContainer>
  96. </TextAlignRight>
  97. );
  98. }
  99. return <OverflowEllipsisTextContainer>{column.name}</OverflowEllipsisTextContainer>;
  100. }
  101. function renderBodyCell(column: GridColumnHeader, row: SpanTableRow): React.ReactNode {
  102. const shouldHighlight = row.span_id === highlightedSpanId;
  103. const commonProps = {
  104. style: (shouldHighlight ? {fontWeight: 'bold'} : {}) satisfies CSSProperties,
  105. onMouseEnter: () => handleMouseOverBodyCell(row),
  106. };
  107. if (column.key === 'transaction_id') {
  108. return (
  109. <Link
  110. to={`/performance/${row.project}:${row['transaction.id']}#span-${row.span_id}`}
  111. {...commonProps}
  112. >
  113. {row['transaction.id'].slice(0, 8)}
  114. </Link>
  115. );
  116. }
  117. if (column.key === HTTP_RESPONSE_CONTENT_LENGTH) {
  118. const size = parseInt(row[HTTP_RESPONSE_CONTENT_LENGTH], 10);
  119. return <ResourceSize bytes={size} />;
  120. }
  121. if (column.key === 'profile_id') {
  122. return (
  123. <IconWrapper>
  124. {row.profile_id ? (
  125. <Tooltip title={t('View Profile')}>
  126. <LinkButton
  127. to={`/profiling/profile/${row.project}/${row.profile_id}/flamechart/`}
  128. size="xs"
  129. >
  130. <IconProfiling size="xs" />
  131. </LinkButton>
  132. </Tooltip>
  133. ) : (
  134. <div {...commonProps}>(no value)</div>
  135. )}
  136. </IconWrapper>
  137. );
  138. }
  139. if (column.key === 'duration') {
  140. return (
  141. <DurationCell containerProps={commonProps} milliseconds={row['span.self_time']} />
  142. );
  143. }
  144. if (column.key === 'avg_comparison') {
  145. return (
  146. <DurationComparisonCell
  147. containerProps={commonProps}
  148. duration={row['span.self_time']}
  149. compareToDuration={avg}
  150. />
  151. );
  152. }
  153. return <span {...commonProps}>{row[column.key]}</span>;
  154. }
  155. return (
  156. <div onMouseLeave={handleMouseLeave}>
  157. <GridEditable
  158. isLoading={isLoading}
  159. data={data}
  160. columnOrder={columnOrder ?? DEFAULT_COLUMN_ORDER}
  161. columnSortBy={[]}
  162. grid={{
  163. renderHeadCell,
  164. renderBodyCell,
  165. }}
  166. location={location}
  167. />
  168. </div>
  169. );
  170. }
  171. const IconWrapper = styled('div')`
  172. text-align: right;
  173. width: 100%;
  174. height: 26px;
  175. `;