releaseAdoption.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import {useTheme} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import ChartZoom from 'sentry/components/charts/chartZoom';
  4. import ErrorPanel from 'sentry/components/charts/errorPanel';
  5. import {LineChart, LineChartProps} from 'sentry/components/charts/lineChart';
  6. import TransitionChart from 'sentry/components/charts/transitionChart';
  7. import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
  8. import ErrorBoundary from 'sentry/components/errorBoundary';
  9. import NotAvailable from 'sentry/components/notAvailable';
  10. import QuestionTooltip from 'sentry/components/questionTooltip';
  11. import * as SidebarSection from 'sentry/components/sidebarSection';
  12. import Tag from 'sentry/components/tag';
  13. import {Tooltip} from 'sentry/components/tooltip';
  14. import {IconWarning} from 'sentry/icons';
  15. import {t, tct} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import {
  18. ReleaseProject,
  19. ReleaseWithHealth,
  20. SessionApiResponse,
  21. SessionFieldWithOperation,
  22. } from 'sentry/types';
  23. import {formatAbbreviatedNumber} from 'sentry/utils/formatters';
  24. import {getAdoptionSeries, getCount, getCountAtIndex} from 'sentry/utils/sessions';
  25. import {useLocation} from 'sentry/utils/useLocation';
  26. import useRouter from 'sentry/utils/useRouter';
  27. import {
  28. ADOPTION_STAGE_LABELS,
  29. getReleaseBounds,
  30. getReleaseParams,
  31. isMobileRelease,
  32. } from '../../../utils';
  33. import {generateReleaseMarkLines, releaseMarkLinesLabels} from '../../utils';
  34. const sessionsAxisIndex = 0;
  35. const usersAxisIndex = 1;
  36. const axisIndexToSessionsField = {
  37. [sessionsAxisIndex]: SessionFieldWithOperation.SESSIONS,
  38. [usersAxisIndex]: SessionFieldWithOperation.USERS,
  39. };
  40. type Props = {
  41. allSessions: SessionApiResponse | null;
  42. environment: string[];
  43. errored: boolean;
  44. loading: boolean;
  45. project: ReleaseProject;
  46. release: ReleaseWithHealth;
  47. releaseSessions: SessionApiResponse | null;
  48. reloading: boolean;
  49. };
  50. function ReleaseAdoption({
  51. release,
  52. project,
  53. environment,
  54. releaseSessions,
  55. allSessions,
  56. loading,
  57. reloading,
  58. errored,
  59. }: Props) {
  60. const location = useLocation();
  61. const router = useRouter();
  62. const theme = useTheme();
  63. const hasUsers = !!getCount(releaseSessions?.groups, SessionFieldWithOperation.USERS);
  64. function getSeries() {
  65. if (!releaseSessions) {
  66. return [];
  67. }
  68. const sessionsMarkLines = generateReleaseMarkLines(
  69. release,
  70. project,
  71. theme,
  72. location,
  73. {
  74. hideLabel: true,
  75. axisIndex: sessionsAxisIndex,
  76. }
  77. );
  78. const sessionSeriesData = getAdoptionSeries(
  79. releaseSessions.groups,
  80. allSessions?.groups,
  81. releaseSessions.intervals,
  82. SessionFieldWithOperation.SESSIONS
  83. );
  84. // echarts doesn't seem to like displaying marklines when there's only one data point.
  85. // Usually, there is one data point because there is very little sessions data.
  86. const hasMultipleDataPoints = sessionSeriesData.length > 1;
  87. const series = [
  88. ...(hasMultipleDataPoints ? sessionsMarkLines : []),
  89. {
  90. seriesName: t('Sessions'),
  91. connectNulls: true,
  92. yAxisIndex: sessionsAxisIndex,
  93. xAxisIndex: sessionsAxisIndex,
  94. data: sessionSeriesData,
  95. },
  96. ];
  97. if (hasUsers) {
  98. const usersMarkLines = generateReleaseMarkLines(release, project, theme, location, {
  99. hideLabel: true,
  100. axisIndex: usersAxisIndex,
  101. });
  102. series.push(...usersMarkLines);
  103. series.push({
  104. seriesName: t('Users'),
  105. connectNulls: true,
  106. yAxisIndex: usersAxisIndex,
  107. xAxisIndex: usersAxisIndex,
  108. data: getAdoptionSeries(
  109. releaseSessions.groups,
  110. allSessions?.groups,
  111. releaseSessions.intervals,
  112. SessionFieldWithOperation.USERS
  113. ),
  114. });
  115. }
  116. return series;
  117. }
  118. const colors = theme.charts.getColorPalette(2);
  119. const axisLineConfig = {
  120. scale: true,
  121. axisLine: {
  122. show: false,
  123. },
  124. axisTick: {
  125. show: false,
  126. },
  127. splitLine: {
  128. show: false,
  129. },
  130. max: 100,
  131. axisLabel: {
  132. formatter: (value: number) => `${value}%`,
  133. color: theme.chartLabel,
  134. },
  135. };
  136. const chartOptions: Omit<LineChartProps, 'series' | 'ref'> = {
  137. height: hasUsers ? 280 : 140,
  138. grid: [
  139. {
  140. top: '40px',
  141. left: '10px',
  142. right: '10px',
  143. height: '100px',
  144. },
  145. {
  146. top: '180px',
  147. left: '10px',
  148. right: '10px',
  149. height: '100px',
  150. },
  151. ],
  152. axisPointer: {
  153. // Link each x-axis together.
  154. link: [{xAxisIndex: [sessionsAxisIndex, usersAxisIndex]}],
  155. },
  156. xAxes: Array.from(new Array(2)).map((_i, index) => ({
  157. gridIndex: index,
  158. type: 'time' as const,
  159. show: false,
  160. })),
  161. yAxes: [
  162. {
  163. gridIndex: sessionsAxisIndex,
  164. ...axisLineConfig,
  165. },
  166. {
  167. gridIndex: usersAxisIndex,
  168. ...axisLineConfig,
  169. },
  170. ],
  171. // utc: utc === 'true', //TODO(release-comparison)
  172. isGroupedByDate: true,
  173. showTimeInTooltip: true,
  174. colors: [colors[0], colors[1]] as string[],
  175. tooltip: {
  176. trigger: 'axis' as const,
  177. truncate: 80,
  178. valueFormatter: (value, label, seriesParams: any) => {
  179. const {axisIndex, dataIndex} = seriesParams || {};
  180. const absoluteCount = getCountAtIndex(
  181. releaseSessions?.groups,
  182. axisIndexToSessionsField[axisIndex ?? 0],
  183. dataIndex ?? 0
  184. );
  185. return label && Object.values(releaseMarkLinesLabels).includes(label)
  186. ? ''
  187. : `<span>${formatAbbreviatedNumber(absoluteCount)} <span style="color: ${
  188. theme.textColor
  189. };margin-left: ${space(0.5)}">${value}%</span></span>`;
  190. },
  191. filter: (_, seriesParam: any) => {
  192. const {seriesName, axisIndex} = seriesParam;
  193. // do not display tooltips for "Users Adopted" marklines
  194. if (
  195. axisIndex === usersAxisIndex &&
  196. Object.values(releaseMarkLinesLabels).includes(seriesName)
  197. ) {
  198. return false;
  199. }
  200. return true;
  201. },
  202. },
  203. };
  204. const {
  205. statsPeriod: period,
  206. start,
  207. end,
  208. utc,
  209. } = getReleaseParams({
  210. location,
  211. releaseBounds: getReleaseBounds(release),
  212. });
  213. const adoptionStage = release.adoptionStages?.[project.slug]?.stage;
  214. const adoptionStageLabel = adoptionStage ? ADOPTION_STAGE_LABELS[adoptionStage] : null;
  215. const multipleEnvironments = environment.length === 0 || environment.length > 1;
  216. return (
  217. <div>
  218. {isMobileRelease(project.platform) && (
  219. <SidebarSection.Wrap>
  220. <SidebarSection.Title>
  221. {t('Adoption Stage')}
  222. {multipleEnvironments && (
  223. <QuestionTooltip
  224. position="top"
  225. title={t(
  226. 'See if a release has low adoption, been adopted by users, or replaced by another release. Select an environment above to view the stage this release is in.'
  227. )}
  228. size="sm"
  229. />
  230. )}
  231. </SidebarSection.Title>
  232. <SidebarSection.Content>
  233. {adoptionStageLabel && !multipleEnvironments ? (
  234. <div>
  235. <Tooltip title={adoptionStageLabel.tooltipTitle} isHoverable>
  236. <Tag type={adoptionStageLabel.type}>{adoptionStageLabel.name}</Tag>
  237. </Tooltip>
  238. <AdoptionEnvironment>
  239. {tct(`in [environment]`, {environment})}
  240. </AdoptionEnvironment>
  241. </div>
  242. ) : (
  243. <NotAvailableWrapper>
  244. <NotAvailable />
  245. </NotAvailableWrapper>
  246. )}
  247. </SidebarSection.Content>
  248. </SidebarSection.Wrap>
  249. )}
  250. <SidebarSection.Wrap>
  251. <RelativeBox>
  252. <ErrorBoundary mini>
  253. {!loading && (
  254. <ChartLabel top="0px">
  255. <SidebarSection.Title>
  256. {t('Sessions Adopted')}
  257. <TooltipWrapper>
  258. <QuestionTooltip
  259. position="top"
  260. title={t(
  261. 'Adoption compares the sessions of a release with the total sessions for this project.'
  262. )}
  263. size="sm"
  264. />
  265. </TooltipWrapper>
  266. </SidebarSection.Title>
  267. </ChartLabel>
  268. )}
  269. {!loading && hasUsers && (
  270. <ChartLabel top="140px">
  271. <SidebarSection.Title>
  272. {t('Users Adopted')}
  273. <TooltipWrapper>
  274. <QuestionTooltip
  275. position="top"
  276. title={t(
  277. 'Adoption compares the users of a release with the total users for this project.'
  278. )}
  279. size="sm"
  280. />
  281. </TooltipWrapper>
  282. </SidebarSection.Title>
  283. </ChartLabel>
  284. )}
  285. {errored ? (
  286. <ErrorPanel height="280px">
  287. <IconWarning color="gray300" size="lg" />
  288. </ErrorPanel>
  289. ) : (
  290. <TransitionChart loading={loading} reloading={reloading} height="280px">
  291. <TransparentLoadingMask visible={reloading} />
  292. <ChartZoom
  293. router={router}
  294. period={period ?? undefined}
  295. utc={utc === 'true'}
  296. start={start}
  297. end={end}
  298. usePageDate
  299. xAxisIndex={[sessionsAxisIndex, usersAxisIndex]}
  300. >
  301. {zoomRenderProps => (
  302. <LineChart
  303. {...chartOptions}
  304. {...zoomRenderProps}
  305. series={getSeries()}
  306. transformSinglePointToLine
  307. />
  308. )}
  309. </ChartZoom>
  310. </TransitionChart>
  311. )}
  312. </ErrorBoundary>
  313. </RelativeBox>
  314. </SidebarSection.Wrap>
  315. </div>
  316. );
  317. }
  318. const NotAvailableWrapper = styled('div')`
  319. display: flex;
  320. align-items: center;
  321. `;
  322. const ChartLabel = styled('div')<{top: string}>`
  323. position: absolute;
  324. top: ${p => p.top};
  325. z-index: 1;
  326. left: 0;
  327. right: 0;
  328. `;
  329. const TooltipWrapper = styled('span')`
  330. margin-left: ${space(0.5)};
  331. `;
  332. const AdoptionEnvironment = styled('span')`
  333. color: ${p => p.theme.textColor};
  334. margin-left: ${space(0.5)};
  335. font-size: ${p => p.theme.fontSizeSmall};
  336. `;
  337. const RelativeBox = styled('div')`
  338. position: relative;
  339. `;
  340. export default ReleaseAdoption;