index.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 sort = formatSort<ProfilingFieldType>(decodeScalar(location.query.sort), fields, {
  41. key: 'timestamp',
  42. order: 'desc',
  43. });
  44. const [query, setQuery] = useState(() => {
  45. // The search fields from the URL differ between profiling and
  46. // events dataset. For now, just drop everything except transaction
  47. const search = new MutableSearch('');
  48. const transaction = decodeScalar(location.query.transaction);
  49. if (defined(transaction)) {
  50. search.setFilterValues('transaction', [transaction]);
  51. }
  52. return search;
  53. });
  54. const profiles = useProfileEvents<ProfilingFieldType>({
  55. cursor: profilesCursor,
  56. fields,
  57. query: query.formatString(),
  58. sort,
  59. limit: 30,
  60. referrer: 'api.profiling.transactions-profiles-table',
  61. });
  62. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  63. (searchQuery: string) => {
  64. setQuery(new MutableSearch(searchQuery));
  65. browserHistory.push({
  66. ...location,
  67. query: {
  68. ...location.query,
  69. cursor: undefined,
  70. query: searchQuery || undefined,
  71. },
  72. });
  73. },
  74. [location]
  75. );
  76. const profileFilters = useProfileFilters({query: '', selection});
  77. const transaction = decodeScalar(location.query.transaction);
  78. return (
  79. <PageLayout
  80. location={location}
  81. organization={organization}
  82. projects={projects.projects}
  83. tab={Tab.PROFILING}
  84. generateEventView={() => EventView.fromLocation(location)}
  85. getDocumentTitle={() => t(`Profile: %s`, transaction)}
  86. childComponent={() => {
  87. return (
  88. <Layout.Main fullWidth>
  89. <FilterActions>
  90. <PageFilterBar condensed>
  91. <EnvironmentPageFilter />
  92. <DatePageFilter />
  93. </PageFilterBar>
  94. <SmartSearchBar
  95. organization={organization}
  96. hasRecentSearches
  97. searchSource="profile_landing"
  98. supportedTags={profileFilters}
  99. query={query.formatString()}
  100. onSearch={handleSearch}
  101. maxQueryLength={MAX_QUERY_LENGTH}
  102. />
  103. </FilterActions>
  104. <ProfileEventsTable
  105. columns={fields}
  106. data={profiles.status === 'success' ? profiles.data : null}
  107. error={profiles.status === 'error' ? t('Unable to load profiles') : null}
  108. isLoading={profiles.status === 'loading'}
  109. sort={sort}
  110. />
  111. </Layout.Main>
  112. );
  113. }}
  114. />
  115. );
  116. }
  117. const FilterActions = styled('div')`
  118. margin-bottom: ${space(2)};
  119. gap: ${space(2)};
  120. display: grid;
  121. grid-template-columns: min-content 1fr;
  122. `;
  123. export default Profiles;