screensTable.tsx 9.2 KB

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