index.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import {Location} from 'history';
  2. import pick from 'lodash/pick';
  3. import round from 'lodash/round';
  4. import moment from 'moment';
  5. import {DateTimeObject} from 'sentry/components/charts/utils';
  6. import ExternalLink from 'sentry/components/links/externalLink';
  7. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  8. import {PAGE_URL_PARAM, URL_PARAM} from 'sentry/constants/pageFilters';
  9. import {desktop, mobile, PlatformKey} from 'sentry/data/platformCategories';
  10. import {t, tct} from 'sentry/locale';
  11. import {Release, ReleaseStatus} from 'sentry/types';
  12. import {defined} from 'sentry/utils';
  13. import {Theme} from 'sentry/utils/theme';
  14. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  15. import {IssueSortOptions} from 'sentry/views/issueList/utils';
  16. export const CRASH_FREE_DECIMAL_THRESHOLD = 95;
  17. export const roundDuration = (seconds: number) => {
  18. return round(seconds, seconds > 60 ? 0 : 3);
  19. };
  20. export const getCrashFreePercent = (
  21. percent: number,
  22. decimalThreshold = CRASH_FREE_DECIMAL_THRESHOLD,
  23. decimalPlaces = 3
  24. ): number => {
  25. const roundedValue = round(percent, percent > decimalThreshold ? decimalPlaces : 0);
  26. if (roundedValue === 100 && percent < 100) {
  27. return (
  28. Math.floor(percent * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces)
  29. );
  30. }
  31. return roundedValue;
  32. };
  33. export const displayCrashFreePercent = (
  34. percent: number,
  35. decimalThreshold = CRASH_FREE_DECIMAL_THRESHOLD,
  36. decimalPlaces = 3
  37. ): string => {
  38. if (isNaN(percent)) {
  39. return '\u2015';
  40. }
  41. if (percent < 1 && percent > 0) {
  42. return `<1\u0025`;
  43. }
  44. const rounded = getCrashFreePercent(
  45. percent,
  46. decimalThreshold,
  47. decimalPlaces
  48. ).toLocaleString();
  49. return `${rounded}\u0025`;
  50. };
  51. export const getSessionStatusPercent = (percent: number, absolute = true) => {
  52. return round(absolute ? Math.abs(percent) : percent, 3);
  53. };
  54. export const displaySessionStatusPercent = (percent: number, absolute = true) => {
  55. return `${getSessionStatusPercent(percent, absolute).toLocaleString()}\u0025`;
  56. };
  57. export const getReleaseNewIssuesUrl = (
  58. orgSlug: string,
  59. projectId: string | number | null,
  60. version: string
  61. ) => {
  62. return {
  63. pathname: `/organizations/${orgSlug}/issues/`,
  64. query: {
  65. project: projectId,
  66. // we are resetting time selector because releases' new issues count doesn't take time selector into account
  67. statsPeriod: undefined,
  68. start: undefined,
  69. end: undefined,
  70. query: new MutableSearch([`firstRelease:${version}`]).formatString(),
  71. sort: IssueSortOptions.FREQ,
  72. },
  73. };
  74. };
  75. export const getReleaseUnhandledIssuesUrl = (
  76. orgSlug: string,
  77. projectId: string | number | null,
  78. version: string,
  79. dateTime: DateTimeObject = {}
  80. ) => {
  81. return {
  82. pathname: `/organizations/${orgSlug}/issues/`,
  83. query: {
  84. ...dateTime,
  85. project: projectId,
  86. query: new MutableSearch([
  87. `release:${version}`,
  88. 'error.unhandled:true',
  89. ]).formatString(),
  90. sort: IssueSortOptions.FREQ,
  91. },
  92. };
  93. };
  94. export const getReleaseHandledIssuesUrl = (
  95. orgSlug: string,
  96. projectId: string | number | null,
  97. version: string,
  98. dateTime: DateTimeObject = {}
  99. ) => {
  100. return {
  101. pathname: `/organizations/${orgSlug}/issues/`,
  102. query: {
  103. ...dateTime,
  104. project: projectId,
  105. query: new MutableSearch([
  106. `release:${version}`,
  107. 'error.handled:true',
  108. ]).formatString(),
  109. sort: IssueSortOptions.FREQ,
  110. },
  111. };
  112. };
  113. export const isReleaseArchived = (release: Release) =>
  114. release.status === ReleaseStatus.Archived;
  115. export type ReleaseBounds = {
  116. type: 'normal' | 'clamped' | 'ancient';
  117. releaseEnd?: string | null;
  118. releaseStart?: string | null;
  119. };
  120. export function getReleaseBounds(release?: Release): ReleaseBounds {
  121. const retentionBound = moment().subtract(90, 'days');
  122. const {lastEvent, currentProjectMeta, dateCreated} = release || {};
  123. const {sessionsUpperBound} = currentProjectMeta || {};
  124. let type: ReleaseBounds['type'] = 'normal';
  125. let releaseStart = moment(dateCreated).startOf('minute');
  126. let releaseEnd = moment(
  127. (moment(sessionsUpperBound).isAfter(lastEvent) ? sessionsUpperBound : lastEvent) ??
  128. undefined
  129. ).endOf('minute');
  130. if (moment(releaseStart).isSame(releaseEnd, 'minute')) {
  131. releaseEnd = moment(releaseEnd).add(1, 'minutes');
  132. }
  133. if (releaseStart.isBefore(retentionBound)) {
  134. releaseStart = retentionBound;
  135. type = 'clamped';
  136. if (
  137. releaseEnd.isBefore(releaseStart) ||
  138. (!defined(sessionsUpperBound) && !defined(lastEvent))
  139. ) {
  140. releaseEnd = moment();
  141. type = 'ancient';
  142. }
  143. }
  144. return {
  145. type,
  146. releaseStart: releaseStart.utc().format(),
  147. releaseEnd: releaseEnd.utc().format(),
  148. };
  149. }
  150. type GetReleaseParams = {
  151. location: Location;
  152. releaseBounds: ReleaseBounds;
  153. };
  154. export function getReleaseParams({location, releaseBounds}: GetReleaseParams) {
  155. const params = normalizeDateTimeParams(
  156. pick(location.query, [
  157. ...Object.values(URL_PARAM),
  158. ...Object.values(PAGE_URL_PARAM),
  159. 'cursor',
  160. ]),
  161. {
  162. allowAbsolutePageDatetime: true,
  163. allowEmptyPeriod: true,
  164. }
  165. );
  166. if (
  167. !Object.keys(params).some(param =>
  168. [URL_PARAM.START, URL_PARAM.END, URL_PARAM.UTC, URL_PARAM.PERIOD].includes(param)
  169. )
  170. ) {
  171. params[URL_PARAM.START] = releaseBounds.releaseStart;
  172. params[URL_PARAM.END] = releaseBounds.releaseEnd;
  173. }
  174. return params;
  175. }
  176. const adoptionStagesLink = (
  177. <ExternalLink href="https://docs.sentry.io/product/releases/health/#adoption-stages" />
  178. );
  179. export const ADOPTION_STAGE_LABELS: Record<
  180. string,
  181. {name: string; tooltipTitle: React.ReactNode; type: keyof Theme['tag']}
  182. > = {
  183. low_adoption: {
  184. name: t('Low Adoption'),
  185. tooltipTitle: tct(
  186. 'This release has a low percentage of sessions compared to other releases in this project. [link:Learn more]',
  187. {link: adoptionStagesLink}
  188. ),
  189. type: 'warning',
  190. },
  191. adopted: {
  192. name: t('Adopted'),
  193. tooltipTitle: tct(
  194. 'This release has a high percentage of sessions compared to other releases in this project. [link:Learn more]',
  195. {link: adoptionStagesLink}
  196. ),
  197. type: 'success',
  198. },
  199. replaced: {
  200. name: t('Replaced'),
  201. tooltipTitle: tct(
  202. 'This release was previously Adopted, but now has a lower level of sessions compared to other releases in this project. [link:Learn more]',
  203. {link: adoptionStagesLink}
  204. ),
  205. type: 'default',
  206. },
  207. };
  208. export const isMobileRelease = (releaseProjectPlatform: PlatformKey) =>
  209. ([...mobile, ...desktop] as string[]).includes(releaseProjectPlatform);