appStartup.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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(ModuleName.APP_START, 'insight.page_loads.app_start');
  141. if (!defined(primaryRelease) && !isReleasesLoading) {
  142. return (
  143. <Alert type="warning" showIcon>
  144. {t(
  145. 'No screens found on recent releases. Please try a single iOS or Android project, a single environment or a smaller date range.'
  146. )}
  147. </Alert>
  148. );
  149. }
  150. const derivedQuery = getTransactionSearchQuery(location, tableEventView.query);
  151. const tableSearchFilters = new MutableSearch([
  152. 'event.type:transaction',
  153. 'transaction.op:ui.load',
  154. ]);
  155. const transformedReleaseEvents = transformReleaseEvents({
  156. yAxes: Y_AXES,
  157. primaryRelease,
  158. secondaryRelease,
  159. colorPalette: theme.charts.getColorPalette(TOP_SCREENS - 2),
  160. releaseEvents,
  161. topTransactions,
  162. });
  163. const countTopScreens = Math.min(TOP_SCREENS, topTransactions.length);
  164. const [singularTopScreenTitle, pluralTopScreenTitle] =
  165. appStartType === COLD_START_TYPE
  166. ? [t('Top Screen Cold Start'), t('Top %s Screen Cold Starts', countTopScreens)]
  167. : [t('Top Screen Warm Start'), t('Top %s Screen Warm Starts', countTopScreens)];
  168. const yAxis =
  169. YAXIS_COLUMNS[appStartType === COLD_START_TYPE ? YAxis.COLD_START : YAxis.WARM_START];
  170. return (
  171. <div data-test-id="starfish-mobile-app-startup-view">
  172. <ChartContainer>
  173. <AverageComparisonChart chartHeight={chartHeight} />
  174. <ScreensBarChart
  175. chartOptions={[
  176. {
  177. title: countTopScreens > 1 ? pluralTopScreenTitle : singularTopScreenTitle,
  178. yAxis,
  179. xAxisLabel: topTransactions,
  180. series: Object.values(transformedReleaseEvents[yAxis]),
  181. subtitle: primaryRelease
  182. ? t(
  183. '%s v. %s',
  184. truncatedPrimaryRelease,
  185. secondaryRelease ? truncatedSecondaryRelease : ''
  186. )
  187. : '',
  188. },
  189. ]}
  190. chartHeight={chartHeight}
  191. isLoading={isReleaseEventsLoading || isReleasesLoading}
  192. chartKey={`${appStartType}Start`}
  193. />
  194. <CountChart chartHeight={chartHeight} />
  195. </ChartContainer>
  196. <StyledSearchBar
  197. eventView={tableEventView}
  198. onSearch={search => {
  199. trackAnalytics('insight.general.search', {
  200. organization,
  201. query: search,
  202. source: ModuleName.APP_START,
  203. });
  204. router.push({
  205. pathname: router.location.pathname,
  206. query: {
  207. ...location.query,
  208. cursor: undefined,
  209. query: String(search).trim() || undefined,
  210. },
  211. });
  212. }}
  213. organization={organization}
  214. query={getFreeTextFromQuery(derivedQuery)}
  215. placeholder={t('Search for Screen')}
  216. additionalConditions={
  217. new MutableSearch(
  218. appendReleaseFilters(tableSearchFilters, primaryRelease, secondaryRelease)
  219. )
  220. }
  221. />
  222. <AppStartScreens
  223. eventView={tableEventView}
  224. data={topTransactionsData}
  225. isLoading={topTransactionsLoading}
  226. pageLinks={pageLinks}
  227. />
  228. </div>
  229. );
  230. }
  231. export default AppStartup;
  232. const StyledSearchBar = styled(SearchBar)`
  233. margin-bottom: ${space(1)};
  234. `;
  235. const ChartContainer = styled('div')`
  236. display: grid;
  237. grid-template-columns: 33% 33% 33%;
  238. gap: ${space(1)};
  239. `;