screensTable.tsx 9.5 KB

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