index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 displayCrashFreeDiff = (
  42. diffPercent: number,
  43. crashFreePercent?: number | null
  44. ) =>
  45. `${Math.abs(
  46. round(
  47. diffPercent,
  48. crashFreePercent && crashFreePercent > CRASH_FREE_DECIMAL_THRESHOLD ? 3 : 0
  49. )
  50. ).toLocaleString()}\u0025`;
  51. export const getReleaseNewIssuesUrl = (
  52. orgSlug: string,
  53. projectId: string | number | null,
  54. version: string
  55. ) => {
  56. return {
  57. pathname: `/organizations/${orgSlug}/issues/`,
  58. query: {
  59. project: projectId,
  60. // we are resetting time selector because releases' new issues count doesn't take time selector into account
  61. statsPeriod: undefined,
  62. start: undefined,
  63. end: undefined,
  64. query: new QueryResults([`firstRelease:${version}`]).formatString(),
  65. sort: IssueSortOptions.FREQ,
  66. },
  67. };
  68. };
  69. export const getReleaseUnhandledIssuesUrl = (
  70. orgSlug: string,
  71. projectId: string | number | null,
  72. version: string
  73. ) => {
  74. return {
  75. pathname: `/organizations/${orgSlug}/issues/`,
  76. query: {
  77. project: projectId,
  78. query: new QueryResults([
  79. `release:${version}`,
  80. 'error.unhandled:true',
  81. ]).formatString(),
  82. sort: IssueSortOptions.FREQ,
  83. },
  84. };
  85. };
  86. export const isReleaseArchived = (release: Release) =>
  87. release.status === ReleaseStatus.Archived;
  88. export function releaseDisplayLabel(displayOption: DisplayOption, count?: number | null) {
  89. if (displayOption === DisplayOption.USERS) {
  90. return tn('user', 'users', count);
  91. }
  92. return tn('session', 'sessions', count);
  93. }
  94. export type ReleaseBounds = {releaseStart?: string | null; releaseEnd?: string | null};
  95. export function getReleaseBounds(release?: Release): ReleaseBounds {
  96. const {lastEvent, currentProjectMeta, dateCreated} = release || {};
  97. const {sessionsUpperBound} = currentProjectMeta || {};
  98. return {
  99. releaseStart: dateCreated,
  100. releaseEnd:
  101. (moment(sessionsUpperBound).isAfter(lastEvent) ? sessionsUpperBound : lastEvent) ??
  102. moment().utc().format(),
  103. };
  104. }
  105. type GetReleaseParams = {
  106. location: Location;
  107. releaseBounds: ReleaseBounds;
  108. defaultStatsPeriod: string;
  109. allowEmptyPeriod: boolean;
  110. };
  111. // these options are here only temporarily while we still support older and newer release details page
  112. export function getReleaseParams({
  113. location,
  114. releaseBounds,
  115. defaultStatsPeriod,
  116. allowEmptyPeriod,
  117. }: GetReleaseParams) {
  118. const params = getParams(
  119. pick(location.query, [
  120. ...Object.values(URL_PARAM),
  121. ...Object.values(PAGE_URL_PARAM),
  122. 'cursor',
  123. ]),
  124. {
  125. allowAbsolutePageDatetime: true,
  126. defaultStatsPeriod,
  127. allowEmptyPeriod,
  128. }
  129. );
  130. if (
  131. !Object.keys(params).some(param =>
  132. [URL_PARAM.START, URL_PARAM.END, URL_PARAM.UTC, URL_PARAM.PERIOD].includes(param)
  133. )
  134. ) {
  135. params[URL_PARAM.START] = releaseBounds.releaseStart;
  136. params[URL_PARAM.END] = releaseBounds.releaseEnd;
  137. }
  138. return params;
  139. }