utils.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import {Location} from 'history';
  2. import pick from 'lodash/pick';
  3. import {URL_PARAM} from 'app/constants/globalSelectionHeader';
  4. import {t} from 'app/locale';
  5. import {
  6. Commit,
  7. CommitFile,
  8. FilesByRepository,
  9. GlobalSelection,
  10. LightWeightOrganization,
  11. Repository,
  12. } from 'app/types';
  13. import {getUtcDateString} from 'app/utils/dates';
  14. import EventView from 'app/utils/discover/eventView';
  15. import {QueryResults, stringifyQueryObject} from 'app/utils/tokenizeSearch';
  16. export type CommitsByRepository = {
  17. [key: string]: Commit[];
  18. };
  19. /**
  20. * Convert list of individual file changes into a per-file summary grouped by repository
  21. */
  22. export function getFilesByRepository(fileList: CommitFile[]) {
  23. return fileList.reduce<FilesByRepository>((filesByRepository, file) => {
  24. const {filename, repoName, author, type} = file;
  25. if (!filesByRepository.hasOwnProperty(repoName)) {
  26. filesByRepository[repoName] = {};
  27. }
  28. if (!filesByRepository[repoName].hasOwnProperty(filename)) {
  29. filesByRepository[repoName][filename] = {
  30. authors: {},
  31. types: new Set(),
  32. };
  33. }
  34. if (author.email) {
  35. filesByRepository[repoName][filename].authors[author.email] = author;
  36. }
  37. filesByRepository[repoName][filename].types.add(type);
  38. return filesByRepository;
  39. }, {});
  40. }
  41. /**
  42. * Convert list of individual commits into a summary grouped by repository
  43. */
  44. export function getCommitsByRepository(commitList: Commit[]): CommitsByRepository {
  45. return commitList.reduce((commitsByRepository, commit) => {
  46. const repositoryName = commit.repository?.name ?? t('unknown');
  47. if (!commitsByRepository.hasOwnProperty(repositoryName)) {
  48. commitsByRepository[repositoryName] = [];
  49. }
  50. commitsByRepository[repositoryName].push(commit);
  51. return commitsByRepository;
  52. }, {});
  53. }
  54. /**
  55. * Get request query according to the url params and active repository
  56. */
  57. type GetQueryProps = {
  58. location: Location;
  59. perPage?: number;
  60. activeRepository?: Repository;
  61. };
  62. export function getQuery({location, perPage = 40, activeRepository}: GetQueryProps) {
  63. const query = {
  64. ...pick(location.query, [...Object.values(URL_PARAM), 'cursor']),
  65. per_page: perPage,
  66. };
  67. if (!activeRepository) {
  68. return query;
  69. }
  70. return {...query, repo_name: activeRepository.name};
  71. }
  72. /**
  73. * Get repositories to render according to the activeRepository
  74. */
  75. export function getReposToRender(repos: Array<string>, activeRepository?: Repository) {
  76. if (!activeRepository) {
  77. return repos;
  78. }
  79. return [activeRepository.name];
  80. }
  81. /**
  82. * Get high level transaction information for this release
  83. */
  84. export function getReleaseEventView(
  85. selection: GlobalSelection,
  86. version: string,
  87. organization: LightWeightOrganization
  88. ): EventView {
  89. const {projects, environments, datetime} = selection;
  90. const {start, end, period} = datetime;
  91. const apdexField = organization.features.includes('project-transaction-threshold')
  92. ? 'apdex()'
  93. : `apdex(${organization.apdexThreshold})`;
  94. const discoverQuery = {
  95. id: undefined,
  96. version: 2,
  97. name: `${t('Release Apdex')}`,
  98. fields: [apdexField],
  99. query: stringifyQueryObject(
  100. new QueryResults([`release:${version}`, 'event.type:transaction', 'count():>0'])
  101. ),
  102. range: period,
  103. environment: environments,
  104. projects,
  105. start: start ? getUtcDateString(start) : undefined,
  106. end: end ? getUtcDateString(end) : undefined,
  107. } as const;
  108. return EventView.fromSavedQuery(discoverQuery);
  109. }