content.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 {ProfilingOnboardingPanel} from './profilingOnboardingPanel';
  39. interface ProfilingContentProps {
  40. location: Location;
  41. }
  42. function ProfilingContent({location}: ProfilingContentProps) {
  43. const organization = useOrganization();
  44. const {selection} = usePageFilters();
  45. const cursor = decodeScalar(location.query.cursor);
  46. const query = decodeScalar(location.query.query, '');
  47. const sort = formatSort<FieldType>(decodeScalar(location.query.sort), FIELDS, {
  48. key: 'p99()',
  49. order: 'desc',
  50. });
  51. const profileFilters = useProfileFilters({query: '', selection});
  52. const {projects} = useProjects();
  53. const transactions = useProfileEvents<FieldType>({
  54. cursor,
  55. fields: FIELDS,
  56. query,
  57. sort,
  58. referrer: 'api.profiling.landing-table',
  59. });
  60. const transactionsError =
  61. transactions.status === 'error' ? formatError(transactions.error) : null;
  62. useEffect(() => {
  63. trackAdvancedAnalyticsEvent('profiling_views.landing', {
  64. organization,
  65. });
  66. }, [organization]);
  67. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  68. (searchQuery: string) => {
  69. browserHistory.push({
  70. ...location,
  71. query: {
  72. ...location.query,
  73. cursor: undefined,
  74. query: searchQuery || undefined,
  75. },
  76. });
  77. },
  78. [location]
  79. );
  80. // Open the modal on demand
  81. const onSetupProfilingClick = useCallback(() => {
  82. trackAdvancedAnalyticsEvent('profiling_views.onboarding', {
  83. organization,
  84. });
  85. SidebarPanelStore.activatePanel(SidebarPanelKey.ProfilingOnboarding);
  86. }, [organization]);
  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. <Layout.Page>
  109. <NoProjectMessage organization={organization}>
  110. <Layout.Header>
  111. <Layout.HeaderContent>
  112. <Layout.Title>
  113. {t('Profiling')}
  114. <PageHeadingQuestionTooltip
  115. docsUrl="https://docs.sentry.io/product/profiling/"
  116. title={t(
  117. '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.'
  118. )}
  119. />
  120. </Layout.Title>
  121. </Layout.HeaderContent>
  122. <Layout.HeaderActions>
  123. <ButtonBar gap={1}>
  124. <Button size="sm" onClick={onSetupProfilingClick}>
  125. {t('Set Up Profiling')}
  126. </Button>
  127. <Button
  128. size="sm"
  129. priority="primary"
  130. href="https://discord.gg/zrMjKA4Vnz"
  131. external
  132. onClick={() => {
  133. trackAdvancedAnalyticsEvent(
  134. 'profiling_views.visit_discord_channel',
  135. {
  136. organization,
  137. }
  138. );
  139. }}
  140. >
  141. {t('Join Discord')}
  142. </Button>
  143. </ButtonBar>
  144. </Layout.HeaderActions>
  145. </Layout.Header>
  146. <Layout.Body>
  147. <Layout.Main fullWidth>
  148. {transactionsError && (
  149. <Alert type="error" showIcon>
  150. {transactionsError}
  151. </Alert>
  152. )}
  153. <ActionBar>
  154. <PageFilterBar condensed>
  155. <ProjectPageFilter />
  156. <EnvironmentPageFilter />
  157. <DatePageFilter alignDropdown="left" />
  158. </PageFilterBar>
  159. <SmartSearchBar
  160. organization={organization}
  161. hasRecentSearches
  162. searchSource="profile_landing"
  163. supportedTags={profileFilters}
  164. query={query}
  165. onSearch={handleSearch}
  166. maxQueryLength={MAX_QUERY_LENGTH}
  167. />
  168. </ActionBar>
  169. {shouldShowProfilingOnboardingPanel ? (
  170. <ProfilingOnboardingPanel>
  171. <Button href="https://docs.sentry.io/product/profiling/" external>
  172. {t('Read Docs')}
  173. </Button>
  174. <Button onClick={onSetupProfilingClick} priority="primary">
  175. {t('Set Up Profiling')}
  176. </Button>
  177. </ProfilingOnboardingPanel>
  178. ) : (
  179. <Fragment>
  180. <ProfileCharts query={query} selection={selection} />
  181. <ProfileEventsTable
  182. columns={FIELDS.slice()}
  183. data={
  184. transactions.status === 'success' ? transactions.data[0] : null
  185. }
  186. error={
  187. transactions.status === 'error'
  188. ? t('Unable to load profiles')
  189. : null
  190. }
  191. isLoading={transactions.status === 'loading'}
  192. sort={sort}
  193. sortableColumns={new Set(FIELDS)}
  194. />
  195. <Pagination
  196. pageLinks={
  197. transactions.status === 'success'
  198. ? transactions.data?.[2]?.getResponseHeader('Link') ?? null
  199. : null
  200. }
  201. />
  202. </Fragment>
  203. )}
  204. </Layout.Main>
  205. </Layout.Body>
  206. </NoProjectMessage>
  207. </Layout.Page>
  208. </PageFiltersContainer>
  209. </SentryDocumentTitle>
  210. );
  211. }
  212. const FIELDS = [
  213. 'transaction',
  214. 'project.id',
  215. 'last_seen()',
  216. 'p75()',
  217. 'p95()',
  218. 'p99()',
  219. 'count()',
  220. ] as const;
  221. type FieldType = typeof FIELDS[number];
  222. const ActionBar = styled('div')`
  223. display: grid;
  224. gap: ${space(2)};
  225. grid-template-columns: min-content auto;
  226. margin-bottom: ${space(2)};
  227. `;
  228. export default ProfilingContent;