screensTable.tsx 7.7 KB

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