systemApplicationBreakdown.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  48. acc[row.release!] = {
  49. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  50. ...acc[row.release!],
  51. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  52. [type]: (acc[row.release!]?.[type] ?? 0) + (row['sum(span.self_time)'] ?? 0),
  53. };
  54. return acc;
  55. }, {});
  56. }
  57. function SystemApplicationBreakdown({additionalFilters}: any) {
  58. const pageFilter = usePageFilters();
  59. const location = useLocation();
  60. const {query: locationQuery} = location;
  61. const {
  62. primaryRelease,
  63. secondaryRelease,
  64. isLoading: isReleasesLoading,
  65. } = useReleaseSelection();
  66. const query = new MutableSearch([
  67. 'span.op:[app.start.warm,app.start.cold,contentprovider.load,application.load,activity.load]',
  68. '!span.description:"Cold Start"',
  69. '!span.description:"Warm Start"',
  70. ...(additionalFilters ?? []),
  71. ]);
  72. const searchQuery = decodeScalar(locationQuery.query, '');
  73. if (searchQuery) {
  74. query.addStringFilter(prepareQueryForLandingPage(searchQuery, false));
  75. }
  76. const queryString = appendReleaseFilters(query, primaryRelease, secondaryRelease);
  77. const {data, isPending} = useTableQuery({
  78. eventView: EventView.fromNewQueryWithPageFilters(
  79. {
  80. name: '',
  81. fields: ['release', 'span.op', 'span.description', 'sum(span.self_time)'],
  82. topEvents: '2',
  83. query: queryString,
  84. dataset: DiscoverDatasets.SPANS_METRICS,
  85. version: 2,
  86. interval: getInterval(
  87. pageFilter.selection.datetime,
  88. STARFISH_CHART_INTERVAL_FIDELITY
  89. ),
  90. },
  91. pageFilter.selection
  92. ),
  93. enabled: !isReleasesLoading,
  94. referrer: 'api.starfish.mobile-startup-breakdown',
  95. initialData: {data: []},
  96. });
  97. if (isPending) {
  98. return <LoadingContainer isLoading />;
  99. }
  100. if (!data) {
  101. return (
  102. <EmptyStateWarning small>
  103. <p>{t('There was no app start data found for these two releases')}</p>
  104. </EmptyStateWarning>
  105. );
  106. }
  107. const breakdownByReleaseData = aggregateSystemApplicationBreakdown(data.data);
  108. const breakdownGroups = [
  109. {
  110. color: SYSTEM_COLOR,
  111. key: 'system',
  112. name: t('System'),
  113. },
  114. {
  115. color: APPLICATION_COLOR,
  116. key: 'application',
  117. name: t('Application'),
  118. },
  119. ];
  120. return (
  121. <MiniChartPanel
  122. title={t('System v. Application')}
  123. subtitle={
  124. primaryRelease
  125. ? t(
  126. '%s v. %s',
  127. formatVersionAndCenterTruncate(primaryRelease, 12),
  128. secondaryRelease ? formatVersionAndCenterTruncate(secondaryRelease, 12) : ''
  129. )
  130. : ''
  131. }
  132. >
  133. <Legend>
  134. <LegendEntry>
  135. <StyledStartTypeDot style={{backgroundColor: SYSTEM_COLOR}} /> {t('System')}
  136. </LegendEntry>
  137. <LegendEntry>
  138. <StyledStartTypeDot style={{backgroundColor: APPLICATION_COLOR}} />
  139. {t('Application')}
  140. </LegendEntry>
  141. </Legend>
  142. <AppStartBreakdownContent>
  143. {primaryRelease && (
  144. <ReleaseAppStartBreakdown>
  145. <TextOverflow>{formatVersion(primaryRelease)}</TextOverflow>
  146. <Breakdown
  147. data-test-id="primary-release-breakdown"
  148. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  149. row={breakdownByReleaseData[primaryRelease]}
  150. breakdownGroups={breakdownGroups}
  151. />
  152. </ReleaseAppStartBreakdown>
  153. )}
  154. {secondaryRelease && (
  155. <ReleaseAppStartBreakdown>
  156. <TextOverflow>{formatVersion(secondaryRelease)}</TextOverflow>
  157. <Breakdown
  158. data-test-id="secondary-release-breakdown"
  159. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  160. row={breakdownByReleaseData[secondaryRelease]}
  161. breakdownGroups={breakdownGroups}
  162. />
  163. </ReleaseAppStartBreakdown>
  164. )}
  165. </AppStartBreakdownContent>
  166. </MiniChartPanel>
  167. );
  168. }
  169. export default SystemApplicationBreakdown;
  170. const ReleaseAppStartBreakdown = styled('div')`
  171. display: grid;
  172. grid-template-columns: 20% auto;
  173. gap: ${space(1)};
  174. color: ${p => p.theme.subText};
  175. `;
  176. const AppStartBreakdownContent = styled('div')`
  177. display: flex;
  178. flex-direction: column;
  179. gap: ${space(1)};
  180. margin-top: ${space(1)};
  181. `;
  182. const Legend = styled('div')`
  183. display: flex;
  184. gap: ${space(1.5)};
  185. position: absolute;
  186. top: ${space(1.5)};
  187. right: ${space(2)};
  188. `;
  189. const LegendEntry = styled('div')`
  190. display: flex;
  191. justify-content: center;
  192. align-items: center;
  193. font-size: ${p => p.theme.fontSizeSmall};
  194. `;
  195. const StyledStartTypeDot = styled(OpsDot)`
  196. position: relative;
  197. top: -1px;
  198. `;