content.tsx 9.9 KB

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