profilesTable.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import {useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import Pagination from 'sentry/components/pagination';
  4. import {ProfileEventsTable} from 'sentry/components/profiling/profileEventsTable';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. import {defined} from 'sentry/utils';
  8. import {useProfileEvents} from 'sentry/utils/profiling/hooks/useProfileEvents';
  9. import {formatSort} from 'sentry/utils/profiling/hooks/utils';
  10. import {decodeScalar} from 'sentry/utils/queryString';
  11. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  12. import {useLocation} from 'sentry/utils/useLocation';
  13. import useOrganization from 'sentry/utils/useOrganization';
  14. const ALL_FIELDS = [
  15. 'profile.id',
  16. 'timestamp',
  17. 'transaction.duration',
  18. 'release',
  19. 'environment',
  20. 'trace',
  21. 'trace.transaction',
  22. 'id',
  23. ] as const;
  24. type FieldType = (typeof ALL_FIELDS)[number];
  25. const FIELDS = [
  26. 'profile.id',
  27. 'timestamp',
  28. 'transaction.duration',
  29. 'release',
  30. 'environment',
  31. 'trace',
  32. 'trace.transaction',
  33. ] as const;
  34. export function ProfilesTable() {
  35. const location = useLocation();
  36. const organization = useOrganization();
  37. const useTransactions = organization.features.includes('profiling-using-transactions');
  38. const queryFields = useMemo(() => {
  39. const transactionIdColumn = useTransactions
  40. ? ('id' as const)
  41. : ('trace.transaction' as const);
  42. return [
  43. 'profile.id',
  44. 'timestamp',
  45. 'transaction.duration',
  46. 'release',
  47. 'environment',
  48. 'trace',
  49. transactionIdColumn,
  50. ] as const;
  51. }, [useTransactions]);
  52. const sort = useMemo(() => {
  53. return formatSort<FieldType>(decodeScalar(location.query.sort), queryFields, {
  54. key: 'timestamp',
  55. order: 'desc',
  56. });
  57. }, [queryFields, location.query.sort]);
  58. const rawQuery = useMemo(() => {
  59. return decodeScalar(location.query.query, '');
  60. }, [location.query.query]);
  61. const query = useMemo(() => {
  62. const search = new MutableSearch(rawQuery);
  63. const transaction = decodeScalar(location.query.transaction);
  64. if (defined(transaction)) {
  65. search.setFilterValues('transaction', [transaction]);
  66. }
  67. return search.formatString();
  68. }, [rawQuery, location.query.transaction]);
  69. const profilesCursor = useMemo(
  70. () => decodeScalar(location.query.cursor),
  71. [location.query.cursor]
  72. );
  73. const profiles = useProfileEvents<FieldType>({
  74. cursor: profilesCursor,
  75. fields: queryFields,
  76. query,
  77. sort,
  78. limit: 20,
  79. referrer: 'api.profiling.profile-summary-table',
  80. });
  81. const eventsTableProps = useMemo(() => {
  82. return {columns: FIELDS, sortableColumns: new Set(FIELDS)};
  83. }, []);
  84. // Transform the response so that the data is compatible with the renderer
  85. // regardless if we're using the transactions or profiles table
  86. const data = useMemo(() => {
  87. if (!profiles.data) {
  88. return null;
  89. }
  90. const _data = {
  91. data: profiles.data.data.map(row => {
  92. return {
  93. ...row,
  94. 'trace.transaction': useTransactions ? row.id : row['trace.transaction'],
  95. };
  96. }),
  97. meta: profiles.data.meta,
  98. };
  99. return _data;
  100. }, [profiles.data, useTransactions]);
  101. return (
  102. <ProfileEvents>
  103. <ProfileEventsTableContainer>
  104. <ProfileEventsTable
  105. sort={sort}
  106. data={profiles.status === 'success' ? data : null}
  107. error={profiles.status === 'error' ? t('Unable to load profiles') : null}
  108. isLoading={profiles.status === 'loading'}
  109. {...eventsTableProps}
  110. />
  111. </ProfileEventsTableContainer>
  112. <StyledPagination pageLinks={profiles.getResponseHeader?.('Link')} />
  113. </ProfileEvents>
  114. );
  115. }
  116. const ProfileEvents = styled('div')``;
  117. const StyledPagination = styled(Pagination)`
  118. margin-top: ${space(1)};
  119. margin-right: ${space(1)};
  120. margin-bottom: ${space(2)};
  121. `;
  122. const ProfileEventsTableContainer = styled('div')`
  123. th,
  124. tr {
  125. border-radius: 0 !important;
  126. }
  127. > div {
  128. border-radius: 0;
  129. border-top: none;
  130. margin-bottom: 0;
  131. }
  132. `;