screensTable.tsx 9.7 KB

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