content.tsx 9.5 KB

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