functionsTable.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import {useCallback, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import Count from 'sentry/components/count';
  4. import GridEditable, {
  5. COL_WIDTH_UNDEFINED,
  6. GridColumnOrder,
  7. } from 'sentry/components/gridEditable';
  8. import PerformanceDuration from 'sentry/components/performanceDuration';
  9. import {ArrayLinks} from 'sentry/components/profiling/arrayLinks';
  10. import {t} from 'sentry/locale';
  11. import {Project} from 'sentry/types';
  12. import {SuspectFunction} from 'sentry/types/profiling/core';
  13. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  14. import {Container, NumberContainer} from 'sentry/utils/discover/styles';
  15. import {getShortEventId} from 'sentry/utils/events';
  16. import {generateProfileFlamechartRouteWithQuery} 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. interface FunctionsTableProps {
  21. analyticsPageSource: 'performance_transaction' | 'profiling_transaction';
  22. error: string | null;
  23. functions: SuspectFunction[];
  24. isLoading: boolean;
  25. project: Project | undefined;
  26. sort: string;
  27. }
  28. function FunctionsTable(props: FunctionsTableProps) {
  29. const location = useLocation();
  30. const organization = useOrganization();
  31. const sort = useMemo(() => {
  32. let column = props.sort;
  33. let order: 'asc' | 'desc' = 'asc' as const;
  34. if (props.sort.startsWith('-')) {
  35. column = props.sort.substring(1);
  36. order = 'desc' as const;
  37. }
  38. if (!SORTABLE_COLUMNS.has(column as any)) {
  39. column = 'p99';
  40. }
  41. return {
  42. key: column as TableColumnKey,
  43. order,
  44. };
  45. }, [props.sort]);
  46. const functions: TableDataRow[] = useMemo(() => {
  47. const project = props.project;
  48. if (!project) {
  49. return [];
  50. }
  51. return props.functions.map(func => {
  52. const {worst, examples, ...rest} = func;
  53. const allExamples = examples.filter(example => example !== worst);
  54. allExamples.unshift(worst);
  55. return {
  56. ...rest,
  57. examples: allExamples.map(example => {
  58. const profileId = example.replaceAll('-', '');
  59. return {
  60. value: getShortEventId(profileId),
  61. onClick: () =>
  62. trackAdvancedAnalyticsEvent('profiling_views.go_to_flamegraph', {
  63. organization,
  64. source: `${props.analyticsPageSource}.suspect_functions_table`,
  65. }),
  66. target: generateProfileFlamechartRouteWithQuery({
  67. orgSlug: organization.slug,
  68. projectSlug: project.slug,
  69. profileId,
  70. query: {
  71. // specify the frame to focus, the flamegraph will switch
  72. // to the appropriate thread when these are specified
  73. frameName: func.name,
  74. framePackage: func.package,
  75. },
  76. }),
  77. };
  78. }),
  79. };
  80. });
  81. }, [organization, props.project, props.functions, props.analyticsPageSource]);
  82. const generateSortLink = useCallback(
  83. (column: TableColumnKey) => {
  84. if (!SORTABLE_COLUMNS.has(column)) {
  85. return () => undefined;
  86. }
  87. const direction =
  88. sort.key !== column ? 'desc' : sort.order === 'desc' ? 'asc' : 'desc';
  89. return () => ({
  90. ...location,
  91. query: {
  92. ...location.query,
  93. functionsSort: `${direction === 'desc' ? '-' : ''}${column}`,
  94. },
  95. });
  96. },
  97. [location, sort]
  98. );
  99. return (
  100. <GridEditable
  101. isLoading={props.isLoading}
  102. error={props.error}
  103. data={functions}
  104. columnOrder={COLUMN_ORDER.map(key => COLUMNS[key])}
  105. columnSortBy={[]}
  106. grid={{
  107. renderHeadCell: renderTableHead({
  108. currentSort: sort,
  109. rightAlignedColumns: RIGHT_ALIGNED_COLUMNS,
  110. sortableColumns: SORTABLE_COLUMNS,
  111. generateSortLink,
  112. }),
  113. renderBodyCell: renderFunctionsTableCell,
  114. }}
  115. location={location}
  116. />
  117. );
  118. }
  119. const RIGHT_ALIGNED_COLUMNS = new Set<TableColumnKey>(['p75', 'p99', 'count']);
  120. const SORTABLE_COLUMNS = RIGHT_ALIGNED_COLUMNS;
  121. function renderFunctionsTableCell(
  122. column: TableColumn,
  123. dataRow: TableDataRow,
  124. rowIndex: number,
  125. columnIndex: number
  126. ) {
  127. return (
  128. <ProfilingFunctionsTableCell
  129. column={column}
  130. dataRow={dataRow}
  131. rowIndex={rowIndex}
  132. columnIndex={columnIndex}
  133. />
  134. );
  135. }
  136. interface ProfilingFunctionsTableCellProps {
  137. column: TableColumn;
  138. columnIndex: number;
  139. dataRow: TableDataRow;
  140. rowIndex: number;
  141. }
  142. const EmptyValueContainer = styled('span')`
  143. color: ${p => p.theme.gray300};
  144. `;
  145. function ProfilingFunctionsTableCell({
  146. column,
  147. dataRow,
  148. }: ProfilingFunctionsTableCellProps) {
  149. const value = dataRow[column.key];
  150. switch (column.key) {
  151. case 'count':
  152. return (
  153. <NumberContainer>
  154. <Count value={value} />
  155. </NumberContainer>
  156. );
  157. case 'p75':
  158. case 'p99':
  159. return (
  160. <NumberContainer>
  161. <PerformanceDuration nanoseconds={value} abbreviation />
  162. </NumberContainer>
  163. );
  164. case 'examples':
  165. return <ArrayLinks items={value} />;
  166. case 'name':
  167. case 'package':
  168. const name = value || <EmptyValueContainer>{t('Unknown')}</EmptyValueContainer>;
  169. return <Container>{name}</Container>;
  170. default:
  171. return <Container>{value}</Container>;
  172. }
  173. }
  174. type TableColumnKey = keyof Omit<SuspectFunction, 'fingerprint' | 'worst'>;
  175. type TableDataRow = Record<TableColumnKey, any>;
  176. type TableColumn = GridColumnOrder<TableColumnKey>;
  177. const COLUMN_ORDER: TableColumnKey[] = [
  178. 'name',
  179. 'package',
  180. 'count',
  181. 'p75',
  182. 'p99',
  183. 'examples',
  184. ];
  185. const COLUMNS: Record<Exclude<TableColumnKey, 'p95'>, TableColumn> = {
  186. name: {
  187. key: 'name',
  188. name: t('Name'),
  189. width: COL_WIDTH_UNDEFINED,
  190. },
  191. package: {
  192. key: 'package',
  193. name: t('Package'),
  194. width: COL_WIDTH_UNDEFINED,
  195. },
  196. path: {
  197. key: 'path',
  198. name: t('Path'),
  199. width: COL_WIDTH_UNDEFINED,
  200. },
  201. p75: {
  202. key: 'p75',
  203. name: t('P75 Total Duration'),
  204. width: COL_WIDTH_UNDEFINED,
  205. },
  206. p99: {
  207. key: 'p99',
  208. name: t('P99 Total Duration'),
  209. width: COL_WIDTH_UNDEFINED,
  210. },
  211. count: {
  212. key: 'count',
  213. name: t('Total Occurrences'),
  214. width: COL_WIDTH_UNDEFINED,
  215. },
  216. examples: {
  217. key: 'examples',
  218. name: t('Example Profiles'),
  219. width: COL_WIDTH_UNDEFINED,
  220. },
  221. };
  222. export {FunctionsTable};