index.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {Location} from 'history';
  2. import pick from 'lodash/pick';
  3. import round from 'lodash/round';
  4. import moment from 'moment';
  5. import {getParams} from 'app/components/organizations/globalSelectionHeader/getParams';
  6. import {PAGE_URL_PARAM, URL_PARAM} from 'app/constants/globalSelectionHeader';
  7. import {tn} from 'app/locale';
  8. import {Release, ReleaseStatus} from 'app/types';
  9. import {QueryResults} from 'app/utils/tokenizeSearch';
  10. import {IssueSortOptions} from 'app/views/issueList/utils';
  11. import {DisplayOption} from '../list/utils';
  12. export const CRASH_FREE_DECIMAL_THRESHOLD = 95;
  13. export const roundDuration = (seconds: number) => {
  14. return round(seconds, seconds > 60 ? 0 : 3);
  15. };
  16. export const getCrashFreePercent = (
  17. percent: number,
  18. decimalThreshold = CRASH_FREE_DECIMAL_THRESHOLD,
  19. decimalPlaces = 3
  20. ): number => {
  21. return round(percent, percent > decimalThreshold ? decimalPlaces : 0);
  22. };
  23. export const displayCrashFreePercent = (
  24. percent: number,
  25. decimalThreshold = CRASH_FREE_DECIMAL_THRESHOLD,
  26. decimalPlaces = 3
  27. ): string => {
  28. if (isNaN(percent)) {
  29. return '\u2015';
  30. }
  31. if (percent < 1 && percent > 0) {
  32. return `<1\u0025`;
  33. }
  34. const rounded = getCrashFreePercent(
  35. percent,
  36. decimalThreshold,
  37. decimalPlaces
  38. ).toLocaleString();
  39. return `${rounded}\u0025`;
  40. };
  41. export const getSessionStatusPercent = (percent: number) => {
  42. return round(percent, 3);
  43. };
  44. export const displaySessionStatusPercent = (percent: number) => {
  45. return `${getSessionStatusPercent(percent).toLocaleString()}\u0025`;
  46. };
  47. export const displayCrashFreeDiff = (
  48. diffPercent: number,
  49. crashFreePercent?: number | null
  50. ) =>
  51. `${Math.abs(
  52. round(
  53. diffPercent,
  54. crashFreePercent && crashFreePercent > CRASH_FREE_DECIMAL_THRESHOLD ? 3 : 0
  55. )
  56. ).toLocaleString()}\u0025`;
  57. export const getReleaseNewIssuesUrl = (
  58. orgSlug: string,
  59. projectId: string | number | null,
  60. version: string
  61. ) => {
  62. return {
  63. pathname: `/organizations/${orgSlug}/issues/`,
  64. query: {
  65. project: projectId,
  66. // we are resetting time selector because releases' new issues count doesn't take time selector into account
  67. statsPeriod: undefined,
  68. start: undefined,
  69. end: undefined,
  70. query: new QueryResults([`firstRelease:${version}`]).formatString(),
  71. sort: IssueSortOptions.FREQ,
  72. },
  73. };
  74. };
  75. export const getReleaseUnhandledIssuesUrl = (
  76. orgSlug: string,
  77. projectId: string | number | null,
  78. version: string
  79. ) => {
  80. return {
  81. pathname: `/organizations/${orgSlug}/issues/`,
  82. query: {
  83. project: projectId,
  84. query: new QueryResults([
  85. `release:${version}`,
  86. 'error.unhandled:true',
  87. ]).formatString(),
  88. sort: IssueSortOptions.FREQ,
  89. },
  90. };
  91. };
  92. export const isReleaseArchived = (release: Release) =>
  93. release.status === ReleaseStatus.Archived;
  94. export function releaseDisplayLabel(displayOption: DisplayOption, count?: number | null) {
  95. if (displayOption === DisplayOption.USERS) {
  96. return tn('user', 'users', count);
  97. }
  98. return tn('session', 'sessions', count);
  99. }
  100. export type ReleaseBounds = {releaseStart?: string | null; releaseEnd?: string | null};
  101. export function getReleaseBounds(release?: Release): ReleaseBounds {
  102. const {lastEvent, currentProjectMeta, dateCreated} = release || {};
  103. const {sessionsUpperBound} = currentProjectMeta || {};
  104. return {
  105. releaseStart: dateCreated,
  106. releaseEnd:
  107. (moment(sessionsUpperBound).isAfter(lastEvent) ? sessionsUpperBound : lastEvent) ??
  108. moment().utc().format(),
  109. };
  110. }
  111. type GetReleaseParams = {
  112. location: Location;
  113. releaseBounds: ReleaseBounds;
  114. defaultStatsPeriod: string;
  115. allowEmptyPeriod: boolean;
  116. };
  117. // these options are here only temporarily while we still support older and newer release details page
  118. export function getReleaseParams({
  119. location,
  120. releaseBounds,
  121. defaultStatsPeriod,
  122. allowEmptyPeriod,
  123. }: GetReleaseParams) {
  124. const params = getParams(
  125. pick(location.query, [
  126. ...Object.values(URL_PARAM),
  127. ...Object.values(PAGE_URL_PARAM),
  128. 'cursor',
  129. ]),
  130. {
  131. allowAbsolutePageDatetime: true,
  132. defaultStatsPeriod,
  133. allowEmptyPeriod,
  134. }
  135. );
  136. if (
  137. !Object.keys(params).some(param =>
  138. [URL_PARAM.START, URL_PARAM.END, URL_PARAM.UTC, URL_PARAM.PERIOD].includes(param)
  139. )
  140. ) {
  141. params[URL_PARAM.START] = releaseBounds.releaseStart;
  142. params[URL_PARAM.END] = releaseBounds.releaseEnd;
  143. }
  144. return params;
  145. }