totalCrashFreeUsers.tsx 4.6 KB

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