index.tsx 7.0 KB

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