totalCrashFreeUsers.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import styled from '@emotion/styled';
  2. import type {Location} from 'history';
  3. import pick from 'lodash/pick';
  4. import moment from 'moment-timezone';
  5. import Count from 'sentry/components/count';
  6. import LoadingError from 'sentry/components/loadingError';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  9. import * as SidebarSection from 'sentry/components/sidebarSection';
  10. import {URL_PARAM} from 'sentry/constants/pageFilters';
  11. import {t, tn} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {Organization} from 'sentry/types/organization';
  14. import type {CrashFreeTimeBreakdown} from 'sentry/types/release';
  15. import {defined} from 'sentry/utils';
  16. import {useApiQuery} from 'sentry/utils/queryClient';
  17. import {displayCrashFreePercent} from '../../../utils';
  18. type Props = {
  19. location: Location;
  20. organization: Organization;
  21. projectSlug: string;
  22. version: string;
  23. };
  24. type ReleaseStatsType = {usersBreakdown: CrashFreeTimeBreakdown} | null;
  25. function TotalCrashFreeUsers({location, organization, projectSlug, version}: Props) {
  26. const {
  27. data: releaseStats,
  28. isPending,
  29. isError,
  30. } = useApiQuery<ReleaseStatsType>(
  31. [
  32. `/projects/${organization.slug}/${projectSlug}/releases/${encodeURIComponent(
  33. version
  34. )}/stats/`,
  35. {
  36. query: {
  37. ...normalizeDateTimeParams(
  38. pick(location.query, [URL_PARAM.PROJECT, URL_PARAM.ENVIRONMENT])
  39. ),
  40. type: 'sessions',
  41. },
  42. },
  43. ],
  44. {staleTime: 0}
  45. );
  46. if (isPending) {
  47. return <LoadingIndicator />;
  48. }
  49. if (isError) {
  50. return <LoadingError />;
  51. }
  52. const crashFreeTimeBreakdown = releaseStats?.usersBreakdown;
  53. if (!crashFreeTimeBreakdown?.length) {
  54. return null;
  55. }
  56. const timeline = crashFreeTimeBreakdown
  57. .map(({date, crashFreeUsers, totalUsers}, index, data) => {
  58. // count number of crash free users from knowing percent and total
  59. const crashFreeUserCount = Math.round(((crashFreeUsers ?? 0) * totalUsers) / 100);
  60. // first item of timeline is release creation date, then we want to have relative date label
  61. const dateLabel =
  62. index === 0
  63. ? t('Release created')
  64. : `${moment(data[0].date).from(date, true)} ${t('later')}`;
  65. return {date: moment(date), dateLabel, crashFreeUsers, crashFreeUserCount};
  66. })
  67. // remove those timeframes that are in the future
  68. .filter(item => item.date.isBefore())
  69. // we want timeline to go from bottom to up
  70. .reverse();
  71. if (!timeline.length) {
  72. return null;
  73. }
  74. return (
  75. <SidebarSection.Wrap>
  76. <SidebarSection.Title>{t('Total Crash Free Users')}</SidebarSection.Title>
  77. <SidebarSection.Content>
  78. <Timeline>
  79. {timeline.map(row => (
  80. <Row key={row.date.toString()}>
  81. <InnerRow>
  82. <Text bold>{row.date.format('MMMM D')}</Text>
  83. <Text bold right>
  84. <Count value={row.crashFreeUserCount} />{' '}
  85. {tn('user', 'users', row.crashFreeUserCount)}
  86. </Text>
  87. </InnerRow>
  88. <InnerRow>
  89. <Text>{row.dateLabel}</Text>
  90. <Percent right>
  91. {defined(row.crashFreeUsers)
  92. ? displayCrashFreePercent(row.crashFreeUsers)
  93. : '-'}
  94. </Percent>
  95. </InnerRow>
  96. </Row>
  97. ))}
  98. </Timeline>
  99. </SidebarSection.Content>
  100. </SidebarSection.Wrap>
  101. );
  102. }
  103. const Timeline = styled('div')`
  104. font-size: ${p => p.theme.fontSizeMedium};
  105. line-height: 1.2;
  106. `;
  107. const DOT_SIZE = 10;
  108. const Row = styled('div')`
  109. border-left: 1px solid ${p => p.theme.border};
  110. padding-left: ${space(2)};
  111. padding-bottom: ${space(1)};
  112. margin-left: ${space(1)};
  113. position: relative;
  114. &:before {
  115. content: '';
  116. width: ${DOT_SIZE}px;
  117. height: ${DOT_SIZE}px;
  118. border-radius: 100%;
  119. background-color: ${p => p.theme.purple300};
  120. position: absolute;
  121. top: 0;
  122. left: -${Math.floor(DOT_SIZE / 2)}px;
  123. }
  124. &:last-child {
  125. border-left: 0;
  126. }
  127. `;
  128. const InnerRow = styled('div')`
  129. display: grid;
  130. grid-column-gap: ${space(2)};
  131. grid-auto-flow: column;
  132. grid-auto-columns: 1fr;
  133. padding-bottom: ${space(0.5)};
  134. `;
  135. const Text = styled('div')<{bold?: boolean; right?: boolean}>`
  136. text-align: ${p => (p.right ? 'right' : 'left')};
  137. color: ${p => (p.bold ? p.theme.textColor : p.theme.gray300)};
  138. padding-bottom: ${space(0.25)};
  139. ${p => p.theme.overflowEllipsis};
  140. `;
  141. const Percent = styled(Text)`
  142. font-variant-numeric: tabular-nums;
  143. `;
  144. export default TotalCrashFreeUsers;