screensTable.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import {Fragment} from 'react';
  2. import * as qs from 'query-string';
  3. import type {GridColumnHeader} from 'sentry/components/gridEditable';
  4. import GridEditable from 'sentry/components/gridEditable';
  5. import SortLink from 'sentry/components/gridEditable/sortLink';
  6. import ExternalLink from 'sentry/components/links/externalLink';
  7. import Link from 'sentry/components/links/link';
  8. import type {CursorHandler} from 'sentry/components/pagination';
  9. import Pagination from 'sentry/components/pagination';
  10. import {Tooltip} from 'sentry/components/tooltip';
  11. import {t, tct} from 'sentry/locale';
  12. import type {Project} from 'sentry/types/project';
  13. import type {TableData, TableDataRow} from 'sentry/utils/discover/discoverQuery';
  14. import {useDiscoverQuery} from 'sentry/utils/discover/discoverQuery';
  15. import type {MetaType} from 'sentry/utils/discover/eventView';
  16. import type EventView from 'sentry/utils/discover/eventView';
  17. import {isFieldSortable} from 'sentry/utils/discover/eventView';
  18. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  19. import {fieldAlignment} from 'sentry/utils/discover/fields';
  20. import {useLocation} from 'sentry/utils/useLocation';
  21. import useOrganization from 'sentry/utils/useOrganization';
  22. import usePageFilters from 'sentry/utils/usePageFilters';
  23. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  24. import TopResultsIndicator from 'sentry/views/discover/table/topResultsIndicator';
  25. import type {TableColumn} from 'sentry/views/discover/table/types';
  26. import {TOP_SCREENS} from 'sentry/views/performance/mobile/constants';
  27. import {
  28. PRIMARY_RELEASE_ALIAS,
  29. SECONDARY_RELEASE_ALIAS,
  30. } from 'sentry/views/starfish/components/releaseSelector';
  31. import {useReleaseSelection} from 'sentry/views/starfish/queries/useReleases';
  32. import {SpanMetricsField} from 'sentry/views/starfish/types';
  33. type Props = {
  34. data: TableData | undefined;
  35. eventView: EventView;
  36. isLoading: boolean;
  37. pageLinks: string | undefined;
  38. onCursor?: CursorHandler;
  39. project?: Project | null;
  40. };
  41. export function ScreensTable({
  42. data,
  43. eventView,
  44. isLoading,
  45. pageLinks,
  46. onCursor,
  47. project,
  48. }: Props) {
  49. const location = useLocation();
  50. const organization = useOrganization();
  51. const {primaryRelease, secondaryRelease} = useReleaseSelection();
  52. const eventViewColumns = eventView.getColumns();
  53. const columnNameMap = {
  54. transaction: t('Screen'),
  55. [`avg_if(measurements.time_to_initial_display,release,${primaryRelease})`]: t(
  56. 'TTID (%s)',
  57. PRIMARY_RELEASE_ALIAS
  58. ),
  59. [`avg_if(measurements.time_to_initial_display,release,${secondaryRelease})`]: t(
  60. 'TTID (%s)',
  61. SECONDARY_RELEASE_ALIAS
  62. ),
  63. [`avg_if(measurements.time_to_full_display,release,${primaryRelease})`]: t(
  64. 'TTFD (%s)',
  65. PRIMARY_RELEASE_ALIAS
  66. ),
  67. [`avg_if(measurements.time_to_full_display,release,${secondaryRelease})`]: t(
  68. 'TTFD (%s)',
  69. SECONDARY_RELEASE_ALIAS
  70. ),
  71. 'count()': t('Total Count'),
  72. };
  73. function renderBodyCell(column, row): React.ReactNode {
  74. if (!data?.meta || !data?.meta.fields) {
  75. return row[column.key];
  76. }
  77. const index = data.data.indexOf(row);
  78. const field = String(column.key);
  79. if (field === 'transaction') {
  80. return (
  81. <Fragment>
  82. <TopResultsIndicator count={TOP_SCREENS} index={index} />
  83. <Link
  84. to={normalizeUrl(
  85. `/organizations/${
  86. organization.slug
  87. }/performance/mobile/screens/spans/?${qs.stringify({
  88. ...location.query,
  89. project: row['project.id'],
  90. transaction: row.transaction,
  91. primaryRelease,
  92. secondaryRelease,
  93. })}`
  94. )}
  95. style={{display: `block`, width: `100%`}}
  96. >
  97. {row.transaction}
  98. </Link>
  99. </Fragment>
  100. );
  101. }
  102. const renderer = getFieldRenderer(column.key, data?.meta.fields, false);
  103. const rendered = renderer(row, {
  104. location,
  105. organization,
  106. unit: data?.meta.units?.[column.key],
  107. });
  108. if (
  109. column.key.includes('time_to_full_display') &&
  110. row[column.key] === 0 &&
  111. project?.platform &&
  112. ['android', 'apple-ios'].includes(project.platform)
  113. ) {
  114. const docsUrl =
  115. project?.platform === 'android'
  116. ? 'https://docs.sentry.io/platforms/android/performance/instrumentation/automatic-instrumentation/#time-to-full-display'
  117. : 'https://docs.sentry.io/platforms/apple/guides/ios/performance/instrumentation/automatic-instrumentation/#time-to-full-display';
  118. return (
  119. <div style={{textAlign: 'right'}}>
  120. <Tooltip
  121. title={tct(
  122. 'Measuring TTFD requires manual instrumentation in your application. To learn how to collect TTFD, see the documentation [link].',
  123. {
  124. link: <ExternalLink href={docsUrl}>{t('here')}</ExternalLink>,
  125. }
  126. )}
  127. showUnderline
  128. isHoverable
  129. >
  130. {rendered}
  131. </Tooltip>
  132. </div>
  133. );
  134. }
  135. return rendered;
  136. }
  137. function renderHeadCell(
  138. column: GridColumnHeader,
  139. tableMeta?: MetaType
  140. ): React.ReactNode {
  141. const fieldType = tableMeta?.fields?.[column.key];
  142. const alignment = fieldAlignment(column.key as string, fieldType);
  143. const field = {
  144. field: column.key as string,
  145. width: column.width,
  146. };
  147. function generateSortLink() {
  148. if (!tableMeta) {
  149. return undefined;
  150. }
  151. const nextEventView = eventView.sortOnField(field, tableMeta);
  152. const queryStringObject = nextEventView.generateQueryStringObject();
  153. return {
  154. ...location,
  155. query: {...location.query, sort: queryStringObject.sort},
  156. };
  157. }
  158. const currentSort = eventView.sortForField(field, tableMeta);
  159. const currentSortKind = currentSort ? currentSort.kind : undefined;
  160. const canSort = isFieldSortable(field, tableMeta);
  161. const sortLink = (
  162. <SortLink
  163. align={alignment}
  164. title={column.name}
  165. direction={currentSortKind}
  166. canSort={canSort}
  167. generateSortLink={generateSortLink}
  168. />
  169. );
  170. return sortLink;
  171. }
  172. return (
  173. <Fragment>
  174. <GridEditable
  175. isLoading={isLoading}
  176. data={data?.data as TableDataRow[]}
  177. columnOrder={eventViewColumns
  178. .filter(
  179. (col: TableColumn<React.ReactText>) =>
  180. col.name !== SpanMetricsField.PROJECT_ID &&
  181. !col.name.startsWith('avg_compare')
  182. )
  183. .map((col: TableColumn<React.ReactText>) => {
  184. return {...col, name: columnNameMap[col.key] ?? col.name};
  185. })}
  186. columnSortBy={[
  187. {
  188. key: 'count()',
  189. order: 'desc',
  190. },
  191. ]}
  192. location={location}
  193. grid={{
  194. renderHeadCell: column => renderHeadCell(column, data?.meta),
  195. renderBodyCell,
  196. }}
  197. />
  198. <Pagination pageLinks={pageLinks} onCursor={onCursor} />
  199. </Fragment>
  200. );
  201. }
  202. export function useTableQuery({
  203. eventView,
  204. enabled,
  205. referrer,
  206. initialData,
  207. limit,
  208. staleTime,
  209. cursor,
  210. }: {
  211. eventView: EventView;
  212. cursor?: string;
  213. enabled?: boolean;
  214. excludeOther?: boolean;
  215. initialData?: TableData;
  216. limit?: number;
  217. referrer?: string;
  218. staleTime?: number;
  219. }) {
  220. const location = useLocation();
  221. const organization = useOrganization();
  222. const {isReady: pageFiltersReady} = usePageFilters();
  223. const result = useDiscoverQuery({
  224. eventView,
  225. location,
  226. orgSlug: organization.slug,
  227. limit: limit ?? 25,
  228. referrer,
  229. cursor,
  230. options: {
  231. refetchOnWindowFocus: false,
  232. enabled: enabled && pageFiltersReady,
  233. staleTime,
  234. },
  235. });
  236. return {
  237. ...result,
  238. data: result.isLoading ? initialData : result.data,
  239. pageLinks: result.pageLinks,
  240. };
  241. }