screensView.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import {Fragment, useMemo} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import Alert from 'sentry/components/alert';
  5. import LoadingContainer from 'sentry/components/loading/loadingContainer';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import type {CursorHandler} from 'sentry/components/pagination';
  8. import SearchBar from 'sentry/components/performance/searchBar';
  9. import {t} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import type {NewQuery} from 'sentry/types/organization';
  12. import {defined} from 'sentry/utils';
  13. import {trackAnalytics} from 'sentry/utils/analytics';
  14. import {browserHistory} from 'sentry/utils/browserHistory';
  15. import EventView from 'sentry/utils/discover/eventView';
  16. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  17. import {decodeScalar} from 'sentry/utils/queryString';
  18. import {escapeFilterValue, MutableSearch} from 'sentry/utils/tokenizeSearch';
  19. import {useLocation} from 'sentry/utils/useLocation';
  20. import useOrganization from 'sentry/utils/useOrganization';
  21. import usePageFilters from 'sentry/utils/usePageFilters';
  22. import useRouter from 'sentry/utils/useRouter';
  23. import ChartPanel from 'sentry/views/insights/common/components/chartPanel';
  24. import {useTTFDConfigured} from 'sentry/views/insights/common/queries/useHasTtfdConfigured';
  25. import {useReleaseSelection} from 'sentry/views/insights/common/queries/useReleases';
  26. import {appendReleaseFilters} from 'sentry/views/insights/common/utils/releaseComparison';
  27. import {useHasDataTrackAnalytics} from 'sentry/views/insights/common/utils/useHasDataTrackAnalytics';
  28. import useCrossPlatformProject from 'sentry/views/insights/mobile/common/queries/useCrossPlatformProject';
  29. import useTruncatedReleaseNames from 'sentry/views/insights/mobile/common/queries/useTruncatedRelease';
  30. import {TOP_SCREENS} from 'sentry/views/insights/mobile/constants';
  31. import {ScreensBarChart} from 'sentry/views/insights/mobile/screenload/components/charts/screenBarChart';
  32. import {TabbedCodeSnippet} from 'sentry/views/insights/mobile/screenload/components/tabbedCodeSnippets';
  33. import {
  34. ScreensTable,
  35. useTableQuery,
  36. } from 'sentry/views/insights/mobile/screenload/components/tables/screensTable';
  37. import {
  38. CHART_TITLES,
  39. MobileCursors,
  40. YAxis,
  41. YAXIS_COLUMNS,
  42. } from 'sentry/views/insights/mobile/screenload/constants';
  43. import {SETUP_CONTENT} from 'sentry/views/insights/mobile/screenload/data/setupContent';
  44. import {transformReleaseEvents} from 'sentry/views/insights/mobile/screenload/utils';
  45. import {ModuleName, SpanMetricsField} from 'sentry/views/insights/types';
  46. import {prepareQueryForLandingPage} from 'sentry/views/performance/data';
  47. import {getTransactionSearchQuery} from 'sentry/views/performance/utils';
  48. type Props = {
  49. yAxes: YAxis[];
  50. additionalFilters?: string[];
  51. chartHeight?: number;
  52. };
  53. export function ScreensView({yAxes, additionalFilters, chartHeight}: Props) {
  54. const pageFilter = usePageFilters();
  55. const {selection} = pageFilter;
  56. const location = useLocation();
  57. const theme = useTheme();
  58. const organization = useOrganization();
  59. const {isProjectCrossPlatform, selectedPlatform} = useCrossPlatformProject();
  60. const {query: locationQuery} = location;
  61. const cursor = decodeScalar(location.query?.[MobileCursors.SCREENS_TABLE]);
  62. const yAxisCols = yAxes.map(val => YAXIS_COLUMNS[val]);
  63. const {
  64. primaryRelease,
  65. secondaryRelease,
  66. isLoading: isReleasesLoading,
  67. } = useReleaseSelection();
  68. const {truncatedPrimaryRelease, truncatedSecondaryRelease} = useTruncatedReleaseNames();
  69. const router = useRouter();
  70. const {hasTTFD} = useTTFDConfigured(additionalFilters);
  71. const queryString = useMemo(() => {
  72. const query = new MutableSearch([
  73. 'event.type:transaction',
  74. 'transaction.op:ui.load',
  75. ...(additionalFilters ?? []),
  76. ]);
  77. if (isProjectCrossPlatform) {
  78. query.addFilterValue('os.name', selectedPlatform);
  79. }
  80. const searchQuery = decodeScalar(locationQuery.query, '');
  81. if (searchQuery) {
  82. query.addStringFilter(prepareQueryForLandingPage(searchQuery, false));
  83. }
  84. return appendReleaseFilters(query, primaryRelease, secondaryRelease);
  85. }, [
  86. additionalFilters,
  87. isProjectCrossPlatform,
  88. locationQuery.query,
  89. primaryRelease,
  90. secondaryRelease,
  91. selectedPlatform,
  92. ]);
  93. const orderby = decodeScalar(locationQuery.sort, `-count`);
  94. const newQuery: NewQuery = {
  95. name: '',
  96. fields: [
  97. 'transaction',
  98. SpanMetricsField.PROJECT_ID,
  99. `avg_if(measurements.time_to_initial_display,release,${primaryRelease})`,
  100. `avg_if(measurements.time_to_initial_display,release,${secondaryRelease})`,
  101. `avg_if(measurements.time_to_full_display,release,${primaryRelease})`,
  102. `avg_if(measurements.time_to_full_display,release,${secondaryRelease})`,
  103. 'count()',
  104. ],
  105. query: queryString,
  106. dataset: DiscoverDatasets.METRICS,
  107. version: 2,
  108. projects: selection.projects,
  109. };
  110. newQuery.orderby = orderby;
  111. const tableEventView = EventView.fromNewQueryWithLocation(newQuery, location);
  112. const {
  113. data: topTransactionsData,
  114. isLoading: topTransactionsLoading,
  115. pageLinks,
  116. } = useTableQuery({
  117. eventView: tableEventView,
  118. enabled: !isReleasesLoading,
  119. referrer: 'api.starfish.mobile-screen-table',
  120. cursor,
  121. });
  122. const topTransactions = useMemo(() => {
  123. return (
  124. topTransactionsData?.data?.slice(0, 5).map(datum => datum.transaction as string) ??
  125. []
  126. );
  127. }, [topTransactionsData?.data]);
  128. const topEventsQueryString = useMemo(() => {
  129. const topEventsQuery = new MutableSearch([
  130. 'event.type:transaction',
  131. 'transaction.op:ui.load',
  132. ...(additionalFilters ?? []),
  133. ]);
  134. if (isProjectCrossPlatform) {
  135. topEventsQuery.addFilterValue('os.name', selectedPlatform);
  136. }
  137. return `${appendReleaseFilters(topEventsQuery, primaryRelease, secondaryRelease)} ${
  138. topTransactions.length > 0
  139. ? escapeFilterValue(
  140. `transaction:[${topTransactions.map(name => `"${name}"`).join()}]`
  141. )
  142. : ''
  143. }`.trim();
  144. }, [
  145. additionalFilters,
  146. isProjectCrossPlatform,
  147. primaryRelease,
  148. secondaryRelease,
  149. topTransactions,
  150. selectedPlatform,
  151. ]);
  152. const {data: releaseEvents, isLoading: isReleaseEventsLoading} = useTableQuery({
  153. eventView: EventView.fromNewQueryWithLocation(
  154. {
  155. name: '',
  156. fields: ['transaction', 'release', ...yAxisCols],
  157. orderby: yAxisCols[0],
  158. yAxis: yAxisCols,
  159. query: topEventsQueryString,
  160. dataset: DiscoverDatasets.METRICS,
  161. version: 2,
  162. },
  163. location
  164. ),
  165. enabled: !topTransactionsLoading,
  166. referrer: 'api.starfish.mobile-screen-bar-chart',
  167. });
  168. useHasDataTrackAnalytics(
  169. new MutableSearch('transaction.op:ui.load'),
  170. 'api.performance.mobile.screen-load-landing',
  171. 'insight.page_loads.screen_load'
  172. );
  173. if (isReleasesLoading) {
  174. return (
  175. <LoadingContainer>
  176. <LoadingIndicator />
  177. </LoadingContainer>
  178. );
  179. }
  180. if (!defined(primaryRelease) && !isReleasesLoading) {
  181. return (
  182. <Alert type="warning" showIcon>
  183. {t(
  184. 'No screens found on recent releases. Please try a single iOS or Android project, a single environment or a smaller date range.'
  185. )}
  186. </Alert>
  187. );
  188. }
  189. const transformedReleaseEvents = transformReleaseEvents({
  190. yAxes,
  191. primaryRelease,
  192. secondaryRelease,
  193. colorPalette: theme.charts.getColorPalette(TOP_SCREENS - 2),
  194. releaseEvents,
  195. topTransactions,
  196. });
  197. const derivedQuery = getTransactionSearchQuery(location, tableEventView.query);
  198. const tableSearchFilters = new MutableSearch(['transaction.op:ui.load']);
  199. const handleCursor: CursorHandler = (newCursor, pathname, query_) => {
  200. browserHistory.push({
  201. pathname,
  202. query: {...query_, [MobileCursors.SCREENS_TABLE]: newCursor},
  203. });
  204. };
  205. return (
  206. <div data-test-id="starfish-mobile-view">
  207. <ChartsContainer>
  208. <Fragment>
  209. <ChartsContainerItem key="ttid">
  210. <ScreensBarChart
  211. chartOptions={[
  212. {
  213. title: t('%s by Top Screen', CHART_TITLES[yAxes[0]]),
  214. yAxis: YAXIS_COLUMNS[yAxes[0]],
  215. xAxisLabel: topTransactions,
  216. series: Object.values(
  217. transformedReleaseEvents[YAXIS_COLUMNS[yAxes[0]]]
  218. ),
  219. subtitle: primaryRelease
  220. ? t(
  221. '%s v. %s',
  222. truncatedPrimaryRelease,
  223. secondaryRelease ? truncatedSecondaryRelease : ''
  224. )
  225. : '',
  226. },
  227. ]}
  228. chartHeight={chartHeight ?? 180}
  229. isLoading={isReleaseEventsLoading}
  230. chartKey="screensChart1"
  231. />
  232. </ChartsContainerItem>
  233. {defined(hasTTFD) && !hasTTFD && yAxes[1] === YAxis.TTFD ? (
  234. <ChartsContainerWithHiddenOverflow>
  235. <ChartPanel title={CHART_TITLES[yAxes[1]]}>
  236. <TabbedCodeSnippet tabs={SETUP_CONTENT} />
  237. </ChartPanel>
  238. </ChartsContainerWithHiddenOverflow>
  239. ) : (
  240. <ChartsContainerItem key="ttfd">
  241. <ScreensBarChart
  242. chartOptions={[
  243. {
  244. title: t('%s by Top Screen', CHART_TITLES[yAxes[1]]),
  245. yAxis: YAXIS_COLUMNS[yAxes[1]],
  246. xAxisLabel: topTransactions,
  247. series: Object.values(
  248. transformedReleaseEvents[YAXIS_COLUMNS[yAxes[1]]]
  249. ),
  250. subtitle: primaryRelease
  251. ? t(
  252. '%s v. %s',
  253. truncatedPrimaryRelease,
  254. secondaryRelease ? truncatedSecondaryRelease : ''
  255. )
  256. : '',
  257. },
  258. ]}
  259. chartHeight={chartHeight ?? 180}
  260. isLoading={isReleaseEventsLoading}
  261. chartKey="screensChart1"
  262. />
  263. </ChartsContainerItem>
  264. )}
  265. </Fragment>
  266. </ChartsContainer>
  267. <StyledSearchBar
  268. eventView={tableEventView}
  269. onSearch={search => {
  270. trackAnalytics('insight.general.search', {
  271. organization,
  272. query: search,
  273. source: ModuleName.SCREEN_LOAD,
  274. });
  275. router.push({
  276. pathname: router.location.pathname,
  277. query: {
  278. ...location.query,
  279. cursor: undefined,
  280. query: String(search).trim() || undefined,
  281. },
  282. });
  283. }}
  284. organization={organization}
  285. query={getFreeTextFromQuery(derivedQuery)}
  286. placeholder={t('Search for Screens')}
  287. additionalConditions={
  288. new MutableSearch(
  289. appendReleaseFilters(tableSearchFilters, primaryRelease, secondaryRelease)
  290. )
  291. }
  292. />
  293. <ScreensTable
  294. eventView={tableEventView}
  295. data={topTransactionsData}
  296. isLoading={topTransactionsLoading}
  297. pageLinks={pageLinks}
  298. onCursor={handleCursor}
  299. />
  300. </div>
  301. );
  302. }
  303. export function getFreeTextFromQuery(query: string) {
  304. const conditions = new MutableSearch(query);
  305. const transactionValues = conditions.getFilterValues('transaction');
  306. if (transactionValues.length) {
  307. return transactionValues[0];
  308. }
  309. if (conditions.freeText.length > 0) {
  310. // raw text query will be wrapped in wildcards in generatePerformanceEventView
  311. // so no need to wrap it here
  312. return conditions.freeText.join(' ');
  313. }
  314. return '';
  315. }
  316. const ChartsContainer = styled('div')`
  317. display: flex;
  318. flex-direction: row;
  319. flex-wrap: wrap;
  320. gap: ${space(2)};
  321. `;
  322. const ChartsContainerWithHiddenOverflow = styled('div')`
  323. flex: 1;
  324. overflow: hidden;
  325. `;
  326. const ChartsContainerItem = styled('div')`
  327. flex: 1;
  328. `;
  329. export const Spacer = styled('div')`
  330. margin-top: ${space(3)};
  331. `;
  332. const StyledSearchBar = styled(SearchBar)`
  333. margin-bottom: ${space(1)};
  334. `;