performance.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import {
  2. addErrorMessage,
  3. addLoadingMessage,
  4. clearIndicators,
  5. } from 'app/actionCreators/indicator';
  6. import {Client} from 'app/api';
  7. import {t} from 'app/locale';
  8. import parseLinkHeader from 'app/utils/parseLinkHeader';
  9. type TeamKeyTransaction = {
  10. team: string;
  11. count: number;
  12. keyed: {
  13. project_id: string;
  14. transaction: string;
  15. }[];
  16. };
  17. export async function fetchTeamKeyTransactions(
  18. api: Client,
  19. orgSlug: string,
  20. teams: string[],
  21. projects?: string[]
  22. ): Promise<TeamKeyTransaction[]> {
  23. const url = `/organizations/${orgSlug}/key-transactions-list/`;
  24. const datas: TeamKeyTransaction[][] = [];
  25. let cursor: string | undefined = undefined;
  26. let hasMore = true;
  27. while (hasMore) {
  28. try {
  29. const payload = {cursor, team: teams, project: projects};
  30. if (!payload.cursor) {
  31. delete payload.cursor;
  32. }
  33. if (!payload.project?.length) {
  34. delete payload.project;
  35. }
  36. const [data, , xhr] = await api.requestPromise(url, {
  37. method: 'GET',
  38. includeAllArgs: true,
  39. query: payload,
  40. });
  41. datas.push(data);
  42. const pageLinks = xhr && xhr.getResponseHeader('Link');
  43. if (pageLinks) {
  44. const paginationObject = parseLinkHeader(pageLinks);
  45. hasMore = paginationObject?.next?.results ?? false;
  46. cursor = paginationObject.next?.cursor;
  47. } else {
  48. hasMore = false;
  49. }
  50. } catch (err) {
  51. addErrorMessage(
  52. err.responseJSON?.detail ?? t('Error fetching team key transactions')
  53. );
  54. throw err;
  55. }
  56. }
  57. return datas.flat();
  58. }
  59. export function toggleKeyTransaction(
  60. api: Client,
  61. isKeyTransaction: boolean,
  62. orgId: string,
  63. projects: Readonly<number[]>,
  64. transactionName: string,
  65. teamIds?: string[] // TODO(txiao): make this required
  66. ): Promise<undefined> {
  67. addLoadingMessage(t('Saving changes\u2026'));
  68. const promise: Promise<undefined> = api.requestPromise(
  69. `/organizations/${orgId}/key-transactions/`,
  70. {
  71. method: isKeyTransaction ? 'DELETE' : 'POST',
  72. query: {
  73. project: projects.map(id => String(id)),
  74. },
  75. data: {
  76. transaction: transactionName,
  77. team: teamIds,
  78. },
  79. }
  80. );
  81. promise.then(clearIndicators);
  82. promise.catch(response => {
  83. const non_field_errors = response?.responseJSON?.non_field_errors;
  84. if (
  85. Array.isArray(non_field_errors) &&
  86. non_field_errors.length &&
  87. non_field_errors[0]
  88. ) {
  89. addErrorMessage(response.responseJSON.non_field_errors[0]);
  90. } else {
  91. addErrorMessage(t('Unable to update key transaction'));
  92. }
  93. });
  94. return promise;
  95. }