content.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 FeatureBadge from 'sentry/components/featureBadge';
  11. import * as Layout from 'sentry/components/layouts/thirds';
  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. <Layout.Header>
  114. <Layout.HeaderContent>
  115. <Layout.Title>
  116. {t('Profiling')}
  117. <PageHeadingQuestionTooltip
  118. docsUrl="https://docs.sentry.io/product/profiling/"
  119. title={t(
  120. '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.'
  121. )}
  122. />
  123. <FeatureBadge type="beta" />
  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('profiling_views.visit_discord_channel', {
  138. organization,
  139. });
  140. }}
  141. >
  142. {t('Join Discord')}
  143. </Button>
  144. </ButtonBar>
  145. </Layout.HeaderActions>
  146. </Layout.Header>
  147. <Layout.Body>
  148. <Layout.Main fullWidth>
  149. {transactionsError && (
  150. <Alert type="error" showIcon>
  151. {transactionsError}
  152. </Alert>
  153. )}
  154. <ActionBar>
  155. <PageFilterBar condensed>
  156. <ProjectPageFilter />
  157. <EnvironmentPageFilter />
  158. <DatePageFilter alignDropdown="left" />
  159. </PageFilterBar>
  160. <SmartSearchBar
  161. organization={organization}
  162. hasRecentSearches
  163. searchSource="profile_landing"
  164. supportedTags={profileFilters}
  165. query={query}
  166. onSearch={handleSearch}
  167. maxQueryLength={MAX_QUERY_LENGTH}
  168. />
  169. </ActionBar>
  170. {shouldShowProfilingOnboardingPanel ? (
  171. <ProfilingOnboardingPanel>
  172. <Button onClick={onSetupProfilingClick} priority="primary">
  173. {t('Set Up Profiling')}
  174. </Button>
  175. <Button href="https://docs.sentry.io/product/profiling/" external>
  176. {t('Read Docs')}
  177. </Button>
  178. </ProfilingOnboardingPanel>
  179. ) : (
  180. <Fragment>
  181. {isNewProfilingDashboardEnabled ? (
  182. <PanelsGrid>
  183. <ProfilingSlowestTransactionsPanel />
  184. <ProfileCharts
  185. query={query}
  186. selection={selection}
  187. hideCount={isNewProfilingDashboardEnabled}
  188. />
  189. </PanelsGrid>
  190. ) : (
  191. <ProfileCharts
  192. query={query}
  193. selection={selection}
  194. hideCount={isNewProfilingDashboardEnabled}
  195. />
  196. )}
  197. <ProfileEventsTable
  198. columns={FIELDS.slice()}
  199. data={transactions.status === 'success' ? transactions.data[0] : null}
  200. error={
  201. transactions.status === 'error'
  202. ? t('Unable to load profiles')
  203. : null
  204. }
  205. isLoading={transactions.status === 'loading'}
  206. sort={sort}
  207. sortableColumns={new Set(FIELDS)}
  208. />
  209. <Pagination
  210. pageLinks={
  211. transactions.status === 'success'
  212. ? transactions.data?.[2]?.getResponseHeader('Link') ?? null
  213. : null
  214. }
  215. />
  216. </Fragment>
  217. )}
  218. </Layout.Main>
  219. </Layout.Body>
  220. </Layout.Page>
  221. </PageFiltersContainer>
  222. </SentryDocumentTitle>
  223. );
  224. }
  225. const FIELDS = [
  226. 'transaction',
  227. 'project.id',
  228. 'last_seen()',
  229. 'p75()',
  230. 'p95()',
  231. 'p99()',
  232. 'count()',
  233. ] as const;
  234. type FieldType = (typeof FIELDS)[number];
  235. const ActionBar = styled('div')`
  236. display: grid;
  237. gap: ${space(2)};
  238. grid-template-columns: min-content auto;
  239. margin-bottom: ${space(2)};
  240. `;
  241. // TODO: another simple primitive that can easily be <Grid columns={2} />
  242. const PanelsGrid = styled('div')`
  243. display: grid;
  244. grid-template-columns: minmax(0, 1fr) 1fr;
  245. gap: ${space(2)};
  246. @media (max-width: ${p => p.theme.breakpoints.small}) {
  247. grid-template-columns: minmax(0, 1fr);
  248. }
  249. `;
  250. export default ProfilingContent;