content.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {Fragment, useCallback, useMemo} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import {CompactSelect} from 'sentry/components/compactSelect';
  6. import * as Layout from 'sentry/components/layouts/thirds';
  7. import Pagination from 'sentry/components/pagination';
  8. import {AggregateFlamegraphPanel} from 'sentry/components/profiling/aggregateFlamegraphPanel';
  9. import {ProfileEventsTable} from 'sentry/components/profiling/profileEventsTable';
  10. import {SuspectFunctionsTable} from 'sentry/components/profiling/suspectFunctions/suspectFunctionsTable';
  11. import {mobile} from 'sentry/data/platformCategories';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import {PageFilters, Project} from 'sentry/types';
  15. import {useProfileEvents} from 'sentry/utils/profiling/hooks/useProfileEvents';
  16. import {formatSort} from 'sentry/utils/profiling/hooks/utils';
  17. import {decodeScalar} from 'sentry/utils/queryString';
  18. import {ProfileCharts} from 'sentry/views/profiling/landing/profileCharts';
  19. interface ProfileSummaryContentProps {
  20. location: Location;
  21. project: Project;
  22. query: string;
  23. selection: PageFilters;
  24. transaction: string;
  25. }
  26. function ProfileSummaryContent(props: ProfileSummaryContentProps) {
  27. const fields = useMemo(
  28. () => getProfilesTableFields(props.project.platform),
  29. [props.project]
  30. );
  31. const profilesCursor = useMemo(
  32. () => decodeScalar(props.location.query.cursor),
  33. [props.location.query.cursor]
  34. );
  35. const sort = formatSort<ProfilingFieldType>(
  36. decodeScalar(props.location.query.sort),
  37. fields,
  38. {
  39. key: 'timestamp',
  40. order: 'desc',
  41. }
  42. );
  43. const profiles = useProfileEvents<ProfilingFieldType>({
  44. cursor: profilesCursor,
  45. fields,
  46. query: props.query,
  47. sort,
  48. limit: 5,
  49. referrer: 'api.profiling.profile-summary-table',
  50. });
  51. const handleFilterChange = useCallback(
  52. value => {
  53. browserHistory.push({
  54. ...props.location,
  55. query: {...props.location.query, cursor: undefined, sort: value},
  56. });
  57. },
  58. [props.location]
  59. );
  60. return (
  61. <Fragment>
  62. <Layout.Main fullWidth>
  63. <ProfileCharts
  64. referrer="api.profiling.profile-summary-chart"
  65. query={props.query}
  66. hideCount
  67. compact
  68. />
  69. <AggregateFlamegraphPanel transaction={props.transaction} />
  70. <SuspectFunctionsTable
  71. project={props.project}
  72. transaction={props.transaction}
  73. analyticsPageSource="profiling_transaction"
  74. />
  75. <TableHeader>
  76. <CompactSelect
  77. triggerProps={{prefix: t('Filter'), size: 'xs'}}
  78. value={sort.order === 'asc' ? sort.key : `-${sort.key}`}
  79. options={FILTER_OPTIONS}
  80. onChange={opt => handleFilterChange(opt.value)}
  81. />
  82. <StyledPagination
  83. pageLinks={
  84. profiles.status === 'success'
  85. ? profiles.getResponseHeader?.('Link') ?? null
  86. : null
  87. }
  88. size="xs"
  89. />
  90. </TableHeader>
  91. <ProfileEventsTable
  92. columns={fields}
  93. data={profiles.status === 'success' ? profiles.data : null}
  94. error={profiles.status === 'error' ? t('Unable to load profiles') : null}
  95. isLoading={profiles.status === 'loading'}
  96. sort={sort}
  97. />
  98. </Layout.Main>
  99. </Fragment>
  100. );
  101. }
  102. const ALL_FIELDS = [
  103. 'profile.id',
  104. 'timestamp',
  105. 'release',
  106. 'device.model',
  107. 'device.classification',
  108. 'device.arch',
  109. 'transaction.duration',
  110. ] as const;
  111. export type ProfilingFieldType = (typeof ALL_FIELDS)[number];
  112. export function getProfilesTableFields(platform: Project['platform']) {
  113. if (mobile.includes(platform as any)) {
  114. return MOBILE_FIELDS;
  115. }
  116. return DEFAULT_FIELDS;
  117. }
  118. const MOBILE_FIELDS: ProfilingFieldType[] = [...ALL_FIELDS];
  119. const DEFAULT_FIELDS: ProfilingFieldType[] = [
  120. 'profile.id',
  121. 'timestamp',
  122. 'release',
  123. 'transaction.duration',
  124. ];
  125. const FILTER_OPTIONS = [
  126. {
  127. label: t('Recent Profiles'),
  128. value: '-timestamp',
  129. },
  130. {
  131. label: t('Slowest Profiles'),
  132. value: '-transaction.duration',
  133. },
  134. {
  135. label: t('Fastest Profiles'),
  136. value: 'transaction.duration',
  137. },
  138. ];
  139. const TableHeader = styled('div')`
  140. display: flex;
  141. justify-content: space-between;
  142. margin-bottom: ${space(1)};
  143. `;
  144. const StyledPagination = styled(Pagination)`
  145. margin: 0 0 0 ${space(1)};
  146. `;
  147. export {ProfileSummaryContent};