index.tsx 7.2 KB

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