-
-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
185 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
frontend/src/component/banners/TrafficOverageBanner.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { screen } from '@testing-library/react'; | ||
import { render } from 'utils/testRenderer'; | ||
import { testServerRoute, testServerSetup } from 'utils/testServer'; | ||
import { TrafficOverageBanner } from './TrafficOverageBanner'; | ||
import { toSelectablePeriod } from 'hooks/useTrafficData'; | ||
import { BILLING_INCLUDED_REQUESTS } from 'component/admin/billing/BillingDashboard/BillingPlan/BillingPlan'; | ||
|
||
const server = testServerSetup(); | ||
|
||
const setupApi = (totalRequests = 0) => { | ||
const period = toSelectablePeriod(new Date()).key; | ||
|
||
testServerRoute(server, '/api/admin/ui-config', { | ||
environment: 'Enterprise', | ||
versionInfo: { | ||
current: { | ||
enterprise: 'Enterprise', | ||
}, | ||
}, | ||
billing: 'pay-as-you-go', | ||
flags: { | ||
'enterprise-payg': true, | ||
estimateTrafficDataCost: true, | ||
}, | ||
}); | ||
|
||
testServerRoute(server, `/api/admin/metrics/traffic/${period}`, { | ||
period, | ||
apiData: [ | ||
{ | ||
apiPath: '/api/client', | ||
days: [ | ||
{ | ||
day: `${period}-01T00:00:00.000Z`, | ||
trafficTypes: [ | ||
{ | ||
group: 'successful-requests', | ||
count: totalRequests, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}; | ||
|
||
test('Displays overage banner when overage cost is calculated', async () => { | ||
setupApi(BILLING_INCLUDED_REQUESTS + 1_000_000); | ||
|
||
render(<TrafficOverageBanner />); | ||
|
||
const bannerMessage = await screen.findByText( | ||
/You're using more requests than your plan/, | ||
); | ||
expect(bannerMessage).toBeInTheDocument(); | ||
}); | ||
|
||
test('Displays estimated monthly cost banner when usage is projected to exceed', async () => { | ||
setupApi(BILLING_INCLUDED_REQUESTS - 1_000_000); | ||
|
||
render(<TrafficOverageBanner />); | ||
|
||
const bannerMessage = await screen.findByText( | ||
/Based on your current usage, you're projected to exceed your plan/, | ||
); | ||
expect(bannerMessage).toBeInTheDocument(); | ||
}); | ||
|
||
test('Does not display banner when no overage or estimated cost', async () => { | ||
setupApi(); | ||
|
||
render(<TrafficOverageBanner />); | ||
|
||
expect(screen.queryByText('Heads up!')).not.toBeInTheDocument(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { useUiFlag } from 'hooks/useUiFlag'; | ||
import { Banner } from './Banner/Banner'; | ||
import { useTrafficDataEstimation } from 'hooks/useTrafficData'; | ||
import { useTrafficLimit } from 'component/admin/network/NetworkTrafficUsage/hooks/useTrafficLimit'; | ||
import { useInstanceTrafficMetrics } from 'hooks/api/getters/useInstanceTrafficMetrics/useInstanceTrafficMetrics'; | ||
import { BILLING_TRAFFIC_BUNDLE_PRICE } from 'component/admin/billing/BillingDashboard/BillingPlan/BillingPlan'; | ||
import { useMemo } from 'react'; | ||
|
||
export const TrafficOverageBanner = () => { | ||
const estimateTrafficDataCostEnabled = useUiFlag('estimateTrafficDataCost'); | ||
const includedTraffic = useTrafficLimit(); | ||
const { | ||
currentPeriod, | ||
toChartData, | ||
toTrafficUsageSum, | ||
endpointsInfo, | ||
getDayLabels, | ||
calculateOverageCost, | ||
calculateEstimatedMonthlyCost, | ||
} = useTrafficDataEstimation(); | ||
const traffic = useInstanceTrafficMetrics(currentPeriod.key); | ||
|
||
const trafficData = useMemo( | ||
() => | ||
toChartData( | ||
getDayLabels(currentPeriod.dayCount), | ||
traffic, | ||
endpointsInfo, | ||
), | ||
[traffic, currentPeriod, endpointsInfo], | ||
); | ||
|
||
const calculatedOverageCost = useMemo( | ||
() => | ||
includedTraffic | ||
? calculateOverageCost( | ||
toTrafficUsageSum(trafficData), | ||
includedTraffic, | ||
BILLING_TRAFFIC_BUNDLE_PRICE, | ||
) | ||
: 0, | ||
[includedTraffic, trafficData], | ||
); | ||
|
||
const estimatedMonthlyCost = useMemo( | ||
() => | ||
includedTraffic && estimateTrafficDataCostEnabled | ||
? calculateEstimatedMonthlyCost( | ||
currentPeriod.key, | ||
trafficData, | ||
includedTraffic, | ||
new Date(), | ||
BILLING_TRAFFIC_BUNDLE_PRICE, | ||
) | ||
: 0, | ||
[ | ||
includedTraffic, | ||
estimateTrafficDataCostEnabled, | ||
trafficData, | ||
currentPeriod, | ||
], | ||
); | ||
|
||
const overageMessage = | ||
calculatedOverageCost > 0 | ||
? `**Heads up!** You're using more requests than your plan [includes](https://www.getunleash.io/pricing), and additional charges will apply per our [terms](https://www.getunleash.io/fair-use-policy).` | ||
: estimatedMonthlyCost > 0 | ||
? `**Heads up!** Based on your current usage, you're projected to exceed your plan's [limit](https://www.getunleash.io/pricing), and additional charges may apply per our [terms](https://www.getunleash.io/fair-use-policy).` | ||
: undefined; | ||
|
||
if (!overageMessage) return null; | ||
|
||
return ( | ||
<Banner | ||
banner={{ | ||
message: overageMessage, | ||
variant: 'warning', | ||
sticky: true, | ||
link: '/admin/network/data-usage', | ||
linkText: 'See data usage', | ||
}} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters