index.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import {useCallback, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import SearchBar from 'sentry/components/events/searchBar';
  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 {MAX_QUERY_LENGTH} from 'sentry/constants';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {Organization} from 'sentry/types/organization';
  14. import {defined} from 'sentry/utils';
  15. import {browserHistory} from 'sentry/utils/browserHistory';
  16. import EventView from 'sentry/utils/discover/eventView';
  17. import {isAggregateField} from 'sentry/utils/discover/fields';
  18. import type {ProfilingFieldType} from 'sentry/utils/profiling/hooks/useProfileEvents';
  19. import {
  20. getProfilesTableFields,
  21. useProfileEvents,
  22. } from 'sentry/utils/profiling/hooks/useProfileEvents';
  23. import {formatSort} from 'sentry/utils/profiling/hooks/utils';
  24. import {decodeScalar} from 'sentry/utils/queryString';
  25. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  26. import {useLocation} from 'sentry/utils/useLocation';
  27. import useOrganization from 'sentry/utils/useOrganization';
  28. import useProjects from 'sentry/utils/useProjects';
  29. import Tab from 'sentry/views/performance/transactionSummary/tabs';
  30. import PageLayout, {redirectToPerformanceHomepage} from '../pageLayout';
  31. import {TransactionProfilesContent} from './content';
  32. function ProfilesLegacy() {
  33. const location = useLocation();
  34. const organization = useOrganization();
  35. const projects = useProjects();
  36. const profilesCursor = useMemo(
  37. () => decodeScalar(location.query.cursor),
  38. [location.query.cursor]
  39. );
  40. const project = projects.projects.find(p => p.id === location.query.project);
  41. const fields = getProfilesTableFields(project?.platform);
  42. const sortableFields = useMemo(() => new Set(fields), [fields]);
  43. const sort = formatSort<ProfilingFieldType>(decodeScalar(location.query.sort), fields, {
  44. key: 'timestamp',
  45. order: 'desc',
  46. });
  47. const [query, setQuery] = useState(() => {
  48. // The search fields from the URL differ between profiling and
  49. // events dataset. For now, just drop everything except transaction
  50. const search = new MutableSearch('');
  51. const transaction = decodeScalar(location.query.transaction);
  52. if (defined(transaction)) {
  53. search.setFilterValues('transaction', [transaction]);
  54. }
  55. return search;
  56. });
  57. const profiles = useProfileEvents<ProfilingFieldType>({
  58. cursor: profilesCursor,
  59. fields,
  60. query: query.formatString(),
  61. sort,
  62. limit: 30,
  63. referrer: 'api.profiling.transactions-profiles-table',
  64. });
  65. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  66. (searchQuery: string) => {
  67. setQuery(new MutableSearch(searchQuery));
  68. browserHistory.push({
  69. ...location,
  70. query: {
  71. ...location.query,
  72. cursor: undefined,
  73. query: searchQuery || undefined,
  74. },
  75. });
  76. },
  77. [location]
  78. );
  79. const transaction = decodeScalar(location.query.transaction);
  80. return (
  81. <PageLayout
  82. location={location}
  83. organization={organization}
  84. projects={projects.projects}
  85. tab={Tab.PROFILING}
  86. generateEventView={() => EventView.fromLocation(location)}
  87. getDocumentTitle={() => t(`Profile: %s`, transaction)}
  88. childComponent={() => {
  89. return (
  90. <Layout.Main fullWidth>
  91. <FilterActions>
  92. <PageFilterBar condensed>
  93. <EnvironmentPageFilter />
  94. <DatePageFilter />
  95. </PageFilterBar>
  96. <SearchBar
  97. searchSource="transaction_profiles"
  98. organization={organization}
  99. projectIds={projects.projects.map(p => parseInt(p.id, 10))}
  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. function ProfilesWrapper() {
  120. const organization = useOrganization();
  121. const location = useLocation();
  122. const transaction = decodeScalar(location.query.transaction);
  123. if (!transaction) {
  124. redirectToPerformanceHomepage(organization, location);
  125. return null;
  126. }
  127. return <Profiles organization={organization} transaction={transaction} />;
  128. }
  129. interface ProfilesProps {
  130. organization: Organization;
  131. transaction: string;
  132. }
  133. function Profiles({organization, transaction}: ProfilesProps) {
  134. const location = useLocation();
  135. const projects = useProjects();
  136. const rawQuery = decodeScalar(location.query.query, '');
  137. const query = useMemo(() => {
  138. const conditions = new MutableSearch(rawQuery);
  139. conditions.setFilterValues('event.type', ['transaction']);
  140. conditions.setFilterValues('transaction', [transaction]);
  141. Object.keys(conditions.filters).forEach(field => {
  142. if (isAggregateField(field)) {
  143. conditions.removeFilter(field);
  144. }
  145. });
  146. return conditions.formatString();
  147. }, [transaction, rawQuery]);
  148. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  149. (searchQuery: string) => {
  150. browserHistory.push({
  151. ...location,
  152. query: {
  153. ...location.query,
  154. cursor: undefined,
  155. query: searchQuery || undefined,
  156. },
  157. });
  158. },
  159. [location]
  160. );
  161. return (
  162. <PageLayout
  163. location={location}
  164. organization={organization}
  165. projects={projects.projects}
  166. tab={Tab.PROFILING}
  167. generateEventView={() => EventView.fromLocation(location)}
  168. getDocumentTitle={() => t(`Profile: %s`, transaction)}
  169. fillSpace
  170. childComponent={() => {
  171. return (
  172. <StyledMain fullWidth>
  173. <FilterActions>
  174. <PageFilterBar condensed>
  175. <EnvironmentPageFilter />
  176. <DatePageFilter />
  177. </PageFilterBar>
  178. <StyledSearchBar
  179. searchSource="transaction_profiles"
  180. organization={organization}
  181. projectIds={projects.projects.map(p => parseInt(p.id, 10))}
  182. query={rawQuery}
  183. onSearch={handleSearch}
  184. maxQueryLength={MAX_QUERY_LENGTH}
  185. />
  186. </FilterActions>
  187. <TransactionProfilesContent query={query} />
  188. </StyledMain>
  189. );
  190. }}
  191. />
  192. );
  193. }
  194. const FilterActions = styled('div')`
  195. margin-bottom: ${space(2)};
  196. gap: ${space(2)};
  197. display: grid;
  198. grid-template-columns: min-content 1fr;
  199. `;
  200. const StyledSearchBar = styled(SearchBar)`
  201. @media (min-width: ${p => p.theme.breakpoints.small}) {
  202. order: 1;
  203. grid-column: 1/4;
  204. }
  205. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  206. order: initial;
  207. grid-column: auto;
  208. }
  209. `;
  210. const StyledMain = styled(Layout.Main)`
  211. display: flex;
  212. flex-direction: column;
  213. flex: 1;
  214. `;
  215. function ProfilesIndex() {
  216. const organization = useOrganization();
  217. if (organization.features.includes('continuous-profiling-compat')) {
  218. return <ProfilesWrapper />;
  219. }
  220. return <ProfilesLegacy />;
  221. }
  222. export default ProfilesIndex;