screensView.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. isPending: 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, isPending: 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(ModuleName.SCREEN_LOAD, 'insight.page_loads.screen_load');
  169. if (isReleasesLoading) {
  170. return (
  171. <LoadingContainer>
  172. <LoadingIndicator />
  173. </LoadingContainer>
  174. );
  175. }
  176. if (!defined(primaryRelease) && !isReleasesLoading) {
  177. return (
  178. <Alert type="warning" showIcon>
  179. {t(
  180. 'No screens found on recent releases. Please try a single iOS or Android project, a single environment or a smaller date range.'
  181. )}
  182. </Alert>
  183. );
  184. }
  185. const transformedReleaseEvents = transformReleaseEvents({
  186. yAxes,
  187. primaryRelease,
  188. secondaryRelease,
  189. colorPalette: theme.charts.getColorPalette(TOP_SCREENS - 2),
  190. releaseEvents,
  191. topTransactions,
  192. });
  193. const derivedQuery = getTransactionSearchQuery(location, tableEventView.query);
  194. const tableSearchFilters = new MutableSearch(['transaction.op:ui.load']);
  195. const handleCursor: CursorHandler = (newCursor, pathname, query_) => {
  196. browserHistory.push({
  197. pathname,
  198. query: {...query_, [MobileCursors.SCREENS_TABLE]: newCursor},
  199. });
  200. };
  201. return (
  202. <div data-test-id="starfish-mobile-view">
  203. <ChartsContainer>
  204. <Fragment>
  205. <ChartsContainerItem key="ttid">
  206. <ScreensBarChart
  207. chartOptions={[
  208. {
  209. title: t('%s by Top Screen', CHART_TITLES[yAxes[0]]),
  210. yAxis: YAXIS_COLUMNS[yAxes[0]],
  211. xAxisLabel: topTransactions,
  212. series: Object.values(
  213. transformedReleaseEvents[YAXIS_COLUMNS[yAxes[0]]]
  214. ),
  215. subtitle: primaryRelease
  216. ? t(
  217. '%s v. %s',
  218. truncatedPrimaryRelease,
  219. secondaryRelease ? truncatedSecondaryRelease : ''
  220. )
  221. : '',
  222. },
  223. ]}
  224. chartHeight={chartHeight ?? 180}
  225. isLoading={isReleaseEventsLoading}
  226. chartKey="screensChart1"
  227. />
  228. </ChartsContainerItem>
  229. {defined(hasTTFD) && !hasTTFD && yAxes[1] === YAxis.TTFD ? (
  230. <ChartsContainerWithHiddenOverflow>
  231. <ChartPanel title={CHART_TITLES[yAxes[1]]}>
  232. <TabbedCodeSnippet tabs={SETUP_CONTENT} />
  233. </ChartPanel>
  234. </ChartsContainerWithHiddenOverflow>
  235. ) : (
  236. <ChartsContainerItem key="ttfd">
  237. <ScreensBarChart
  238. chartOptions={[
  239. {
  240. title: t('%s by Top Screen', CHART_TITLES[yAxes[1]]),
  241. yAxis: YAXIS_COLUMNS[yAxes[1]],
  242. xAxisLabel: topTransactions,
  243. series: Object.values(
  244. transformedReleaseEvents[YAXIS_COLUMNS[yAxes[1]]]
  245. ),
  246. subtitle: primaryRelease
  247. ? t(
  248. '%s v. %s',
  249. truncatedPrimaryRelease,
  250. secondaryRelease ? truncatedSecondaryRelease : ''
  251. )
  252. : '',
  253. },
  254. ]}
  255. chartHeight={chartHeight ?? 180}
  256. isLoading={isReleaseEventsLoading}
  257. chartKey="screensChart1"
  258. />
  259. </ChartsContainerItem>
  260. )}
  261. </Fragment>
  262. </ChartsContainer>
  263. <StyledSearchBar
  264. eventView={tableEventView}
  265. onSearch={search => {
  266. trackAnalytics('insight.general.search', {
  267. organization,
  268. query: search,
  269. source: ModuleName.SCREEN_LOAD,
  270. });
  271. router.push({
  272. pathname: router.location.pathname,
  273. query: {
  274. ...location.query,
  275. cursor: undefined,
  276. query: String(search).trim() || undefined,
  277. },
  278. });
  279. }}
  280. organization={organization}
  281. query={getFreeTextFromQuery(derivedQuery)}
  282. placeholder={t('Search for Screens')}
  283. additionalConditions={
  284. new MutableSearch(
  285. appendReleaseFilters(tableSearchFilters, primaryRelease, secondaryRelease)
  286. )
  287. }
  288. />
  289. <ScreensTable
  290. eventView={tableEventView}
  291. data={topTransactionsData}
  292. isLoading={topTransactionsLoading}
  293. pageLinks={pageLinks}
  294. onCursor={handleCursor}
  295. />
  296. </div>
  297. );
  298. }
  299. export function getFreeTextFromQuery(query: string) {
  300. const conditions = new MutableSearch(query);
  301. const transactionValues = conditions.getFilterValues('transaction');
  302. if (transactionValues.length) {
  303. return transactionValues[0];
  304. }
  305. if (conditions.freeText.length > 0) {
  306. // raw text query will be wrapped in wildcards in generatePerformanceEventView
  307. // so no need to wrap it here
  308. return conditions.freeText.join(' ');
  309. }
  310. return '';
  311. }
  312. const ChartsContainer = styled('div')`
  313. display: flex;
  314. flex-direction: row;
  315. flex-wrap: wrap;
  316. gap: ${space(2)};
  317. `;
  318. const ChartsContainerWithHiddenOverflow = styled('div')`
  319. flex: 1;
  320. overflow: hidden;
  321. `;
  322. const ChartsContainerItem = styled('div')`
  323. flex: 1;
  324. `;
  325. export const Spacer = styled('div')`
  326. margin-top: ${space(3)};
  327. `;
  328. const StyledSearchBar = styled(SearchBar)`
  329. margin-bottom: ${space(1)};
  330. `;