content.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 query={props.query} hideCount compact />
  64. <AggregateFlamegraphPanel transaction={props.transaction} />
  65. <SuspectFunctionsTable
  66. project={props.project}
  67. transaction={props.transaction}
  68. analyticsPageSource="profiling_transaction"
  69. />
  70. <TableHeader>
  71. <CompactSelect
  72. triggerProps={{prefix: t('Filter'), size: 'xs'}}
  73. value={sort.order === 'asc' ? sort.key : `-${sort.key}`}
  74. options={FILTER_OPTIONS}
  75. onChange={opt => handleFilterChange(opt.value)}
  76. />
  77. <StyledPagination
  78. pageLinks={
  79. profiles.status === 'success'
  80. ? profiles.getResponseHeader?.('Link') ?? null
  81. : null
  82. }
  83. size="xs"
  84. />
  85. </TableHeader>
  86. <ProfileEventsTable
  87. columns={fields}
  88. data={profiles.status === 'success' ? profiles.data : null}
  89. error={profiles.status === 'error' ? t('Unable to load profiles') : null}
  90. isLoading={profiles.status === 'loading'}
  91. sort={sort}
  92. />
  93. </Layout.Main>
  94. </Fragment>
  95. );
  96. }
  97. const ALL_FIELDS = [
  98. 'profile.id',
  99. 'timestamp',
  100. 'release',
  101. 'device.model',
  102. 'device.classification',
  103. 'device.arch',
  104. 'transaction.duration',
  105. ] as const;
  106. export type ProfilingFieldType = (typeof ALL_FIELDS)[number];
  107. export function getProfilesTableFields(platform: Project['platform']) {
  108. if (mobile.includes(platform as any)) {
  109. return MOBILE_FIELDS;
  110. }
  111. return DEFAULT_FIELDS;
  112. }
  113. const MOBILE_FIELDS: ProfilingFieldType[] = [...ALL_FIELDS];
  114. const DEFAULT_FIELDS: ProfilingFieldType[] = [
  115. 'profile.id',
  116. 'timestamp',
  117. 'release',
  118. 'transaction.duration',
  119. ];
  120. const FILTER_OPTIONS = [
  121. {
  122. label: t('Recent Profiles'),
  123. value: '-timestamp',
  124. },
  125. {
  126. label: t('Slowest Profiles'),
  127. value: '-transaction.duration',
  128. },
  129. {
  130. label: t('Fastest Profiles'),
  131. value: 'transaction.duration',
  132. },
  133. ];
  134. const TableHeader = styled('div')`
  135. display: flex;
  136. justify-content: space-between;
  137. margin-bottom: ${space(1)};
  138. `;
  139. const StyledPagination = styled(Pagination)`
  140. margin: 0 0 0 ${space(1)};
  141. `;
  142. export {ProfileSummaryContent};