index.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. const roundedValue = round(percent, percent > decimalThreshold ? decimalPlaces : 0);
  23. if (roundedValue === 100 && percent < 100) {
  24. return (
  25. Math.floor(percent * Math.pow(10, decimalPlaces)) / Math.pow(10, decimalPlaces)
  26. );
  27. }
  28. return roundedValue;
  29. };
  30. export const displayCrashFreePercent = (
  31. percent: number,
  32. decimalThreshold = CRASH_FREE_DECIMAL_THRESHOLD,
  33. decimalPlaces = 3
  34. ): string => {
  35. if (isNaN(percent)) {
  36. return '\u2015';
  37. }
  38. if (percent < 1 && percent > 0) {
  39. return `<1\u0025`;
  40. }
  41. const rounded = getCrashFreePercent(
  42. percent,
  43. decimalThreshold,
  44. decimalPlaces
  45. ).toLocaleString();
  46. return `${rounded}\u0025`;
  47. };
  48. export const getSessionStatusPercent = (percent: number, absolute = true) => {
  49. return round(absolute ? Math.abs(percent) : percent, 3);
  50. };
  51. export const displaySessionStatusPercent = (percent: number, absolute = true) => {
  52. return `${getSessionStatusPercent(percent, absolute).toLocaleString()}\u0025`;
  53. };
  54. export const displayCrashFreeDiff = (
  55. diffPercent: number,
  56. crashFreePercent?: number | null
  57. ) =>
  58. `${Math.abs(
  59. round(
  60. diffPercent,
  61. crashFreePercent && crashFreePercent > CRASH_FREE_DECIMAL_THRESHOLD ? 3 : 0
  62. )
  63. ).toLocaleString()}\u0025`;
  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 function releaseDisplayLabel(displayOption: DisplayOption, count?: number | null) {
  123. if (displayOption === DisplayOption.USERS) {
  124. return tn('user', 'users', count);
  125. }
  126. return tn('session', 'sessions', count);
  127. }
  128. export type ReleaseBounds = {releaseStart?: string | null; releaseEnd?: string | null};
  129. export function getReleaseBounds(release?: Release): ReleaseBounds {
  130. const {lastEvent, currentProjectMeta, dateCreated} = release || {};
  131. const {sessionsUpperBound} = currentProjectMeta || {};
  132. const releaseStart = moment(dateCreated).startOf('minute').utc().format();
  133. const releaseEnd = moment(
  134. (moment(sessionsUpperBound).isAfter(lastEvent) ? sessionsUpperBound : lastEvent) ??
  135. undefined
  136. )
  137. .endOf('minute')
  138. .utc()
  139. .format();
  140. if (moment(releaseStart).isSame(releaseEnd, 'minute')) {
  141. return {
  142. releaseStart,
  143. releaseEnd: moment(releaseEnd).add(1, 'minutes').utc().format(),
  144. };
  145. }
  146. return {
  147. releaseStart,
  148. releaseEnd,
  149. };
  150. }
  151. type GetReleaseParams = {
  152. location: Location;
  153. releaseBounds: ReleaseBounds;
  154. defaultStatsPeriod: string;
  155. allowEmptyPeriod: boolean;
  156. };
  157. // these options are here only temporarily while we still support older and newer release details page
  158. export function getReleaseParams({
  159. location,
  160. releaseBounds,
  161. defaultStatsPeriod,
  162. allowEmptyPeriod,
  163. }: GetReleaseParams) {
  164. const params = getParams(
  165. pick(location.query, [
  166. ...Object.values(URL_PARAM),
  167. ...Object.values(PAGE_URL_PARAM),
  168. 'cursor',
  169. ]),
  170. {
  171. allowAbsolutePageDatetime: true,
  172. defaultStatsPeriod,
  173. allowEmptyPeriod,
  174. }
  175. );
  176. if (
  177. !Object.keys(params).some(param =>
  178. [URL_PARAM.START, URL_PARAM.END, URL_PARAM.UTC, URL_PARAM.PERIOD].includes(param)
  179. )
  180. ) {
  181. params[URL_PARAM.START] = releaseBounds.releaseStart;
  182. params[URL_PARAM.END] = releaseBounds.releaseEnd;
  183. }
  184. return params;
  185. }