profilesTable.tsx 3.5 KB

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