performance.tsx 2.7 KB

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