mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	feat: traffic limits for enterprise-payg (#8596)
This commit is contained in:
		
							parent
							
								
									9f297052c4
								
							
						
					
					
						commit
						1c9527654d
					
				@ -31,6 +31,7 @@ import {
 | 
				
			|||||||
} from 'hooks/useTrafficData';
 | 
					} from 'hooks/useTrafficData';
 | 
				
			||||||
import { customHighlightPlugin } from 'component/common/Chart/customHighlightPlugin';
 | 
					import { customHighlightPlugin } from 'component/common/Chart/customHighlightPlugin';
 | 
				
			||||||
import { formatTickValue } from 'component/common/Chart/formatTickValue';
 | 
					import { formatTickValue } from 'component/common/Chart/formatTickValue';
 | 
				
			||||||
 | 
					import { useTrafficLimit } from './hooks/useTrafficLimit';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const StyledBox = styled(Box)(({ theme }) => ({
 | 
					const StyledBox = styled(Box)(({ theme }) => ({
 | 
				
			||||||
    display: 'grid',
 | 
					    display: 'grid',
 | 
				
			||||||
@ -136,13 +137,11 @@ const createBarChartOptions = (
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const proPlanIncludedRequests = 53_000_000;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export const NetworkTrafficUsage: VFC = () => {
 | 
					export const NetworkTrafficUsage: VFC = () => {
 | 
				
			||||||
    usePageTitle('Network - Data Usage');
 | 
					    usePageTitle('Network - Data Usage');
 | 
				
			||||||
    const theme = useTheme();
 | 
					    const theme = useTheme();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const { isOss, isPro } = useUiConfig();
 | 
					    const { isOss } = useUiConfig();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const {
 | 
					    const {
 | 
				
			||||||
        record,
 | 
					        record,
 | 
				
			||||||
@ -157,7 +156,7 @@ export const NetworkTrafficUsage: VFC = () => {
 | 
				
			|||||||
        calculateEstimatedMonthlyCost,
 | 
					        calculateEstimatedMonthlyCost,
 | 
				
			||||||
    } = useTrafficDataEstimation();
 | 
					    } = useTrafficDataEstimation();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const includedTraffic = isPro() ? proPlanIncludedRequests : 0;
 | 
					    const includedTraffic = useTrafficLimit();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const options = useMemo(() => {
 | 
					    const options = useMemo(() => {
 | 
				
			||||||
        return createBarChartOptions(
 | 
					        return createBarChartOptions(
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,77 @@
 | 
				
			|||||||
 | 
					import { renderHook } from '@testing-library/react';
 | 
				
			||||||
 | 
					import { useTrafficLimit } from './useTrafficLimit';
 | 
				
			||||||
 | 
					import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
 | 
				
			||||||
 | 
					import { useUiFlag } from 'hooks/useUiFlag';
 | 
				
			||||||
 | 
					import { vi, describe, it, expect } from 'vitest';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					vi.mock('hooks/api/getters/useUiConfig/useUiConfig');
 | 
				
			||||||
 | 
					vi.mock('hooks/useUiFlag');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('useTrafficLimit', () => {
 | 
				
			||||||
 | 
					    it('should return requests limit if user is on pro plan', () => {
 | 
				
			||||||
 | 
					        vi.mocked(useUiConfig).mockReturnValue({
 | 
				
			||||||
 | 
					            isPro: () => true,
 | 
				
			||||||
 | 
					            isEnterprise: () => false,
 | 
				
			||||||
 | 
					            uiConfig: {
 | 
				
			||||||
 | 
					                billing: 'pay-as-you-go',
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        } as any);
 | 
				
			||||||
 | 
					        vi.mocked(useUiFlag).mockReturnValue(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const { result } = renderHook(() => useTrafficLimit());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        expect(result.current).toBe(53_000_000);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it('should return PAYG plan requests limit if enterprise-payg is enabled and billing is pay-as-you-go', () => {
 | 
				
			||||||
 | 
					        vi.mocked(useUiConfig).mockReturnValue({
 | 
				
			||||||
 | 
					            isPro: () => false,
 | 
				
			||||||
 | 
					            isEnterprise: () => true,
 | 
				
			||||||
 | 
					            uiConfig: { billing: 'pay-as-you-go' },
 | 
				
			||||||
 | 
					        } as any);
 | 
				
			||||||
 | 
					        vi.mocked(useUiFlag).mockReturnValue(true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const { result } = renderHook(() => useTrafficLimit());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        expect(result.current).toBe(53_000_000);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it('should return 0 if user is not on pro plan and pay-as-you-go conditions are not met', () => {
 | 
				
			||||||
 | 
					        vi.mocked(useUiConfig).mockReturnValue({
 | 
				
			||||||
 | 
					            isPro: () => false,
 | 
				
			||||||
 | 
					            isEnterprise: () => false,
 | 
				
			||||||
 | 
					            uiConfig: {},
 | 
				
			||||||
 | 
					        } as any);
 | 
				
			||||||
 | 
					        vi.mocked(useUiFlag).mockReturnValue(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const { result } = renderHook(() => useTrafficLimit());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        expect(result.current).toBe(0);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it('should return 0 if user is not on pro plan and flag is disabled', () => {
 | 
				
			||||||
 | 
					        vi.mocked(useUiConfig).mockReturnValue({
 | 
				
			||||||
 | 
					            isPro: () => false,
 | 
				
			||||||
 | 
					            isEnterprise: () => true,
 | 
				
			||||||
 | 
					            uiConfig: { billing: 'pay-as-you-go' },
 | 
				
			||||||
 | 
					        } as any);
 | 
				
			||||||
 | 
					        vi.mocked(useUiFlag).mockReturnValue(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const { result } = renderHook(() => useTrafficLimit());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        expect(result.current).toBe(0);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it('should return 0 if enterprise PAYG is enabled but billing is not pay-as-you-go', () => {
 | 
				
			||||||
 | 
					        vi.mocked(useUiConfig).mockReturnValue({
 | 
				
			||||||
 | 
					            isPro: () => false,
 | 
				
			||||||
 | 
					            isEnterprise: () => false,
 | 
				
			||||||
 | 
					            uiConfig: { billing: 'subscription' },
 | 
				
			||||||
 | 
					        } as any);
 | 
				
			||||||
 | 
					        vi.mocked(useUiFlag).mockReturnValue(true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const { result } = renderHook(() => useTrafficLimit());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        expect(result.current).toBe(0);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
@ -0,0 +1,24 @@
 | 
				
			|||||||
 | 
					import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
 | 
				
			||||||
 | 
					import { useUiFlag } from 'hooks/useUiFlag';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const proPlanIncludedRequests = 53_000_000;
 | 
				
			||||||
 | 
					const paygPlanIncludedRequests = proPlanIncludedRequests;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const useTrafficLimit = () => {
 | 
				
			||||||
 | 
					    const { isPro, isEnterprise, uiConfig } = useUiConfig();
 | 
				
			||||||
 | 
					    const isEnterprisePaygEnabled = useUiFlag('enterprise-payg');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (isPro()) {
 | 
				
			||||||
 | 
					        return proPlanIncludedRequests;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (
 | 
				
			||||||
 | 
					        isEnterprisePaygEnabled &&
 | 
				
			||||||
 | 
					        isEnterprise() &&
 | 
				
			||||||
 | 
					        uiConfig.billing === 'pay-as-you-go'
 | 
				
			||||||
 | 
					    ) {
 | 
				
			||||||
 | 
					        return paygPlanIncludedRequests;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
@ -92,6 +92,7 @@ export type UiFlags = {
 | 
				
			|||||||
    purchaseAdditionalEnvironments?: boolean;
 | 
					    purchaseAdditionalEnvironments?: boolean;
 | 
				
			||||||
    unleashAI?: boolean;
 | 
					    unleashAI?: boolean;
 | 
				
			||||||
    releasePlans?: boolean;
 | 
					    releasePlans?: boolean;
 | 
				
			||||||
 | 
					    'enterprise-payg'?: boolean;
 | 
				
			||||||
    simplifyProjectOverview?: boolean;
 | 
					    simplifyProjectOverview?: boolean;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -60,6 +60,7 @@ export type IFlagKey =
 | 
				
			|||||||
    | 'releasePlans'
 | 
					    | 'releasePlans'
 | 
				
			||||||
    | 'navigationSidebar'
 | 
					    | 'navigationSidebar'
 | 
				
			||||||
    | 'productivityReportEmail'
 | 
					    | 'productivityReportEmail'
 | 
				
			||||||
 | 
					    | 'enterprise-payg'
 | 
				
			||||||
    | 'simplifyProjectOverview';
 | 
					    | 'simplifyProjectOverview';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type IFlags = Partial<{ [key in IFlagKey]: boolean | Variant }>;
 | 
					export type IFlags = Partial<{ [key in IFlagKey]: boolean | Variant }>;
 | 
				
			||||||
@ -297,6 +298,10 @@ const flags: IFlags = {
 | 
				
			|||||||
        process.env.UNLEASH_EXPERIMENTAL_PRODUCTIVITY_REPORT_EMAIL,
 | 
					        process.env.UNLEASH_EXPERIMENTAL_PRODUCTIVITY_REPORT_EMAIL,
 | 
				
			||||||
        false,
 | 
					        false,
 | 
				
			||||||
    ),
 | 
					    ),
 | 
				
			||||||
 | 
					    'enterprise-payg': parseEnvVarBoolean(
 | 
				
			||||||
 | 
					        process.env.UNLEASH_EXPERIMENTAL_ENTERPRISE_PAYG,
 | 
				
			||||||
 | 
					        false,
 | 
				
			||||||
 | 
					    ),
 | 
				
			||||||
    simplifyProjectOverview: parseEnvVarBoolean(
 | 
					    simplifyProjectOverview: parseEnvVarBoolean(
 | 
				
			||||||
        process.env.UNLEASH_EXPERIMENTAL_SIMPLIFY_PROJECT_OVERVIEW,
 | 
					        process.env.UNLEASH_EXPERIMENTAL_SIMPLIFY_PROJECT_OVERVIEW,
 | 
				
			||||||
        false,
 | 
					        false,
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user