content.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 Alert from 'sentry/components/alert';
  6. import Button from 'sentry/components/button';
  7. import DatePageFilter from 'sentry/components/datePageFilter';
  8. import EnvironmentPageFilter from 'sentry/components/environmentPageFilter';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import ExternalLink from 'sentry/components/links/externalLink';
  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 {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  16. import Pagination from 'sentry/components/pagination';
  17. import {ProfileEventsTable} from 'sentry/components/profiling/profileEventsTable';
  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, tct} 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. SidebarPanelStore.activatePanel(SidebarPanelKey.ProfilingOnboarding);
  86. }, []);
  87. const shouldShowProfilingOnboardingPanel = useMemo((): boolean => {
  88. // if it's My Projects or All projects, only show onboarding if we can't
  89. // find any projects with profiles
  90. if (
  91. selection.projects.length === 0 ||
  92. selection.projects[0] === ALL_ACCESS_PROJECTS
  93. ) {
  94. return projects.every(project => !project.hasProfiles);
  95. }
  96. // otherwise, only show onboarding if we can't find any projects with profiles
  97. // from those that were selected
  98. const projectsWithProfiles = new Set(
  99. projects.filter(project => project.hasProfiles).map(project => project.id)
  100. );
  101. return selection.projects.every(
  102. project => !projectsWithProfiles.has(String(project))
  103. );
  104. }, [selection.projects, projects]);
  105. return (
  106. <SentryDocumentTitle title={t('Profiling')} orgSlug={organization.slug}>
  107. <PageFiltersContainer>
  108. <NoProjectMessage organization={organization}>
  109. <StyledPageContent>
  110. <Layout.Header>
  111. <StyledLayoutHeaderContent>
  112. <StyledHeading>
  113. {t('Profiling')}
  114. <PageHeadingQuestionTooltip
  115. title={tct(
  116. 'A view of how your application performs in a variety of environments, based off of the performance profiles collected from real user devices in production. [link: Read the docs].',
  117. {
  118. link: (
  119. <ExternalLink href="https://docs.sentry.io/product/profiling/" />
  120. ),
  121. }
  122. )}
  123. />
  124. </StyledHeading>
  125. <HeadingActions>
  126. <Button size="sm" onClick={onSetupProfilingClick}>
  127. {t('Set Up Profiling')}
  128. </Button>
  129. <Button
  130. size="sm"
  131. priority="primary"
  132. href="https://discord.gg/zrMjKA4Vnz"
  133. external
  134. onClick={() => {
  135. trackAdvancedAnalyticsEvent(
  136. 'profiling_views.visit_discord_channel',
  137. {
  138. organization,
  139. }
  140. );
  141. }}
  142. >
  143. {t('Join Discord')}
  144. </Button>
  145. </HeadingActions>
  146. </StyledLayoutHeaderContent>
  147. </Layout.Header>
  148. <Layout.Body>
  149. <Layout.Main fullWidth>
  150. {transactionsError && (
  151. <Alert type="error" showIcon>
  152. {transactionsError}
  153. </Alert>
  154. )}
  155. <ActionBar>
  156. <PageFilterBar condensed>
  157. <ProjectPageFilter />
  158. <EnvironmentPageFilter />
  159. <DatePageFilter alignDropdown="left" />
  160. </PageFilterBar>
  161. <SmartSearchBar
  162. organization={organization}
  163. hasRecentSearches
  164. searchSource="profile_landing"
  165. supportedTags={profileFilters}
  166. query={query}
  167. onSearch={handleSearch}
  168. maxQueryLength={MAX_QUERY_LENGTH}
  169. />
  170. </ActionBar>
  171. {shouldShowProfilingOnboardingPanel ? (
  172. <ProfilingOnboardingPanel>
  173. <Button href="https://docs.sentry.io/product/profiling/" external>
  174. {t('Read Docs')}
  175. </Button>
  176. <Button onClick={onSetupProfilingClick} priority="primary">
  177. {t('Set Up Profiling')}
  178. </Button>
  179. </ProfilingOnboardingPanel>
  180. ) : (
  181. <Fragment>
  182. <ProfileCharts router={router} query={query} selection={selection} />
  183. <ProfileEventsTable
  184. columns={FIELDS.slice()}
  185. data={
  186. transactions.status === 'success' ? transactions.data[0] : null
  187. }
  188. error={
  189. transactions.status === 'error'
  190. ? t('Unable to load profiles')
  191. : null
  192. }
  193. isLoading={transactions.status === 'loading'}
  194. sort={sort}
  195. sortableColumns={new Set(FIELDS)}
  196. />
  197. <Pagination
  198. pageLinks={
  199. transactions.status === 'success'
  200. ? transactions.data?.[2]?.getResponseHeader('Link') ?? null
  201. : null
  202. }
  203. />
  204. </Fragment>
  205. )}
  206. </Layout.Main>
  207. </Layout.Body>
  208. </StyledPageContent>
  209. </NoProjectMessage>
  210. </PageFiltersContainer>
  211. </SentryDocumentTitle>
  212. );
  213. }
  214. const FIELDS = [
  215. 'transaction',
  216. 'project.id',
  217. 'last_seen()',
  218. 'p75()',
  219. 'p95()',
  220. 'p99()',
  221. 'count()',
  222. ] as const;
  223. type FieldType = typeof FIELDS[number];
  224. const StyledPageContent = styled(PageContent)`
  225. padding: 0;
  226. `;
  227. const StyledLayoutHeaderContent = styled(Layout.HeaderContent)`
  228. display: flex;
  229. justify-content: space-between;
  230. flex-direction: row;
  231. `;
  232. const HeadingActions = styled('div')`
  233. display: flex;
  234. align-items: center;
  235. button:not(:last-child) {
  236. margin-right: ${space(1)};
  237. }
  238. `;
  239. const StyledHeading = styled(PageHeading)`
  240. line-height: 40px;
  241. `;
  242. const ActionBar = styled('div')`
  243. display: grid;
  244. gap: ${space(2)};
  245. grid-template-columns: min-content auto;
  246. margin-bottom: ${space(2)};
  247. `;
  248. export default ProfilingContent;