index.tsx 7.1 KB

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