index.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 SmartSearchBar, {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 {defined} from 'sentry/utils';
  14. import EventView from 'sentry/utils/discover/eventView';
  15. import {useProfileEvents} from 'sentry/utils/profiling/hooks/useProfileEvents';
  16. import {useProfileFilters} from 'sentry/utils/profiling/hooks/useProfileFilters';
  17. import {formatSort} from 'sentry/utils/profiling/hooks/utils';
  18. import {decodeScalar} from 'sentry/utils/queryString';
  19. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  20. import {useLocation} from 'sentry/utils/useLocation';
  21. import useOrganization from 'sentry/utils/useOrganization';
  22. import usePageFilters from 'sentry/utils/usePageFilters';
  23. import useProjects from 'sentry/utils/useProjects';
  24. import Tab from 'sentry/views/performance/transactionSummary/tabs';
  25. import {
  26. getProfilesTableFields,
  27. ProfilingFieldType,
  28. } from 'sentry/views/profiling/profileSummary/content';
  29. import PageLayout from '../pageLayout';
  30. function Profiles(): React.ReactElement {
  31. const location = useLocation();
  32. const organization = useOrganization();
  33. const projects = useProjects();
  34. const {selection} = usePageFilters();
  35. const profilesCursor = useMemo(
  36. () => decodeScalar(location.query.cursor),
  37. [location.query.cursor]
  38. );
  39. const project = projects.projects.find(p => p.id === location.query.project);
  40. const fields = getProfilesTableFields(project?.platform);
  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. />
  112. </Layout.Main>
  113. );
  114. }}
  115. />
  116. );
  117. }
  118. const FilterActions = styled('div')`
  119. margin-bottom: ${space(2)};
  120. gap: ${space(2)};
  121. display: grid;
  122. grid-template-columns: min-content 1fr;
  123. `;
  124. export default Profiles;