123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739 |
- import {OrganizationFixture} from 'sentry-fixture/organization';
- import {
- Am3DsEnterpriseSubscriptionFixture,
- SubscriptionFixture,
- } from 'getsentry-test/fixtures/subscription';
- import {ChartDataTransform} from 'sentry/views/organizationStats/usageChart';
- import {type BillingStats, PlanTier} from 'getsentry/types';
- import {
- getCategoryOptions,
- mapCostStatsToChart,
- mapReservedBudgetStatsToChart,
- mapStatsToChart,
- } from './reservedUsageChart';
- describe('mapStatsToChart', () => {
- it('should map stats to chart data', () => {
- const stats: BillingStats = [
- {
- date: '2019-01-01',
- ts: '',
- accepted: 1,
- filtered: 0,
- total: 1,
- dropped: {total: 0},
- onDemandCostRunningTotal: 0,
- isProjected: false,
- },
- ];
- const result = mapStatsToChart({
- stats,
- transform: ChartDataTransform.CUMULATIVE,
- });
- expect(result).toEqual({
- accepted: [
- {
- value: ['Jan 1', 1],
- },
- ],
- dropped: [
- {
- dropped: {
- other: 0,
- overQuota: 0,
- spikeProtection: 0,
- },
- value: ['Jan 1', 0],
- },
- ],
- projected: [],
- onDemand: [],
- reserved: [],
- });
- });
- });
- describe('mapCostStatsToChart', () => {
- const organization = OrganizationFixture();
- const subscription = SubscriptionFixture({
- organization,
- plan: 'am2_business',
- });
- it('should map cost stats to chart data', () => {
- const stats: BillingStats = [
- {
- date: '2019-01-01',
- ts: '',
- accepted: 1,
- filtered: 0,
- total: 1,
- dropped: {total: 0},
- onDemandCostRunningTotal: 100,
- isProjected: false,
- },
- {
- date: '2019-01-02',
- ts: '',
- accepted: 2,
- filtered: 0,
- total: 2,
- dropped: {total: 0},
- onDemandCostRunningTotal: 200,
- isProjected: false,
- },
- ];
- const result = mapCostStatsToChart({
- category: 'errors',
- stats,
- transform: ChartDataTransform.CUMULATIVE,
- subscription,
- });
- expect(result).toEqual({
- accepted: [],
- dropped: [],
- projected: [],
- onDemand: [
- {
- value: ['Jan 1', 100],
- },
- {
- value: ['Jan 2', 200],
- },
- ],
- reserved: [
- {
- value: ['Jan 1', 0],
- },
- {
- value: ['Jan 2', 0],
- },
- ],
- });
- });
- it('should subtract previous onDemandCostRunningTotal to get periodic', () => {
- const stats: BillingStats = [
- {
- date: '2019-01-01',
- ts: '',
- accepted: 1,
- filtered: 0,
- total: 1,
- dropped: {total: 0},
- onDemandCostRunningTotal: 100,
- isProjected: false,
- },
- {
- date: '2019-01-02',
- ts: '',
- accepted: 2,
- filtered: 0,
- total: 2,
- dropped: {total: 0},
- onDemandCostRunningTotal: 200,
- isProjected: false,
- },
- ];
- const result = mapCostStatsToChart({
- category: 'errors',
- stats,
- transform: ChartDataTransform.PERIODIC,
- subscription,
- });
- expect(result).toEqual({
- accepted: [],
- dropped: [],
- projected: [],
- onDemand: [
- {
- value: ['Jan 1', 100],
- },
- {
- value: ['Jan 2', 100],
- },
- ],
- reserved: [
- {
- value: ['Jan 1', 0],
- },
- {
- value: ['Jan 2', 0],
- },
- ],
- });
- });
- it('should ignore projected days from periodic onDemandCost', () => {
- const stats: BillingStats = [
- {
- date: '2019-01-01',
- ts: '',
- accepted: 0,
- filtered: 0,
- total: 0,
- dropped: {total: 0},
- onDemandCostRunningTotal: 100,
- isProjected: false,
- },
- {
- date: '2019-01-02',
- ts: '',
- accepted: 2,
- filtered: 0,
- total: 2,
- dropped: {total: 0},
- onDemandCostRunningTotal: 200,
- isProjected: true,
- },
- ];
- const result = mapCostStatsToChart({
- category: 'errors',
- stats,
- transform: ChartDataTransform.PERIODIC,
- subscription,
- });
- expect(result).toEqual({
- accepted: [],
- dropped: [],
- projected: [],
- onDemand: [
- {
- value: ['Jan 1', 100],
- },
- ],
- reserved: expect.any(Array),
- });
- });
- it('should subtract previous onDemandCostRunningTotal for periodic', () => {
- const stats: BillingStats = [100, 200, 200, 200].map(
- (onDemandCostRunningTotal, i) => ({
- date: `2019-01-0${i + 1}`,
- ts: '',
- accepted: 0,
- filtered: 0,
- total: 0,
- dropped: {total: 0},
- onDemandCostRunningTotal,
- isProjected: false,
- })
- );
- const result = mapCostStatsToChart({
- category: 'errors',
- stats,
- transform: ChartDataTransform.PERIODIC,
- subscription,
- });
- expect(result.onDemand?.map(item => (item as any).value?.[1])).toEqual([
- 100, 100, 0, 0,
- ]);
- });
- });
- describe('mapReservedBudgetStatsToChart', () => {
- const organization = OrganizationFixture();
- const subscription = Am3DsEnterpriseSubscriptionFixture({organization});
- it('should map cumulative individual reserved budget stats to chart data', () => {
- const statsByDateAndCategory = {
- '2019-01-01': {
- spans: [
- {
- date: '2019-01-01',
- ts: '',
- accepted: 500,
- filtered: 0,
- total: 500,
- dropped: {total: 0},
- onDemandCostRunningTotal: 0,
- isProjected: false,
- },
- ],
- },
- '2019-01-02': {
- spans: [
- {
- date: '2019-01-02',
- ts: '',
- accepted: 2000,
- filtered: 0,
- total: 2000,
- dropped: {total: 0},
- onDemandCostRunningTotal: 500,
- isProjected: false,
- },
- ],
- },
- '2019-01-03': {
- spans: [
- {
- date: '2019-01-03',
- ts: '',
- accepted: 2000,
- filtered: 0,
- total: 2000,
- dropped: {total: 0},
- onDemandCostRunningTotal: 2500,
- isProjected: false,
- },
- ],
- },
- };
- const reservedBudgetCategoryInfo = {
- spans: {
- freeBudget: 0,
- prepaidBudget: 2000_00,
- reservedCpe: 1_00,
- reservedSpend: 2000_00,
- totalReservedBudget: 2000_00,
- },
- };
- const result = mapReservedBudgetStatsToChart({
- statsByDateAndCategory,
- transform: ChartDataTransform.CUMULATIVE,
- subscription,
- reservedBudgetCategoryInfo,
- });
- expect(result).toEqual({
- accepted: [],
- dropped: [],
- projected: [],
- onDemand: [
- {
- value: ['Jan 1', 0],
- },
- {
- value: ['Jan 2', 500],
- },
- {
- value: ['Jan 3', 2500],
- },
- ],
- reserved: [
- {
- value: ['Jan 1', 500_00],
- },
- {
- value: ['Jan 2', 2000_00],
- },
- {
- value: ['Jan 3', 2000_00],
- },
- ],
- });
- });
- it('should map cumulative combined reserved budget stats to chart data', () => {
- const statsByDateAndCategory = {
- '2019-01-01': {
- spans: [
- {
- date: '2019-01-01',
- ts: '',
- accepted: 500,
- filtered: 0,
- total: 500,
- dropped: {total: 0},
- onDemandCostRunningTotal: 0,
- isProjected: false,
- },
- ],
- spansIndexed: [
- {
- date: '2019-01-01',
- ts: '',
- accepted: 250,
- filtered: 0,
- total: 250,
- dropped: {total: 0},
- onDemandCostRunningTotal: 0,
- isProjected: false,
- },
- ],
- },
- '2019-01-02': {
- spans: [
- {
- date: '2019-01-02',
- ts: '',
- accepted: 1000,
- filtered: 0,
- total: 1000,
- dropped: {total: 0},
- onDemandCostRunningTotal: 1000,
- isProjected: false,
- },
- ],
- spansIndexed: [
- {
- date: '2019-01-02',
- ts: '',
- accepted: 1000,
- filtered: 0,
- total: 1000,
- dropped: {total: 0},
- onDemandCostRunningTotal: 2000,
- isProjected: false,
- },
- ],
- },
- '2019-01-03': {
- spans: [
- {
- date: '2019-01-03',
- ts: '',
- accepted: 2000,
- filtered: 0,
- total: 2000,
- dropped: {total: 0},
- onDemandCostRunningTotal: 2000,
- isProjected: false,
- },
- ],
- spansIndexed: [
- {
- date: '2019-01-03',
- ts: '',
- accepted: 1000,
- filtered: 0,
- total: 1000,
- dropped: {total: 0},
- onDemandCostRunningTotal: 4000,
- isProjected: false,
- },
- ],
- },
- };
- const reservedBudgetCategoryInfo = {
- spans: {
- freeBudget: 0,
- prepaidBudget: 2000_00,
- reservedCpe: 1_00,
- reservedSpend: 1500_00,
- totalReservedBudget: 2000_00,
- },
- spansIndexed: {
- freeBudget: 0,
- prepaidBudget: 2000_00,
- reservedCpe: 2_00,
- reservedSpend: 500_00,
- totalReservedBudget: 2000_00,
- },
- };
- const result = mapReservedBudgetStatsToChart({
- statsByDateAndCategory,
- transform: ChartDataTransform.CUMULATIVE,
- subscription,
- reservedBudgetCategoryInfo,
- });
- expect(result).toEqual({
- accepted: [],
- dropped: [],
- projected: [],
- onDemand: [
- {
- value: ['Jan 1', 0],
- },
- {
- value: ['Jan 2', 3000],
- },
- {
- value: ['Jan 3', 6000],
- },
- ],
- reserved: [
- {
- value: ['Jan 1', 1000_00],
- },
- {
- value: ['Jan 2', 2000_00],
- },
- {
- value: ['Jan 3', 2000_00],
- },
- ],
- });
- });
- it('should map periodic individual reserved budget stats to chart data', () => {
- const statsByDateAndCategory = {
- '2019-01-01': {
- spans: [
- {
- date: '2019-01-01',
- ts: '',
- accepted: 500,
- filtered: 0,
- total: 500,
- dropped: {total: 0},
- onDemandCostRunningTotal: 0,
- isProjected: false,
- },
- ],
- },
- '2019-01-02': {
- spans: [
- {
- date: '2019-01-02',
- ts: '',
- accepted: 1500,
- filtered: 0,
- total: 1500,
- dropped: {total: 0},
- onDemandCostRunningTotal: 0,
- isProjected: false,
- },
- ],
- },
- };
- const reservedBudgetCategoryInfo = {
- spans: {
- freeBudget: 0,
- prepaidBudget: 2000_00,
- reservedCpe: 1_00,
- reservedSpend: 2000_00,
- totalReservedBudget: 2000_00,
- },
- };
- const result = mapReservedBudgetStatsToChart({
- statsByDateAndCategory,
- transform: ChartDataTransform.PERIODIC,
- subscription,
- reservedBudgetCategoryInfo,
- });
- expect(result).toEqual({
- accepted: [],
- dropped: [],
- projected: [],
- onDemand: [
- {
- value: ['Jan 1', 0],
- },
- {
- value: ['Jan 2', 0],
- },
- ],
- reserved: [
- {
- value: ['Jan 1', 500_00],
- },
- {
- value: ['Jan 2', 1500_00],
- },
- ],
- });
- });
- it('should map periodic combined reserved budget stats to chart data', () => {
- const statsByDateAndCategory = {
- '2019-01-01': {
- spans: [
- {
- date: '2019-01-01',
- ts: '',
- accepted: 500,
- filtered: 0,
- total: 500,
- dropped: {total: 0},
- onDemandCostRunningTotal: 0,
- isProjected: false,
- },
- ],
- spansIndexed: [
- {
- date: '2019-01-01',
- ts: '',
- accepted: 250,
- filtered: 0,
- total: 250,
- dropped: {total: 0},
- onDemandCostRunningTotal: 0,
- isProjected: false,
- },
- ],
- },
- '2019-01-02': {
- spans: [
- {
- date: '2019-01-02',
- ts: '',
- accepted: 750,
- filtered: 0,
- total: 750,
- dropped: {total: 0},
- onDemandCostRunningTotal: 0,
- isProjected: false,
- },
- ],
- spansIndexed: [
- {
- date: '2019-01-02',
- ts: '',
- accepted: 125,
- filtered: 0,
- total: 125,
- dropped: {total: 0},
- onDemandCostRunningTotal: 0,
- isProjected: false,
- },
- ],
- },
- };
- const reservedBudgetCategoryInfo = {
- spans: {
- freeBudget: 0,
- prepaidBudget: 2000_00,
- reservedCpe: 1_00,
- reservedSpend: 1250_00,
- totalReservedBudget: 2000_00,
- },
- spansIndexed: {
- freeBudget: 0,
- prepaidBudget: 2000_00,
- reservedCpe: 2_00,
- reservedSpend: 750_00,
- totalReservedBudget: 2000_00,
- },
- };
- const result = mapReservedBudgetStatsToChart({
- statsByDateAndCategory,
- transform: ChartDataTransform.PERIODIC,
- subscription,
- reservedBudgetCategoryInfo,
- });
- expect(result).toEqual({
- accepted: [],
- dropped: [],
- projected: [],
- onDemand: [
- {
- value: ['Jan 1', 0],
- },
- {
- value: ['Jan 2', 0],
- },
- ],
- reserved: [
- {
- value: ['Jan 1', 1000_00],
- },
- {
- value: ['Jan 2', 1000_00],
- },
- ],
- });
- });
- });
- describe('getCategoryOptions', () => {
- const organization = OrganizationFixture({access: ['org:billing']});
- it('should return am3 categories', () => {
- const subscription = SubscriptionFixture({
- plan: 'am3_f',
- planTier: PlanTier.AM3,
- organization,
- });
- const result = getCategoryOptions({
- plan: subscription.planDetails,
- hadCustomDynamicSampling: subscription.hadCustomDynamicSampling,
- });
- result.forEach(option => {
- expect(subscription.planDetails.checkoutCategories).toContain(option.value);
- });
- });
- it('should return am3 categories with stored spans for custom dynamic sampling', () => {
- const subscription = SubscriptionFixture({
- plan: 'am3_f',
- planTier: PlanTier.AM3,
- organization,
- hadCustomDynamicSampling: true,
- });
- const result = getCategoryOptions({
- plan: subscription.planDetails,
- hadCustomDynamicSampling: subscription.hadCustomDynamicSampling,
- });
- result.forEach(option => {
- expect(subscription.planDetails.categories).toContain(option.value);
- });
- });
- it('should return am2 categories', () => {
- const subscription = SubscriptionFixture({
- plan: 'am2_f',
- planTier: PlanTier.AM2,
- organization,
- });
- const result = getCategoryOptions({
- plan: subscription.planDetails,
- hadCustomDynamicSampling: subscription.hadCustomDynamicSampling,
- });
- result.forEach(option => {
- expect(subscription.planDetails.categories).toContain(option.value);
- });
- });
- it('should return am1 categories', () => {
- const subscription = SubscriptionFixture({
- plan: 'am1_f',
- planTier: PlanTier.AM1,
- organization,
- });
- const result = getCategoryOptions({
- plan: subscription.planDetails,
- hadCustomDynamicSampling: subscription.hadCustomDynamicSampling,
- });
- result.forEach(option => {
- expect(subscription.planDetails.categories).toContain(option.value);
- });
- });
- it('should return mm2 categories', () => {
- const subscription = SubscriptionFixture({
- plan: 'mm2_f',
- planTier: PlanTier.MM2,
- organization,
- });
- const result = getCategoryOptions({
- plan: subscription.planDetails,
- hadCustomDynamicSampling: subscription.hadCustomDynamicSampling,
- });
- result.forEach(option => {
- expect(subscription.planDetails.categories).toContain(option.value);
- });
- });
- });
|