profileTransactionsTable.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import {useMemo, useState} from 'react';
  2. import Count from 'sentry/components/count';
  3. import DateTime from 'sentry/components/dateTime';
  4. import GridEditable, {
  5. COL_WIDTH_UNDEFINED,
  6. GridColumnOrder,
  7. GridColumnSortBy,
  8. } from 'sentry/components/gridEditable';
  9. import ProjectBadge from 'sentry/components/idBadge/projectBadge';
  10. import Link from 'sentry/components/links/link';
  11. import PerformanceDuration from 'sentry/components/performanceDuration';
  12. import {t} from 'sentry/locale';
  13. import {ProfileTransaction} from 'sentry/types/profiling/core';
  14. import {defined} from 'sentry/utils';
  15. import {Container, NumberContainer} from 'sentry/utils/discover/styles';
  16. import {generateProfileSummaryRouteWithQuery} from 'sentry/utils/profiling/routes';
  17. import {renderTableHead} from 'sentry/utils/profiling/tableRenderer';
  18. import {useLocation} from 'sentry/utils/useLocation';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import useProjects from 'sentry/utils/useProjects';
  21. interface ProfileTransactionsTableProps {
  22. error: string | null;
  23. isLoading: boolean;
  24. transactions: ProfileTransaction[];
  25. }
  26. function ProfileTransactionsTable(props: ProfileTransactionsTableProps) {
  27. const location = useLocation();
  28. const organization = useOrganization();
  29. const {projects} = useProjects();
  30. const [currentSort, setCurrentSort] = useState<GridColumnSortBy<string> | null>(() => {
  31. if (location.query.orderBy) {
  32. const [key, order] = (location.query.orderBy as string).split(',');
  33. return {
  34. key,
  35. order,
  36. } as GridColumnSortBy<string>;
  37. }
  38. return null;
  39. });
  40. const sortableColumns = useMemo(
  41. () => new Set(['transaction', 'project', 'lastSeen', 'p75', 'p95', 'count']),
  42. []
  43. );
  44. const transactions: TableDataRow[] = useMemo(() => {
  45. const rows = props.transactions.map(transaction => {
  46. const project = projects.find(proj => proj.id === transaction.project_id);
  47. return {
  48. _transactionName: transaction.name,
  49. transaction: project ? (
  50. <Link
  51. to={generateProfileSummaryRouteWithQuery({
  52. location,
  53. orgSlug: organization.slug,
  54. projectSlug: project.slug,
  55. transaction: transaction.name,
  56. })}
  57. >
  58. {transaction.name}
  59. </Link>
  60. ) : (
  61. transaction.name
  62. ),
  63. count: transaction.profiles_count,
  64. project,
  65. p50: transaction.duration_ms.p50,
  66. p75: transaction.duration_ms.p75,
  67. p90: transaction.duration_ms.p90,
  68. p95: transaction.duration_ms.p95,
  69. p99: transaction.duration_ms.p99,
  70. lastSeen: transaction.last_profile_at,
  71. };
  72. });
  73. if (currentSort) {
  74. rows.sort((txA, txB) => {
  75. const column = currentSort.key;
  76. switch (column) {
  77. case 'transaction':
  78. return txA._transactionName.localeCompare(txB._transactionName);
  79. case 'lastSeen':
  80. return new Date(txA.lastSeen).getTime() - new Date(txB.lastSeen).getTime();
  81. case 'project':
  82. if (!txA.project?.slug || !txB.project?.slug) {
  83. return 1;
  84. }
  85. return txA.project.slug.localeCompare(txB.project.slug);
  86. case 'count':
  87. return txA.count - txB.count;
  88. case 'p75':
  89. return txA.p75 - txB.p75;
  90. case 'p95':
  91. return txA.p95 - txB.p95;
  92. default:
  93. return 1;
  94. }
  95. });
  96. if (currentSort.order === 'desc') {
  97. rows.reverse();
  98. }
  99. }
  100. return rows;
  101. }, [props.transactions, location, organization, projects, currentSort]);
  102. const generateSortLink = (column: string) => () => {
  103. let dir = 'asc';
  104. if (column === currentSort?.key && currentSort.order === 'asc') {
  105. dir = 'desc';
  106. }
  107. return {
  108. ...location,
  109. query: {
  110. ...location.query,
  111. orderBy: `${column},${dir}`,
  112. },
  113. };
  114. };
  115. const handleHeadCellOnClick = (column: GridColumnOrder<string>) => {
  116. if (currentSort?.key === column.key) {
  117. setCurrentSort({
  118. key: column.key,
  119. order: currentSort.order === 'asc' ? 'desc' : 'asc',
  120. });
  121. return;
  122. }
  123. setCurrentSort({
  124. key: column.key,
  125. order: 'asc',
  126. });
  127. };
  128. return (
  129. <GridEditable
  130. isLoading={props.isLoading}
  131. error={props.error}
  132. data={transactions}
  133. columnOrder={COLUMN_ORDER.map(key => COLUMNS[key])}
  134. columnSortBy={currentSort ? [currentSort] : []}
  135. grid={{
  136. renderHeadCell: renderTableHead<string>({
  137. generateSortLink,
  138. onClick: handleHeadCellOnClick,
  139. sortableColumns,
  140. currentSort,
  141. rightAlignedColumns: RIGHT_ALIGNED_COLUMNS,
  142. }),
  143. renderBodyCell: renderTableBody,
  144. }}
  145. location={location}
  146. />
  147. );
  148. }
  149. const RIGHT_ALIGNED_COLUMNS = new Set<TableColumnKey>([
  150. 'count',
  151. 'p50',
  152. 'p75',
  153. 'p90',
  154. 'p95',
  155. 'p99',
  156. ]);
  157. function renderTableBody(
  158. column: GridColumnOrder,
  159. dataRow: TableDataRow,
  160. rowIndex: number,
  161. columnIndex: number
  162. ) {
  163. return (
  164. <ProfilingTransactionsTableCell
  165. column={column}
  166. dataRow={dataRow}
  167. rowIndex={rowIndex}
  168. columnIndex={columnIndex}
  169. />
  170. );
  171. }
  172. interface ProfilingTransactionsTableCellProps {
  173. column: GridColumnOrder;
  174. columnIndex: number;
  175. dataRow: TableDataRow;
  176. rowIndex: number;
  177. }
  178. function ProfilingTransactionsTableCell({
  179. column,
  180. dataRow,
  181. }: ProfilingTransactionsTableCellProps) {
  182. const value = dataRow[column.key];
  183. switch (column.key) {
  184. case 'project':
  185. if (!defined(value)) {
  186. // should never happen but just in case
  187. return <Container>{t('n/a')}</Container>;
  188. }
  189. return (
  190. <Container>
  191. <ProjectBadge project={value} avatarSize={16} />
  192. </Container>
  193. );
  194. case 'count':
  195. return (
  196. <NumberContainer>
  197. <Count value={value} />
  198. </NumberContainer>
  199. );
  200. case 'p50':
  201. case 'p75':
  202. case 'p90':
  203. case 'p95':
  204. case 'p99':
  205. return (
  206. <NumberContainer>
  207. <PerformanceDuration milliseconds={value} abbreviation />
  208. </NumberContainer>
  209. );
  210. case 'lastSeen':
  211. return (
  212. <Container>
  213. <DateTime date={value} />
  214. </Container>
  215. );
  216. default:
  217. return <Container>{value}</Container>;
  218. }
  219. }
  220. type TableColumnKey =
  221. | 'transaction'
  222. | 'count'
  223. | 'project'
  224. | 'p50'
  225. | 'p75'
  226. | 'p90'
  227. | 'p95'
  228. | 'p99'
  229. | 'lastSeen';
  230. type TableDataRow = Record<TableColumnKey, any>;
  231. type TableColumn = GridColumnOrder<TableColumnKey>;
  232. const COLUMN_ORDER: TableColumnKey[] = [
  233. 'transaction',
  234. 'project',
  235. 'lastSeen',
  236. 'p75',
  237. 'p95',
  238. 'count',
  239. ];
  240. const COLUMNS: Record<TableColumnKey, TableColumn> = {
  241. transaction: {
  242. key: 'transaction',
  243. name: t('Transaction'),
  244. width: COL_WIDTH_UNDEFINED,
  245. },
  246. count: {
  247. key: 'count',
  248. name: t('Count'),
  249. width: COL_WIDTH_UNDEFINED,
  250. },
  251. project: {
  252. key: 'project',
  253. name: t('Project'),
  254. width: COL_WIDTH_UNDEFINED,
  255. },
  256. p50: {
  257. key: 'p50',
  258. name: t('P50'),
  259. width: COL_WIDTH_UNDEFINED,
  260. },
  261. p75: {
  262. key: 'p75',
  263. name: t('P75'),
  264. width: COL_WIDTH_UNDEFINED,
  265. },
  266. p90: {
  267. key: 'p90',
  268. name: t('P90'),
  269. width: COL_WIDTH_UNDEFINED,
  270. },
  271. p95: {
  272. key: 'p95',
  273. name: t('P95'),
  274. width: COL_WIDTH_UNDEFINED,
  275. },
  276. p99: {
  277. key: 'p99',
  278. name: t('P99'),
  279. width: COL_WIDTH_UNDEFINED,
  280. },
  281. lastSeen: {
  282. key: 'lastSeen',
  283. name: t('Last Seen'),
  284. width: COL_WIDTH_UNDEFINED,
  285. },
  286. };
  287. export {ProfileTransactionsTable};