appStartup.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import {useTheme} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import Alert from 'sentry/components/alert';
  4. import SearchBar from 'sentry/components/performance/searchBar';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. import type {NewQuery} from 'sentry/types/organization';
  8. import {defined} from 'sentry/utils';
  9. import {trackAnalytics} from 'sentry/utils/analytics';
  10. import EventView from 'sentry/utils/discover/eventView';
  11. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  12. import {decodeScalar} from 'sentry/utils/queryString';
  13. import {escapeFilterValue, MutableSearch} from 'sentry/utils/tokenizeSearch';
  14. import {useLocation} from 'sentry/utils/useLocation';
  15. import useOrganization from 'sentry/utils/useOrganization';
  16. import usePageFilters from 'sentry/utils/usePageFilters';
  17. import useRouter from 'sentry/utils/useRouter';
  18. import {useReleaseSelection} from 'sentry/views/insights/common/queries/useReleases';
  19. import {appendReleaseFilters} from 'sentry/views/insights/common/utils/releaseComparison';
  20. import {useHasDataTrackAnalytics} from 'sentry/views/insights/common/utils/useHasDataTrackAnalytics';
  21. import {AverageComparisonChart} from 'sentry/views/insights/mobile/appStarts/components/charts/averageComparisonChart';
  22. import {CountChart} from 'sentry/views/insights/mobile/appStarts/components/charts/countChart';
  23. import {COLD_START_TYPE} from 'sentry/views/insights/mobile/appStarts/components/startTypeSelector';
  24. import {AppStartScreens} from 'sentry/views/insights/mobile/appStarts/components/tables/screensTable';
  25. import useCrossPlatformProject from 'sentry/views/insights/mobile/common/queries/useCrossPlatformProject';
  26. import useTruncatedReleaseNames from 'sentry/views/insights/mobile/common/queries/useTruncatedRelease';
  27. import {TOP_SCREENS} from 'sentry/views/insights/mobile/constants';
  28. import {ScreensBarChart} from 'sentry/views/insights/mobile/screenload/components/charts/screenBarChart';
  29. import {getFreeTextFromQuery} from 'sentry/views/insights/mobile/screenload/components/screensView';
  30. import {useTableQuery} from 'sentry/views/insights/mobile/screenload/components/tables/screensTable';
  31. import {YAxis, YAXIS_COLUMNS} from 'sentry/views/insights/mobile/screenload/constants';
  32. import {transformReleaseEvents} from 'sentry/views/insights/mobile/screenload/utils';
  33. import {ModuleName, SpanMetricsField} from 'sentry/views/insights/types';
  34. import {prepareQueryForLandingPage} from 'sentry/views/performance/data';
  35. import {getTransactionSearchQuery} from 'sentry/views/performance/utils';
  36. const Y_AXES = [YAxis.COLD_START, YAxis.WARM_START];
  37. const Y_AXIS_COLS = [YAXIS_COLUMNS[YAxis.COLD_START], YAXIS_COLUMNS[YAxis.WARM_START]];
  38. type Props = {
  39. additionalFilters?: string[];
  40. chartHeight?: number;
  41. };
  42. function AppStartup({additionalFilters, chartHeight}: Props) {
  43. const theme = useTheme();
  44. const pageFilter = usePageFilters();
  45. const {selection} = pageFilter;
  46. const location = useLocation();
  47. const organization = useOrganization();
  48. const {query: locationQuery} = location;
  49. const {
  50. primaryRelease,
  51. secondaryRelease,
  52. isLoading: isReleasesLoading,
  53. } = useReleaseSelection();
  54. const {truncatedPrimaryRelease, truncatedSecondaryRelease} = useTruncatedReleaseNames();
  55. const {isProjectCrossPlatform, selectedPlatform} = useCrossPlatformProject();
  56. const router = useRouter();
  57. const appStartType =
  58. decodeScalar(location.query[SpanMetricsField.APP_START_TYPE]) ?? COLD_START_TYPE;
  59. const query = new MutableSearch([
  60. 'event.type:transaction',
  61. 'transaction.op:ui.load',
  62. `count_starts(measurements.app_start_${appStartType}):>0`,
  63. ...(additionalFilters ?? []),
  64. ]);
  65. if (isProjectCrossPlatform) {
  66. query.addFilterValue('os.name', selectedPlatform);
  67. }
  68. const searchQuery = decodeScalar(locationQuery.query, '');
  69. if (searchQuery) {
  70. query.addStringFilter(prepareQueryForLandingPage(searchQuery, false));
  71. }
  72. const queryString = appendReleaseFilters(query, primaryRelease, secondaryRelease);
  73. const sortCountField = `count_starts_measurements_app_start_${appStartType}`;
  74. const orderby = decodeScalar(locationQuery.sort, `-${sortCountField}`);
  75. const newQuery: NewQuery = {
  76. name: '',
  77. fields: [
  78. 'transaction',
  79. SpanMetricsField.PROJECT_ID,
  80. `avg_if(measurements.app_start_${appStartType},release,${primaryRelease})`,
  81. `avg_if(measurements.app_start_${appStartType},release,${secondaryRelease})`,
  82. `avg_compare(measurements.app_start_${appStartType},release,${primaryRelease},${secondaryRelease})`,
  83. 'count_starts(measurements.app_start_cold)',
  84. 'count_starts(measurements.app_start_warm)',
  85. ],
  86. query: queryString,
  87. dataset: DiscoverDatasets.METRICS,
  88. version: 2,
  89. projects: selection.projects,
  90. };
  91. newQuery.orderby = orderby;
  92. const tableEventView = EventView.fromNewQueryWithLocation(newQuery, location);
  93. const {
  94. data: topTransactionsData,
  95. isLoading: topTransactionsLoading,
  96. pageLinks,
  97. } = useTableQuery({
  98. eventView: tableEventView,
  99. enabled: !isReleasesLoading,
  100. referrer: 'api.starfish.mobile-startup-screen-table',
  101. });
  102. const topTransactions =
  103. topTransactionsData?.data?.slice(0, 5).map(datum => datum.transaction as string) ??
  104. [];
  105. const topEventsQuery = new MutableSearch([
  106. 'event.type:transaction',
  107. 'transaction.op:ui.load',
  108. ...(additionalFilters ?? []),
  109. ]);
  110. if (isProjectCrossPlatform) {
  111. topEventsQuery.addFilterValue('os.name', selectedPlatform);
  112. }
  113. const topEventsQueryString = `${appendReleaseFilters(
  114. topEventsQuery,
  115. primaryRelease,
  116. secondaryRelease
  117. )} ${
  118. topTransactions.length > 0
  119. ? escapeFilterValue(
  120. `transaction:[${topTransactions.map(name => `"${name}"`).join()}]`
  121. )
  122. : ''
  123. }`.trim();
  124. const {data: releaseEvents, isLoading: isReleaseEventsLoading} = useTableQuery({
  125. eventView: EventView.fromNewQueryWithPageFilters(
  126. {
  127. name: '',
  128. fields: ['transaction', 'release', ...Y_AXIS_COLS],
  129. orderby: Y_AXIS_COLS[0],
  130. yAxis: Y_AXIS_COLS,
  131. query: topEventsQueryString,
  132. dataset: DiscoverDatasets.METRICS,
  133. version: 2,
  134. },
  135. selection
  136. ),
  137. enabled: !topTransactionsLoading,
  138. referrer: 'api.starfish.mobile-startup-bar-chart',
  139. });
  140. useHasDataTrackAnalytics(
  141. new MutableSearch('span.op:[app.start.cold,app.start.warm]'),
  142. 'api.performance.mobile.app-startup-landing',
  143. 'insight.page_loads.app_start'
  144. );
  145. if (!defined(primaryRelease) && !isReleasesLoading) {
  146. return (
  147. <Alert type="warning" showIcon>
  148. {t(
  149. 'No screens found on recent releases. Please try a single iOS or Android project, a single environment or a smaller date range.'
  150. )}
  151. </Alert>
  152. );
  153. }
  154. const derivedQuery = getTransactionSearchQuery(location, tableEventView.query);
  155. const tableSearchFilters = new MutableSearch([
  156. 'event.type:transaction',
  157. 'transaction.op:ui.load',
  158. ]);
  159. const transformedReleaseEvents = transformReleaseEvents({
  160. yAxes: Y_AXES,
  161. primaryRelease,
  162. secondaryRelease,
  163. colorPalette: theme.charts.getColorPalette(TOP_SCREENS - 2),
  164. releaseEvents,
  165. topTransactions,
  166. });
  167. const countTopScreens = Math.min(TOP_SCREENS, topTransactions.length);
  168. const [singularTopScreenTitle, pluralTopScreenTitle] =
  169. appStartType === COLD_START_TYPE
  170. ? [t('Top Screen Cold Start'), t('Top %s Screen Cold Starts', countTopScreens)]
  171. : [t('Top Screen Warm Start'), t('Top %s Screen Warm Starts', countTopScreens)];
  172. const yAxis =
  173. YAXIS_COLUMNS[appStartType === COLD_START_TYPE ? YAxis.COLD_START : YAxis.WARM_START];
  174. return (
  175. <div data-test-id="starfish-mobile-app-startup-view">
  176. <ChartContainer>
  177. <AverageComparisonChart chartHeight={chartHeight} />
  178. <ScreensBarChart
  179. chartOptions={[
  180. {
  181. title: countTopScreens > 1 ? pluralTopScreenTitle : singularTopScreenTitle,
  182. yAxis,
  183. xAxisLabel: topTransactions,
  184. series: Object.values(transformedReleaseEvents[yAxis]),
  185. subtitle: primaryRelease
  186. ? t(
  187. '%s v. %s',
  188. truncatedPrimaryRelease,
  189. secondaryRelease ? truncatedSecondaryRelease : ''
  190. )
  191. : '',
  192. },
  193. ]}
  194. chartHeight={chartHeight}
  195. isLoading={isReleaseEventsLoading || isReleasesLoading}
  196. chartKey={`${appStartType}Start`}
  197. />
  198. <CountChart chartHeight={chartHeight} />
  199. </ChartContainer>
  200. <StyledSearchBar
  201. eventView={tableEventView}
  202. onSearch={search => {
  203. trackAnalytics('insight.general.search', {
  204. organization,
  205. query: search,
  206. source: ModuleName.APP_START,
  207. });
  208. router.push({
  209. pathname: router.location.pathname,
  210. query: {
  211. ...location.query,
  212. cursor: undefined,
  213. query: String(search).trim() || undefined,
  214. },
  215. });
  216. }}
  217. organization={organization}
  218. query={getFreeTextFromQuery(derivedQuery)}
  219. placeholder={t('Search for Screen')}
  220. additionalConditions={
  221. new MutableSearch(
  222. appendReleaseFilters(tableSearchFilters, primaryRelease, secondaryRelease)
  223. )
  224. }
  225. />
  226. <AppStartScreens
  227. eventView={tableEventView}
  228. data={topTransactionsData}
  229. isLoading={topTransactionsLoading}
  230. pageLinks={pageLinks}
  231. />
  232. </div>
  233. );
  234. }
  235. export default AppStartup;
  236. const StyledSearchBar = styled(SearchBar)`
  237. margin-bottom: ${space(1)};
  238. `;
  239. const ChartContainer = styled('div')`
  240. display: grid;
  241. grid-template-columns: 33% 33% 33%;
  242. gap: ${space(1)};
  243. `;