profilesTable.tsx 3.5 KB

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