index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 'app/components/charts/utils';
  6. import {getParams} from 'app/components/organizations/globalSelectionHeader/getParams';
  7. import {PAGE_URL_PARAM, URL_PARAM} from 'app/constants/globalSelectionHeader';
  8. import {tn} from 'app/locale';
  9. import {Release, ReleaseStatus} from 'app/types';
  10. import {MutableSearch} from 'app/utils/tokenizeSearch';
  11. import {IssueSortOptions} from 'app/views/issueList/utils';
  12. import {DisplayOption} from '../list/utils';
  13. export const CRASH_FREE_DECIMAL_THRESHOLD = 95;
  14. export const roundDuration = (seconds: number) => {
  15. return round(seconds, seconds > 60 ? 0 : 3);
  16. };
  17. export const getCrashFreePercent = (
  18. percent: number,
  19. decimalThreshold = CRASH_FREE_DECIMAL_THRESHOLD,
  20. decimalPlaces = 3
  21. ): number => {
  22. return round(percent, percent > decimalThreshold ? decimalPlaces : 0);
  23. };
  24. export const displayCrashFreePercent = (
  25. percent: number,
  26. decimalThreshold = CRASH_FREE_DECIMAL_THRESHOLD,
  27. decimalPlaces = 3
  28. ): string => {
  29. if (isNaN(percent)) {
  30. return '\u2015';
  31. }
  32. if (percent < 1 && percent > 0) {
  33. return `<1\u0025`;
  34. }
  35. const rounded = getCrashFreePercent(
  36. percent,
  37. decimalThreshold,
  38. decimalPlaces
  39. ).toLocaleString();
  40. return `${rounded}\u0025`;
  41. };
  42. export const getSessionStatusPercent = (percent: number, absolute = true) => {
  43. return round(absolute ? Math.abs(percent) : percent, 3);
  44. };
  45. export const displaySessionStatusPercent = (percent: number, absolute = true) => {
  46. return `${getSessionStatusPercent(percent, absolute).toLocaleString()}\u0025`;
  47. };
  48. export const displayCrashFreeDiff = (
  49. diffPercent: number,
  50. crashFreePercent?: number | null
  51. ) =>
  52. `${Math.abs(
  53. round(
  54. diffPercent,
  55. crashFreePercent && crashFreePercent > CRASH_FREE_DECIMAL_THRESHOLD ? 3 : 0
  56. )
  57. ).toLocaleString()}\u0025`;
  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 function releaseDisplayLabel(displayOption: DisplayOption, count?: number | null) {
  117. if (displayOption === DisplayOption.USERS) {
  118. return tn('user', 'users', count);
  119. }
  120. return tn('session', 'sessions', count);
  121. }
  122. export type ReleaseBounds = {releaseStart?: string | null; releaseEnd?: string | null};
  123. export function getReleaseBounds(release?: Release): ReleaseBounds {
  124. const {lastEvent, currentProjectMeta, dateCreated} = release || {};
  125. const {sessionsUpperBound} = currentProjectMeta || {};
  126. const releaseStart = moment(dateCreated).startOf('minute').utc().format();
  127. const releaseEnd = moment(
  128. (moment(sessionsUpperBound).isAfter(lastEvent) ? sessionsUpperBound : lastEvent) ??
  129. undefined
  130. )
  131. .startOf('minute')
  132. .utc()
  133. .format();
  134. if (moment(releaseStart).isSame(releaseEnd, 'minute')) {
  135. return {
  136. releaseStart,
  137. releaseEnd: moment(releaseEnd).add(1, 'minutes').utc().format(),
  138. };
  139. }
  140. return {
  141. releaseStart,
  142. releaseEnd,
  143. };
  144. }
  145. type GetReleaseParams = {
  146. location: Location;
  147. releaseBounds: ReleaseBounds;
  148. defaultStatsPeriod: string;
  149. allowEmptyPeriod: boolean;
  150. };
  151. // these options are here only temporarily while we still support older and newer release details page
  152. export function getReleaseParams({
  153. location,
  154. releaseBounds,
  155. defaultStatsPeriod,
  156. allowEmptyPeriod,
  157. }: GetReleaseParams) {
  158. const params = getParams(
  159. pick(location.query, [
  160. ...Object.values(URL_PARAM),
  161. ...Object.values(PAGE_URL_PARAM),
  162. 'cursor',
  163. ]),
  164. {
  165. allowAbsolutePageDatetime: true,
  166. defaultStatsPeriod,
  167. allowEmptyPeriod,
  168. }
  169. );
  170. if (
  171. !Object.keys(params).some(param =>
  172. [URL_PARAM.START, URL_PARAM.END, URL_PARAM.UTC, URL_PARAM.PERIOD].includes(param)
  173. )
  174. ) {
  175. params[URL_PARAM.START] = releaseBounds.releaseStart;
  176. params[URL_PARAM.END] = releaseBounds.releaseEnd;
  177. }
  178. return params;
  179. }