content.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import {Fragment, useCallback, useEffect, useMemo} from 'react';
  2. import {browserHistory, InjectedRouter} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import {openModal} from 'sentry/actionCreators/modal';
  6. import Alert from 'sentry/components/alert';
  7. import Button from 'sentry/components/button';
  8. import DatePageFilter from 'sentry/components/datePageFilter';
  9. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  10. import * as Layout from 'sentry/components/layouts/thirds';
  11. import NoProjectMessage from 'sentry/components/noProjectMessage';
  12. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  13. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  14. import PageHeading from 'sentry/components/pageHeading';
  15. import Pagination from 'sentry/components/pagination';
  16. import {ProfileEventsTable} from 'sentry/components/profiling/profileEventsTable';
  17. import {ProfilingOnboardingModal} from 'sentry/components/profiling/ProfilingOnboarding/profilingOnboardingModal';
  18. import ProjectPageFilter from 'sentry/components/projectPageFilter';
  19. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  20. import {SidebarPanelKey} from 'sentry/components/sidebar/types';
  21. import SmartSearchBar, {SmartSearchBarProps} from 'sentry/components/smartSearchBar';
  22. import {MAX_QUERY_LENGTH} from 'sentry/constants';
  23. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  24. import {t} from 'sentry/locale';
  25. import SidebarPanelStore from 'sentry/stores/sidebarPanelStore';
  26. import {PageContent} from 'sentry/styles/organization';
  27. import space from 'sentry/styles/space';
  28. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  29. import {
  30. formatError,
  31. formatSort,
  32. useProfileEvents,
  33. } from 'sentry/utils/profiling/hooks/useProfileEvents';
  34. import {useProfileFilters} from 'sentry/utils/profiling/hooks/useProfileFilters';
  35. import {decodeScalar} from 'sentry/utils/queryString';
  36. import useOrganization from 'sentry/utils/useOrganization';
  37. import usePageFilters from 'sentry/utils/usePageFilters';
  38. import useProjects from 'sentry/utils/useProjects';
  39. import {ProfileCharts} from './landing/profileCharts';
  40. import {ProfilingOnboardingPanel} from './profilingOnboardingPanel';
  41. interface ProfilingContentProps {
  42. location: Location;
  43. router: InjectedRouter;
  44. }
  45. function ProfilingContent({location, router}: ProfilingContentProps) {
  46. const organization = useOrganization();
  47. const {selection} = usePageFilters();
  48. const cursor = decodeScalar(location.query.cursor);
  49. const query = decodeScalar(location.query.query, '');
  50. const sort = formatSort<FieldType>(decodeScalar(location.query.sort), FIELDS, {
  51. key: 'count()',
  52. order: 'desc',
  53. });
  54. const profileFilters = useProfileFilters({query: '', selection});
  55. const {projects} = useProjects();
  56. const transactions = useProfileEvents<FieldType>({
  57. cursor,
  58. fields: FIELDS,
  59. query,
  60. sort,
  61. referrer: 'api.profiling.landing-table',
  62. });
  63. const transactionsError =
  64. transactions.status === 'error' ? formatError(transactions.error) : null;
  65. useEffect(() => {
  66. trackAdvancedAnalyticsEvent('profiling_views.landing', {
  67. organization,
  68. });
  69. }, [organization]);
  70. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  71. (searchQuery: string) => {
  72. browserHistory.push({
  73. ...location,
  74. query: {
  75. ...location.query,
  76. cursor: undefined,
  77. query: searchQuery || undefined,
  78. },
  79. });
  80. },
  81. [location]
  82. );
  83. // Open the modal on demand
  84. const onSetupProfilingClick = useCallback(() => {
  85. const profilingOnboardingChecklistEnabled = organization.features?.includes(
  86. 'profiling-onboarding-checklist'
  87. );
  88. if (profilingOnboardingChecklistEnabled) {
  89. SidebarPanelStore.activatePanel(SidebarPanelKey.ProfilingOnboarding);
  90. return;
  91. }
  92. openModal(props => {
  93. return <ProfilingOnboardingModal {...props} organization={organization} />;
  94. });
  95. }, [organization]);
  96. const shouldShowProfilingOnboardingPanel = useMemo((): boolean => {
  97. // if it's My Projects or All projects, only show onboarding if we can't
  98. // find any projects with profiles
  99. if (
  100. selection.projects.length === 0 ||
  101. selection.projects[0] === ALL_ACCESS_PROJECTS
  102. ) {
  103. return projects.every(project => !project.hasProfiles);
  104. }
  105. // otherwise, only show onboarding if we can't find any projects with profiles
  106. // from those that were selected
  107. const projectsWithProfiles = new Set(
  108. projects.filter(project => project.hasProfiles).map(project => project.id)
  109. );
  110. return selection.projects.every(
  111. project => !projectsWithProfiles.has(String(project))
  112. );
  113. }, [selection.projects, projects]);
  114. return (
  115. <SentryDocumentTitle title={t('Profiling')} orgSlug={organization.slug}>
  116. <PageFiltersContainer>
  117. <NoProjectMessage organization={organization}>
  118. <StyledPageContent>
  119. <Layout.Header>
  120. <StyledLayoutHeaderContent>
  121. <StyledHeading>{t('Profiling')}</StyledHeading>
  122. <HeadingActions>
  123. <Button size="sm" onClick={onSetupProfilingClick}>
  124. {t('Set Up Profiling')}
  125. </Button>
  126. <Button
  127. size="sm"
  128. priority="primary"
  129. href="https://discord.gg/zrMjKA4Vnz"
  130. external
  131. onClick={() => {
  132. trackAdvancedAnalyticsEvent(
  133. 'profiling_views.visit_discord_channel',
  134. {
  135. organization,
  136. }
  137. );
  138. }}
  139. >
  140. {t('Join Discord')}
  141. </Button>
  142. </HeadingActions>
  143. </StyledLayoutHeaderContent>
  144. </Layout.Header>
  145. <Layout.Body>
  146. <Layout.Main fullWidth>
  147. {transactionsError && (
  148. <Alert type="error" showIcon>
  149. {transactionsError}
  150. </Alert>
  151. )}
  152. <ActionBar>
  153. <PageFilterBar condensed>
  154. <ProjectPageFilter />
  155. <EnvironmentPageFilter />
  156. <DatePageFilter alignDropdown="left" />
  157. </PageFilterBar>
  158. <SmartSearchBar
  159. organization={organization}
  160. hasRecentSearches
  161. searchSource="profile_landing"
  162. supportedTags={profileFilters}
  163. query={query}
  164. onSearch={handleSearch}
  165. maxQueryLength={MAX_QUERY_LENGTH}
  166. />
  167. </ActionBar>
  168. {shouldShowProfilingOnboardingPanel ? (
  169. <ProfilingOnboardingPanel>
  170. <Button href="https://docs.sentry.io/product/profiling/" external>
  171. {t('Read Docs')}
  172. </Button>
  173. <Button onClick={onSetupProfilingClick} priority="primary">
  174. {t('Set Up Profiling')}
  175. </Button>
  176. </ProfilingOnboardingPanel>
  177. ) : (
  178. <Fragment>
  179. <ProfileCharts router={router} query={query} selection={selection} />
  180. <ProfileEventsTable
  181. columns={FIELDS.slice()}
  182. data={
  183. transactions.status === 'success' ? transactions.data[0] : null
  184. }
  185. error={
  186. transactions.status === 'error'
  187. ? t('Unable to load profiles')
  188. : null
  189. }
  190. isLoading={transactions.status === 'loading'}
  191. sort={sort}
  192. sortableColumns={new Set(FIELDS)}
  193. />
  194. <Pagination
  195. pageLinks={
  196. transactions.status === 'success'
  197. ? transactions.data?.[2]?.getResponseHeader('Link') ?? null
  198. : null
  199. }
  200. />
  201. </Fragment>
  202. )}
  203. </Layout.Main>
  204. </Layout.Body>
  205. </StyledPageContent>
  206. </NoProjectMessage>
  207. </PageFiltersContainer>
  208. </SentryDocumentTitle>
  209. );
  210. }
  211. const FIELDS = [
  212. 'transaction',
  213. 'project.id',
  214. 'last_seen()',
  215. 'p75()',
  216. 'p95()',
  217. 'p99()',
  218. 'count()',
  219. ] as const;
  220. type FieldType = typeof FIELDS[number];
  221. const StyledPageContent = styled(PageContent)`
  222. padding: 0;
  223. `;
  224. const StyledLayoutHeaderContent = styled(Layout.HeaderContent)`
  225. display: flex;
  226. justify-content: space-between;
  227. flex-direction: row;
  228. `;
  229. const HeadingActions = styled('div')`
  230. display: flex;
  231. align-items: center;
  232. button:not(:last-child) {
  233. margin-right: ${space(1)};
  234. }
  235. `;
  236. const StyledHeading = styled(PageHeading)`
  237. line-height: 40px;
  238. `;
  239. const ActionBar = styled('div')`
  240. display: grid;
  241. gap: ${space(2)};
  242. grid-template-columns: min-content auto;
  243. margin-bottom: ${space(2)};
  244. `;
  245. export default ProfilingContent;