content.tsx 4.2 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 {ProfileEventsTable} from 'sentry/components/profiling/profileEventsTable';
  9. import {SuspectFunctionsTable} from 'sentry/components/profiling/suspectFunctions/suspectFunctionsTable';
  10. import {mobile} from 'sentry/data/platformCategories';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import {PageFilters, Project} from 'sentry/types';
  14. import {
  15. formatSort,
  16. useProfileEvents,
  17. } from 'sentry/utils/profiling/hooks/useProfileEvents';
  18. import {decodeScalar} from 'sentry/utils/queryString';
  19. import {ProfileCharts} from 'sentry/views/profiling/landing/profileCharts';
  20. interface ProfileSummaryContentProps {
  21. location: Location;
  22. project: Project;
  23. query: string;
  24. selection: PageFilters;
  25. transaction: string;
  26. }
  27. function ProfileSummaryContent(props: ProfileSummaryContentProps) {
  28. const fields = useMemo(
  29. () => getProfilesTableFields(props.project.platform),
  30. [props.project]
  31. );
  32. const profilesCursor = useMemo(
  33. () => decodeScalar(props.location.query.cursor),
  34. [props.location.query.cursor]
  35. );
  36. const sort = formatSort<ProfilingFieldType>(
  37. decodeScalar(props.location.query.sort),
  38. fields,
  39. {
  40. key: 'timestamp',
  41. order: 'desc',
  42. }
  43. );
  44. const profiles = useProfileEvents<ProfilingFieldType>({
  45. cursor: profilesCursor,
  46. fields,
  47. query: props.query,
  48. sort,
  49. limit: 5,
  50. referrer: 'api.profiling.profile-summary-table',
  51. });
  52. const handleFilterChange = useCallback(
  53. value => {
  54. browserHistory.push({
  55. ...props.location,
  56. query: {...props.location.query, cursor: undefined, sort: value},
  57. });
  58. },
  59. [props.location]
  60. );
  61. return (
  62. <Fragment>
  63. <Layout.Main fullWidth>
  64. <ProfileCharts query={props.query} hideCount />
  65. <TableHeader>
  66. <CompactSelect
  67. triggerProps={{prefix: t('Filter'), size: 'xs'}}
  68. value={sort.order === 'asc' ? sort.key : `-${sort.key}`}
  69. options={FILTER_OPTIONS}
  70. onChange={opt => handleFilterChange(opt.value)}
  71. />
  72. <StyledPagination
  73. pageLinks={
  74. profiles.status === 'success'
  75. ? profiles.data?.[2]?.getResponseHeader('Link') ?? null
  76. : null
  77. }
  78. size="xs"
  79. />
  80. </TableHeader>
  81. <ProfileEventsTable
  82. columns={fields}
  83. data={profiles.status === 'success' ? profiles.data[0] : null}
  84. error={profiles.status === 'error' ? t('Unable to load profiles') : null}
  85. isLoading={profiles.status === 'loading'}
  86. sort={sort}
  87. />
  88. <SuspectFunctionsTable
  89. project={props.project}
  90. transaction={props.transaction}
  91. analyticsPageSource="profiling_transaction"
  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};