index.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import {useCallback, useMemo, useState} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import * as Layout from 'sentry/components/layouts/thirds';
  5. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  6. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  7. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  8. import {ProfileEventsTable} from 'sentry/components/profiling/profileEventsTable';
  9. import type {SmartSearchBarProps} from 'sentry/components/smartSearchBar';
  10. import SmartSearchBar from 'sentry/components/smartSearchBar';
  11. import {MAX_QUERY_LENGTH} from 'sentry/constants';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {defined} from 'sentry/utils';
  15. import EventView from 'sentry/utils/discover/eventView';
  16. import {useProfileEvents} from 'sentry/utils/profiling/hooks/useProfileEvents';
  17. import {useProfileFilters} from 'sentry/utils/profiling/hooks/useProfileFilters';
  18. import {formatSort} from 'sentry/utils/profiling/hooks/utils';
  19. import {decodeScalar} from 'sentry/utils/queryString';
  20. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  21. import {useLocation} from 'sentry/utils/useLocation';
  22. import useOrganization from 'sentry/utils/useOrganization';
  23. import usePageFilters from 'sentry/utils/usePageFilters';
  24. import useProjects from 'sentry/utils/useProjects';
  25. import Tab from 'sentry/views/performance/transactionSummary/tabs';
  26. import type {ProfilingFieldType} from 'sentry/views/profiling/profileSummary/content';
  27. import {getProfilesTableFields} from 'sentry/views/profiling/profileSummary/content';
  28. import PageLayout from '../pageLayout';
  29. function Profiles(): React.ReactElement {
  30. const location = useLocation();
  31. const organization = useOrganization();
  32. const projects = useProjects();
  33. const {selection} = usePageFilters();
  34. const profilesCursor = useMemo(
  35. () => decodeScalar(location.query.cursor),
  36. [location.query.cursor]
  37. );
  38. const project = projects.projects.find(p => p.id === location.query.project);
  39. const fields = getProfilesTableFields(project?.platform);
  40. const sortableFields = useMemo(() => new Set(fields), [fields]);
  41. const sort = formatSort<ProfilingFieldType>(decodeScalar(location.query.sort), fields, {
  42. key: 'timestamp',
  43. order: 'desc',
  44. });
  45. const [query, setQuery] = useState(() => {
  46. // The search fields from the URL differ between profiling and
  47. // events dataset. For now, just drop everything except transaction
  48. const search = new MutableSearch('');
  49. const transaction = decodeScalar(location.query.transaction);
  50. if (defined(transaction)) {
  51. search.setFilterValues('transaction', [transaction]);
  52. }
  53. return search;
  54. });
  55. const profiles = useProfileEvents<ProfilingFieldType>({
  56. cursor: profilesCursor,
  57. fields,
  58. query: query.formatString(),
  59. sort,
  60. limit: 30,
  61. referrer: 'api.profiling.transactions-profiles-table',
  62. });
  63. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  64. (searchQuery: string) => {
  65. setQuery(new MutableSearch(searchQuery));
  66. browserHistory.push({
  67. ...location,
  68. query: {
  69. ...location.query,
  70. cursor: undefined,
  71. query: searchQuery || undefined,
  72. },
  73. });
  74. },
  75. [location]
  76. );
  77. const profileFilters = useProfileFilters({query: '', selection});
  78. const transaction = decodeScalar(location.query.transaction);
  79. return (
  80. <PageLayout
  81. location={location}
  82. organization={organization}
  83. projects={projects.projects}
  84. tab={Tab.PROFILING}
  85. generateEventView={() => EventView.fromLocation(location)}
  86. getDocumentTitle={() => t(`Profile: %s`, transaction)}
  87. childComponent={() => {
  88. return (
  89. <Layout.Main fullWidth>
  90. <FilterActions>
  91. <PageFilterBar condensed>
  92. <EnvironmentPageFilter />
  93. <DatePageFilter />
  94. </PageFilterBar>
  95. <SmartSearchBar
  96. organization={organization}
  97. hasRecentSearches
  98. searchSource="profile_landing"
  99. supportedTags={profileFilters}
  100. query={query.formatString()}
  101. onSearch={handleSearch}
  102. maxQueryLength={MAX_QUERY_LENGTH}
  103. />
  104. </FilterActions>
  105. <ProfileEventsTable
  106. columns={fields}
  107. data={profiles.status === 'success' ? profiles.data : null}
  108. error={profiles.status === 'error' ? t('Unable to load profiles') : null}
  109. isLoading={profiles.status === 'loading'}
  110. sort={sort}
  111. sortableColumns={sortableFields}
  112. />
  113. </Layout.Main>
  114. );
  115. }}
  116. />
  117. );
  118. }
  119. const FilterActions = styled('div')`
  120. margin-bottom: ${space(2)};
  121. gap: ${space(2)};
  122. display: grid;
  123. grid-template-columns: min-content 1fr;
  124. `;
  125. export default Profiles;