pendingChanges.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import moment from 'moment-timezone';
  4. import {Alert} from 'sentry/components/core/alert';
  5. import {DATA_CATEGORY_INFO} from 'sentry/constants';
  6. import {tct} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import {DataCategory} from 'sentry/types/core';
  9. import type {Organization} from 'sentry/types/organization';
  10. import oxfordizeArray from 'sentry/utils/oxfordizeArray';
  11. import {RESERVED_BUDGET_QUOTA} from 'getsentry/constants';
  12. import {type PendingOnDemandBudgets, PlanTier, type Subscription} from 'getsentry/types';
  13. import {
  14. formatReservedWithUnits,
  15. getAmPlanTier,
  16. hasPerformance,
  17. isAm3DsPlan,
  18. } from 'getsentry/utils/billing';
  19. import {
  20. getPlanCategoryName,
  21. getReservedBudgetDisplayName,
  22. } from 'getsentry/utils/dataCategory';
  23. import formatCurrency from 'getsentry/utils/formatCurrency';
  24. import {
  25. formatOnDemandBudget,
  26. hasOnDemandBudgetsFeature,
  27. isOnDemandBudgetsEqual,
  28. parseOnDemandBudgets,
  29. parseOnDemandBudgetsFromSubscription,
  30. } from 'getsentry/views/onDemandBudgets/utils';
  31. type Props = {
  32. organization: Organization;
  33. subscription: Subscription;
  34. };
  35. class PendingChanges extends Component<Props> {
  36. hasChange(pendingChangeKey: string, subscriptionKey: string | null = null) {
  37. const {subscription} = this.props;
  38. const {pendingChanges} = subscription;
  39. if (!pendingChanges) {
  40. return false;
  41. }
  42. subscriptionKey = subscriptionKey ?? pendingChangeKey;
  43. const pendingChange = this.getNestedValue(pendingChanges, pendingChangeKey);
  44. const currentValue = this.getNestedValue(subscription, subscriptionKey);
  45. return pendingChange !== null && pendingChange !== currentValue;
  46. }
  47. getNestedValue<T = any>(object: Record<string, any>, keys: string): T | null {
  48. return keys.split('.').reduce((acc, key) => acc?.[key] ?? null, object) as T | null;
  49. }
  50. getOnDemandChanges() {
  51. const {subscription, organization} = this.props;
  52. const {pendingChanges} = subscription;
  53. const results: React.ReactNode[] = [];
  54. if (!pendingChanges) {
  55. return results;
  56. }
  57. if (
  58. hasOnDemandBudgetsFeature(organization, subscription) ||
  59. (pendingChanges.onDemandBudgets && subscription.partner?.isActive)
  60. ) {
  61. const nextOnDemandBudgets = this.getNestedValue<PendingOnDemandBudgets>(
  62. pendingChanges,
  63. 'onDemandBudgets'
  64. );
  65. if (nextOnDemandBudgets) {
  66. const pendingOnDemandBudgets = parseOnDemandBudgets(nextOnDemandBudgets);
  67. const currentOnDemandBudgets = parseOnDemandBudgetsFromSubscription(subscription);
  68. const planTier = getAmPlanTier(pendingChanges.plan);
  69. if (!isOnDemandBudgetsEqual(pendingOnDemandBudgets, currentOnDemandBudgets)) {
  70. results.push(
  71. tct(
  72. '[budgetType] budget change from [currentOnDemandBudgets] to [nextOnDemandBudgets]',
  73. {
  74. budgetType: planTier === PlanTier.AM3 ? 'Pay-as-you-go' : 'On-demand',
  75. currentOnDemandBudgets: formatOnDemandBudget(
  76. subscription.planDetails,
  77. subscription.planTier,
  78. currentOnDemandBudgets,
  79. subscription.planDetails.onDemandCategories
  80. ),
  81. nextOnDemandBudgets: formatOnDemandBudget(
  82. subscription.planDetails,
  83. planTier?.toString() || subscription.planTier,
  84. nextOnDemandBudgets,
  85. pendingChanges.planDetails.onDemandCategories
  86. ),
  87. }
  88. )
  89. );
  90. }
  91. }
  92. } else if (this.hasChange('onDemandMaxSpend')) {
  93. const nextOnDemandMaxSpend =
  94. this.getNestedValue<number>(pendingChanges, 'onDemandMaxSpend') ?? 0;
  95. const currentOnDemandMaxSpend =
  96. this.getNestedValue<number>(subscription, 'onDemandMaxSpend') ?? 0;
  97. results.push(
  98. tct('[budgetType] spend change from [currentAmount] to [newAmount]', {
  99. budgetType:
  100. subscription.planTier === PlanTier.AM3 ? 'Pay-as-you-go' : 'On-demand',
  101. newAmount: formatCurrency(nextOnDemandMaxSpend),
  102. currentAmount: formatCurrency(currentOnDemandMaxSpend),
  103. })
  104. );
  105. }
  106. return results;
  107. }
  108. getPlanChanges() {
  109. const {subscription} = this.props;
  110. const {pendingChanges} = subscription;
  111. const results: React.ReactNode[] = [];
  112. if (!pendingChanges) {
  113. return results;
  114. }
  115. if (this.hasChange('plan')) {
  116. results.push(
  117. tct('Plan change to [name]', {
  118. name: pendingChanges.planDetails.name,
  119. })
  120. );
  121. }
  122. if (hasPerformance(subscription.pendingChanges?.planDetails)) {
  123. results.push(...this.getAMPlanChanges());
  124. } else if (this.hasChange('reservedEvents')) {
  125. results.push(
  126. tct('Reserved errors change to [quantity]', {
  127. quantity: pendingChanges.reservedEvents.toLocaleString(),
  128. })
  129. );
  130. }
  131. if (this.hasChange('planDetails.contractInterval')) {
  132. results.push(
  133. tct('Contract period change to [contractInterval]', {
  134. contractInterval: pendingChanges.planDetails.contractInterval,
  135. })
  136. );
  137. }
  138. if (this.hasChange('planDetails.billingInterval')) {
  139. results.push(
  140. tct('Billing period change to [billingInterval]', {
  141. billingInterval: pendingChanges.planDetails.billingInterval,
  142. })
  143. );
  144. }
  145. if (isAm3DsPlan(subscription.pendingChanges?.plan)) {
  146. results.push(...this.getReservedBudgetChanges());
  147. }
  148. return results;
  149. }
  150. getAMPlanChanges() {
  151. const {subscription} = this.props;
  152. const {pendingChanges} = subscription;
  153. const results: React.ReactNode[] = [];
  154. if (!pendingChanges) {
  155. return results;
  156. }
  157. Object.values(DATA_CATEGORY_INFO)
  158. .filter(categoryInfo => categoryInfo.isBilledCategory)
  159. .forEach(categoryInfo => {
  160. const plural = categoryInfo.plural;
  161. if (
  162. this.hasChange(`reserved.${plural}`, `categories.${plural}.reserved`) &&
  163. pendingChanges.reserved[plural] !== RESERVED_BUDGET_QUOTA
  164. ) {
  165. results.push(
  166. tct('Reserved [displayName] change to [quantity]', {
  167. displayName: getPlanCategoryName({
  168. plan: pendingChanges.planDetails,
  169. category: plural,
  170. capitalize: false,
  171. }),
  172. quantity: formatReservedWithUnits(
  173. pendingChanges.reserved[plural] ?? null,
  174. plural
  175. ),
  176. })
  177. );
  178. }
  179. });
  180. return results;
  181. }
  182. hasReservedBudgetChange() {
  183. const {subscription} = this.props;
  184. const {pendingChanges} = subscription;
  185. if (!pendingChanges) {
  186. return false;
  187. }
  188. const pendingChange = pendingChanges.reservedBudgets;
  189. const currentValue = subscription.reservedBudgets ?? [];
  190. if (pendingChange.length !== currentValue.length) {
  191. return true;
  192. }
  193. const sortedPendingBudgets = pendingChange.sort((a, b) => {
  194. return a.reservedBudget - b.reservedBudget;
  195. });
  196. const sortedCurrentBudgets = currentValue.sort((a, b) => {
  197. return a.reservedBudget - b.reservedBudget;
  198. });
  199. for (let i = 0; i < sortedPendingBudgets.length; i++) {
  200. if (
  201. sortedPendingBudgets[i]?.reservedBudget !==
  202. sortedCurrentBudgets[i]?.reservedBudget
  203. ) {
  204. return true;
  205. }
  206. const pendingBudgetCategories = Object.keys(
  207. sortedPendingBudgets[i]?.categories ?? {}
  208. ).sort();
  209. const currentBudgetCategories = Object.keys(
  210. sortedCurrentBudgets[i]?.categories ?? {}
  211. ).sort();
  212. if (pendingBudgetCategories.length !== currentBudgetCategories.length) {
  213. return true;
  214. }
  215. for (let j = 0; j < pendingBudgetCategories.length; j++) {
  216. if (pendingBudgetCategories[j] !== currentBudgetCategories[j]) {
  217. return true;
  218. }
  219. }
  220. }
  221. return false;
  222. }
  223. getReservedBudgetChanges() {
  224. const {subscription} = this.props;
  225. const {pendingChanges} = subscription;
  226. const results: React.ReactNode[] = [];
  227. if (!pendingChanges) {
  228. return results;
  229. }
  230. if (this.hasReservedBudgetChange()) {
  231. const reservedBudgetChanges = pendingChanges.reservedBudgets.map(budget => {
  232. const budgetCategories = Object.keys(budget.categories);
  233. const isSpansBudget =
  234. budgetCategories.length === 2 &&
  235. budgetCategories.includes(DataCategory.SPANS) &&
  236. budgetCategories.includes(DataCategory.SPANS_INDEXED);
  237. const adjustedCategories =
  238. isSpansBudget && !subscription.hadCustomDynamicSampling
  239. ? [DataCategory.SPANS]
  240. : budgetCategories;
  241. const newAmount = formatCurrency(budget.reservedBudget);
  242. const budgetName = getReservedBudgetDisplayName({
  243. plan: pendingChanges.planDetails,
  244. categories: adjustedCategories,
  245. hadCustomDynamicSampling: subscription.hadCustomDynamicSampling,
  246. });
  247. return `${newAmount} for ${budgetName}`;
  248. });
  249. results.push(
  250. tct('Reserved [budgetWord] updated to [reservedBudgets]', {
  251. budgetWord: reservedBudgetChanges.length === 1 ? 'budget' : 'budgets',
  252. reservedBudgets: oxfordizeArray(reservedBudgetChanges),
  253. })
  254. );
  255. }
  256. return results;
  257. }
  258. getChanges() {
  259. const {subscription} = this.props;
  260. const {pendingChanges} = subscription;
  261. const results: {
  262. [key: string]: React.ReactNode[];
  263. } = {};
  264. if (!pendingChanges) {
  265. return results;
  266. }
  267. const onDemandChanges = this.getOnDemandChanges();
  268. const planChanges = this.getPlanChanges();
  269. // the on-demand effective date should always be before
  270. // or the same as the plan effective date
  271. if (onDemandChanges.length && pendingChanges.onDemandEffectiveDate) {
  272. results[pendingChanges.onDemandEffectiveDate] = onDemandChanges;
  273. }
  274. if (planChanges.length && pendingChanges.effectiveDate) {
  275. if (pendingChanges.effectiveDate in results) {
  276. results[pendingChanges.effectiveDate]!.unshift(...planChanges);
  277. } else {
  278. results[pendingChanges.effectiveDate] = planChanges;
  279. }
  280. }
  281. return results;
  282. }
  283. render() {
  284. const {subscription} = this.props;
  285. const {pendingChanges} = subscription;
  286. if (!pendingChanges) {
  287. return null;
  288. }
  289. const changes = this.getChanges();
  290. if (!Object.keys(changes)?.length) {
  291. return null;
  292. }
  293. return (
  294. <Alert.Container>
  295. <Alert type="info" showIcon>
  296. <PendingLists>
  297. {Object.entries(changes).map(([effectiveDate, items]) => (
  298. <div key={effectiveDate} data-test-id="pending-list">
  299. {tct('The following changes will take effect on [date]:', {
  300. date: <strong>{moment(effectiveDate).format('ll')}</strong>,
  301. })}
  302. <ItemList>
  303. {items.map((item, itemIdx) => (
  304. <li key={itemIdx} data-test-id="pending-item">
  305. {item}
  306. </li>
  307. ))}
  308. </ItemList>
  309. </div>
  310. ))}
  311. </PendingLists>
  312. </Alert>
  313. </Alert.Container>
  314. );
  315. }
  316. }
  317. const PendingLists = styled('div')`
  318. display: grid;
  319. grid-auto-rows: auto;
  320. gap: ${space(1.5)};
  321. `;
  322. const ItemList = styled('ul')`
  323. margin-bottom: 0;
  324. `;
  325. export default PendingChanges;