correct discount model
This commit is contained in:
parent
f5f4ec8ac9
commit
3896636f9b
6 changed files with 201 additions and 55 deletions
|
@ -5,13 +5,11 @@ from hub.services.models import ComputePlan, VSHNAppCatPrice
|
|||
|
||||
|
||||
def natural_sort_key(name):
|
||||
"""Extract numeric part from compute plan name for natural sorting"""
|
||||
match = re.search(r"compute-std-(\d+)", name)
|
||||
return int(match.group(1)) if match else 0
|
||||
|
||||
|
||||
def pricelist(request):
|
||||
# Get all active compute plans with their pricing data
|
||||
compute_plans = (
|
||||
ComputePlan.objects.filter(active=True)
|
||||
.select_related("cloud_provider")
|
||||
|
@ -19,12 +17,10 @@ def pricelist(request):
|
|||
.order_by("cloud_provider__name")
|
||||
)
|
||||
|
||||
# Sort compute plans naturally by provider and plan number
|
||||
compute_plans = sorted(
|
||||
compute_plans, key=lambda x: (x.cloud_provider.name, natural_sort_key(x.name))
|
||||
)
|
||||
|
||||
# Get all VSHNAppCat pricing configurations
|
||||
appcat_prices = (
|
||||
VSHNAppCatPrice.objects.all()
|
||||
.select_related("service", "discount_model")
|
||||
|
@ -33,16 +29,12 @@ def pricelist(request):
|
|||
)
|
||||
|
||||
pricing_data_by_service_level = defaultdict(list)
|
||||
|
||||
# Track processed combinations to avoid duplicates
|
||||
processed_combinations = set()
|
||||
|
||||
for plan in compute_plans:
|
||||
# Get all currencies available for this compute plan
|
||||
plan_currencies = set(plan.prices.values_list("currency", flat=True))
|
||||
|
||||
for appcat_price in appcat_prices:
|
||||
# Determine units based on variable unit type (RAM or CPU)
|
||||
if appcat_price.variable_unit == VSHNAppCatPrice.VariableUnit.RAM:
|
||||
units = int(plan.ram)
|
||||
elif appcat_price.variable_unit == VSHNAppCatPrice.VariableUnit.CPU:
|
||||
|
@ -50,36 +42,29 @@ def pricelist(request):
|
|||
else:
|
||||
continue
|
||||
|
||||
# Get currencies available for base fees and unit rates
|
||||
base_fee_currencies = set(
|
||||
appcat_price.base_fees.values_list("currency", flat=True)
|
||||
)
|
||||
|
||||
# Get all distinct service levels for this pricing config
|
||||
service_levels = appcat_price.unit_rates.values_list(
|
||||
"service_level", flat=True
|
||||
).distinct()
|
||||
|
||||
for service_level in service_levels:
|
||||
# Get currencies available for this specific service level
|
||||
unit_rate_currencies = set(
|
||||
appcat_price.unit_rates.filter(
|
||||
service_level=service_level
|
||||
).values_list("currency", flat=True)
|
||||
)
|
||||
|
||||
# Find currencies that exist in ALL three places: plan, base fee, and unit rate
|
||||
matching_currencies = plan_currencies.intersection(
|
||||
base_fee_currencies
|
||||
).intersection(unit_rate_currencies)
|
||||
|
||||
# Skip if no common currencies found
|
||||
if not matching_currencies:
|
||||
continue
|
||||
|
||||
# Process each matching currency
|
||||
for currency in matching_currencies:
|
||||
# Create unique combination key to prevent duplicates
|
||||
combination_key = (
|
||||
plan.cloud_provider.name,
|
||||
plan.name,
|
||||
|
@ -88,13 +73,11 @@ def pricelist(request):
|
|||
currency,
|
||||
)
|
||||
|
||||
# Skip if this combination was already processed
|
||||
if combination_key in processed_combinations:
|
||||
continue
|
||||
|
||||
processed_combinations.add(combination_key)
|
||||
|
||||
# Get pricing data for this currency - skip if any is missing
|
||||
compute_plan_price = plan.get_price(currency)
|
||||
if compute_plan_price is None:
|
||||
continue
|
||||
|
@ -107,15 +90,15 @@ def pricelist(request):
|
|||
if unit_rate is None:
|
||||
continue
|
||||
|
||||
# Calculate replica enforcement based on service level
|
||||
if service_level == VSHNAppCatPrice.ServiceLevel.GUARANTEED:
|
||||
replica_enforce = appcat_price.ha_replica_min
|
||||
else:
|
||||
replica_enforce = 1
|
||||
|
||||
total_units = units * replica_enforce
|
||||
standard_sla_price = base_fee + (total_units * unit_rate)
|
||||
|
||||
# Apply discount model if available and active
|
||||
discount_breakdown = None
|
||||
if (
|
||||
appcat_price.discount_model
|
||||
and appcat_price.discount_model.active
|
||||
|
@ -126,19 +109,28 @@ def pricelist(request):
|
|||
)
|
||||
)
|
||||
sla_price = base_fee + discounted_price
|
||||
discount_savings = standard_sla_price - sla_price
|
||||
discount_percentage = (
|
||||
(discount_savings / standard_sla_price) * 100
|
||||
if standard_sla_price > 0
|
||||
else 0
|
||||
)
|
||||
discount_breakdown = (
|
||||
appcat_price.discount_model.get_discount_breakdown(
|
||||
unit_rate, total_units
|
||||
)
|
||||
)
|
||||
else:
|
||||
# Standard pricing without discount
|
||||
sla_price = base_fee + (total_units * unit_rate)
|
||||
sla_price = standard_sla_price
|
||||
discounted_price = total_units * unit_rate
|
||||
discount_savings = 0
|
||||
discount_percentage = 0
|
||||
|
||||
# Calculate final price (compute + SLA)
|
||||
final_price = compute_plan_price + sla_price
|
||||
|
||||
# Get human-readable service level name
|
||||
service_level_display = dict(VSHNAppCatPrice.ServiceLevel.choices)[
|
||||
service_level
|
||||
]
|
||||
|
||||
# Add row to the appropriate service level group
|
||||
pricing_data_by_service_level[service_level_display].append(
|
||||
{
|
||||
"cloud_provider": plan.cloud_provider.name,
|
||||
|
@ -153,10 +145,21 @@ def pricelist(request):
|
|||
"variable_unit": appcat_price.get_variable_unit_display(),
|
||||
"units": units,
|
||||
"replica_enforce": replica_enforce,
|
||||
"total_units": total_units,
|
||||
"service_level": service_level_display,
|
||||
"sla_base": base_fee,
|
||||
"sla_per_unit": unit_rate,
|
||||
"sla_price": sla_price,
|
||||
"standard_sla_price": standard_sla_price,
|
||||
"discounted_sla_price": (
|
||||
base_fee + discounted_price
|
||||
if appcat_price.discount_model
|
||||
and appcat_price.discount_model.active
|
||||
else None
|
||||
),
|
||||
"discount_savings": discount_savings,
|
||||
"discount_percentage": discount_percentage,
|
||||
"discount_breakdown": discount_breakdown,
|
||||
"final_price": final_price,
|
||||
"discount_model": (
|
||||
appcat_price.discount_model.name
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue