index.tsx 7.1 KB

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