releaseAdoption.tsx 11 KB

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