utils.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import {Theme} from '@emotion/react';
  2. import {Location} from 'history';
  3. import pick from 'lodash/pick';
  4. import moment from 'moment';
  5. import MarkLine from 'sentry/components/charts/components/markLine';
  6. import {parseStatsPeriod} from 'sentry/components/organizations/timeRangeSelector/utils';
  7. import {URL_PARAM} from 'sentry/constants/pageFilters';
  8. import {t} from 'sentry/locale';
  9. import {
  10. Commit,
  11. CommitFile,
  12. FilesByRepository,
  13. ReleaseComparisonChartType,
  14. ReleaseProject,
  15. ReleaseWithHealth,
  16. Repository,
  17. } from 'sentry/types';
  18. import {Series} from 'sentry/types/echarts';
  19. import {decodeList} from 'sentry/utils/queryString';
  20. import {getReleaseBounds, getReleaseParams, isMobileRelease} from '../utils';
  21. import {commonTermsDescription, SessionTerm} from '../utils/sessionTerm';
  22. export type CommitsByRepository = {
  23. [key: string]: Commit[];
  24. };
  25. /**
  26. * Convert list of individual file changes into a per-file summary grouped by repository
  27. */
  28. export function getFilesByRepository(fileList: CommitFile[]) {
  29. return fileList.reduce<FilesByRepository>((filesByRepository, file) => {
  30. const {filename, repoName, author, type} = file;
  31. if (!filesByRepository.hasOwnProperty(repoName)) {
  32. filesByRepository[repoName] = {};
  33. }
  34. if (!filesByRepository[repoName].hasOwnProperty(filename)) {
  35. filesByRepository[repoName][filename] = {
  36. authors: {},
  37. types: new Set(),
  38. };
  39. }
  40. if (author.email) {
  41. filesByRepository[repoName][filename].authors[author.email] = author;
  42. }
  43. filesByRepository[repoName][filename].types.add(type);
  44. return filesByRepository;
  45. }, {});
  46. }
  47. /**
  48. * Convert list of individual commits into a summary grouped by repository
  49. */
  50. export function getCommitsByRepository(commitList: Commit[]): CommitsByRepository {
  51. return commitList.reduce((commitsByRepository, commit) => {
  52. const repositoryName = commit.repository?.name ?? t('unknown');
  53. if (!commitsByRepository.hasOwnProperty(repositoryName)) {
  54. commitsByRepository[repositoryName] = [];
  55. }
  56. commitsByRepository[repositoryName].push(commit);
  57. return commitsByRepository;
  58. }, {});
  59. }
  60. /**
  61. * Get request query according to the url params and active repository
  62. */
  63. type GetQueryProps = {
  64. location: Location;
  65. activeRepository?: Repository;
  66. perPage?: number;
  67. };
  68. export function getQuery({location, perPage = 40, activeRepository}: GetQueryProps) {
  69. const query = {
  70. ...pick(location.query, [...Object.values(URL_PARAM), 'cursor']),
  71. per_page: perPage,
  72. };
  73. if (!activeRepository) {
  74. return query;
  75. }
  76. return {...query, repo_name: activeRepository.name};
  77. }
  78. /**
  79. * Get repositories to render according to the activeRepository
  80. */
  81. export function getReposToRender(repos: Array<string>, activeRepository?: Repository) {
  82. if (!activeRepository) {
  83. return repos;
  84. }
  85. return [activeRepository.name];
  86. }
  87. export const releaseComparisonChartLabels = {
  88. [ReleaseComparisonChartType.CRASH_FREE_SESSIONS]: t('Crash Free Session Rate'),
  89. [ReleaseComparisonChartType.HEALTHY_SESSIONS]: t('Healthy'),
  90. [ReleaseComparisonChartType.ABNORMAL_SESSIONS]: t('Abnormal'),
  91. [ReleaseComparisonChartType.ERRORED_SESSIONS]: t('Errored'),
  92. [ReleaseComparisonChartType.CRASHED_SESSIONS]: t('Crashed Session Rate'),
  93. [ReleaseComparisonChartType.CRASH_FREE_USERS]: t('Crash Free User Rate'),
  94. [ReleaseComparisonChartType.HEALTHY_USERS]: t('Healthy'),
  95. [ReleaseComparisonChartType.ABNORMAL_USERS]: t('Abnormal'),
  96. [ReleaseComparisonChartType.ERRORED_USERS]: t('Errored'),
  97. [ReleaseComparisonChartType.CRASHED_USERS]: t('Crashed User Rate'),
  98. [ReleaseComparisonChartType.SESSION_COUNT]: t('Session Count'),
  99. [ReleaseComparisonChartType.USER_COUNT]: t('User Count'),
  100. [ReleaseComparisonChartType.ERROR_COUNT]: t('Error Count'),
  101. [ReleaseComparisonChartType.TRANSACTION_COUNT]: t('Transaction Count'),
  102. [ReleaseComparisonChartType.FAILURE_RATE]: t('Failure Rate'),
  103. };
  104. export const releaseComparisonChartTitles = {
  105. [ReleaseComparisonChartType.CRASH_FREE_SESSIONS]: t('Crash Free Session Rate'),
  106. [ReleaseComparisonChartType.HEALTHY_SESSIONS]: t('Healthy Session Rate'),
  107. [ReleaseComparisonChartType.ABNORMAL_SESSIONS]: t('Abnormal Session Rate'),
  108. [ReleaseComparisonChartType.ERRORED_SESSIONS]: t('Errored Session Rate'),
  109. [ReleaseComparisonChartType.CRASHED_SESSIONS]: t('Crashed Session Rate'),
  110. [ReleaseComparisonChartType.CRASH_FREE_USERS]: t('Crash Free User Rate'),
  111. [ReleaseComparisonChartType.HEALTHY_USERS]: t('Healthy User Rate'),
  112. [ReleaseComparisonChartType.ABNORMAL_USERS]: t('Abnormal User Rate'),
  113. [ReleaseComparisonChartType.ERRORED_USERS]: t('Errored User Rate'),
  114. [ReleaseComparisonChartType.CRASHED_USERS]: t('Crashed User Rate'),
  115. [ReleaseComparisonChartType.SESSION_COUNT]: t('Session Count'),
  116. [ReleaseComparisonChartType.USER_COUNT]: t('User Count'),
  117. [ReleaseComparisonChartType.ERROR_COUNT]: t('Error Count'),
  118. [ReleaseComparisonChartType.TRANSACTION_COUNT]: t('Transaction Count'),
  119. [ReleaseComparisonChartType.FAILURE_RATE]: t('Failure Rate'),
  120. };
  121. export const releaseComparisonChartHelp = {
  122. [ReleaseComparisonChartType.CRASH_FREE_SESSIONS]:
  123. commonTermsDescription[SessionTerm.CRASH_FREE_SESSIONS],
  124. [ReleaseComparisonChartType.CRASH_FREE_USERS]:
  125. commonTermsDescription[SessionTerm.CRASH_FREE_USERS],
  126. [ReleaseComparisonChartType.SESSION_COUNT]: t(
  127. 'The number of sessions in a given period.'
  128. ),
  129. [ReleaseComparisonChartType.USER_COUNT]: t('The number of users in a given period.'),
  130. };
  131. type GenerateReleaseMarklineOptions = {
  132. axisIndex?: number;
  133. hideLabel?: boolean;
  134. };
  135. function generateReleaseMarkLine(
  136. title: string,
  137. position: number,
  138. theme: Theme,
  139. options?: GenerateReleaseMarklineOptions
  140. ) {
  141. const {hideLabel, axisIndex} = options || {};
  142. return {
  143. seriesName: title,
  144. type: 'line',
  145. data: [{name: position, value: null as any}], // TODO(ts): echart types
  146. yAxisIndex: axisIndex ?? undefined,
  147. xAxisIndex: axisIndex ?? undefined,
  148. color: theme.gray300,
  149. markLine: MarkLine({
  150. silent: true,
  151. lineStyle: {color: theme.gray300, type: 'solid'},
  152. label: {
  153. position: 'insideEndBottom',
  154. formatter: hideLabel ? '' : title,
  155. // @ts-expect-error weird echart types
  156. font: 'Rubik',
  157. fontSize: 14,
  158. color: theme.chartLabel,
  159. backgroundColor: theme.chartOther,
  160. },
  161. data: [
  162. {
  163. xAxis: position,
  164. },
  165. ],
  166. }),
  167. };
  168. }
  169. export const releaseMarkLinesLabels = {
  170. created: t('Release Created'),
  171. adopted: t('Adopted'),
  172. unadopted: t('Replaced'),
  173. };
  174. export function generateReleaseMarkLines(
  175. release: ReleaseWithHealth,
  176. project: ReleaseProject,
  177. theme: Theme,
  178. location: Location,
  179. options?: GenerateReleaseMarklineOptions
  180. ) {
  181. const markLines: Series[] = [];
  182. const adoptionStages = release.adoptionStages?.[project.slug];
  183. const isSingleEnv = decodeList(location.query.environment).length === 1;
  184. const releaseBounds = getReleaseBounds(release);
  185. const {statsPeriod, ...releaseParamsRest} = getReleaseParams({
  186. location,
  187. releaseBounds,
  188. });
  189. let {start, end} = releaseParamsRest;
  190. const isDefaultPeriod = !(
  191. location.query.pageStart ||
  192. location.query.pageEnd ||
  193. location.query.pageStatsPeriod
  194. );
  195. if (statsPeriod) {
  196. const parsedStatsPeriod = parseStatsPeriod(statsPeriod, null);
  197. start = parsedStatsPeriod.start;
  198. end = parsedStatsPeriod.end;
  199. }
  200. const releaseCreated = moment(release.dateCreated).startOf('minute');
  201. if (
  202. releaseCreated.isBetween(start, end) ||
  203. (isDefaultPeriod && releaseBounds.type === 'normal')
  204. ) {
  205. markLines.push(
  206. generateReleaseMarkLine(
  207. releaseMarkLinesLabels.created,
  208. releaseCreated.valueOf(),
  209. theme,
  210. options
  211. )
  212. );
  213. }
  214. if (!isSingleEnv || !isMobileRelease(project.platform)) {
  215. // for now want to show marklines only on mobile platforms with single environment selected
  216. return markLines;
  217. }
  218. const releaseAdopted = adoptionStages?.adopted && moment(adoptionStages.adopted);
  219. if (releaseAdopted && releaseAdopted.isBetween(start, end)) {
  220. markLines.push(
  221. generateReleaseMarkLine(
  222. releaseMarkLinesLabels.adopted,
  223. releaseAdopted.valueOf(),
  224. theme,
  225. options
  226. )
  227. );
  228. }
  229. const releaseReplaced = adoptionStages?.unadopted && moment(adoptionStages.unadopted);
  230. if (releaseReplaced && releaseReplaced.isBetween(start, end)) {
  231. markLines.push(
  232. generateReleaseMarkLine(
  233. releaseMarkLinesLabels.unadopted,
  234. releaseReplaced.valueOf(),
  235. theme,
  236. options
  237. )
  238. );
  239. }
  240. return markLines;
  241. }