index.tsx 8.7 KB

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