巴莱纳定价/保存计算器
这个库提供了一套可以用来计算信用购买成本的功能 和与动态计费相比的节约。 信用定价定义可在 即时性,可用于测试和试验,但用于生产的价格定义 在此套件中也定义了.
使用量
import { CreditPricing } from '@balena/balena-pricing';
// Format output to dollar currency.
const dollar = Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
function toDollar(pennies: number): string {
return dollar.format(pennies / 100);
}
// Set dynamic base price.
const dynamicPrice = 150;
// Use production credit pricing definitions.
let pricing = new CreditPricing();
// Use production credit pricing definitions.
// Explicitly target current versions (default).
let pricing = new CreditPricing({
target: 'current',
});
// Use production credit pricing definitions.
// Target most recent versions, even those slated for future use.
pricing = new CreditPricing({
target: 'latest',
});
// Use production credit pricing parameters.
// Target version 2, will return undefined for features without a version 2.
let pricing = new CreditPricing({
target: 2,
});
// Use production credit pricing parameters.
// Target most recent versions valid up to a given date.
let pricing = new CreditPricing({
target: new Date('2023-03-01T00:00:00Z'),
});
// Use custom credit pricing parameters.
// Mostly useful for testing parameters against the curve,
// should not normally be used in production.
pricing = CreditPricing({
credits: {
'foo:bar': [
{
version: 1,
validFrom: new Date('2023-02-01T00:00:00Z'),
basePriceCents: 150,
firstDiscountPriceCents: 149,
discountRate: 0.33,
discountThreshold: 12000,
discountThresholdPriceCents: 125,
},
],
},
});
// Get individual credit price
const creditPrice = pricing.getCreditPrice('foo:bar', 0, 1000);
console.log('Credit unit price:', toDollar(creditPrice));
// Get total price for credit purchase
const totalPrice = pricing.getCreditTotalPrice('foo:bar', 0, 1000);
console.log('Credit total price:', toDollar(totalPrice));
// Get discount percentage compared to dynamic
const discount = pricing.getDiscountOverDynamic('foo:bar', 0, 1000, dynamicPrice);
console.log('Discount over dynamic:', `${discount}%`);
// Get total savings for credit purchase compared to dynamic
const totalSavings = pricing.getTotalSavings('foo:bar', 0, 1000, dynamicPrice);
console.log('Total savings:', toDollar(totalSavings));