systemApplicationBreakdown.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import styled from '@emotion/styled';
  2. import {getInterval} from 'sentry/components/charts/utils';
  3. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  4. import {OpsDot} from 'sentry/components/events/opsBreakdown';
  5. import LoadingContainer from 'sentry/components/loading/loadingContainer';
  6. import TextOverflow from 'sentry/components/textOverflow';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import type {TableDataRow} from 'sentry/utils/discover/discoverQuery';
  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 {MutableSearch} from 'sentry/utils/tokenizeSearch';
  14. import {useLocation} from 'sentry/utils/useLocation';
  15. import usePageFilters from 'sentry/utils/usePageFilters';
  16. import {formatVersion} from 'sentry/utils/versions/formatVersion';
  17. import MiniChartPanel from 'sentry/views/insights/common/components/miniChartPanel';
  18. import {useReleaseSelection} from 'sentry/views/insights/common/queries/useReleases';
  19. import {formatVersionAndCenterTruncate} from 'sentry/views/insights/common/utils/centerTruncate';
  20. import {STARFISH_CHART_INTERVAL_FIDELITY} from 'sentry/views/insights/common/utils/constants';
  21. import {appendReleaseFilters} from 'sentry/views/insights/common/utils/releaseComparison';
  22. import Breakdown from 'sentry/views/insights/mobile/appStarts/components/breakdown';
  23. import {useTableQuery} from 'sentry/views/insights/mobile/screenload/components/tables/screensTable';
  24. import {prepareQueryForLandingPage} from 'sentry/views/performance/data';
  25. const SYSTEM_COLOR = '#D6567F';
  26. const APPLICATION_COLOR = '#444674';
  27. const ANDROID_APPLICATION_SPAN_OPS = [
  28. 'contentprovider.load',
  29. 'application.load',
  30. 'activity.load',
  31. ];
  32. const IOS_APPLICATION_SPAN_DESCRIPTIONS = ['Initial Frame Render'];
  33. // Since we don't collect a tag that indicates whether a span is system or
  34. // application, we're going to use the span.op and span.description to
  35. // determine whether a span is system or application.
  36. function aggregateSystemApplicationBreakdown(data: TableDataRow[]) {
  37. return data.reduce((acc, row) => {
  38. const spanOp = row['span.op'] as string;
  39. const spanDescription = row['span.description'] as string;
  40. let type: 'system' | 'application' = 'system';
  41. if (
  42. ANDROID_APPLICATION_SPAN_OPS.includes(spanOp) ||
  43. IOS_APPLICATION_SPAN_DESCRIPTIONS.includes(spanDescription)
  44. ) {
  45. type = 'application';
  46. }
  47. acc[row.release] = {
  48. ...acc[row.release],
  49. [type]: (acc[row.release]?.[type] ?? 0) + (row['sum(span.self_time)'] ?? 0),
  50. };
  51. return acc;
  52. }, {});
  53. }
  54. function SystemApplicationBreakdown({additionalFilters}) {
  55. const pageFilter = usePageFilters();
  56. const location = useLocation();
  57. const {query: locationQuery} = location;
  58. const {
  59. primaryRelease,
  60. secondaryRelease,
  61. isLoading: isReleasesLoading,
  62. } = useReleaseSelection();
  63. const query = new MutableSearch([
  64. 'span.op:[app.start.warm,app.start.cold,contentprovider.load,application.load,activity.load]',
  65. '!span.description:"Cold Start"',
  66. '!span.description:"Warm Start"',
  67. ...(additionalFilters ?? []),
  68. ]);
  69. const searchQuery = decodeScalar(locationQuery.query, '');
  70. if (searchQuery) {
  71. query.addStringFilter(prepareQueryForLandingPage(searchQuery, false));
  72. }
  73. const queryString = appendReleaseFilters(query, primaryRelease, secondaryRelease);
  74. const {data, isPending} = useTableQuery({
  75. eventView: EventView.fromNewQueryWithPageFilters(
  76. {
  77. name: '',
  78. fields: ['release', 'span.op', 'span.description', 'sum(span.self_time)'],
  79. topEvents: '2',
  80. query: queryString,
  81. dataset: DiscoverDatasets.SPANS_METRICS,
  82. version: 2,
  83. interval: getInterval(
  84. pageFilter.selection.datetime,
  85. STARFISH_CHART_INTERVAL_FIDELITY
  86. ),
  87. },
  88. pageFilter.selection
  89. ),
  90. enabled: !isReleasesLoading,
  91. referrer: 'api.starfish.mobile-startup-breakdown',
  92. initialData: {data: []},
  93. });
  94. if (isPending) {
  95. return <LoadingContainer isLoading />;
  96. }
  97. if (!data) {
  98. return (
  99. <EmptyStateWarning small>
  100. <p>{t('There was no app start data found for these two releases')}</p>
  101. </EmptyStateWarning>
  102. );
  103. }
  104. const breakdownByReleaseData = aggregateSystemApplicationBreakdown(data.data);
  105. const breakdownGroups = [
  106. {
  107. color: SYSTEM_COLOR,
  108. key: 'system',
  109. name: t('System'),
  110. },
  111. {
  112. color: APPLICATION_COLOR,
  113. key: 'application',
  114. name: t('Application'),
  115. },
  116. ];
  117. return (
  118. <MiniChartPanel
  119. title={t('System v. Application')}
  120. subtitle={
  121. primaryRelease
  122. ? t(
  123. '%s v. %s',
  124. formatVersionAndCenterTruncate(primaryRelease, 12),
  125. secondaryRelease ? formatVersionAndCenterTruncate(secondaryRelease, 12) : ''
  126. )
  127. : ''
  128. }
  129. >
  130. <Legend>
  131. <LegendEntry>
  132. <StyledStartTypeDot style={{backgroundColor: SYSTEM_COLOR}} /> {t('System')}
  133. </LegendEntry>
  134. <LegendEntry>
  135. <StyledStartTypeDot style={{backgroundColor: APPLICATION_COLOR}} />
  136. {t('Application')}
  137. </LegendEntry>
  138. </Legend>
  139. <AppStartBreakdownContent>
  140. {primaryRelease && (
  141. <ReleaseAppStartBreakdown>
  142. <TextOverflow>{formatVersion(primaryRelease)}</TextOverflow>
  143. <Breakdown
  144. data-test-id="primary-release-breakdown"
  145. row={breakdownByReleaseData[primaryRelease]}
  146. breakdownGroups={breakdownGroups}
  147. />
  148. </ReleaseAppStartBreakdown>
  149. )}
  150. {secondaryRelease && (
  151. <ReleaseAppStartBreakdown>
  152. <TextOverflow>{formatVersion(secondaryRelease)}</TextOverflow>
  153. <Breakdown
  154. data-test-id="secondary-release-breakdown"
  155. row={breakdownByReleaseData[secondaryRelease]}
  156. breakdownGroups={breakdownGroups}
  157. />
  158. </ReleaseAppStartBreakdown>
  159. )}
  160. </AppStartBreakdownContent>
  161. </MiniChartPanel>
  162. );
  163. }
  164. export default SystemApplicationBreakdown;
  165. const ReleaseAppStartBreakdown = styled('div')`
  166. display: grid;
  167. grid-template-columns: 20% auto;
  168. gap: ${space(1)};
  169. color: ${p => p.theme.subText};
  170. `;
  171. const AppStartBreakdownContent = styled('div')`
  172. display: flex;
  173. flex-direction: column;
  174. gap: ${space(1)};
  175. margin-top: ${space(1)};
  176. `;
  177. const Legend = styled('div')`
  178. display: flex;
  179. gap: ${space(1.5)};
  180. position: absolute;
  181. top: ${space(1.5)};
  182. right: ${space(2)};
  183. `;
  184. const LegendEntry = styled('div')`
  185. display: flex;
  186. justify-content: center;
  187. align-items: center;
  188. font-size: ${p => p.theme.fontSizeSmall};
  189. `;
  190. const StyledStartTypeDot = styled(OpsDot)`
  191. position: relative;
  192. top: -1px;
  193. `;